Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use svn+Spring Cloud Config as a configuration center

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces "how to use svn+Spring Cloud Config to do configuration center" related knowledge, editor through the actual case to show you the operation process, the method of operation is simple and fast, practical, I hope that this "how to use svn+Spring Cloud Config to do configuration center" article can help you solve the problem.

1. Add dependencies

Org.springframework.cloud spring-cloud-config-server org.tmatesoft.svnkit svnkit

Need to introduce more svnkitr packages

2. Configuration file

Server: port: 8001 spring: cloud: config: server: svn: uri: http://192.168.0.6/svn/repo/config-repo username: username password: password default-label: trunk profiles: active: subversion application: name: spring-cloud-config-server

Slightly different from the git version, you need to display the declaration subversion.

3. Startup class

The startup class has not changed, add @ EnableConfigServer to activate the support for configuration center

@ EnableConfigServer@SpringBootApplicationpublic class ConfigServerApplication {public static void main (String [] args) {SpringApplication.run (ConfigServerApplication.class, args);}}

4. Test

Server-side testing

Access: http://localhost:8001/neo-config-dev.properties, return: neo.hello: hello im dev, indicating that the server can read the configuration information in the svn code base normally. Modify the configuration information in the configuration file neo-config-dev.properties to: neo.hello=hello im dev update, visit http://localhost:8001/neo-config-dev.properties in the browser again, and return: neo.hello: hello im dev update. Indicates that the server will automatically read the latest submission

Client test

The client directly uses the previous sample project spring-cloud-config-client to test, and the configuration is basically unchanged. After starting the project, visit: http://localhost:8002/hello, return: hello im dev update indicates that the parameters have been correctly obtained from the server. Also modify the svn configuration and submit, and visit http://localhost:8002/hello`` again to get the old information, just like the git version.

Refresh

Now let's solve the legacy of the previous article, which still exists in the svn version. The Spring Cloud Config is divided into the server and the client. The server is responsible for publishing the configuration files stored in git (svn) into the REST interface, and the client can obtain the configuration from the server REST interface. However, the client can not actively perceive the change of the configuration and take the initiative to obtain the new configuration. How can the client take the initiative to get new configuration information? springcloud has provided us with a solution, and each client triggers its own / refresh through the POST method.

Modifying the spring-cloud-config-client project has reached the ability to refresh.

1. Add dependencies

Org.springframework.boot spring-boot-starter-actuator

Added spring-boot-starter-actuator package, spring-boot-starter-actuator is a set of monitoring functions, you can monitor the status of the program at run time, including the function of / refresh.

2. Turn on the update mechanism

You need to load @ RefreshScope on the class that loads the variable, and the value of the variable below this class will be updated when the client executes / refresh.

@ RestController@RefreshScope / / classes that use this annotation will automatically update the new configuration to the corresponding fields of the class when the configuration of the SpringCloud configuration center is refreshed. Class HelloController {@ Value ("${neo.hello}") private String hello; @ RequestMapping ("/ hello") public String from () {return this.hello;}}

3. Testing

Security authentication is enabled by default above springboot 1.5.X, so you need to add the following configuration in the configuration file application.properties

Management.security.enabled=false

OK has been modified in this way, and accessing http://localhost:8002/refresh in the way requested by post will update the modified configuration file.

Let's test again, first visit http://localhost:8002/hello and return: hello im dev, I change the value in the library to hello im dev update. Open cmd to execute curl-X POST http://localhost:8002/refresh on win, and return ["neo.hello"] indicating that the value of neo.hello has been updated. We visit http://localhost:8002/hello again and return: hello im dev update, the client has got the latest value.

It is also troublesome to refresh the client manually every time. Is there any way to automatically call the client to update as long as the code is submitted? github's webhook is a good way.

4 、 webhook

WebHook is to notify the recipient of information by sending a http post request when an event occurs. Webhook is used to monitor your events on Github.com, the most common of which is push. If you set up a Webhook that monitors push events, the Webhook will be triggered every time your project is submitted, and Github will send a HTTP POST request to your configured address.

In this way, you can automate repetitive tasks in this way, for example, you can use Webhook to automatically trigger the operation of continuous integration (CI) tools, such as Travis CI; or to deploy your online server through Webhook. The following figure shows the webhook configuration above github.

Payload URL: URL of callback after triggering

Content type: data format, two commonly used json

Secret: a string used to encrypt POST's body. Using HMAC algorithm

Events: list of events triggered.

Events event type description

Triggered when there is a push in the push warehouse. Default event

Create triggers when a branch or label is created

Delete triggers when a branch or tag is deleted

Svn also has a similar hook mechanism. A post-commit script is triggered after each submission, and we can write some post requests here.

In this way, we can use the mechanism of hook to trigger client updates, but when there are more and more clients, hook support is not elegant enough, and it is unrealistic to change hook every time we add a client.

This is the end of the content about "how to use svn+Spring Cloud Config to do the configuration center". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

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.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report