1. Introduction
When there are more services, each service has its own configuration file; When the configuration file needs to be changed, the service needs to be restarted for the configuration file to take effect. Real-time updates are not possible. SpringCloud has its own configuration center config, a component that allows configuration files to be placed locally as well as in Git.
2. Configure the central server
Create a new project and introduce configuration dependency. It can be used as a configuration center server or registered in the registry as a micro service. When there are more services, multiple services can be enabled to achieve high availability.
<? The XML version = "1.0" encoding = "utf-8"? > < project XMLNS = "http://maven.apache.org/POM/4.0.0" XMLNS: xsi = "http://www.w3.org/2001/XMLSchema-instance" Xsi: schemaLocation = "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" > < the parent > < artifactId > XiaYuApplication < / artifactId > < groupId > com. Xiayu < / groupId > < version > 1.0 - the SNAPSHOT < / version > < / parent > The < modelVersion > 4.0.0 < / modelVersion > < artifactId > configcenter < / artifactId > < dependencies > < the dependency > <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> </dependencies> </project>
Add @EnableConfigServer annotation
package com.xiayu.config.center;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
@SpringBootApplication
@EnableConfigServer
@EnableEurekaClient
public class ConfigCenterApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigCenterApplication.class);
}
}
Configure the center profile
spring: application: name: configcenter cloud: config: server: git: uri: https://gitee.com/niruoanh/xiayuapplication # uris git public warehouse address Private warehouse address you need to set up user name and password, because # github.com access is very slow, Paths: /config # /config # username: root # password: root label: ' ' ' ' ' ' ' ' ' ' ' ' port: 8087 eureka: client: service-url: defaultZone: http://localhost:8083/eureka/
The file name of the configuration file: service name +profiles.yml such as: commonintegration-dev. yml configuration file and pathmap
/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties
3. Configure the central client
Introduction of depend on
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
You do not need to turn on the configuration client annotations. You can add them directly to the bootstrap.yml file
Spring: Application: Name: CommonIntegration Cloud: Config: Label: Master #profile: dev #profile discovery: enabled: true # set to retrieve data from configuration center Server: port: 8091 eureka: client: service-url: defaultZone: http://localhost:8083/eureka/ # registry urls
To start the project, you will first go to the configuration center and get the configuration file of the service
(in the process of building, meet the Java. Lang. An IllegalStateException: Instances of configservers in your application file should be set to your bootstrap.yml file. You can do so on your instances of bootstrap.yml.
4. Automatic update of configuration and encryption of configuration files
A. If A configuration file is changed and then committed to Git, the service needs to be restarted to apply the changed configuration file. This can be cumbersome.
B. There are many confidential fields in the configuration file, such as database user name and password, object storage key and secret key, send short message key and secret key, etc. Therefore, it is necessary to encrypt the configuration file. There are symmetric encryption and asymmetric encryption for configuration file encryption.
See the message bus in the next Spring Cloud tutorial