1. 安装nacos,这里采用docker安装,不清楚docker,请移步centos8.0 docker演示
1) 拉取最新版本nacos
docker pull nacos/nacos-server
2) 查看镜像
docker images
3) 启动nacos,这里做了端口映射,映射为8080
docker run --env MODE=standalone --name nacos -d -p 8080:8848 nacos/nacos-server
4) 访问nacos,默认用户名和密码均为nacos,http://127.0.0.1:8080/nacos/#/login
![c8b0a3e703a143d24edc11007b8ea432.png](https://i-blog.csdnimg.cn/blog_migrate/2e9433c3f318db468b2bc0c3904693c9.jpeg)
2. 测试服务注册于发现
1) 创建maven父项目nacos-test,删除src目录
2) 引入POM依赖
org.springframework.boot spring-boot-starter-parent 2.2.4.RELEASE org.springframework.boot spring-boot-starter-web com.alibaba.cloud spring-cloud-starter-alibaba-nacos-discovery 2.2.0.RELEASE groupId>com.alibaba.cloud spring-cloud-starter-alibaba-nacos-config 2.2.0.RELEASE org.springframework.cloud spring-cloud-starter-openfeign 2.2.1.RELEASE
3) 创建生产者
A. 在父项目nacos-test下创建module,此处命名为nacos-service
B. 创建启动类NacosServiceApplication.java
import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublic class NacosServiceApplication { public static void main(String[] args) { SpringApplication.run(NacosServiceApplication.class,args); }}
C. 创建配置文件bootstrap.yml,因为加载顺序的问题,此处必须是bootstrap.yml,而不是application.yml,否则引入了nacos config会启动失败
server: port: 8091 spring: application: #定义服务名 name: nacos-service cloud nacos: discovery: #注册中心地址 server-addr: http://127.0.0.1:8080 config: #配置中心地址 server-addr: http://127.0.0.1:8080 #组别 group: DEFAULT_GROUP #文件类别 file-extension: yaml #使用环境 profiles: active: dev
D. 创建类NacosService.java提供服务
import com.alex.nacosconfigfile.ProducerConfigInfo;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Value;import org.springframework.cloud.context.config.annotation.RefreshScope;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RestController;@RestControllerpublic class NacosService { @Value("${server.port}") private String serverPort; @Autowired private ProducerConfigInfo producerConfigInfo; @GetMapping("/sayHello") @RefreshScope public String sayHello(){ return "我是生产者,我的端口号为:"+serverPort+",nacos-config配置内容为:"+producerConfigInfo.getProducerContent(); }}
E. 创建配置文件ProducerConfigInfo.java类,用于测试nacos config
import org.springframework.beans.factory.annotation.Value;import org.springframework.cloud.context.config.annotation.RefreshScope;import org.springframework.context.annotation.Configuration; @Configuration@RefreshScopepublic class ProducerConfigInfo { @Value("${producer.content}") private String producerContent; public String getProducerContent() { return producerContent; } public void setProducerContent(String producerContent) { this.producerContent = producerContent; }}
F. 在控制台创建配置文件nacos-service-dev.yaml
![78553c10ae4f0aaed64ebfac6c8dc881.png](https://i-blog.csdnimg.cn/blog_migrate/be84b0488cfedbebe9ebf7180d2ffd99.jpeg)
G. 分别用8091和8092两个端口启动生产者项目,创建集群,以便验证负载均衡,查看nacos服务列表是否注册成功
![35d61bcea53827e8b17bcfd4b2ade9ec.png](https://i-blog.csdnimg.cn/blog_migrate/88f6cc5ea52722b7462633f938357256.jpeg)
4) 创建消费者
A. 在父项目nacos-test下创建module,此处命名为nacos-customer
B. 创建启动类NacosCustomerApplication.java
import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.openfeign.EnableFeignClients;@SpringBootApplication/***开启openfeign客户端*/@EnableFeignClientspublic class NacosCustomerApplication { public static void main(String[] args) { SpringApplication.run(NacosCustomerApplication.class,args); }}
C. 创建配置文件bootstrap.yml,因为是在父pom中引入的依赖,只要引入了nacos config的依赖,配置文件名称都必须是bootstrap.yml,且必须配置nacos config相关信息,否则启动失败
server: port: 9001spring: application: #定义服务名 name: nacos-customer cloud: nacos: discovery: #注册中心地址 server-addr: http://127.0.0.1:8080 config: #配置中心地址 server-addr: http://127.0.0.1:8080 #组别 group: DEFAULT_GROUP #文件类别 file-extension: yaml
D. 创建openfeign客户端,NacosServiceFeign.java
import org.springframework.cloud.openfeign.FeignClient;import org.springframework.web.bind.annotation.GetMapping;@FeignClient("nacos-service")public interface NacosServiceFeign { @GetMapping("sayHello") String sayHello();}
E. 创建消费者NacosCustomerService.java调用生产者接口
import com.alex.feign.NacosServiceFeign;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RestController;@RestControllerpublic class NacosCustomerService { @Autowired private NacosServiceFeign nacosServiceFeign; @GetMapping("/getTestResult") public String getTestResult() { String result = nacosServiceFeign.sayHello(); return "我是消费者,通过ribbon负载均衡调用sevice返回结果:" + result; }}
F. 启动消费者,并调用http://127.0.0.1:9001/getTestResult
动态均衡演示:
![7b3d53b5c5d2deadcaa40ec8229926ec.png](https://i-blog.csdnimg.cn/blog_migrate/2cf4067ff0adef8c6cd1f2a7e7791d9b.jpeg)
![bf143e972732b5b1d7f2742d853b03a4.png](https://i-blog.csdnimg.cn/blog_migrate/a97f21444c18e1231414ae2867e77049.jpeg)
动态配置修改演示,修改配置文件值为2222,旧值为1111,如图:
![c1dd303cd60a3f5457781ccfd587444b.png](https://i-blog.csdnimg.cn/blog_migrate/977b4e70e1578d35e5357b7c4b455739.jpeg)
![0586e0824e5e07f60affbd81371fdc96.png](https://i-blog.csdnimg.cn/blog_migrate/e1d3f1e01583c1ebe374fab669990449.jpeg)