Spring Boot configuration Dynamic Refresh

Source: Internet
Author: User

The spring cloud version that this article uses for testing is:

Dalston.sr1

Many friends only know that Spring Cloud Config can refresh the configuration of remote git into memory.

Without knowing that the client of Spring Cloud Config can be used away from the server,

It is also not known that the Spring Cloud Config client, combined with actuator, can refresh the local configuration file into memory.

The following are the specific practices:

1, Pom:

<?XML version= "1.0" encoding= "UTF-8"?><Projectxmlns= "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">    <modelversion>4.0.0</modelversion>    <groupId>Com.liuyx</groupId>    <Artifactid>Test-config-refresh</Artifactid>    <version>1.0-snapshot</version>    <Parent>        <groupId>Org.springframework.boot</groupId>        <Artifactid>Spring-boot-starter-parent</Artifactid>        <version>1.5.4.RELEASE</version>        <RelativePath/> <!--Lookup parent from repository -    </Parent>    <Dependencies>        <Dependency>            <groupId>Org.springframework.boot</groupId>            <Artifactid>Spring-boot-starter-web</Artifactid>        </Dependency>        <Dependency>            <groupId>Org.springframework.cloud</groupId>            <Artifactid>Spring-cloud-starter-config</Artifactid>        </Dependency>        <!--Monitoring +refresh Configuration -        <Dependency>            <groupId>Org.springframework.boot</groupId>            <Artifactid>Spring-boot-starter-actuator</Artifactid>        </Dependency>    </Dependencies>    <dependencymanagement>        <Dependencies>            <Dependency>                <groupId>Org.springframework.cloud</groupId>                <Artifactid>Spring-cloud-dependencies</Artifactid>                <version>Dalston.sr1</version>                <type>Pom</type>                <Scope>Import</Scope>            </Dependency>        </Dependencies>    </dependencymanagement></Project>

The Spring-boot-starter-actuator or Spring-cloud-starter-config (the Spring Cloud Config client) is not exposed to the/refresh endpoint alone, Both are introduced before the/refresh endpoint can be exposed.

2, the general use of Spring-cloud-starter-config article will let you in the bootstrap to add the configuration Center server address, here we want to detach from the configuration Center server, so these configurations are not required at all.

3. Use @Value annotations for attributes that need to be refreshed, and mark classes with @RefreshScope annotations, as shown in the following example:

 Packagecom.liuyx.test;ImportOrg.springframework.beans.factory.annotation.Value;Importorg.springframework.boot.SpringApplication;Importorg.springframework.boot.autoconfigure.SpringBootApplication;ImportOrg.springframework.cloud.context.config.annotation.RefreshScope;Importorg.springframework.web.bind.annotation.RequestMapping;ImportOrg.springframework.web.bind.annotation.RestController, @SpringBootApplication @restcontroller@refreshscope  Public classMain { Public Static voidMain (string[] args) {Springapplication.run (main).class); }    Private Static intPort; @Value ("${server.port}")     Public voidSetport (intPort) {         This. port=Port; } @RequestMapping ("/port")     Public intPort () {returnPort; }}

Here my variable is a static variable, so you can only add @value annotations on non-static set methods, not on top of a variable definition line. If it is not a static variable, you can write directly:

    @Value ("${server.port}")    privateint port;

4. Application.properties Configuration

server.port=80localtest=hello1management. Security.enabled=false

5. Testing

1, start the project, Access Http://localhost/port display 80

2, modify the classpath (note is classpath, that is, your compiled class file in the directory) under the application.properties will server.port changed to 801

  

3. Send Empty post (note is post) request to Http://localhost:80/refresh

4. Re-visit Http://localhost/port Display 801 Test success

The final supplement:

Even with the use of the configuration Hub server, the method is valid, and all valid configuration files of valid locations (such as Git, jar, jar) are scanned and overwritten in a certain order, overwriting sequential portals

Finished

Spring Boot configuration Dynamic Refresh

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.