💡 In the previous article, we introduced a new component Zuul, which is a gateway component. It receives Api requests uniformly. Based on the gateway, we can process all requests, log records, request authorization and other operations. Spring Cloud cognitive learning (5): the use of Zuul gateway
💡 This article introduces Spring Cloud Config as a configuration center, which is mainly used to solve the distribution problem of configuration files when deploying multiple servers.
🔴 PS: This is the last one of cognitive learning. Cognitive learning is not close to reality, so components are just a brief introduction. It's just a Spring Cloud cognitive learning. The purpose is to introduce you to the use of a wheel, let you know the wheel, and be interested in the wheel. The specific methods in the wheel will be discussed later.
🔴 The components will be covered in a separate chapter later.
Spring Cloud Config
effect:
- In the distributed deployment, there will be many servers that need to be deployed. In theory, various servers may need to use different configuration files, so the distribution of configuration files for multiple servers is also a problem.
- Spring Cloud Config exists as a configuration center, from which other servers can pull configuration files required by the server.
- Principle: Spring Cloud Config exposes Http API interface. Spring Cloud Config Client obtains configuration file by calling Http interface of Config Server.
Simple example
Spring Cloud Config can pull configuration files from many places, local and remote. Since it is common to pull from remote git, the example here is remote GIT. [the local pull will be demonstrated in a single chapter. ]
The following code can be referred to: Spring Cloud Config configuration center use experiment
Create configuration center
1. Create the module spring cloud config server 11000, which is used as the configuration service center module, just as we used eureka before to create an eureka server.
2. Import dependency for spring cloud config server 11000 module:
<dependencies> <!--Introduce public dependency package start--> <dependency> <groupId>com.progor.study</groupId> <artifactId>spring-cloud-common-data</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <!--Introduce public dependency package end--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> </dependencies>
3. Modify the application.yml :
server: port: 11000 spring: application: name: spring-cloud-config-server cloud: # Configure spring cloud config config: server: git: # Using git to pull uri: https://github.com/alprogor/spring -cloud-config- test.git #Git warehouse name on GitHub search-paths: messageservice # If you do not specify search paths, the default is to search in the root directory, and your config file needs to be placed in the root directory
4. modify the main program class and add @EnableConfigServer
@SpringBootApplication @EnableConfigServer public class SpringCloudConfigServer11000Application { public static void main(String[] args) { SpringApplication.run(SpringCloudConfigServer11000Application.class, args); } }
5. Create a warehouse on github, pull the warehouse locally, and create a folder, messageservice. There is a spring cloud message config in the folder- 11000.yml , fill in the following code and push it to github:
Reference code: spring-cloud-config-test
spring: profiles: active: - dev --- spring: profiles: dev application: name: spring-cloud-message-service-config-dev --- spring: profiles: test application: name: spring-cloud-message-service-config-test server: port: 8006
6. Start spring cloud config server 11000
💡 visit http://localhost:11000/master/spring-cloud-message-config-11000.yml You can see the profile on the main branch.
💡 visit http://localhost:11000/master/spring-cloud-message-config-11000-test.yml , you can see the configuration under test profile
💡 How to splice paths to access configuration files can refer to the following figure:
Pull configuration
1. Create the module spring cloud message service config 8006:
2. Import dependency:
<dependencies> <!--Introduce public dependency package start--> <dependency> <groupId>com.progor.study</groupId> <artifactId>spring-cloud-common-data</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <!--Introduce public dependency package end--> <!--introduce web Development related package start--> <!--web modular--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--jettey As default server--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jetty</artifactId> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-core</artifactId> </dependency> <!--introduce web Development related package end--> <!--spring -cloud -config Scenario package --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> </dependencies>
3. Create under the resources of the module spring cloud message service config 8006 bootstrap.yml :
💡 bootstrap.yml It's a high priority profile, but it takes precedence over application.yml , so it can be used to pull configuration files.
spring: cloud: config: name: spring-cloud-message-config-11000 #The resource name that needs to be read from github, note that there is no yml suffix profile: test #Configuration items accessed this time label: master uri: http://localhost:11000
test
💡 Start spring cloud config server-11000, and then start spring cloud message service config-8006 after the start. Check whether the port of spring cloud message service config-8006 is 8006. If so, the configuration file is pulled successfully.
Add:
- How to pull the configuration file locally from the Spring Cloud Config service, how to use the Spring Cloud Bus to update the configuration file and the configuration of the Spring Cloud Config cluster are not covered here, but will be covered in the Spring Cloud Config chapter.