Spring Cloud Config provides external configuration support for distributed systems for both the server and the client. The configuration server provides a centralized external configuration for all environments for each application. It implements a mapping of the spring environment and propertysource abstractions to both the server side and the client, so it can be used in applications that run in any other language, except for applications that are built by spring. As an application that can be tested or put into production by deploying pipelines, we can create configurations for these environments separately, and get the configuration of the corresponding environment to run when the environment needs to be migrated.
The configuration server uses Git to store configuration information by default, which facilitates versioning of the environment configuration and provides easy management and access to configuration content through the GIT client tool. Of course he also provides a way to store localized file systems, and here are two ways to use distributed configuration to store the configuration content of a microservices application's multi-environment.
Building Config Server
Building a config Server from spring cloud is simple and requires only three steps:
- The dependency is introduced in Pom.xml
spring-cloud-config-server
and the full dependency configuration is as follows:
123456789101112131415161718192021222324252627282930313233 |
<parent> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-parent</artifactid> <version>1.3.5.release</version> <relativepath/> <!--lookup parent from repository to</parent> <dependencies> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-test</artifactid> <scope>test</scope> </dependency> <dependency> <groupid>org.springframework.cloud</groupid> <artifactid>spring-cloud-config-server</artifactid> </dependency> </dependencies> <dependencymanagement> <dependencies> <dependency> <groupid>org.springframework.cloud</groupid> <artifactid>spring-cloud-dependencies</artifactid> <version>brixton.release</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencymanagement> |
- Create a spring boot program main class and add
@EnableConfigServer
annotations to open config Server
123456789 |
@EnableConfigServer @SpringBootApplication public class application { public static void main(string[] args) { new Springapplicationbuilder (Application.class). Web (True). Run (args); } |
application.properties
Configure the service information and git information, such as:
12345678 |
spring.application.name=config-serverserver.port=7001 # git admin configuration spring.cloud.config.server.git.uri=http ://git.oschina.net/didispace/springboot-learning/spring.cloud.config.server.git.searchpaths=chapter9-1-4/ Config-repospring.cloud.config.server.git.username=usernamespring.cloud.config.server.git.password=password |
- Spring.cloud.config.server.git.uri: Configuring the Git repository location
- Spring.cloud.config.server.git.searchPaths: Configure the relative search location under the warehouse path to configure multiple
- Spring.cloud.config.server.git.username: User name to access the GIT repository
- Spring.cloud.config.server.git.password: Access the user password for the GIT repository
Here, using a configuration center implemented with Spring Cloud config and using git to manage content, start the app and start the following content successfully.
Spring Cloud Config also provides a way to configure the local storage. We only need to set properties spring.profiles.active=native
, and config server will retrieve the configuration file from the app's directory by default src/main/resource
. You can also spring.cloud.config.server.native.searchLocations=file:F:/properties/
specify the location of the configuration file through properties. Although Spring Cloud config provides such functionality, it is recommended to use GIT in order to support better functionality for managing content and versioning.
Server-side validation
In order to verify the configuration server completed above, a Config-repo directory was created under http://git.oschina.net/didispace/SpringBoot-Learning/Chapter9-1-4/as the configuration repository, The following four configuration files are created according to different environments:
- Didispace.properties
- Didispace-dev.properties
- Didispace-test.properties
- Didispace-prod.properties
Where a from property is set with different values for each configuration file, such as:
- from=git-default-1.0
- from=git-dev-1.0
- from=git-test-1.0
- from=git-prod-1.0
In order to test version control, in master, we add a suffix of 1.0, create a config-label-test branch, and use 2.0 as a suffix for the values in each configuration file.
After completing these preparations, we will be able to access our configuration directly via a browser or postman tool.
The URL is mapped to the configuration file as follows:
- /{application}/{profile}[/{label}]
- /{application}-{profile}.yml
- /{label}/{application}-{profile}.yml
- /{application}-{profile}.properties
- /{label}/{application}-{profile}.properties
The URL above maps the {application}-{profile}.properties
corresponding configuration file, which {label}
corresponds to a different branch on Git, and defaults to master.
We can try to construct different URLs to access different configuration content, such as: To access the Config-label-test branch, didispace application PROD environment, you can pass this url:http://localhost:7001/ Didispace/prod/config-label-test
12345678910111213141516171819202122 |
{ "name": "Didispace", "profiles": [ ], "label": { "name": http://git.oschina.net/didispace/SpringBoot-Learning/Chapter9-1-4/ Config-repo/didispace-prod.properties ", "git-prod-2.0" }},{ " Http://git.oschina.net/didispace/SpringBoot-Learning/Chapter9-1-4/config-repo/didispace.properties ", }}]} |
Micro-Server Mapping configuration
After you have completed and verified the Configuration Service center, let's look at how we can get configuration information in the MicroServices app.
- Create a spring boot application that introduces Spring-cloud-starter-config dependencies in Pom.xml, complete dependencies as follows:
1234567891011121314151617181920212223242526272829303132333435363738 |
<parent> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-parent</artifactid> <version>1.3.5.release</version> <relativepath/> <!--lookup parent from repository to</parent> <dependencies> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-test</artifactid> <scope>test</scope> </dependency> <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> </dependencies> <dependencymanagement> <dependencies> <dependency> <groupid>org.springframework.cloud</groupid> <artifactid>spring-cloud-dependencies</artifactid> <version>brixton.release</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencymanagement> |
- Create the most basic spring boot startup main class
12345678 |
@SpringBootApplication public class application { public static void main(string[] args) { new Springapplicationbuilder (Application.class). Web (True). Run (args); } |
- Create
bootstrap.properties
a configuration to specify config server, for example:
123456 |
Spring.application.name=didispacespring.cloud.config.profile=devspring.cloud.config.label= masterspring.cloud.config.uri=http://localhost:7001/server.port=7002 |
- Spring.application.name: The {application} section in the corresponding pre-configuration file
- Spring.cloud.config.profile: The {profile} section in the corresponding pre-configuration file
- Spring.cloud.config.label: Git branch that corresponds to the pre-configuration file
- Spring.cloud.config.uri: Address of the configuration center
It is important to note that the above properties must be configured in the bootstrap.properties
config section to be loaded correctly. Because the configuration of config will precede application.properties
, and bootstrap.properties
the load is before application.properties
.
- Create a REST API to return the From property of the configuration center, as follows:
1234567891011121314 |
@RefreshScope @RestController class TestController { @Value ("${from}")private String from; @RequestMapping ("/from") Public String from() { return this.from;} } |
Configure @Value("${from}")
the From property in the service by binding.
Launch the app and access: Http://localhost:7002/from, we can output the from content of the corresponding environment according to the configuration content.
In the above example, ${from} does not directly define the From property in the configuration file and will error.
Spring Cloud Building MicroServices Architecture (iv) Distributed configuration Center