Nacos use as a distribution center
We have already introduced a filter Nacos is easier to build dynamic cloud-native application service discovery, configuration management and service management platform. So it can be used as registration centers and distribution center, as a registration center Nacos allows us the flexibility to configure a variety of environmental parameters, so that the project does not require too much attention to target environmental problems during packaging, a packaged achieve the project, many running aims. Today we'll explain how to use Nacos as a distribution center.
Demo
The first step : Configure the list of menu options click Nacos management platform "+" sign a new configuration, click the Publish button after the new configuration.
Configuration Rules
dataId: format $ {prefix} - $ {spring.profiles.active} $ {file-extension}.
The default value for the prefix spring.application.name
spring.profiles.active profile corresponding to the current environment
file-extension format for the configuration data content, may be configured by the configuration item spring.cloud.nacos.config.file-extension. Currently only type properties.
group 默认DEFAULT_GROUP
When activeprofile empty fill spring.application.name direct value to the default properties
Step Two : Create a new project (here nacos-provider prior to use), pom.xml added nacos dependent distribution center.
<!--Nacos注册中心-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
Step Three : Match Center Address.
spring:
application:
name: nacos-provider
cloud:
nacos:
discovery:
server-addr: 192.168.0.127:8848 #注册中心地址
config:
server-addr: 192.168.0.127:8848 #注册中心地址
server:
port: 17357
Step four : using the configuration parameters center
@SpringBootApplication
@EnableDiscoveryClient
public class NacosProvider {
public static void main(String[] args) {
SpringApplication.run(NacosProvider.class,args);
}
@RestController
@RefreshScope
class ConfigController {
@Value("${config.address}")
private String address;
@GetMapping("/print/address")
public String printAddress(){
return address;
}
}
}
Step Five : After using @RefreshScope notes to refresh parameter value Nacos distribution center to modify in real time.
Notes :
Nacos distribution center configuration items will be covered by the same key configuration items in the project.