1、环境准备
nacos的下载启动请移步https://nacos.io/zh-cn/docs/what-is-nacos.html此地址,本文使用的nacos版本为1.4.3
2、项目创建
使用IDEA创建一个Maven项目,在创建三个模块,分别为spring-cloud-nacos-api、spring-cloud-nacos-provider、spring-cloud-nacos-consumer,其中spring-cloud-nacos-api是抽取出来的公共接口,使用mvn install 命令发布到本地仓库后,在另外两个项目中添加依赖。spring-cloud-nacos-provider、spring-cloud-nacos-consumer为springBoot项目。目录结构如下:
在spring-cloud-nacos-sample父工程中添加一下的依赖如下:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.example</groupId> <artifactId>spring-cloud-nacos-sample</artifactId> <packaging>pom</packaging> <version>1.0-SNAPSHOT</version> <modules> <module>spring-cloud-nacos-api</module> <module>spring-cloud-nacos-provider</module> <module>spring-cloud-nacos-consumer</module> </modules> <properties> </properties> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>2.3.9.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-alibaba-dependencies</artifactId> <version>2.2.5.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Hoxton.SR9</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> <version>2.3.9.RELEASE</version> </dependency> </dependencies> </project>
3、api代码编写
在api中定义一个接口:使用mvn install 发布到仓库
public interface IHelloService { String sayHello(String name); }
4、编写服务提供者spring-cloud-nacos-provider
首先,在pom.xml中添加相关依赖如下:(未指定version的已经在父工程中统一管理)
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>spring-cloud-nacos-sample</artifactId> <groupId>org.example</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>spring-cloud-nacos-provider</artifactId> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter</artifactId> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-dubbo</artifactId> </dependency> <dependency> <groupId>org.example</groupId> <artifactId>spring-cloud-nacos-api</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> </dependencies> </project>
然后,编写主启动类和新建一个类实现api中的接口:
@SpringBootApplication
@DubboComponentScan//会扫描@DubboService发布的远程服务
public class ProviderApplication {
public static void main(String[] args) {
SpringApplication.run(ProviderApplication.class,args);
}
}
1 @DubboService 2 public class HelloServiceImpl implements IHelloService { 3 public String sayHello(String name) { 4 return "hello,"+name; 5 } 6 }
注意:@DubboService作用是将该接口发布为dubbo服务,在老的版本中叫@Service
配置application.yml:
spring: application: name: spring-cloud-nacos-sample cloud: nacos: discovery:
#nacos注册中心地址 server-addr: 127.0.0.1:8848 dubbo: protocol:
#使用dubbo协议 name: dubbo
#端口号 建议都设置一下 port: 9000
#dubbo主机绑定 host: localhost registry:
#Dubbo服务注册中心的配置地址,值spring-cloud://localhost表示挂载到SpringCloud注册中心,不配置会提示没有配置中心的错误
address: spring-cloud://localhost
5、编写服务消费者spring-cloud-nacos-consumer
创建主启动类:
@SpringBootApplication @DubboComponentScan public class ConsumerApplication { public static void main(String[] args) { SpringApplication.run(ConsumerApplication.class,args); } }
添加maven依赖:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>spring-cloud-nacos-sample</artifactId> <groupId>org.example</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>spring-cloud-nacos-consumer</artifactId> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter</artifactId> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-dubbo</artifactId> </dependency> <dependency> <groupId>org.example</groupId> <artifactId>spring-cloud-nacos-api</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> </dependencies> </project>
创建一个HelloController测试类,定义一个接口:
@RestController public class HelloController { @DubboReference private IHelloService helloService; @GetMapping("/say") public String sayHello(){ return helloService.sayHello("mic"); } }
配置application.yml:
spring: application: name: spring-cloud-nacos-consumer cloud: nacos: discovery: server-addr: 127.0.0.1:8848 dubbo: cloud:
#订阅已经注册的服务,(实际为spring-cloud-nacos-provider服务中yml文件配置的spring.application.name的值) subscribed-services: spring-cloud-nacos-sample server: port: 9000
最后,在浏览器端输入 localhost:9000/say 看到响应 hello,mic 说明操作成功!浏览器访问localhost:8848/nacos后,在服务管理——>服务列表查看响应的服务实例