目标:Nacos实现服务端和消费端的注册和发现
工具:IDEA–2020.1、Nacos、云服务器
学习目标:服务端开发和消费端开发
本次学习的工程下载链接放到文本最后面
- 创建一个普通的maven工程spring-cloud-nacos-sample,在项目中先添加我们的服务端的两个模块。
nacos-sample-api,暴露接口,作为服务提供者和服务消费者的接口
nacos-sample-provider,项目类型为springcloud
- spring-cloud-nacos-sample的pom依赖如下:
<?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>com.xmaven</groupId> <artifactId>spring-cloud-nacos-sample</artifactId> <packaging>pom</packaging> <version>1.0-SNAPSHOT</version> <modules> <module>nacos-sample-api</module> <module>nacos-sample-provider</module> <module>nacos-sample-consumer</module> </modules> <name>spring-cloud-nacos-sample</name> <!-- FIXME change it to the project's website --> <url>http://www.example.com</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> <spring-cloud.version>Greenwich.SR2</spring-cloud.version> </properties> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>2.1.11.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-alibaba-dependencies</artifactId> <version>2.1.1.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <pluginManagement> <plugins> <plugin> <artifactId>maven-clean-plugin</artifactId> <version>3.1.0</version> </plugin> <plugin> <artifactId>maven-resources-plugin</artifactId> <version>3.0.2</version> </plugin> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.0</version> </plugin> <plugin> <artifactId>maven-surefire-plugin</artifactId> <version>2.22.1</version> </plugin> <plugin> <artifactId>maven-jar-plugin</artifactId> <version>3.0.2</version> </plugin> <plugin> <artifactId>maven-install-plugin</artifactId> <version>2.5.2</version> </plugin> <plugin> <artifactId>maven-deploy-plugin</artifactId> <version>2.8.2</version> </plugin> <plugin> <artifactId>maven-site-plugin</artifactId> <version>3.7.1</version> </plugin> <plugin> <artifactId>maven-project-info-reports-plugin</artifactId> <version>3.0.0</version> </plugin> </plugins> </pluginManagement> </build> </project>
- 在nacos-sample-api项目中定义一个接口IHelloService:
package com.xmaven; public interface IHelloService{ String sayHello(String name); }
-
nacos-sample-provider项目中的pom.xml文件中添加如下依赖:
<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter</artifactId> <exclusions> <exclusion> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-context</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-dubbo</artifactId> </dependency> <dependency> <groupId>com.xmaven</groupId> <artifactId>nacos-sample-api</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-alibaba-nacos-discovery</artifactId> <exclusions> <exclusion> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-context</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-context</artifactId> <version>2.1.1.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-autoconfigure</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-autoconfigure</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-autoconfigure</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-autoconfigure</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-autoconfigure</artifactId> </dependency> <!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api--> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-api</artifactId> <version>5.5.2</version> <scope>test</scope> </dependency> </dependencies>
-
在nacos-sample-provider中创建接口的实现类HelloServiceImpl,其中@service是Dubbo服务的注解,表示当前服务会发布成一个远程服务:
package com.xmaven; import org.apache.dubbo.config.annotation.Service; @Service public class HelloServiceImpl implements IHelloService { @Override public String sayHello(String s) { return "Hello World:"+s; } }
-
在nacos-sample-provider中提供Dubbo以及Nacos配置,用于声明Dubbo服务暴露的网络端口和协议,以及服务注册的地址信息,配置如下:
spring.application.name=spring-cloud-nacos-sample
dubbo.scan.base-packages=com.xmaven dubbo.protocol.name=dubbo
dubbo.protocol.port=20880
#服务注册中心地址:表示挂载到SpringCloud注册中心
dubbo.registry.address=spring-cloud://localhost
#Nacos服务注册中心地址
spring.cloud.nacos.discovery.server-addr=xx.xx.xx.xx:8848
启动后如下
-
消费端开发
在此工程中再创建一个springboot项目 nacos-sample-consumer
Maven如下:<properties> <java.version>1.8</java.version> <spring-cloud.version>Greenwich.SR2</spring-cloud.version> </properties> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <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> <version>2.1.1.RELEASE</version> </dependency> <dependency> <groupId>com.xmaven</groupId> <artifactId>nacos-sample-api</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-alibaba-nacos-discovery</artifactId> <version>2.1.1.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-api</artifactId> <version>5.5.2</version> <scope>test</scope> </dependency> </dependencies>
-
在application.properties添加配置信息
dubbo.cloud.subscribed-services=spring-cloud-nacos-sample
dubbo.scan.base-packages=com.xmaven
spring.application.name=nacos-sample-consumer
spring.cloud.nacos.discovery.server-addr=xx.xx.xx.xx:8848 #自己的集群中的任意一台机器即可
-
定义HelloController,用户测试Dubbo服务的访问
package com.xmaven; import org.apache.dubbo.config.annotation.Reference; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class HelloController { //实现dubbo服务的暴露。相当@Autowired @Reference private IHelloService helloService; @GetMapping("/say") public String sayHello(){ return helloService.sayHello("xmaven"); } }
先启动nacos-sample-provider在启动nacos-sample-consumer
通过curl http://xx.xx.xx.xx:8080/say
响应结果为Hello World:xmaven
最终效果如下:
评论区