1.准备工作

我这里选择用springboot-2.2.5.RELEASE版本,因为最新版的springboot没有对应的nacos版本,这里选择使用Nacos Spring Cloud,需要导入的依赖有:

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-web</artifactId>
  4. </dependency>
  5. <dependency>
  6. <groupId>com.alibaba.cloud</groupId>
  7. <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
  8. <version>2.2.5.RELEASE</version>
  9. </dependency>

2.服务注册

首先创建一个Maven项目,然后新增两个Module,一个为提供者一个为消费者(provider、consumer),目录如下:
1.png
然后我们分别配置提供者与消费者的配置文件:

提供者:

  1. server.port=8070
  2. spring.application.name=service-provider
  3. spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848

消费者:

  1. server.port=8080
  2. spring.application.name=service-consumer
  3. spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848

然后在提供者与消费者的启动类都加上@EnableDiscoveryClient注解

提供者关键代码:

  1. package com.zym.controller;
  2. import org.springframework.web.bind.annotation.PathVariable;
  3. import org.springframework.web.bind.annotation.RequestMapping;
  4. import org.springframework.web.bind.annotation.RequestMethod;
  5. import org.springframework.web.bind.annotation.RestController;
  6. @RestController
  7. public class TestController {
  8. @RequestMapping(value = "/echo/{string}", method = RequestMethod.GET)
  9. public String echo(@PathVariable String string) {
  10. return "Hello Nacos Discovery " + string;
  11. }
  12. @RequestMapping(value = "test",method = RequestMethod.GET)
  13. public String test(){
  14. return "success";
  15. }
  16. }

消费者关键代码:

  1. package com.zym.consumer;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.boot.SpringApplication;
  4. import org.springframework.boot.autoconfigure.SpringBootApplication;
  5. import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
  6. import org.springframework.cloud.client.loadbalancer.LoadBalanced;
  7. import org.springframework.context.annotation.Bean;
  8. import org.springframework.web.bind.annotation.PathVariable;
  9. import org.springframework.web.bind.annotation.RequestMapping;
  10. import org.springframework.web.bind.annotation.RequestMethod;
  11. import org.springframework.web.bind.annotation.RestController;
  12. import org.springframework.web.client.RestTemplate;
  13. @SpringBootApplication
  14. @EnableDiscoveryClient
  15. public class ConsumerApplication {
  16. @LoadBalanced
  17. @Bean
  18. public RestTemplate restTemplate() {
  19. return new RestTemplate();
  20. }
  21. public static void main(String[] args) {
  22. SpringApplication.run(ConsumerApplication.class, args);
  23. }
  24. @RestController
  25. public class TestController {
  26. private final RestTemplate restTemplate;
  27. @Autowired
  28. public TestController(RestTemplate restTemplate) {this.restTemplate = restTemplate;}
  29. @RequestMapping(value = "/echo/{str}", method = RequestMethod.GET)
  30. public String echo(@PathVariable String str) {
  31. return restTemplate.getForObject("http://service-provider/echo/" + str, String.class);
  32. }
  33. @RequestMapping(value = "/test",method = RequestMethod.GET)
  34. public String test(){
  35. return restTemplate.getForObject("http://service-provider/test",String.class);
  36. }
  37. }
  38. }

然后我们分别启动提供者与消费者,登录Nacos监控页面查看:
2.png
可以看到两个服务都注册到Nacos中了,然后我们分别在浏览器页面的url输入:

http://localhost:8080/echo/2021

http://localhost:8080/test

测试一下是否能调通:
3.png
4.png
至此,Nacos的简单服务注册发现就已经完成~