Previous article: Nacos Server cluster construction
Table of contents
Add configuration set
- Just create a new one in the nacos configuration center
Refresh configuration items in real time
- rely
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
- Modify the configuration file
application.yml
tobootstrap.yaml
- Add config configuration
server:
port: 8083
spring:
application:
name: m-service-resume
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8 &serverTimezone=UTC
username: root
password: root
jpa:
database: MySQL
show-sql: true
hibernate:
naming:
physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl #避免将驼峰命名转换为下划线命名
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848,127.0.0.1:8849,127.0.0.1:8850
#集群名称
cluster-name: BJ
#所属命名空间
namespace: 59486577-18d5-459c-94ad-cbdf6f3d9d5a
#nacos config 配置
config:
server-addr: 127.0.0.1:8848,127.0.0.1:8849,127.0.0.1:8850
namespace: 59486577-18d5-459c-94ad-cbdf6f3d9d5a
group: DEFAULT_GROUP
file-extension: yaml
- Get the configuration config class and add
@RefreshScope
annotations
@RestController
@RequestMapping("/config")
//实时刷新注解
@RefreshScope
public class ConfigController {
@Value("${my.message}")
private String myMessage;
@Value("${mysql.url}")
private String mysqlUrl;
@GetMapping("/viewconfig")
public String viewconfig() {
return "msg==>" + myMessage + " mySqlUrl=>" + mysqlUrl;
}
}
- access
- Modify the content in the configuration, 100 is 200
- Real-time refresh completed
- Modify the content in the configuration, 100 is 200
Listen for queries
- nacos configuration center
dataId extension
Get multiple configuration files from nacos
- Create multiple configuration files in nacos
abc.yaml
,def.yaml
- Get multiple configuration files
cloud:
nacos:
discovery:
config:
server-addr: 127.0.0.1:8848,127.0.0.1:8849,127.0.0.1:8850
namespace: 59486577-18d5-459c-94ad-cbdf6f3d9d5a
group: DEFAULT_GROUP
file-extension: yaml
#获取多个配置文件
ext-config[0]:
data-id: abc.yaml
group: DEFAULT_GROUP
refresh: true #扩展dataId 的动态刷新
ext-config[1]:
data-id: def.yaml
group: DEFAULT_GROUP
refresh: true
- Restart service
- Modify the configuration in nacos and revisit the interface (extended configuration file dynamically refreshes)
dataId priority issue
Priority: dataId generated according to rules > extended dataId (for extended dataId, [n] The larger n, the higher the priority)
-
Copy the configuration
m-service-resume.yaml
in and change the number to 300, 400my.message
abc.yaml
def.yaml
-
Restart the project, the result
-
Priority between extended configuration files, new configuration items
-
result