nacos 的服务注册与发现

nacos的服务注册于发现。

这个要求服务统一注册到注册中心,然后调用的时候就不需要通过ip来调用,直接通过 服务名 即可。

服务提供者

  1. pom.xml配置,需要 spring-cloud-starter-alibaba-nacos-discovery 依赖

        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.cloud
            spring-cloud-starter-alibaba-nacos-discovery
        
    
  2. 指定一下 EnableDiscoveryClient 注解

    @SpringBootApplication
    @EnableDiscoveryClient
    public class ProviderApplication {
        public static void main(String[] args) {
            SpringApplication.run(ProviderApplication.class,args);
        }
    }
  3. 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
  4. 写个controller,用于测试

    @RestController
    public class TestController {
        @GetMapping("hello/{name}")
        public String sayHello(@PathVariable(value = "name") String name) {
            return "hello " + name;
        }
    }
  5. 启动application,可以在nacos后台的服务列表里面看到注册的服务。

服务消费者—基于RestTemplate

  1. pom.xml配置和provider一样。

        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.cloud
            spring-cloud-starter-alibaba-nacos-discovery
        
    
  2. 指定一下@EnableDiscoveryClient注解

    @SpringBootApplication
    @EnableDiscoveryClient
    public class ConsumerApplication {
        public static void main(String[] args) {
            SpringApplication.run(ConsumerApplication.class,args);
        }
    }
  3. 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
  4. consumer去调用provider,这里用基于HttpClient的RestTemplate,所以需要先定义个RestTemplate Bean。需要指定 @LoadBalanced

    @Configuration
    public class ConsumerConfiguration {
        @Bean
        @LoadBalanced
        public RestTemplate restTemplate(){
            return new RestTemplate();
        }
    }
  5. 写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);
        }
    }
  6. 启动,同理,可以在服务列表看到注册进去的服务。

  7. 测试。通过调用consumer的rest api接口就能获取到数据。

服务消费者—基于OpenFeign

  1. pom里面添加 spring-cloud-starter-openfeign 依赖

        org.springframework.cloud
        spring-cloud-starter-openfeign
    
  2. 添加注解 EnableFeignClients

    @SpringBootApplication
    @EnableDiscoveryClient
    @EnableFeignClients//启用
    public class ConsumerApplication {
        public static void main(String[] args) {
            SpringApplication.run(ConsumerApplication.class,args);
        }
    }
  3. 定义接口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);
    }

    不需要实现。

  4. 通过定义的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);//直接调用了方法。
        }
    }
  5. 测试一下。访问hello2/feign,成功。

服务提供者的负责均衡

nacos为服务提供了自动的负载均衡,默认使用 轮询 的策略。