In the previous blog, we placed the configuration files in our respective services, but there is one disadvantage in doing this. Once the configuration is modified, we must shut down and then modify the configuration files before going online. If there are few services, it is understandable to do this. However, if there are hundreds of services, distributed configuration management is needed at this time. And spring cloud config is created to solve this problem. The following is a combination of gitlab to implement the construction of a distributed configuration center. The spring cloud config configuration center consists of the server end and the client end.
Prerequisite: Create a new configuration file configserver-dev.properties under the project in gitlab
1. Configure Server
1. Add dependencies
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency>
2. Enable support in the Application main class
@EnableConfigServer
3. Configure the application.yml file
server: port: 8888 spring: application: name: config cloud: config: server: git: uri: https://gitlab.xxx.com/xxxxx/xxxx.git # Configure the address of the gitlab repository. Note that the search-paths: /config-repo # The relative address under the gitlab repository address can be configured with multiple, use, and split. username: your username # gitlab repository account password: your password # gitlab repository password
Note: If the configuration file is placed in the root directory of the Git repository, there is no need to use the searchPaths parameter. The configuration file in this example is in the config-repo directory, so use the searchPaths parameter to prompt the Config server to search for the config-repo subdirectory.
4. Start the server and enter http://localhost:8888/configserver/dev/master in the browser
{ "name": "configserver", "profiles": [ "dev" ], "label": "master", "version": "073cda9ce85a3eed00e406f4ebcc4651ee4d9b19", "state": null, "propertySources": [ { "name": "https://gitlab.xxx.com/xxxxx/project/config-repo/configserver.properties", "source": { "name": "chhliuxyh", "hello": "i'm the king of the world!!!", "profile": "profile-default" } } ] }You can see that the server side can already read the configuration file from gitlab. You can access resources on gitlab in the form below
/{application}/{profile}[/{label}] /{application}-{profile}.yml /{label}/{application}-{profile}.yml /{application}-{profile}.yml /{application}-{profile}.properties /{label}/{application}-{profile}.propertiesFor example, enter: http://localhost:8888/configserver-dev.yml in the browser, and the result is as follows:
hello: i'm the king of the world!!! name: chhliuxyh profile: profile-default
2. Configure the client
1. Add pom dependencies
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
2. Configure the bootstrap.yml file
Note: The configuration file here needs to be placed in the bootstrap.properties or bootstrap.yml file, because the configuration of config will precede application.properties, and the loading of bootstrap.properties is also precedeed by application.properties.
server: port: 8889 spring: application: name: configserver # It must be consistent with the prefix of the configuration file. For example, our configuration file name here is configserver-dev.properties, so we need to configure it as configserver cloud: config: uri: http://localhost:8888/ //Configure the url profile of the spring cloud config server: dev # Specify profile label: master # Specify the branch of the gitlab repository
3. Verify the client
Add a new controller to the client
package com.chhliu.springcloud.config; 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.GetMapping; import org.springframework.web.bind.annotation.RestController; @SpringBootApplication @RestController @RefreshScope //Annotation @RefreshScope indicates that the Config client also refreshes the injected property value when the server configuration changes public class SpringcloudConfigClientApplication { public static void main(String[] args) { SpringApplication.run(SpringcloudConfigClientApplication.class, args); } @Value("${hello}") // Read the properties in the gitlab configuration file. If we read the value, it means that the client is OK private String profile; @GetMapping("/hello") public String hello() { return this.profile; } }Visit in the browser: http://localhost:8889/hello, the result is as follows:
i'm the king of the world!!!
This means that the client can already obtain the value from the server.
3. Dynamic refresh
Update Spring Cloud Config managed configurations without restarting the client
1. Update the property value corresponding to hello in the configserver-dev.properties configuration file in the gitlab repository
2. Visit http://localhost:8888/configserver/dev/master and find that the server side content has been updated
3. Send a POST request to the Conf client http://localhost:8889/refresh, and return 200 OK. Visit http://localhost:8889/hello again, it can be seen that the read attribute value has been dynamically updated without restarting the client service.
PS: To achieve dynamic refresh, you need to add the following starter to the pom file
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.