tags: springcloud java
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>nacos-config-spring-boot-starter</artifactId>
<version>0.2.1</version>
</dependency>
Add configuration in the configuration file
#NACOS Configuration Center
nacos.config.server-addr=119.22.91.21:8848
Then add annotations to the startup class
@NacosPropertySources(value = {
@NacosPropertySource(dataId = "consumer-server",autoRefreshed = true),
@NacosPropertySource(dataId = "provider-dubbo",autoRefreshed = true),
@NacosPropertySource(dataId = "email",autoRefreshed = true),
@NacosPropertySource(dataId = "system",autoRefreshed = true),
})
Then inject the parameters directly in the place where the parameters need to be injected;
@NacosValue(value = "${server.port}",autoRefreshed = true)
private String port;
Configuration sample
port=[email protected]
host=1467
key=aoiZ9812LA82*l1
useAuth=true
user=[email protected]
from=[email protected]
@NacosConfigurationPropertiesannotation
@Data
@Configuration
@NacosConfigurationProperties(dataId = "email",autoRefreshed = true)
public class EmailConfig {
private String port;
private String host;
private String key;
private boolean useAuth;
private String user;
private String from;
// This kind of configuration can only be said to create a singles bean. In fact, even if the cloud is updated, it will not be updated. There are only many cases that really take effect.
@Bean(name = "emailConf")
public EmailConf getConf(){
return BeanUtil.copyProperties(this,EmailConf.class);
}
// So you get newly generated every time, of course you can return this directly to
public EmailConf getConfs(){
return BeanUtil.copyProperties(this,EmailConf.class);
}
}
@NacosConfigurationProperties+Annotation
Configuration sample
email.port=huatian@163.com
email.host=1467
email.key=aoiZ9812LA82*l1
email.useAuth=true
email.user=huatian@163.com
email.from=huatian@163.com
springboot integrates nacos as a configuration center and registration center springboot integrates nacos as the registration center and configuration center 1. Pom file add dependency Next, create a ...
Write a directory title here 1. Introduction to NACOS 2. NACOS integrated SpringBoot dependencies 3. YAML configuration 4. Verify Refresh refresh 1. Introduction to NACOS NACOS Ali's open source confi...
springboot integrates nacos as a registration and configuration center Be sure to pay attention to the version My springboot version is2.3.12.RELEASE The nacos server selects 2.0.3 and the client is 2...
Purpose NACOS is the registration center and configuration center of the SpringBoot service. Modify the configuration file in Nacosserver, and get the modified content without restarting SpringBoot. T...
Recent projects should be in the NACOS configuration center, and the registration center uses another NACOS, and the configuration center is temporarily separated. The original project is configured w...
Spring version requirements Because Spring.prfiles.Active is available from the Spring 3.1 version, and this attribute is needed in this integration, so the requirements for the Spring version are abo...
First select the compatible version Graduation version (recommended) Go to the official website and download the Nacos server Tags · alibaba/nacos · GitHuban easy-to-use dynamic service ...
table of Contents Brief description Download Nacos Springboot Add NACOS Dependent Configuring NACOS in Application.yml Startup configuration Test data by MVC (Controller) Postman test Change NACOS con...
NACOS realizes the heat update of the configuration file. You can read the latest value of the NACOS configuration center without restarting the service. SpringBoot project reads various methods of lo...
Packet Version you choose NACOS configuration file bootstrap.yml Bootstrap.yml is preferred than Application.yml Springboot configuration file...