SpringCloudAlibaba之Nacos服务的注册与发现
教程首页链接:SpringCloudAlibaba实战教程系列
-----------------------------目录------------------------------------
1、构建父maven项目,引入pom依赖,对应第一步。
2、构建服务提供者,对应第二步。
3、构建服务消费者,对应第三步。
-----------------------------目录------------------------------------
第一步:在父pom的项目中引入dependencyManagement。
在引入父pom之前咱们先来回顾下dependencyManagement与使用他的原因
什么是dependencyManagement:他可以统一管理项目的版本号,确保应用的各个子项目的依赖和版本一致,当需要变更版本号的时候只需要在父类容器里更新,不需要任何一个子项目的修改;如果某个子项目需要另外一个特殊的版本号时,只需要在自己的模块dependencies中声明一个版本号即可。子类就会使用子类声明的版本号,不继承于父类版本号。
为什么用dependencyManagement:首先在springCloudAlibaba的项目中对于版本的依赖要求很严格,所有需要统一来声明管理每隔依赖的版本号。版本之间的关系参考SpringCloudAlibaba实战教程系列
<dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Greenwich.SR3</version> <type>pom</type> <scope>import</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>2.1.8.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-alibaba-dependencies</artifactId> <version>2.1.0.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
在引入上面的版本声明之后在父pom中引入下面几个将要使用的依赖
<dependencies> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies>
完整的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.niu.cloud</groupId> <artifactId>cloud-alibaba</artifactId> <packaging>pom</packaging> <version>1.0-SNAPSHOT</version> <modules> <module>nacos-provider</module> <module>nacos-consumer</module> </modules> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Greenwich.SR3</version> <type>pom</type> <scope>import</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>2.1.8.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-alibaba-dependencies</artifactId> <version>2.1.0.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <!--lombok--> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <!--spring web--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <repositories> <!--指定阿里云镜像库下载依赖--> <repository> <id>central</id> <name>aliyunmaven</name> <url>http://maven.aliyun.com/nexus/content/groups/public/</url> </repository> </repositories> </project>
第二步:构建服务提供者进行服务注册
1、在父项目中创建子module项目名字为nacos-provider,在pom中引入nacos服务注册依赖
<dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency>
完整服务提供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"> <parent> <artifactId>cloud-alibaba</artifactId> <groupId>com.niu.cloud</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>nacos-provider</artifactId> <dependencies> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
2、application.yml或者properties文件
server:
port: 8080
spring:
#服务应用名字
application:
name: nacos-provider
#配置注册ip:端口,注意即使是80端口也不可能省略
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
3、配置启动服务的启动类。为简单化我这里http调用接口也开发出来了
package com.niu.cloud; import lombok.extern.slf4j.Slf4j; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; /** * @author niunafei * @function * @email niunafei0315@163.com * @date 2020/5/1 12:46 PM * @EnableDiscoveryClient 开启服务注册 */ @EnableDiscoveryClient @SpringBootApplication @RestController @Slf4j public class ProviderApplication { public static void main(String[] args) { SpringApplication.run(ProviderApplication.class, args); } /** * @return */ @GetMapping("/provider") public String provider() { log.info("服务提供者正常服务"); return "服务提供者正常服务"; } }
4、在服务启动的时候看到如下日志证明注册成功
5、查看http://localhost:8848/nacos,查看服务的注册情况
第三步:构建服务消费者进行服务调用
1、在父项目中创建子module项目名字为nacos-comsumer,在pom中引入nacos服务注册依赖
<dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency>
完整服务提供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"> <parent> <artifactId>cloud-alibaba</artifactId> <groupId>com.niu.cloud</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>nacos-comsumer</artifactId> <dependencies> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
2、application.yml或者properties文件
server:
port: 8081
spring:
#服务应用名字
application:
name: nacos-consumer
#配置注册ip:端口,注意即使是80端口也不可能省略
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
3、服务消费有两种实现方式:
方式1(推荐):简单理解是在创建RestTemplate对象的时候添加 @LoadBalanced注解,该注解为底层为ribben来实现的负载均衡,详情代码如下:
package com.niu.cloud;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
/**
* @author niunafei
* @function
* @email niunafei0315@163.com
* @date 2020/5/1 3:51 PM
*/
@Slf4j
@SpringBootApplication
@EnableDiscoveryClient
public class ConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class, args);
}
/**
* 创建服务调用的对象
*
* @return
* @LoadBalanced 添加此注解后,RestTemplate就具有了ribben客户端负载均衡能力
*/
@Bean
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
}
@RestController
public class ConsumerController {
@Autowired
private RestTemplate restTemplate;
@GetMapping("/consumer")
public String consumer() {
log.info("---------消费者开始------------");
//调用服务
String result = restTemplate.getForObject("http://nacos-provider/provider", String.class);
log.info("---------消费者结束--------result{}----", result);
return result;
}
}
}
方式2:是springcloudalibaba的官网案列形式,依赖LoadBalancedClient来实现。
package com.niu.cloud; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.ServiceInstance; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.cloud.client.loadbalancer.LoadBalancerClient; import org.springframework.context.annotation.Bean; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; /** * @author niunafei * @function * @email niunafei0315@163.com * @date 2020/5/1 3:51 PM */ @Slf4j @SpringBootApplication @EnableDiscoveryClient public class ConsumerApplication { public static void main(String[] args) { SpringApplication.run(ConsumerApplication.class, args); } /** * 创建服务调用的对象 * * @return */ @Bean public RestTemplate restTemplate() { return new RestTemplate(); } @RestController public class ConsumerController { @Autowired private LoadBalancerClient client; @Autowired private RestTemplate restTemplate; @GetMapping("/consumer") public String consumer() { log.info("---------消费者开始------------"); //获取服务对象,注意nacos-provider是服务提供者应用名称 ServiceInstance instance = client.choose("nacos-provider"); //调用服务 String result = restTemplate.getForObject(instance.getUri() + "/provider", String.class); log.info("---------消费者结束--------result{}----", result); return result; } } }
4、启动consumer服务,浏览器访问消费者http://localhost:8081/consumer接口,可以看到下面服务者正常提供服务。
5、看下方式一消费者调用日志如下图: 首先是正常的注册成功,看接下来的就是ribben实现负载均衡功能的调用日志(大概的流程是,通过完整的url进行拆分,根据服务名获取注册的服务列表,然后负载到某个服务,拼接ip与端口完善url,进行http调用,返回结果)。
教程首页链接:SpringCloudAlibaba实战教程系列
为何一个@LoadBalanced注解就能让RestTemplate拥有负载均衡的能力?【享学Spring Cloud】