Nacos as a distribution center - Basic Configuration
New cloudalibaba-config-nacos-client3377
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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>cloud2020</artifactId>
<groupId>pers.zhang.springcloud</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>cloudalibaba-config-nacos-client3377</artifactId>
<dependencies>
<!--nacos-config-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
<!--nacos-discovery-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!--web + actuator-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!--一般基础配置-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</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>
</dependency>
</dependencies>
</project>
Configuration:
Nacos and SpringCloud-config as initialization during the project, to ensure that the distribution center start to configure pull, pull after successful configuration, in order to guarantee the normal startup items.
Springboot loading sequence is present in the configuration file priorities,bootstrap优先级高于application。
bootstrap.yml:
server:
port: 3377
spring:
application:
name: nacos-config-client
cloud:
nacos:
discovery:
server-addr: localhost:8848 #Nacos服务注册中心地址
config:
server-addr: localhost:8848 #Nacos作为配置中心地址
file-extension: yaml #指定格式
# #{spring.application.name}-${spring.profile.active}.${spring.cloud.nacos.config.file-extension}
application.yml:
spring:
profiles:
active: dev #指定为开发环境
Start categories:
@EnableDiscoveryClient
@SpringBootApplication
public class NacosConfigClientMain3377 {
public static void main(String[] args) {
SpringApplication.run(NacosConfigClientMain3377.class, args);
}
}
Controller:
@RestController
@RefreshScope //支持Nacos的动态刷新
public class ConfigClientController {
@Value("${config.info}")
String configInfo;
@GetMapping("/config/info")
public String getConfigInfo() {
return configInfo;
}
}
Nacos add configuration information
Official website:https://nacos.in/zh-cn/docs/quick-start-spring-cloud.html
Nacos matching rules:
Description: The reason why you need to configure spring.application.name, because it is part of the configuration management constitute Nacos dataId field.
In Nacos Spring Cloud, a full format dataId as follows:
${prefix}-${spring.profile.active}.${file-extension}
prefix
The default isspring.application.name
the value, you can also configure the itemspring.cloud.nacos.config.prefix
to configure.spring.profile.active
The current environment is the corresponding profile, details can refer to the Spring Boot documentation. Note: -spring.profile.active
is empty, the corresponding connector-
will not present,dataId
splicing into the format${prefix}.${file-extension}
-file-exetension
is arranged content data format configuration item canspring.cloud.nacos.config.file-extension
be configured. Currently only supportsproperties
andyaml
types.
The final format:{spring.application.name}-${spring.profile.active}.${spring.cloud.nacos.config.file-extension}
New configuration:
test:
Start 3377 micro-services, go to: localhost:3377/config/info
successfully obtain configuration:
nacos comes with dynamic refresh:
Nacos modify the configuration file for the version = 2, call interface again:
Nacos as a distribution center - Classification Configuration
Multi-multi-project management to solve environmental problems.
Configuration Management:
public as the default namespace reserved.
Namespace + Group + Data ID of the relationship:
Similar inside Java package and class name, the outermost layer can be used to distinguish namespace deployment environment, to distinguish between two targets on the Group Data ID and logical.
Default:Namespace=public, Group=DEFAULT_GROUP,默认Cluster是DEFAULT
Nacos default namespace is public, Namespace is mainly used to achieve isolation.
For example, now has three environments: development, test and production environment, we can create three Namespace, between different Namespace is isolated.
Group default DEFAULT_GROUP, Group different microstructures can be classified into the same packet service inside.
Service is micro service; a Service can contain multiple Cluster (cluster), Nacos default Cluster is DEFAULT, Cluster is a virtual division of the designated micro-services. For example, disaster recovery, the Service micro-services are deployed in the engine room and Hangzhou, Guangzhou room, then you can give room Service in Hangzhou micro-services from a cluster name (HZ), the Service to Guangzhou room service from a micro cluster name (GZ), also allows room with a king i service call each other, in order to improve performance.
Instance is an example of micro-services.
Reading specified spring.profile.active different configurations of Data ID and profile to make different environments
- + + Default spatial default grouping and test two new Data ID dev
Dev New Configuration Data ID:
New test Configuration Data ID:
In this case, the reading can be carried out by a multi-environment configuration file spring.profile.active properties:
group grouping scheme:
Configuration to an increase in group config. Or may be configured to DEV_GROUP TEST_GROUP.
namespace program:
New dev / test of Namespace:
according to domain configuration to fill:
Modify bootstrap.yml, add a namespace:
application.yml: