SpringBoot project integrates NACOS with dynamic configuration files

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

Directly injected

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;

Add the configuration file to the injection of POJO

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);
   }
}

POJO injection with prefix

@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

Intelligent Recommendation

springboot integrates nacos as a configuration center and registration center

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 ...

SpringBoot integrates NacOS, YAML configuration, integrated version

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 center and configuration center

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...

SpringBoot integrates NACOS automatic refresh configuration

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...

Spring Project @Value Integrates NACOS Configuration Center

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...

More Recommendation

SpringMVC project integrates NACOS configuration center

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...

SpringBoot project, Nacos configuration use

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 ...

Springboot integrated NACOS configuration center and dynamic configuration

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 configuration 4 ways of hot update, Spring, SpringBoot read the project configuration files,@Value,@RefreshScope,@NacosConfigurationProperties

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...

Springboot + Nacos Dynamic Configuration - Personal Notes

Packet Version you choose NACOS configuration file bootstrap.yml Bootstrap.yml is preferred than Application.yml Springboot configuration file...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top