In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article shares with you the content of a sample analysis of dynamic refresh of spring boot configuration. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
The specific practices are as follows:
1 、 Pom: 4.0.0 com.liuyx test-config-refresh 1.0-SNAPSHOT org.springframework.boot spring-boot-starter-parent 1.5.4.RELEASE org.springframework.boot spring-boot-starter-web org.springframework.cloud spring-cloud-starter-config Org.springframework.boot spring-boot-starter-actuator org.springframework.cloud spring-cloud-dependencies Dalston.SR1 pom import
The introduction of spring-boot-starter-actuator or spring-cloud-starter-config (the client of springcloudconfig) alone will not expose the / refresh endpoint, but the / refresh endpoint will not be exposed until both are introduced.
2. Generally speaking, articles using spring-cloud-starter-config will let you add the address of the configuration center server to the bootstrap. Here, we want to separate the configuration center server from use, so these configurations are not needed at all. 3. Use @ Value annotation for the attributes that need to be refreshed, and mark the class with @ RefreshScope annotation. The example is as follows: package com.liuyx.test;import org.springframework.beans.factory.annotation.Value;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.context.config.annotation.RefreshScope;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController @ SpringBootApplication@RestController@RefreshScopepublic class Main {public static void main (String [] args) {SpringApplication.run (Main.class);} private static int port; @ Value ("${server.port}") public void setPort (int port) {this.port=port;} @ RequestMapping ("/ port") public int port () {return port;}}
Here my variable is a static variable, so I can only annotate the non-static set method with @ Value, not at the top of the variable definition line. If it is not a static variable, you can write directly:
@ Value ("${server.port}") private int port;4, application.properties configure server.port=80local.test=hello1management.security.enabled=false5, test
1. Start the project and visit http://localhost/port to display 80
2. Modify the application.properties under classpath (note that it is classpath, that is, the directory where your compiled class file is located) and change server.port to 801.
3. Send an empty post (post) request to http://localhost:80/refresh
4. Visit http://localhost/port again to show that the 801 test is successful
Finally, add:
Even if used in combination with the configuration center server, this method is effective, and the valid configuration files of all valid locations (such as those on git, within jar, and outside jar) will be scanned and overwritten according to a certain order.
Thank you for reading! This is the end of the article on "sample analysis of dynamic refresh of spring boot configuration". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.