本章节讲解nacos的服务注册与发现的简单使用
如图所示:按照图示搭建一个nacos作为服务注册中心的简单应用
创建provider服务提供者
- 创建
nacos-discovery-provider
模块 - 引入依赖
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-bootstrap</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-loadbalancer</artifactId> </dependency> </dependencies>
- 创建SpringBoot主启动类
package cn.axj.nacos.discovery; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; @SpringBootApplication @EnableDiscoveryClient public class ProviderApplication { public static void main(String[] args) { SpringApplication.run(ProviderApplication.class, args); } }
这里一定记得要加上@EnableDiscoveryClient
注解,该注解表示该应用要注册到注册中心
去
- 配置bootstrap.yml
spring: application: name: nacos-discovery-provider cloud: nacos: config: group: nacos-discovery-group namespace: nacos-discovery server-addr: 192.168.56.102:8848,192.168.56.104:8848,192.168.56.105:8848 file-extension: yml prefix: ${spring.application.name} discovery: group: nacos-discovery-group namespace: nacos-discovery server-addr: 192.168.56.102:8848,192.168.56.104:8848,192.168.56.105:8848 weight: 1 server: port: 8081
- 创建TestController
@RestController public class TestController { //注入当前实例端口信息 @Value("${server.port}") private int port; @GetMapping("/service") public String service(){ return "provider service.i am from port " + port ; } }
- 启动项目
打开nacos面板,点击服务列表,未找到该服务。那是因为未创建命名空间所致,在nacos面包创建nacos-discovery命令空间
再次点击服务列表
创建服务消费者
- 创建nacos-discovery-consumer模块
- 同上引入相同依赖
- 创建ConsumerApplication主类
@SpringBootApplication @EnableDiscoveryClient public class ConsumerApplication { public static void main(String[] args) { SpringApplication.run(ConsumerApplication.class, args); } }
- 配置bootstrap.yml
spring: application: name: nacos-discovery-consumer cloud: nacos: config: group: nacos-discovery-group namespace: nacos-discovery server-addr: 192.168.56.102:8848,192.168.56.104:8848,192.168.56.105:8848 file-extension: yml prefix: ${spring.application.name} discovery: group: nacos-discovery-group namespace: nacos-discovery server-addr: 192.168.56.102:8848,192.168.56.104:8848,192.168.56.105:8848 server: port: 9091
5.服务调用方需要配置负载均衡,此处利用restTemplate去发起调用
新建一个LoadConfiguration类,配置RestTemplate实例
需要显示标注该@LoadBalanced注解开启服务注册的负载均衡
tips:@LoadBalanced标注了RestTemplate后,RestTemplate可以不用ip加端口的形式访问服务提供方,而是通过在注册中心注册的服务名称通过负载均衡策略,选取服务注册的地址,端口等信息,然后发起调用,并返回。也就是说@LoadBalanced提供了负载均衡和动态获取地址,端口等功能
@Configuration public class LoadConfiguration { @Bean @LoadBalanced //该注解一定不能忘记 public RestTemplate restTemplate(){ return new RestTemplate(); } }
- 创建TestController
@RestController public class TestController { @Value("${server.port}") private int port; @Resource private RestTemplate restTemplate; @GetMapping("/service") public String service(){ return "consumer service.from:" + port +"\t to \t" + restTemplate.getForObject("http://nacos-discovery-provider/service", String.class); } }
6.启动项目,查看nacos面板,观察nacos-discovery-consumer是否注册到nacos中
- 测试,浏览器访问http://localhost:9091/service,实际是访问consumer,通过consumer访问provider服务,返回信息
浏览器返回信息如下,consumer service.from:9091 to provider service.i am from port 8081
- 打开nacos-discovery-provider应用的多实例启动,配置server.port为8082,并启动8082端口实例
当8082启动完成后,打开nacos面包,观察nacos-discovery-provider信息变化
多次访问http://localhost:9091/service,观察返回变化
consumer service.from:9091 to provider service.i am from port 8082
consumer service.from:9091 to provider service.i am from port 8081
可以看到返回在8081,和8082之间反复,证明当前服务调用的负载策略为轮询
nacos discovery 配置