Spring Cloud Getting Started Tutorial (iii): Configuring automatic Refresh

Source: Internet
Author: User
Tags continuous integration tools

Prior to the configuration management, only when the app starts to read the contents of git, and then as long as the app does not restart, git file modification, the application is not aware of, even if restarting config server.

Like the Hello World app in the previous unit (Spring Cloud Starter Tutorial (ii): Configuration Management), manually update the contents of the configuration file config-client-dev.properties in Git (don't forget to push to the server with Git)

Hello=hello World from GIT version 1

Refresh Http://locahost/8881/hello, the page content is still the same as before, does not reflect the latest changes in git, restart Config-server same, no changes. To make the client application aware of this change, Spring cloud provides the solution that the client can refresh the configuration content with the POST request /refresh method.

1. Let the client support the/refresh method

For/refresh to take effect, the client needs to add some code support:

1). First, add the following dependencies in Pom.xml. spring-boot-starter-actuatoris a set of monitoring functions that can monitor the state of the program at run time, including /refresh the functionality.

< Dependency >  < groupId >org.springframework.boot</groupId>  <  Artifactid>spring-boot-starter-actuator</artifactid>  </dependency>

2). Second, to open the refresh mechanism, you need to load the variables of the class above @RefreshScope注解 , the other code can not make any changes, then when the client executes, it /refresh will update the value of the variables below this class, including through the Config client from git to get the configuration.

  @SpringBootApplication @restcontroller  @ Refreshscope    public   Class   configclientapplication { public  static  void   main (string[] args) {Springapplication.run (configclientapplication. ) class      "${hello}" ) String Hello; @RequestMapping (Value  = "/hello"  public< /span> String hello () { return   hello; }}

3). Launch the app to view Http://localhost:8881/hello

4). Modify the contents of the Config-client-dev.properties again

Hello=hello World from GIT version 2

5). Send the POST request with Chome postman: Http://localhost/refesh

Can be seen from the results of the post, this refresh refreshes the configuration variable has hello

6). Access Http://localhost/hello again, visible to the configuration has been refreshed

2. Refresh the configuration by Webhook Auto-Trigger/refresh method

Each time the configuration file in Git is modified, we still need to call the/refresh method (manual call or write code call), there is no way to let git configuration has changed automatically trigger the client's rfresh mechanism? The answer is: yes. The push command can be monitored via the githook provided by Git, and if the project uses continuous integration tools such as Jenkins (also using Githook to listen), you can listen to the/refresh method directly in event handling.

Previous: Spring Cloud Getting Started Tutorial (ii): Configuration Management

Reference:

Http://www.cnblogs.com/ityouknow/p/6906917.html

Spring Cloud Getting Started Tutorial (iii): Configuring automatic 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.