Spring Cloud Config provides service-side and client support for configuration features in distributed systems. For applications of different environments its service side provides a centralized configuration management approach. And it applies not only to spring applications, but also to programs developed in other languages (which is important).
Environmental requirements
- First you need to install JDK1.8 or later
- IDE can choose Spring Tool Suite (STS) or IntelliJ idea, this article chooses STS as an example
Service side
On the File menu, click New Spring Starter Project.
After you enter the project name (here is Configuration-service), select Next, and in the dependencies, select Config Server.
Then, click Finish to complete the project creation.
Next, add the @enableconfigserver annotation on the Configurationserviceapplication class.
package com.example.demo;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.config.server.EnableConfigServer;@EnableConfigServer@SpringBootApplicationpublic class ConfigurationServiceApplication { public static void main(String[] args) { SpringApplication.run(ConfigurationServiceApplication.class, args); }}
The last step is to configure the available ports in the Application.properties file and the warehouse address for saving the server configuration.
server.port=9555#spring.profiles.active=native#spring.cloud.config.server.native.searchLocations=file:///${USERPROFILE}/Desktop/configspring.cloud.config.server.git.uri=file:///${USERPROFILE}/Desktop/config
The most common use of Spring Cloud config is the git repository, which can be spring.cloud.config.server.git.uri
configured by specifying a specific path.
Of course, you can also use Git repositories, such as simple file management.
At this point, you need to use spring.profiles.active=native
the same and spring.cloud.config.server.native.searchLocations={配置文件目录路径}
these two configuration items.
As an example, file:///${USERPROFILE}/Desktop/config
a application.properties file is created in the directory, and a configuration is added message=Hello World!
.
Starting the app, a spring Cloud server is created.
Test it to work properly, you can enter the address in the browser to http://localhost:9555/application/dev
view. If the result is similar to the following, the description is normal.
{"name":"application","profiles":["dev"],"label":null,"version":"c0f022755482d4a98f66dc19c8c4e0af512dc4f2","state":null,"propertySources":[{"name":"file:///C:\\Users\\Ken/Desktop/config/application.properties","source":{"message":"Hello World!"}}]}
Client
Also choose to create a new spring Starter Project. But this time, select Config Client in the dependencies.
Once created, add two dependencies to the Pom.xml.
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId></dependency><dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId></dependency>
Then add a controller to the Configurationclientapplication file.
package com.example.demo;import org.springframework.beans.factory.annotation.Value;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.context.config.annotation.RefreshScope;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@SpringBootApplicationpublic class ConfigurationClientApplication { public static void main(String[] args) { SpringApplication.run(ConfigurationClientApplication.class, args); }}@RefreshScope@RestControllerclass MessageRestController { @Value("${message:Hello default}") private String message; @RequestMapping("/message") String getMessage() { return this.message; }}
If you do not run Config server at this time but run the config client separately, you can see the following results:
The default configuration item in the code is obtained in the description program.
Below, start connecting to config server.
Configure the application launch port in the Config client's application.properties file server.port=9666
.
Create a new bootstrap.propertiess file under the same resources directory, and add the configuration associated with config server.
spring.cloud.config.uri=http://localhost:9555
Then, start the Config server application before starting the Config client application.
The result indicates that config client successfully obtained configuration content from config server.
Next, try changing the configuration item in the Application.properties file in the Configuration warehouse directory message=Hello World, Spring Cloud!
.
Refreshes the http://localhost:9666/message
page. The results did not change. This is because in the default scenario, the config client only gets the configuration once to config server at startup.
Change the message configuration back first message=Hello World!
.
Add the configuration to the Config client's application.properties management.endpoints.web.exposure.include=*
.
Restart the config Client.
Change the message configuration again message=Hello World, Spring Cloud!
.
The update configuration is then performed on the command line curl localhost:9666/actuator/refresh -d {} -H "Content-Type: application/json"
.
You can see the expected results when you refresh the page: