In this series of previous article demonstrates how to use OpenFeign to simplify the development of the Rest client. This article will demonstrate how to use the configuration management service provided in the Spring Cloud framework
In this experiment, using git as the repository of the configuration file, you need to ensure that the git software has been installed on this machine.
Build configuration Repository
Create a directory as a configuration repository:
1 | mkdir config-repo |
Enter the directory and use the git command to initialize:
1 | cd config-repo |
The software creates two configuration files in the directory, namely:
config-client-development.properties:
1 | user.role=Developer |
config-client-production.properties:
1 | user.role=User |
Build configuration service
Create a new maven project:
groupId: cn.com.hohistar.tutorial
artifactId: springcloud-config-server
Then modify pom.xml to:
1 |
|
Then create a new startup class in the package: cn.com.hohistar.tutorial.springcloud.configserver: SpringcloudConfigServerApplication, the content is as follows:
1 |
|
Create a configuration file for the project, create a new file named application.xml in src/main/resource:
1 | server: |
Build and configure the client
Create a new maven project:
groupId: cn.com.hohistar.tutorial.springboot
artifactId: springboot-config-client
Then modify the pom.xml file:
1 |
|
In the package: cn.com.hohistar.tutorial.springboot.springbootconfigclient.web create a new RestController, named: MessageRestController, the code is as follows:
1 |
|
Create a startup class: SpringbootConfigClientApplication, the code is as follows:
1 |
|
Add the configuration file in src/main/resource: bootstrap.properties, the content is as follows:
1 | spring.application.name=config-client |
Now you can start the service and client for testing。