Spring Cloud Alibaba系列之Nacos分布式配置中心
1、前言介绍
Spring Cloud Alibaba Nacos Config提供用于存储配置和其他元数据的 key/value 存储,为分布式系统中的外部化配置提供服务器端和客户端支持,nacos config是Spring Cloud config Server和config Client的替代方案
2、环境准备
- 环境准备:
- 64bit JDK 1.8
- SpringBoot2.3.7.RELEASE
- SpringCloud(Hoxton.SR9)
- SpringCloudAlibaba2.2.2.RELEASE
- Maven 3.2+
- 开发工具
- IntelliJ IDEA
- smartGit
3、新建项目
使用阿里提供的初始化服务链接创建Spring Initializer项目,链接:https://start.aliyun.com
选择jdk版本,packaging方式为jar
选择阿里Nacos的Nacos Configuration配置
服务订阅服务的加上nacos服务订阅 , Nacos Service Discovery
测试需要,可以增加 “Spring Web” 和 “Spring Boot Actuator” 组件,没有安装IDEA开发软件,可以在阿里的平台生成代码:https://start.aliyun.com/bootstrap.html
也可以用代码方式添加,添加阿里的nacos-config
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
生成的配置参考:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example.springcloudalibaba</groupId>
<artifactId>springcloud-alibaba-nacos-config</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springcloud-alibaba-nacos-config</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<spring-boot.version>2.3.7.RELEASE</spring-boot.version>
<spring-cloud-alibaba.version>2.2.2.RELEASE</spring-cloud-alibaba.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>${spring-cloud-alibaba.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.3.7.RELEASE</version>
<configuration>
<mainClass>com.example.springcloudalibaba.nacosconfig.SpringcloudAlibabaNacosConfigApplication
</mainClass>
</configuration>
<executions>
<execution>
<id>repackage</id>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
4、启动Nacos服务
详情可以参考官网:https://nacos.io/zh-cn/docs/quick-start.html,需要先下载nacos服务端源码,下载源码后编译启动项目:
window+R启动cmd窗口,cd到nacos server的bin目录,linux系统直接使用cd ${nacos_server_home}/bin
./startup.sh -m standalone
window系统使用命令startup.cmd -m standalone
启动成功,访问:http://127.0.0.1:8848/nacos,账号密码都是nacos
登录成功,来到主页:
5、添加Nacos配置
Data ID 由应用名(springcloud-alibaba-nacos-config)+ 文件后缀名(.properties) 组成,应用名写在配置文件spring.application.name=springcloud-alibaba-nacos-config
user.name=admin123
6、Nacos配置文件
Nacos config配置,写在bootstrap.properties,bootstrap.properties配置文件的优先级比application.properties的优先级高
# Nacos帮助文档: https://nacos.io/zh-cn/docs/concepts.html
# Nacos认证信息
spring.cloud.nacos.config.username=nacos
spring.cloud.nacos.config.password=nacos
spring.cloud.nacos.config.contextPath=/nacos
# 设置配置中心服务端地址
spring.cloud.nacos.config.server-addr=127.0.0.1:8848
# Nacos 配置中心的namespace。需要注意,如果使用 public 的 namcespace ,请不要填写这个值,直接留空即可
# spring.cloud.nacos.config.namespace=
修改一下nacos服务地址:
注意:Nacos Server 地址必须配置在 bootstrap.properties 文件。
package com.example.springcloudalibaba.nacosconfig.nacosconfig;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.context.annotation.Configuration;
@EnableAutoConfiguration
@Configuration
public class NacosConfigConfiguration {
}
7、@Value属性刷新
使用 Nacos Config 实现 Bean @Value属性刷新
package com.example.springcloudalibaba.nacosconfig;
import com.example.springcloudalibaba.nacosconfig.bean.User;
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.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.PostConstruct;
import java.io.IOException;
import java.io.StringReader;
import java.util.Properties;
@SpringBootApplication
//@RefreshScope
@RestController
public class SpringcloudAlibabaNacosConfigApplication {
@Value("${user.name}")
private String userName;
@PostConstruct
public void init() {
System.out.println(String.format("从Nacos配置中心获取数据,username:%s",userName));
}
@GetMapping("/username")
public String username() {
return String.format("get user name : %s", userName);
}
public static void main(String[] args) {
SpringApplication.run(SpringcloudAlibabaNacosConfigApplication.class, args);
}
}
get user name : admin123
获取用户信息:User{name=‘admin’}
8、@ConfigurationProperties 属性刷新
使用 Nacos Config 实现 @ConfigurationPropertiesBean 属性刷新
package com.example.springcloudalibaba.nacosconfig.bean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.context.config.annotation.RefreshScope;
@RefreshScope
@ConfigurationProperties(prefix = "user")
public class User {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
'}';
}
}
9、动态刷新属性数据
Nacos Confg 支持标准 Spring Cloud @RefreshScope特性,@RefreshScope
主键加在Application类就可以,除此之外,Nacos Confg 也引入了 Nacos Client 底层数据变化监听接口,即com.alibaba.nacos.api.config.listener.Listener
package com.example.springcloudalibaba.nacosconfig;
import com.example.springcloudalibaba.nacosconfig.bean.User;
import org.springframework.beans.factory.annotation.Autowired;
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.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@RefreshScope
@EnableConfigurationProperties(User.class)
@RestController
public class SpringcloudAlibabaNacosConfigApplication {
@Autowired
private User user;
@GetMapping("/user")
public String user() {
return String.format("获取用户信息:%s" , user);
}
public static void main(String[] args) {
SpringApplication.run(SpringcloudAlibabaNacosConfigApplication.class, args);
}
}
ApplicationRunner 进行监听,主要属性变动了,就会自动刷日志
package com.example.springcloudalibaba.nacosconfig;
import com.alibaba.cloud.nacos.NacosConfigManager;
import com.alibaba.nacos.api.config.listener.AbstractListener;
import com.example.springcloudalibaba.nacosconfig.bean.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationRunner;
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.context.annotation.Bean;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.PostConstruct;
import java.io.IOException;
import java.io.StringReader;
import java.util.Properties;
@SpringBootApplication
@RefreshScope
@EnableConfigurationProperties(User.class)
@RestController
public class SpringcloudAlibabaNacosConfigApplication {
@Value("${user.name}")
private String userName;
@Autowired
private NacosConfigManager nacosConfigManager;
@PostConstruct
public void init() {
System.out.println(String.format("从Nacos配置中心获取数据,username:%s",userName));
}
@GetMapping("/username")
public String username() {
return String.format("get user name : %s", userName);
}
@Bean
public ApplicationRunner runner() {
return args -> {
String dataId = "springcloud-alibaba-nacos-config.properties";
String group = "DEFAULT_GROUP";
nacosConfigManager.getConfigService().addListener(dataId, group, new AbstractListener() {
@Override
public void receiveConfigInfo(String configInfo) {
System.out.println(String.format("[Listener] :%s" , configInfo));
System.out.println("[Before User] " + user);
Properties properties = new Properties();
try {
properties.load(new StringReader(configInfo));
String name = properties.getProperty("user.name");
user.setName(name);
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("[After User] " + user);
}
});
};
}
public static void main(String[] args) {
SpringApplication.run(SpringcloudAlibabaNacosConfigApplication.class, args);
}
}
10、Nacos Config其它配置
全局关闭配置
关闭nacos配置,可以通过如下代码配置:spring.cloud.nacos.config.enabled = false
更多的配置
配置项
key
默认值
说明
服务端地址
spring.cloud.nacos.config.server-addr
Nacos 启动监听的ip地址和端口
配置对应的 DataId
spring.cloud.nacos.config.name
先取 prefix,再取 name,最后取 spring.application.name
配置对应的 DataId
spring.cloud.nacos.config.prefix
先取 prefix,再取 name,最后取 spring.application.name
配置对应的 DataId
spring.cloud.nacos.config.prefix
先取 prefix,再取 name,最后取 spring.application.name
配置内容编码
spring.cloud.nacos.config.encode
设置编码既可
Nacos Group配置
spring.cloud.nacos.config.group
DEFAULT_GROUP
配置对应的Group
配置文件拓展名
spring.cloud.nacos.config.fileExtension
properties
配置项对应的文件扩展名,目前支持 properties 和 yaml(yml)
获取配置超时时间
spring.cloud.nacos.config.timeout
3000
客户端获取配置的超时时间,单位毫秒
配置接入点
spring.cloud.nacos.config.endpoint
地域的某个服务的入口域名,通过此域名可以动态地拿到服务端地址
配置命名空间
spring.cloud.nacos.config.namespace
命名空间用于区分不同环境的配置,例如开发测试环境和生产环境的资源
AccessKey
spring.cloud.nacos.config.accessKey
阿里云上面的一个云账号名
SecretKey
spring.cloud.nacos.config.secretKey
阿里云上面的一个云账号密码
配置Nacos Server context path
spring.cloud.nacos.config.contextPath
Nacos Server对外提供的context path
配置Nacos集群
spring.cloud.nacos.config.clusterName
配置成Nacos集群名称
共享配置
spring.cloud.nacos.config.sharedDataids
共享配置的 DataId, “,” 分割
共享配置动态刷新
spring.cloud.nacos.config.refreshableDataids
共享配置中需要动态刷新的 DataId, “,” 分割
共享配置动态刷新
spring.cloud.nacos.config.extConfig
属性是个集合,内部由 ConfigPOJO 组成。Config有 3 个属性,分别是 dataId, group以及 refresh
11、Nacos Actuator监控
Nacos Config Actuator Endpoint,引入maven配置:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
配置文件:
management.endpoints.jmx.exposure.include=*
management.endpoints.web.exposure.include=*
management.endpoint.health.show-details=always
# Actuator Web 访问端口
management.server.port=8081
curl http://127.0.0.1:8081/actuator/nacos-config
{
"NacosConfigProperties":{
"serverAddr":"127.0.0.1:8848",
"username":"nacos",
"password":"nacos",
"encode":null,
"group":"DEFAULT_GROUP",
"prefix":null,
"fileExtension":"properties",
"timeout":3000,
"maxRetry":null,
"configLongPollTimeout":null,
"configRetryTime":null,
"enableRemoteSyncConfig":false,
"endpoint":null,
"namespace":null,
"accessKey":null,
"secretKey":null,
"contextPath":"/nacos",
"clusterName":null,
"name":null,
"sharedConfigs":null,
"extensionConfigs":null,
"refreshEnabled":true,
"sharedDataids":null,
"extConfig":null,
"refreshableDataids":null,
"configServiceProperties":{
"secretKey":"",
"contextPath":"/nacos",
"namespace":"",
"username":"nacos",
"enableRemoteSyncConfig":"false",
"configLongPollTimeout":"",
"configRetryTime":"",
"encode":"",
"serverAddr":"127.0.0.1:8848",
"maxRetry":"",
"clusterName":"",
"password":"nacos",
"accessKey":"",
"endpoint":""
}
},
"RefreshHistory":[
],
"Sources":[
{
"lastSynced":"2021-01-15 15:15:56",
"dataId":"springcloud-alibaba-nacos-config.properties"
},
{
"lastSynced":"2021-01-15 15:15:56",
"dataId":"springcloud-alibaba-nacos-config"
}
]
}
本博客的例子代码可以在github找到下载链接:代码下载
12、附录:参考资料
- Nacos中文手册:https://nacos.io/zh-cn/docs/what-is-nacos.html
- Nacos Github WIKI:https://github.com/alibaba/nacos/wiki
- Nacos官网博客:https://nacos.io/zh-cn/blog/index.html
本文同步分享在 博客“smileNicky”(CSDN)。
如有侵权,请联系 support@oschina.cn 删除。
本文参与“OSC源创计划”,欢迎正在阅读的你也加入,一起分享。