My blog: Smile in Lanling Welcome to blog!
previous chapter Spring cloud basic tutorial (3) - Eureka advanced Among them, based on our basic understanding of Eureka, we have a deep understanding of some configurations used in Eureka high availability clusters and other production environments. This chapter begins to understand the configuration center in a distributed environment.
Preface
why configuration center is needed? In the application of monomer, configuration file can solve the configuration problem well. However, in the microservice architecture mode, the system is inevitably divided into many microservice components. If it is still in the previous way, the workload of configuration will be very large. Therefore, a general distributed configuration management center is essential. Spring Cloud provides another component Spring Cloud Config.
Spring Cloud Config provides server and client support. Based on Spring environment, it can integrate Spring seamlessly. The default implementation is based on Git warehouse. Of course, it also supports SVN, local files, and even custom implementation.
1, Quickly build Config Server configuration server
here we use Git as the storage warehouse of configuration file. First, we establish Maven project and introduce related dependency in Pom.xml file.
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency>
create the configserverapplication.java class, and use the @ enableconfig server annotation to indicate that the service is allowed to provide configuration management services in the form of HTTP:
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.config.server.EnableConfigServer; @SpringBootApplication @EnableConfigServer public class ConfigServerApplicaition { public static void main(String[] args) { SpringApplication.run(ConfigServerApplicaition.class, args); } }
add applicaiton.yml, and the configuration is as follows:
spring: application: name: single cloud: config: server: git: uri: https://gitee.com/lnxxs/springCloudConfig.git password: username: search-paths: conf label: master
- spring.cloud.config.server.git.uri: configure Git's warehouse location.
- spring.cloud.config.server.git.search-paths: configure the relative search location under Git's warehouse path.
- Spring.cloud.config.server.git.username: the user name of GIT. If the warehouse is public, you can leave it blank
- spring.cloud.config.server.git.password: configure Git user password.
- Spring.cloud.config.label: branch name of GIT
before that, create Git warehouse and add configuration files in advance. The master branch adds the following files and contents:
ConfigServer.properties: k1=master-default-v1 ConfigServer-dev.properties: k1=master-dev-v1 ConfigServer-test.properties: k1=master-test-v1 ConfigServer-pro.properties: k1=master-pro-v1
after the service is started, you can access the following URL based on the HTTP request to obtain the configuration information. The format of obtaining is as follows:
- /{application}/{profile}
- /{application}/{profile}[/{lable}]
- /{application}-{profile}.properties
- /{lable}/{application}-{profile}.properties
- /{lable}/{application}/{profile}[/{lable}
Where applicaiton is the name of the file. as
1) http://localhost:6001/ConfigServer-dev.properties
2) http://localhost:6001/master/ConfigServer-dev.properties : load properties for the specified branch:
3) http://localhost:6001/ConfigServer/default : Show default configuration
{ "name":"ConfigServer", "profiles":[ "default" ], "label":null, "version":"f516174f308769468f1ac683cfeffa803a63c9af", "state":null, "propertySources":[ { "name":"https://gitee.com/lnxxs/springCloudConfig.git/ConfigServer.properties", "source":{ "k1":"master-default-v1" } } ] }
4) http://localhost:6001/ConfigServer/dev : display default and dev information
{ "name":"ConfigServer", "profiles":[ "dev" ], "label":null, "version":"f516174f308769468f1ac683cfeffa803a63c9af", "state":null, "propertySources":[ { "name":"https://gitee.com/lnxxs/springCloudConfig.git/ConfigServer-dev.properties", "source":{ "k1":"master-dev-v1" } }, { "name":"https://gitee.com/lnxxs/springCloudConfig.git/ConfigServer.properties", "source":{ "k1":"master-default-v1" } } ] }
5) http://localhost:6001/ConfigServer/test/master Displays the test and default configuration for the specified branch
{ "name":"ConfigServer", "profiles":[ "test" ], "label":"master", "version":"f516174f308769468f1ac683cfeffa803a63c9af", "state":null, "propertySources":[ { "name":"https://gitee.com/lnxxs/springCloudConfig.git/ConfigServer-test.properties", "source":{ "k1":"master-test-v1" } }, { "name":"https://gitee.com/lnxxs/springCloudConfig.git/ConfigServer.properties", "source":{ "k1":"master-default-v1" } } ] }
if the configuration information of Git warehouse is obtained through the above URL, the configuration server is normal.
2, Build Config Client configuration client
next, we create a SpringBoot application as the client to read the configuration provided in Config Server. (any microservice component we develop can be regarded as a Config Client. )
create a new ConfigClientApplication,java startup class, and add a dependency in the project file pom.xml:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <!--web rely on--> <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>
add bootstrap.yml. Note that these configurations must be in bootstrap.yml for the configuration to take effect. If the configuration is in applicaiton.yml, it has no effect.
spring: application: name: server-client cloud: config: label: master name: ConfigServer profile: test uri: http://localhost:6001/
- spring.cloud.config.label: specify branch
- spring.cloud.config.name: Specifies the name of the configuration file. The name of my git repository's configuration file is ConfigServer
- spring.cloud.config.profile: specifies which profile to activate
- spring.cloud.config.uri: Specifies the url of the configuration center. All configuration information is obtained from the configuration center.
create the ConfigClientApplication.java startup class and add the EnableAutoConfiguration annotation to indicate that the configuration variables in the project are automatically obtained.
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication @EnableAutoConfiguration public class ConfigClientApplication { public static void main(String[] args) { SpringApplication.run(ConfigClientApplication.class, args); } }
create a controller and test for configuration information:
@RestController public class ValueController { @Value("${k1}") String value; @GetMapping("/get") public String getValue(){ return value; } }
call the interface test and get the information of the configuration center correctly:
3, Other configurations
3.1 client fast failure
when we are unable to connect to the Config Server, we sometimes require the client to fail to start. We can configure the bootstrap configuration item by:
spring: cloud: config: fail-fast: true retry: initial-interval: 1000 max-attempts: 6 max-interval: 2000 multiplier: 1.1
3.2 client retry
if we require the client component to be unavailable in Config Server, let the client retry, we can also set:
spring: cloud: config: fail-fast: false
introduce spring retry and aop dependency in the client's configuration file at the same time:
<dependency> <groupId>org.springframework.retry</groupId> <artifactId>spring-retry</artifactId> <version>1.2.2.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency>
3.3 HTTP permission
if you need to control the account and password of the http request of Config Server, configure it on the server side:
spring: application: name: single cloud: config: server: git: uri: https://gitee.com/lnxxs/springCloudConfig.git password: username: search-paths: conf label: master username: password: uri: http://localhost:6001/ enabled: true security: user: name: user password: pwd
note that the attributes of user name and password are spring.security.user.name and spring.security.user.password
The server configuration of spring.cloud.config.username and spring.cloud.config.password has no effect. They are used by clients for configuration
And introduce the security dependency in the pom file of the server:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency>
we only need to copy and configure the user name and key in bootstrap.xml of each client. The client does not need to introduce security dependency.
spring: application: name: server-client cloud: config: label: master profile: test uri: http://localhost:6001/ name: ConfigServer username: user password: pwd
when configuring HTTP permissions, if the server does not configure the user name and password after introducing security, a global key will be randomly generated when the server project starts. We don't use random passwords here. If the user name and password are configured, the client must configure the same user name and encrypted key at the same time.
4, Summary:
this article briefly introduces the use of Spring Cloud ConfIf component, and builds a distributed configuration center based on git warehouse, which realizes the isolation of program and configuration, and decouples the coupling between coding and environment, which is very important. Of course, such configuration is still determined. In the actual generation environment, all service configurations depend on the configuration center, so In the next chapter, we will continue to understand the cluster construction of the distributed configuration center
the above is the sharing of this issue. You can follow the blog's# Spring Cloud basic tutorial!#
You can also pay attention to the public number: programmers laugh and laugh, pay more attention to more exciting content!
This article is based on the platform of blog one article multiple sending OpenWrite Release!
My blog address Smile in Lanling , welcome to browse!