Config configuration center
Save the YML configuration file to a Git server, such as github.com or gitee.com
When the microservice starts, the configuration file is retrieved from the server
Store configuration files on GitHub
Create a new “Project” and name it config
Copy the YML configuration files of SP02, SP03, SP04 and SP11 to the config project and rename them
- item-service-dev.yml
- user-service-dev.yml
- order-service-dev.yml
- zuul-dev.yml
Finally, clear out the four itemsapplication.yml
file
Disallow configuration information from the configuration center to overwrite the client configuration
The default configuration center configuration has a higher priority. The configuration center configuration overrides all the configuration of the client, including the command-line parameter configuration, so that the port number startup parameters we configured in item-service and order-service will be invalid
Item-service startup parameters:
--service.port=8001
--service.port=8002
Order-service start parameter
--service.port=8201
--service.port=8202
You can set the configuration that disallows the configuration center to overwrite the client configuration, so that when we pull the configuration file from the Control Center, the local configuration will overwrite the pulled configuration and add the following configuration among the four configuration files
spring:
......
cloud:
config:
override-none: true
To prepare the warehouse
- Create a new module: config as a folder to store configuration files in
- Put the configuration files of project 2,3,4,11 in the config folder
-
The Spring Cloud1 project directory creates a local repository
- VCS – Import into version control – Create git repository
- Select the Spring Cloud1 project directory to set up the local warehouse
-
Push the local repository commit to the Gitee remote repository
- CTRL + K or VCS-COMMIT
- Check the file to be submitted, fill in the submission information, and click Submit
- Ctrl + Shift + K or VCS-git-push
- Click on Define Remote in the upper left corner
The Config server
The config configuration center downloads all configuration files from Git. While other micro-services get configuration information from the CONFIG configuration center when they start.
Create a new sp12-config project
pom.xml
<? The XML version = "1.0" encoding = "utf-8"? > < project XMLNS = "http://maven.apache.org/POM/4.0.0" XMLNS: xsi = "http://www.w3.org/2001/XMLSchema-instance" Xsi: schemaLocation = "http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd" > < modelVersion > 4.0.0 < / modelVersion > < the parent > < groupId > org. Springframework. Boot < / groupId > The < artifactId > spring - the boot - starter - parent < / artifactId > < version > 2.2.1. RELEASE < / version > < relativePath / > <! -- lookup parent from repository --> </parent> <groupId>cn.tedu</groupId> <artifactId>sp12-config</artifactId> <version> 0.0.1-snapshot </version> <name>sp12-config</name> <description>Demo project for Spring Boot</description> < properties > < Java version > 1.8 < / Java version > < spring - cloud. Version > Hoxton. RELEASE < / spring - cloud. Version > < / properties > <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
application.yml
spring: application: name: config-server cloud: config: server: git: uri: https://gitee.com/Mjp3309/springcloud1 search - paths: config # private warehouse need to configure the user name password # username: # password: server: port: 6001 eureka: client: service-url: defaultZone: http://eureka1:2001/eureka,http://eureka2:2002/eureka
Main program add@EnableConfigServer
和 @EnableDiscoveryClient
package cn.tedu.sp12; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.config.server.EnableConfigServer; @EnableConfigServer @EnableDiscoveryClient @SpringBootApplication public class sp12ConfigApplication {public static void main(String[] args) { SpringApplication.run(Sp12ConfigApplication.class, args); }}
Start, access the test
You can access item-service-dev.yml in the following form:
http://localhost:6001/item-service-dev.yml
http://localhost:6001/item-service/dev
Test other files
http://localhost:6001/user-service/dev
http://localhost:6001/zuul/dev
http://localhost:6001/order-service/dev
The config client
Modify the following items to get configuration information from the configuration center
- sp02-itemservice
- sp03-userservice
- sp04-orderservice
- sp11-zuul
Pom.xml adds config client dependencies
Right-click on the project, edit startup dependencies, and add Config Client dependencies
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
Add Bootstrap.yml to the four projects
Yml, the bootstrap.yml configuration file, is loaded before application.yml
- item-service
spring:
cloud:
config:
discovery:
enabled: true
service-id: config-server
name: item-service
profile: dev
eureka:
client:
service-url:
defaultZone: http://eureka1:2001/eureka, http://eureka2:2002/eureka
- user-service
spring:
cloud:
config:
discovery:
enabled: true
service-id: config-server
name: user-service
profile: dev
eureka:
client:
service-url:
defaultZone: http://eureka1:2001/eureka, http://eureka2:2002/eureka
- order-service
spring:
cloud:
config:
discovery:
enabled: true
service-id: config-server
name: order-service
profile: dev
eureka:
client:
service-url:
defaultZone: http://eureka1:2001/eureka, http://eureka2:2002/eureka
- zuul
spring:
cloud:
config:
discovery:
enabled: true
service-id: config-server
name: zuul
profile: dev
eureka:
client:
service-url:
defaultZone: http://eureka1:2001/eureka, http://eureka2:2002/eureka
Start the service and observe the logs getting configuration information from the configuration center (start the configuration center and registry first)
Configure the refresh
Spring Cloud allows the runtime to refresh the configuration dynamically and retrieve new configuration information from the configuration center
Use User-Service as an example to demonstrate configuration refreshes
pom.xml
Add one dependency to user service’s pom.xml
Right click on thesp03-user-service
Project, edit start dependency, add interaction dependency
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>