1、简介
Nacos 致力于帮助您发现、配置和管理微服务。Nacos 提供了一组简单易用的特性集,帮助您快速实现动态服务发现、服务配置、服务元数据及流量管理。
2、准备工作
2.1、安装 MySQL8
从 Nacos 1.3.1 版本开始,数据库存储已经升级到8.0,并且它向下兼容
2.2、安装Nacos
http://ip:端口号/nacos

启动配置管理
- <dependency>
- <groupId>com.alibaba.cloudgroupId>
- <artifactId>spring-cloud-starter-alibaba-nacos-discoveryartifactId>
- <version>${latest.version}version>
- dependency>
在 application.properties 中配置 Nacos server 的地址:
- server.port=8070
- spring.application.name=service-provider
- spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
说明:这里配置的 spring.application.name ,在启动服务后也能在Nacos 服务列表中查看到
2、通过 Spring Cloud 原生注解 @EnableDiscoveryClient 开启服务注册发现功能:
- @SpringBootApplication
- @EnableDiscoveryClient
- public class NacosProviderApplication {
-
- public static void main(String[] args) {
- SpringApplication.run(NacosProviderApplication.class, args);
- }
-
- @RestController
- class EchoController {
- @RequestMapping(value = "/echo/{string}", method = RequestMethod.GET)
- public String echo(@PathVariable String string) {
- return "Hello Nacos Discovery " + string;
- }
- }
- }
3、添加一个消费者:
配置服务消费者,从而服务消费者可以通过 Nacos 的服务注册发现功能从 Nacos server 上获取到它要调用的服务。
在 application.properties 中配置 Nacos server 的地址:
- server.port=8080
- spring.application.name=service-consumer
-
- spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
通过 Spring Cloud 原生注解 @EnableDiscoveryClient 开启服务注册发现功能。给 RestTemplate 实例添加 @LoadBalanced 注解,开启 @LoadBalanced 与 Ribbon 的集成:
- @SpringBootApplication
- @EnableDiscoveryClient
- public class NacosConsumerApplication {
-
- @LoadBalanced
- @Bean
- public RestTemplate restTemplate() {
- return new RestTemplate();
- }
-
- public static void main(String[] args) {
- SpringApplication.run(NacosConsumerApplication.class, args);
- }
-
- @RestController
- public class TestController {
-
- private final RestTemplate restTemplate;
-
- @Autowired
- public TestController(RestTemplate restTemplate) {this.restTemplate = restTemplate;}
-
- @RequestMapping(value = "/echo/{str}", method = RequestMethod.GET)
- public String echo(@PathVariable String str) {
- return restTemplate.getForObject("http://service-provider/echo/" + str, String.class);
- }
- }
- }
4、启动 ProviderApplication 和 ConsumerApplication ,调用 http://localhost:8080/echo/2018,返回内容为 Hello Nacos Discovery 2018。
加入openFeign
- <dependency>
- <groupId>org.springframework.cloudgroupId>
- <artifactId>spring-cloud-starter-openfeignartifactId>
- dependency>
- package com.formiss.feign.service;
-
- @FeignClient(value = "service-provider",path = "/provider")
- public interface FeignClientService {
-
- @GetMapping("/echo")
- String echo();
- }
-
- @RestController
- @RequestMapping(path = "/provider")
- public class ProviderController {
-
- @GetMapping (value = "/echo")
- public String echo() {
- return "Hello Nacos Discovery " ;
- }
- }
-
- @SpringBootApplication
- @EnableFeignClients(value = "com.formiss.feign.service")
- public class NacosConsumerApplication {
-
- public static void main(String[] args) {
- SpringApplication.run(NacosConsumerApplication.class, args);
- }
- }
-
- @RestController
- @RequestMapping("/comsumer")
- public class UserController {
-
- @Autowired
- FeignClientService feignClientService;
-
- @GetMapping("/echo")
- public String echo(){
- return feignClientService.echo();
- }
- }

加入openfeign后 就不需要@EnableDiscoveryClient注解了