springcloudAlibaba---Nacos configuration center

Use of Nacos Configuration Center

Nacos provides a key/value store for storing configuration and other metadata, providing server-side and client-side support for externalized configuration in distributed systems. Using Spring Cloud Alibaba Nacos Config, you can centrally manage the external property configuration of your Spring Cloud application in Nacos Server.
1. Maintenance 2. Timeliness 3. Security
Springcloud config comparison
Three major advantages:
springcloud config is used in conjunction with git in most scenarios. Dynamic changes also need to rely on the Spring Cloud Bus message bus to pass all client changes
.
springcloud config does not Provides a visual interface
nacos config that uses long polling to update the configuration. Once the configuration changes, the process of notifying the provider is very fast, beating the
original springcloud config by a few blocks in speed.

nacos configuration center

1. Prepare the configuration. Create a new nacosconfig.properties Namespace in nacos server
: representing different environments, such as development, testing, and production environments.
Group: represents a certain project, such as XX medical project, XX e-commerce project DataId: There are often several projects (microservices) under each project, and each configuration set (DataId) is the main configuration file startup
of a project (microservice)
Permissions, modify application.properties
nacos.core.auth.enabled=true
2. Build nacos-config service

通过 Nacos Server 和 spring-cloud-starter-alibaba-nacos-config 实现配置的动态变更
1)引入依赖
1 <dependency>
2 <groupId>com.alibaba.cloud</groupId>
3 <artifactId>spring‐cloud‐starter‐alibaba‐nacos‐config</artifactId>
4 </dependency>
2)添加bootstrap.properties
1 spring.application.name=nacos‐config
2 # 配置中心地址
3 spring.cloud.nacos.config.server‐addr=127.0.0.1:8848
4
5 # dataid 为 yaml 的文件扩展名配置方式
6 # `${
    
    spring.application.name}.${
    
    file‐extension:properties}`
7 spring.cloud.nacos.config.file‐extension=yaml
8 #profile粒度的配置 `${
    
    spring.application.name}‐${
    
    profile}.${
    
    file‐extension:properties}`
9 spring.profiles.active=prod
3) 启动服务,测试微服务是否使用配置中心的配置
1 @SpringBootApplication
2 public class NacosConfigApplication {
    
    
3
4 public static void main(String[] args) {
    
    
5  ConfigurableApplicationContext applicationContext = SpringApplication.run(NacosConfigApplication.class, args);
6  String userName = applicationContext.getEnvironment().getProperty("common.name");
7  String userAge = applicationContext.getEnvironment().getProperty("common.age");
8  System.out.println("common name :"+userName+"; age: "+userAge);
9 }
10 }

1.3 Config related configuration
Nacos data model Key is uniquely determined by a triplet. Namespace defaults to an empty string, public namespace (public), and group defaults to
DEFAULT_GROUP
to support dynamic update of configuration.

1 @SpringBootApplication
2 public class NacosConfigApplication {
    
    
3
4 public static void main(String[] args) throws InterruptedException {
    
    
5  ConfigurableApplicationContext applicationContext = SpringApplication.run(NacosConfigApplication.class, args);
6
7 while(true) {
    
    
8 //当动态配置刷新时,会更新到 Enviroment中,因此这里每隔一秒中从Enviroment中获取配置
9  String userName = applicationContext.getEnvironment().getProperty("common.name");
10  String userAge = applicationContext.getEnvironment().getProperty("common.age");
11  System.err.println("common name :" + userName + "; age: " + userAge);
12  TimeUnit.SECONDS.sleep(1);
13 }
14
15 }
16
17 }
18

In addition to the default configuration file, other dataId must be suffixed to
support profile granular configuration
springcloudstarteralibabanacosconfig. When loading the configuration, not only the dataid is loaded as
spring. application. name. {spring.application.name}.s pr in g . a ppl i c a t i o n . nam e . {file-extension:properties} is the basic configuration of the prefix, and the dataid is
spring . application . name − {spring.application.name} -The basic configuration of s p r in g . a ppl i c a t i o n . nam e {profile}.${file-extension:properties}. If you encounter different configurations in multiple environments during daily development
, you can configure it through the ${spring.profiles.active} configuration item provided by Spring.

spring.profiles.active=dev

profile's configuration file is larger than the default configuration file. And form a complementary
ps: only the default configuration file will be applied to
support the configuration of custom namespace
for tenant-granular configuration isolation. Configurations of the same Group or Data ID can exist in different namespaces. One of the common scenarios of Namespace
is the differentiation and isolation of configurations in different environments, such as the isolation of resources (such as configurations and services) between development and testing environments and production environments.
In the absence of explicitly specifying the ${spring.cloud.nacos.config.namespace} configuration, the Public
namespace on Nacos is used by default. If you need to use a custom namespace, you can achieve it through the following configuration:

 spring.cloud.nacos.config.namespace=71bb9785‐231f4eca‐b4dc‐6be446e12ff8

Supporting custom Group configuration
Group is one of the dimensions of organizational configuration. Group configuration sets by a meaningful string (such as Buy or Trade) to distinguish
configuration sets with the same Data ID. When you create a configuration on Nacos, if the name of the configuration group is not filled in, the name of the configuration group defaults to
DEFAULT_GROUP. Common scenarios for configuration grouping: Different applications or components use the same configuration type, such as database_url configuration and
MQ_topic configuration.
In the absence of an explicit ${spring.cloud.nacos.config.group} configuration, the default is DEFAULT_GROUP. If you need to customize your own
Group, you can do so through the following configuration:

spring.cloud.nacos.config.group=DEVELOP_GROUP

Data Id configuration that supports custom extensions
Data ID is one of the dimensions for organizational division configuration. Data IDs are often used to organize configuration sets that divide a system. A system or application can contain multiple configuration
sets, and each configuration set can be identified by a meaningful name. Data ID usually uses the naming
rules of Java-like packages (such as com.taobao.tc.refund.log.level) to ensure global uniqueness. This naming rule is optional.
By customizing the extended Data Id configuration, you can not only solve the problem of configuration sharing between multiple applications, but also support one application to have multiple configuration files.

1 # 自定义 Data Id 的配置
2 #不同工程的通用配置 支持共享的 DataId
3 spring.cloud.nacos.config.sharedConfigs[0].data‐id= common.yaml
4 spring.cloud.nacos.config.sharedConfigs[0].group=REFRESH_GROUP
5 spring.cloud.nacos.config.sharedConfigs[0].refresh=true
6
7 # config external configuration
8 # 支持一个应用多个 DataId 的配置 一定要加扩展名
9 spring.cloud.nacos.config.extensionConfigs[0].data‐id=ext‐config‐common01.properties
10 spring.cloud.nacos.config.extensionConfigs[0].group=REFRESH_GROUP
11 spring.cloud.nacos.config.extensionConfigs[0].refresh=true
12
13 spring.cloud.nacos.config.extensionConfigs[1].data‐id=ext‐config‐common02.properties
14 spring.cloud.nacos.config.extensionConfigs[1].group=REFRESH_GROUP
15 spring.cloud.nacos.config.extensionConfigs[1].refresh=true

1.4 Priority of configuration
Spring Cloud Alibaba Nacos Config currently provides three configuration capabilities to pull related configurations from Nacos.
A: Support the configuration of multiple shared Data Ids through spring.cloud.nacos.config.shared-configs
B: Support multiple extended Data through spring.cloud.nacos.config.ext-config[n].data-id Id configuration
C: Automatically generate relevant Data Id configuration through internal related rules (application name, application name + Profile)
. When the three methods are used together, their priority relationship is: A < B < C, priority
from high to low:

  1. nacosconfigproduct.yaml precise configuration
  2. nacosconfig.yaml Common configuration for different environments in the same project
  3. extconfig: Different project extension configurations
  4. shareddataids common configuration for different projects

1.5 @RefreshScope
@Value annotation can obtain the value of the configuration center, but it cannot dynamically sense the modified value. You need to use the @RefreshScope annotation.

1 @RestController
2 @RefreshScope
3 public class TestController {
    
    
4
5  @Value("${common.age}")
6 private String age;
7
8  @GetMapping("/common")
9 public String hello() {
    
    
10 return age;
11 }
12
13 }

Guess you like

Origin blog.csdn.net/wangjunlei666/article/details/129877279