nacos 的服務註冊與發現

nacos的服務註冊於發現。

這個要求服務統一註冊到註冊中心,而後調用的時候就不須要經過ip來調用,直接經過服務名便可。java

服務提供者

  1. pom.xml配置,須要spring-cloud-starter-alibaba-nacos-discovery 依賴web

    <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>
  2. 指定一下EnableDiscoveryClient 註解spring

    @SpringBootApplication
    @EnableDiscoveryClient
    public class ProviderApplication {
        public static void main(String[] args) {
            SpringApplication.run(ProviderApplication.class,args);
        }
    }
  3. application.yaml配置以下,這裏須要指定spring.application.nameapi

    spring:
      application:
        name: lou-nacos-service-provider
      cloud:
        nacos:
          discovery:
            server-addr: 127.0.0.1:8848
    server:
      port: 8080
  4. 寫個controller,用於測試app

    @RestController
    public class TestController {
        @GetMapping("hello/{name}")
        public String sayHello(@PathVariable(value = "name") String name) {
            return "hello " + name;
        }
    }
  5. 啓動application,能夠在nacos後臺的服務列表裏面看到註冊的服務。負載均衡

    pic

服務消費者---基於RestTemplate

  1. pom.xml配置和provider同樣。ide

    <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>
  2. 指定一下@EnableDiscoveryClient註解spring-boot

    @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。須要指定@LoadBalancedthis

    @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接口就能獲取到數據。

    pic

服務消費者---基於OpenFeign

  1. pom裏面添加spring-cloud-starter-openfeign依賴

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-openfeign</artifactId>
    </dependency>
  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,成功。

    pic

服務提供者的負責均衡

nacos爲服務提供了自動的負載均衡,默認使用輪詢的策略。

相關文章
相關標籤/搜索