分布式配置nacos搭建踩坑指南(下)
注意:本文基于nacos 2.2.0,alibaba Spring Boot 2.6.11,eclipse,操作系统为windows.
一.环境搭建
1.下载nacos
在github上下载nacos的windows安装文件,网址为:https://github.com/alibaba/nacos/releases,这里选择2.2.0下载,选择.zip格式的即可,入下所示:
2.运行nacos
解压下载回来的zip文件至D盘,进入bin文件夹,运行:.startup.cmd -m standalone 启动nacos,如果看到" Nacos started successfully in stand alone mode. use embedded storage",恭喜!运行成功!如下所示:
3.登录配置界面:
在浏览器中访问:http://localhost:8848/nacos/,用户名和密码都是nacos,即可进入后台配置页面,如下所示:
第二部分 Spring Boot配置
1.下载alibaba Spring Boot脚手架
访问https://start.aliyun.com/bootstrap.html,Group输入:com.alibaba.cloud,Artifact:nacos-config-sample,组件选择:Nacos Configuration,Spring Web,Spring Boot Actuator,同时需要引入Spring Cloud下的Cloud Bootstrap。这个在阿里巴巴出版的电子书>中没有介绍。如果没有引入Cloud Bootstrap,在eclipse中运行后必报错,截图如下:
下面是完整的pom.xml文件:
4.0.0 org.springframework.boot spring-boot-starter-parent 2.6.11 com.alibaba.cloud nacos-config-sample 0.0.1-SNAPSHOT nacos-config-sample Demo project for Spring Boot 1.8 2021.0.4.0 2021.0.4 org.springframework.cloud spring-cloud-starter-bootstrap org.springframework.boot spring-boot-starter-actuator org.springframework.boot spring-boot-starter-web com.alibaba.cloud spring-cloud-starter-alibaba-nacos-config org.springframework.cloud spring-cloud-starter org.springframework.boot spring-boot-starter-test test org.springframework.cloud spring-cloud-dependencies ${spring-cloud.version} pom import com.alibaba.cloud spring-cloud-alibaba-dependencies ${spring-cloud-alibaba.version} pom import org.apache.maven.plugins maven-compiler-plugin 3.8.1 1.8 1.8 UTF-8 org.springframework.boot spring-boot-maven-plugin
2.配置namespace
访问http://localhost:8848/nacos/,在后台新建一个名为testnamespace的namespace,记住它的id:bd1caf11-e4f1-4dd0-9a4b-2c32041c64e2,待会在spring boot配置中要用到,如下所示:
3.配置Properties
在配置管理中,我们选择刚才新建的testnamespace命名空间,在这个下面创建配置,新增一个data id为:nacos-config-sample.properties 的配置,配置格式选择properties.注意,data id的格式为{spring boot 应用名}.properties,两边必须要一致,否则报错!如下所示:
4.配置Spring Boot:
1)配置bootstrap.properties
spring.cloud.nacos.config.username=nacos spring.cloud.nacos.config.password=nacos spring.cloud.nacos.config.contextPath=/nacos spring.cloud.nacos.config.server-addr=localhost:8848 spring.cloud.nacos.config.namespace=bd1caf11-e4f1-4dd0-9a4b-2c32041c64e2 spring.cloud.nacos.config.extension-configs[0].data-id=nacos-config-sample.properties spring.cloud.nacos.config.extension-configs[0].refresh=true
注意spring.cloud.nacos.config.namespace=bd1caf11-e4f1-4dd0-9a4b-2c32041c64e2,这个namespace就是上面新建命名空间的id.
2)配置application.properties
spring.application.name=nacos-config-sample server.port=8080 management.server.port=8081 management.endpoints.jmx.exposure.include=* management.endpoints.web.exposure.include=* management.endpoint.health.show-details=always
3)新建实体类
package com.alibaba.cloud.nacosconfigsample; import com.alibaba.nacos.api.config.annotation.*; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.cloud.context.config.annotation.RefreshScope; import org.springframework.cloud.context.scope.*; @RefreshScope //@ConfigurationProperties(prefix="user") public class User { private String name; private int age; public String getName() { return name; } public void setName(String name) { this.name=name; } public int getAge() { return this.age; } public void setAge(int age) { this.age=age; } }
4)Spring boot启动类
package com.alibaba.cloud.nacosconfigsample; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.cloud.context.config.annotation.RefreshScope; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.boot.autoconfigure.SpringBootApplication; import com.alibaba.nacos.api.config.annotation.*; import org.springframework.cloud.context.config.annotation.RefreshScope; import javax.annotation.*; @RestController @SpringBootApplication @RefreshScope //@EnableConfigurationProperties(User.class) public class NacosConfigSampleApplication { @Value("${user.name}") //import org.springframework.cloud.context.config.annotation.RefreshScope; private String userName; @Value("${user.age}") private int age; @PostConstruct public void init() { System.out.printf("[init] user age is: %s",userName); } // @Autowired // private User user; @RequestMapping("/user") public String user() { return "user :"+userName; //return user.getName(); } @PreDestroy public void destroy() { System.out.printf("[destroy] user age : %s ",userName); } public static void main(String[] args) { SpringApplication.run(NacosConfigSampleApplication.class, args); } }
5)修改配置中的user.的name,在spring boot中观察是否更新
文章来源于互联网:分布式配置nacos搭建踩坑指南(下)