Springboot realizes dynamic modification of configuration after startup

Take SpringCloud as an example, hope the service will generate a dynamic instance ID based on some information after startup

That isapplication.yamlConfigured inEurekaConfiguration

eureka:
  instance:
    instance-id: ID

If we want to randomly generate one when the service startsinstance-id, First find

can useBeanPostProcessorModify Bean after instantiationeureka.instanceThe Bean to which this property is bound is passed inapplication.yaml Hold downCtril + left buttonClick to findorg.springframework.cloud.netflix.eureka.EurekaInstanceConfigBean

Then use the post processor to adjust the attributes after instantiation

@Component
public class EurekaConfigBeanPostProcessor implements BeanPostProcessor  {
    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        if (bean instanceof EurekaInstanceConfigBean) {
            EurekaInstanceConfigBean properties = (EurekaInstanceConfigBean) bean;
            String instanceId = properties.getInstanceId();
            System.out.println("Old InstanceID:      " + instanceId);
            instanceId =  UUID.randomUUID().toString();
            // modify ID
            properties.setInstanceId(instanceId);
        }
        return null;
    }
}

In this way, we can see our customized ID in the registry.

Here is mainly to find the Config object corresponding to the configuration in the configuration file.

The following is an example of SpringCloud consul dynamically modifying the instance ID, the configuration example of Consul isorg.springframework.cloud.consul.discovery.ConsulDiscoveryPropertiesclass:

@Component
public class ConsulConfigPostProcessorDemo implements BeanPostProcessor  {

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        if (bean instanceof ConsulDiscoveryProperties) {
            
            ConsulDiscoveryProperties properties = (ConsulDiscoveryProperties) bean;
            String instanceId = properties.getInstanceId();
            System.out.println("InstanceID:      " + instanceId);
            instanceId += "__MODIFY";
            properties.setInstanceId(instanceId);
        }
        return null;
    }
}

reference

[1]. Spring BeanpostProcessor interface implementation Demo

Intelligent Recommendation

SpringBoot project modification startup port

method one: Configure Server.Port in the Application configuration file (default defamation 8080) Method two: -Dserver.port=8082 Method three: Implement the WebServerFactoryCustomizer interface, rewri...

@ScHEDuled scheduling tasks in Springboot Dynamic Modification Based on NACOS Configuration

Spring frameworks come from 3.0, with task scheduling feature, it is a lightweight quartz, and it is convenient, simple, and does not need to rely on other JAR packets. It is more convenient to use in...

Java SpringBoot local debugging dynamic modification NACOS configuration information

、 nacos, nacos , 。 ? You can modify it when you start the NACOS configuration at the local SpringBoot project. 2. Take an example For example:The server.tomcat.basedir configuration configured in NACO...

SpringBoot after the war package, import the external configuration file at startup

I have always used all the configuration files into a single package. Later, because it is difficult to maintain the configuration files, I moved the configuration files out and relied on the outside ...

SpringBoot is executed after startup

If you want to perform some initialization operations after your SpringBoot project is started, you can operate by implementing the ApplicationRunner and CommandLinerunner. E.g: This is a simple demo ...

More Recommendation

[Springboot] Custom banner realizes dynamic configuration parameters through ${}

You can use ${} to dynamically get the properties in the configuration in the banner. Compared with writing some information to the banner, the advantage is that you don’t have to modify the ban...

Springboot Maven project profile configuration realizes dynamic switching environment

Before active writes a parameter to death, you need to change the parameter and restart to start Now change active to @env@ There must be the following configuration in pom   After reimport maven...

Springboot: Fourth configuration modification

1. Start the port to modify Application.properties server.port=8080 2. Different environment profiles support Application.properties 3. Configure the XML ---- Startup class Add configuration 4. Use ot...

Springboot configuration file modification

Spring boot uses a global profile Application.properties or Applicaton.yml in the src / main / resources directory, there is a blog that I wrote the order of the resource, this resource is also mentio...

SpringBoot startup configuration principle

Start an application for SpringBoot; its entry is: the main method in the following code; 1 2 3 4 5 6 7 @SpringBootApplication public class SpringbootMybatisApplication {    &...

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

Top