Spring Cloud Config is used to provide centralized external configuration support for infrastructure and microservice applications in distributed systems, divided into servers and clients.
The server is a distributed configuration center, which is an independent microservice application. The client is a basic configuration or micro-service application in a distributed system, and manages related configurations by specifying a configuration center.
The configuration hub built by Spring Cloud Config can be used in applications built in any other language in addition to spring-built applications.
By default, Spring Cloud Config uses Git to store configuration information and naturally supports version management of configuration information.
Build the configuration Center
Create the Spring Boot project config-server and follow these steps to complete the build of the configuration center.
1. Add dependencies
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
Copy the code
2. Enable the configuration center through annotations
package com.ulyssesss.configserver;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
@EnableConfigServer
@SpringBootApplication
public class ConfigServerApplication {
public static void main(String[] args) { SpringApplication.run(ConfigServerApplication.class, args); }}Copy the code
3. Modify the configuration
Name =config-server server.port=7001 # Git repository location spring.cloud.config.server.git.uri=https://github.com/Ulyssesss/spring-cloud-config-example.git # warehouse under relative search path, Configurable multiple spring. Cloud. Config. Server. Git. Search - paths = # config access git repository user name spring. Cloud. Config. Server. Git. The username = # access Git repository password spring. Cloud. Config. Server. Git. Password =Copy the code
4. Create the configuration repository and submit the modification
Properties and ulyssesss-dev. Properties. Add from=default-1.0 and from=dev-1.0, respectively.
Create the config-label-test branch and change the version number in the configuration file from 1.0 to 2.0.
After submitting the changes and pushing them to the remote repository, you can start the config-server and access the configuration information according to the following rules:
- / {application} / {profile} [ / {label} ]
- / {application} – {profile} . yml
- / {label} / {application} – {profile} . yml
- / {application} – {profile} . properties
- / {label} / {application} – {profile} . properties
The client obtains the configuration
Create the Spring Boot project config-client and write the client as follows:
1. Add 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>
Copy the code
2. Add configurations
Create bootstrap.properties and add the configuration for the configuration center.
spring.application.name=ulyssesss
spring.cloud.config.profile=dev
spring.cloud.config.label=config-label-test
spring.cloud.config.uri=http://localhost:7001
Copy the code
Note that the above properties must be configured in bootstrap.properties.
The Spring Boot application preferentially loads the configuration outside the application JAR package, and the config-server configuration through bootstrap.properties enables the application to obtain the external configuration from the config-server, which takes precedence over the local configuration.
3. Create a controller and view the configuration
Create ConfigClientController, by visiting http://localhost:8080/from for configuration.
package com.ulyssesss.configclient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TestConfigController {
@Value("${from}")
private String from;
@GetMapping("from")
public String from(a) {
returnfrom; }}Copy the code
Visit http://localhost:8080/from to get return results from = dev – 2.0. Modify the configuration of the spring. Cloud. Config. Profile and spring cloud. Config. Label, restart the application access link again to get the corresponding configuration information.
Service mode High availability configuration
In traditional high availability mode, no additional configuration is required. You only need to point all config-server instances to the same Git repository, and when the client specifies config-server, it points to the upper-layer load balancer device address.
Service mode Config-server is incorporated into Eureka service governance system and registered as a micro-service application. The client obtains instance information of the configuration center from the service registry through the service name.
1. Add a dependency on the server
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
Copy the code
2. Annotations enable service discovery
package com.ulyssesss.configserver;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.config.server.EnableConfigServer;
@EnableConfigServer
@EnableDiscoveryClient
@SpringBootApplication
public class ConfigServerApplication {
public static void main(String[] args) { SpringApplication.run(ConfigServerApplication.class, args); }}Copy the code
3. Add the server configuration
Name =config-server server.port=7001 # Git repository location spring.cloud.config.server.git.uri=https://github.com/Ulyssesss/spring-cloud-config-example.git # warehouse under relative search path, Configurable multiple spring. Cloud. Config. Server. Git. Search - paths = # config access git repository user name spring. Cloud. Config. Server. Git. The username = # access Git repository password spring. Cloud. Config. Server. Git. Password = # service registry address eureka.client.service-url.defaultZone=http://localhost:8761/eureka/Copy the code
4. Add client dependencies
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
Copy the code
5. Enable service discovery on the client
package com.ulyssesss.configclient;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@EnableDiscoveryClient
@SpringBootApplication
public class ConfigClientApplication {
public static void main(String[] args) { SpringApplication.run(ConfigClientApplication.class, args); }}Copy the code
6. Modify the client configuration
spring.application.name=ulyssesss spring.cloud.config.profile=test spring.cloud.config.label=config-label-test Uri =http://localhost:7001 # service registry address Eureka. Client. The service - url. DefaultZone = http://localhost:8761/eureka/ # enable configuration function of client service discovery Spring. Cloud. Config. Discovery. Enabled = true # specifies the service configuration center spring. Cloud. Config. Discovery. The service - id = config - serverCopy the code
Dynamically Refreshing configurations
Sometimes configurations need to be updated in real time. Spring Cloud Config enables this function through actuators.
1. Add a dependency on the client
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-actuator</artifactId>
</dependency>
Copy the code
2. Add refresh scope annotations
package com.ulyssesss.configclient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RefreshScope
@RestController
public class TestConfigController {
@Value("${from}")
private String from;
@GetMapping("from")
public String from(a) {
returnfrom; }}Copy the code
Changes the value of the configuration from the submitted to the remote warehouse, through the post method to http://localhost:8080/refresh to refresh the value of the configuration items.
Note that spring Boot 1.5 will enable security authentication by default. You can disable security authentication by following the configuration.
# close security authentication management. Security. Enabled = falseCopy the code
The original address
The sample code welcomes Star