Preface
In distributed systems, due to the huge number of services, in order to facilitate unified management of service configuration files and real-time updates, the distributed configuration center component: spring-cloud-config is required. It supports configuration services to be placed in the memory of the configuration service (i.e. local), and also supports placement in remote Git repository.
This section mainly demonstrates how to use Git repository as configuration source.
Open source address: https://github.com/bigbeef
Create a configuration project
Create a project in github specifically to save the configuration files of all our projects. The project is my project structure
Configuration project address: https://github.com/bigbeef/cppba-config
eureka-server.properties
eureka.client.register-with-eureka=falseeureka.client.fetch-registry=falspring.application.name=eureka-serverserver.port=18761eureka.instance.hostname=peer1eureka.client.serviceUrl.defaultZone=http://peer1:18761/eureka/
Create a spring-cloud-config-server project
The project structure is shown in the figure:
pom.xml core code
<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency></dependencies>
SpringCloudConfigServerApplication.java
package com.cppba;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.config.server.EnableConfigServer;@SpringBootApplication@EnableConfigServerpublic class SpringCloudConfigServerApplication { public static void main(String[] args) { SpringApplication.run(SpringCloudConfigServerApplication.class, args); }}application.properties
This is based on your actual git project
server.port=8888spring.application.name=config-serverspring.cloud.config.server.git.uri=https://github.com/bigbeef/cppba-configspring.cloud.config.label=master# spring.cloud.config.server.git.username=# spring.cloud.config.server.git.password=spring.cloud.config.server.git.searchPaths=/ cppba-spring-cloud/*,/ cppba-spring-cloud/eureka-client/*
spring.cloud.config.server.git.uri: configure the git repository address
spring.cloud.config.server.git.searchPaths: Configure the repository path, separated by commas
spring.cloud.config.label: configure the branch of the repository
spring.cloud.config.server.git.username: Username to access the git repository
spring.cloud.config.server.git.password: user password to access the git repository
Start the project
Access address: http://127.0.0.1:8888
The http request address and resource file mapping is as follows:
/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties
According to our own configuration, we can access it like this: http://127.0.0.1:8888/eureka-server/default/master
application -> eureka-server (application name)
profile -> default (Enabled configuration, usually suffix, explained below)
label -> master (branch)
The result of visiting is:
Profile is more important, it can be understood as reading which configuration files. If I have more than one configuration file, there may be:
eureka-server.properties (this is a general configuration file and will be loaded by default),
eureka-server-mysql.properties,
eureka-server-oracle.properties,
eureka-server-jpa.properties,
eureka-server-mysql.properties......
We may selectively load some of the properties configuration files in it, so we can write it like this: http://127.0.0.1:8888/eureka-server/default,mysql,jpa/master
At this point, our spring-cloud-config-server is simply set up. In the following chapters, I will teach you how to read configurations in the project.
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.