I. Overview
See Address:
Https://cloud.spring.io/spring-cloud-static/Edgware.SR3/single/spring-cloud.html#_spring_cloud_config
Https://gitee.com/itmuch/spring-cloud-book
1.1, why need unified management configuration
Centralized management, different environment configuration, dynamic adjustment of configuration during operation, automatic refresh
The default is configured in the configuration file, database, etc.
1.2. Introduction
There are two main kinds of distributed configuration zookeeper, Consul.
In fact, there are many, such as Baidu's disconf, Ali's diamond, Ctrip's Apollo
Provides server-side and client support for distributed system Extranet configuration. It includes the config server and config client two parts. Because both the config server and the Config client implement mappings to spring environment and propertysource abstractions, Spring Cloud config is well-suited for spring applications. Of course, it can be used in conjunction with applications written in any other language.
Config server is a scale-out, centralized configuration servers. It is used to centrally manage configurations in each environment of the application. Use Git to store configuration content by default (you can also use Subversion, local file system, or vault storage configuration.) Therefore, it is easy to implement version control and content auditing for configuration.
The config client is the config server clients that manipulate the configuration properties stored in config server.
1.3. Architecture diagram
The config server can have dev, stage, prod and other environments
Second, Config Server Development 2.1, project construction
Like git address: https://github.com/bjlhx15/spring-cloud.git
Add Pom
<Dependencies> <Dependency> <groupId>Org.springframework.cloud</groupId> <Artifactid>Spring-cloud-config-server</Artifactid> </Dependency> </Dependencies>
Start class Add annotations
@SpringBootApplication @enableconfigserver Public class configserverapplication { publicstaticvoid main (string[] args) { Springapplication.run (configserverapplication. class , args);} }
2.2. Add Configuration item git
such as: Https://github.com/bjlhx15/spring-cloud-config-test-repo.git
2.3. Increase in APPLICATION.YML
Server: 8080 Spring: Cloud: config: server: git: uri:https://github.com/bjlhx15/ Spring-cloud-config-test-repo
Start the project test.
2.4. Access Address
The HTTP service has the following form of resource mapping rules:
/{application}/{profile}[/{label}]/{application}-{profile}.yml/{label}/{application}- {profile}.yml/{application}-{profile}.properties/{label}/{application}-{profile}. Properties
In fact, according to the format of arbitrary splicing can. such as: Http://localhost:8080/aa-profile.properties or Http://localhost:8080/aa-profile.yml
Where label is the branch version of Git.
2.5. Testing
Increase the FOOBAR-DEV.YML test.
Access: HTTP://LOCALHOST:8080/FOOBAR-DEV.YML, Discovery is the configured file.
that is, Access priority : First find the matching file "label/specific file name", if the mismatch will find the application configuration file.
It is recommended to use a configuration file in the usual
Third, Config client development 3.1, create the project
Git address: https://github.com/bjlhx15/spring-cloud.git
Add Pom "Attention is spring-cloud-starter-config"
<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>
Configuration file
Add a Bootstrap.yml
Spring: Cloud: config: uri:http://localhost:8080 Name:foobar Profile:dev label:master # when the back-end storage of configserver is git, the default is master
Application: #和配置文件匹配最好
Name:foobar
In Application.yml
Server: 8040
The reason: Spring Cloud context
The Spring cloud application runs by creating a "bootstrap" context that is the parent context of the main application. The "BOOTSTRAP.YML priority is higher than application.yml" is out of the box and is responsible for loading configuration properties from an external source and also decrypting the properties in the local external configuration file. The two contexts share an environment, which is the source of the external properties of any spring application. The Bootstrap property is added with high priority, so they cannot be overwritten by the local configuration by default.
Bootstrap Program Context Default convention: Overwrite Application.yml (or. Properties) with Bootstrap.yml "Bootstrap.properties"
However, you can use Spring.cloud.bootstrap.enabled=false to disable
bootstrap.* configuration → Link config Server, after loading remote configuration → load application.* configuration
2.2. Increase the Startup class
@SpringBootApplication Public class configclientapplication { publicstaticvoid main (string[] args) { Springapplication.run (configclientapplication. class , args);} }
2.3. Increased testing
@RestController Public class Configclientcontroller { // Traditional mode requires configuration in configuration file, but priority is lower than configuration loaded with Bootstap @Value ("${ Profile} ,private String profile; @GetMapping ("/profile") public String getprofile () { return the. Profile; }}
Note the point:
1, Pom spring-cloud-starter-config do not lead the wrong;
2, do not use BOOTSTRAP.YML. Otherwise, the default is 8888 port
3, if the local attribute also has a remote attribute, to bootstrap.yml first with the local application, so use the remote property profile
4. Note that if the configuration file on Git is yml type, note that the property ":" Has a space after it.
0701-spring Cloud config-Profile, config server Development, config client development