nacos 的服务注册于发现.
这个要求服务统一注册到注册中心, 然后调用的时候就不需要通过 ip 来调用, 直接通过服务名即可.
服务提供者
pom.xml 配置, 需要 spring-cloud-starter-alibaba-nacos-discovery 依赖
- <dependencies>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-web</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
- </dependency>
- </dependencies>
指定一下 EnableDiscoveryClient 注解
- @SpringBootApplication
- @EnableDiscoveryClient
- public class ProviderApplication {
- public static void main(String[] args) {
- SpringApplication.run(ProviderApplication.class,args);
- }
- }
application.YAML 配置如下, 这里需要指定 spring.application.name
- spring:
- application:
- name: lou-nacos-service-provider
- cloud:
- nacos:
- discovery:
- server-addr: 127.0.0.1:8848
- server:
- port: 8080
写个 controller, 用于测试
- @RestController
- public class TestController {
- @GetMapping("hello/{name}")
- public String sayHello(@PathVariable(value = "name") String name) {
- return "hello" + name;
- }
- }
启动 application, 可以在 nacos 后台的服务列表里面看到注册的服务.
服务消费者 --- 基于 RestTemplate
pom.xml 配置和 provider 一样.
- <dependencies>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-Web</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
- </dependency>
- </dependencies>
指定一下 @EnableDiscoveryClient 注解
- @SpringBootApplication
- @EnableDiscoveryClient
- public class ConsumerApplication {
- public static void main(String[] args) {
- SpringApplication.run(ConsumerApplication.class,args);
- }
- }
application.YAML 配置和 provider 的基本一样, 除了 spring.application.name
- spring:
- application:
- name: lou-nacos-service-consumer
- cloud:
- nacos:
- discovery:
- server-addr: 127.0.0.1:8848
- server:
- port: 8070
consumer 去调用 provider, 这里用基于 HttpClient 的 RestTemplate, 所以需要先定义个 RestTemplate Bean. 需要指定 @LoadBalanced.
- @Configuration
- public class ConsumerConfiguration {
- @Bean
- @LoadBalanced
- public RestTemplate restTemplate(){
- return new RestTemplate();
- }
- }
写 controller. 方法没有写具体 ip, 而是用了服务名 lou-nacos-service-provider 来访问.
- @RestController
- public class TestController {
- private final RestTemplate restTemplate;
- @Autowired
- public TestController(RestTemplate restTemplate) {
- this.restTemplate = restTemplate;
- }
- @GetMapping("hello/{name}")
- public String hello(@PathVariable("name") String name) {
- return restTemplate.getForObject("http://lou-nacos-service-provider/hello/" + name, String.class);
- }
- }
启动, 同理, 可以在服务列表看到注册进去的服务.
测试. 通过调用 consumer 的 REST API 接口就能获取到数据.
服务消费者 --- 基于 OpenFeign
pom 里面添加 spring-cloud-starter-openfeign 依赖
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-openfeign</artifactId>
- </dependency>
添加注解 EnableFeignClients
- @SpringBootApplication
- @EnableDiscoveryClient
- @EnableFeignClients// 启用
- public class ConsumerApplication {
- public static void main(String[] args) {
- SpringApplication.run(ConsumerApplication.class,args);
- }
- }
定义接口 UserService
- @FeignClient(name = "lou-nacos-service-provider")// 服务名.
- public interface UserService {
- @GetMapping("/hello/{name}")// 这里表示请求 lou-nacos-service-provider 的 / hello/{name} 地址
- String hello(@PathVariable(value = "name") String name);
- }
不需要实现.
通过定义的 UserService 接口调用服务, 还是原先的 TestController
- @RestController
- public class TestController {
- @Autowired// 启用了 FeignClient, 所以可以 Autowired
- private UserService userService;
- @GetMapping("hello2/{name}")
- public String hello2(@PathVariable("name") String name) {
- return userService.hello(name);// 直接调用了方法.
- }
- }
测试一下. 访问 hello2/feign, 成功.
服务提供者的负责均衡
nacos 为服务提供了自动的负载均衡, 默认使用轮询的策略.
来源: https://www.cnblogs.com/sheldon-lou/p/11462569.html