Explanation of SpringCloud Config Configuration Center
- 2021-07-01 07:34:41
- OfStack
1. Create an Config Configuration Center project
1. Add dependencies
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
2. To start the class, you need to add @ EnableConfigServer
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
/**
* @author fusw
*/
@SpringBootApplication
@EnableConfigServer
public class ConfigCenterApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigCenterApplication.class, args);
}
}
3. Configuration files
eureka.instance.appname=base-iov-config
security.user.name=test
security.user.password=test
# GitLab You must have the configuration mode of .git Suffix
# Configure default git The address of the warehouse
spring.cloud.config.server.git.uri=http://xxxx/config-repo.git
# git Relative address under warehouse address, you can configure multiple, using " , "Segmentation
spring.cloud.config.server.git.search-paths=*
# Configuration center clone The address after the warehouse configuration file is stored
spring.cloud.config.server.git.basedir=/data/deploy/config-repo
spring.cloud.config.server.git.force-pull=true
# git Warehouse account number
spring.cloud.config.server.git.username=test
# git Warehouse password
spring.cloud.config.server.git.password=test
# Configure Others git The address of the warehouse spring.cloud.config.server.git.repos.x.uri, iot For example
spring.cloud.config.server.git.repos.iot.uri=http://xxxx/iot/config-repo.git
spring.cloud.config.server.git.repos.iot.search-paths=*
spring.cloud.config.server.git.repos.iot.basedir=/data/deploy/config-repo
spring.cloud.config.server.git.repos.iot.force-pull=true
# git Warehouse account number
spring.cloud.config.server.git.repos.iot.username=test
# git Warehouse password
spring.cloud.config.server.git.repos.iot.password=test
# Prefix 1 Must be configured to distinguish from the default warehouse
spring.cloud.config.server.git.repos.iot.pattern=Iot*
# Registry center
eureka.client.serviceUrl.defaultZone=http://xxx/eureka/
2. git repository for configuration
Create an git warehouse to store the configuration of each project. You can create folders according to the project in the project level 1 directory (the names of each project folder can be started at will, and the Config configuration center only finds the configuration according to the configuration file name), and then each project folder stores the configuration files of different environments.
For example:
Iot-TestProject-dev.yml
Iot-TestProject-prd.yml
Iot-TestProject-test.yml
It is best not to place a default configuration similar to test. yml, and if spring. profiles. active is not used in the individual project configuration files, the default configuration file will be loaded.
3. Each SpringCloud Project Configuration Access Configuration Center
In the bootstrap. properties configuration file under resource, the relevant configuration of the configuration center is as follows
# Use the configuration file of the default warehouse
spring.cloud.config.name=TestProject
# Use iot Configuration file of factory library
#spring.cloud.config.name=Iot-TestProject
# Which environment configuration file to use, in conjunction with the above configuration, determines which to use 1 Configuration files :TestProject -test.yml
spring.cloud.config.profile=test
# Correspondence Object for storing the profile repository git Branching
spring.cloud.config.label=master
spring.cloud.config.username=test
spring.cloud.config.password=test
# Open Config Service discovery support
spring.cloud.config.discovery.enabled=true
# Specify Server End-to-end name That is, Server End spring.application.name Value of
spring.cloud.config.discovery.serviceId=base-iov-config