Spring Cloud Config
The Configuration Management Toolkit allows you to place configurations on remote servers, centralize management of cluster configurations, and currently support local storage, git, and subversion.
That is, we can use Spring Cloud config to get configuration information on the remote server.
Can be divided into two parts:
Server-side: Configure service side, service management configuration information
Client: Client invokes server-side exposed interface to get configuration information
Building the service side
The following dependencies are added to the pom file:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId> Spring-cloud-config-server</artifactid>
</dependency>
Adding a @enableconfigserver annotation to the program application indicates that this is a config server side
@SpringBootApplication
@EnableConfigServer Public
class Springcloudconfigapplication {public
static void Main (string[] args) {
springapplication.run (springcloudconfigapplication.class, args);
}
}
Configuring remote Warehouse information in Application.properties
Spring.application.name=config-server
server.port=7001
# Remote warehouse address, specific to project
spring.cloud.config.server.git.uri= ... # The specific path under the project can be configured with multiple
spring.cloud.config.server.git.searchpaths= ...
# User name
spring.cloud.config.server.git.username= ...
# password
spring.cloud.config.server.git.password= ...
Of course, we can also load local profiles and use Spring.profiles.active=native to automatically search for files in the resources directory.
You can also specify a specific location by setting the Spring.cloud.config.server.native.searchLocations.
Store four configuration files representing different environments in the remote repository:
is stored with a from attribute, each given a different value.
After the server is started, we can access the configuration file by URL, and the access relationship is as follows:
Url:
/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties
The above URL maps {application}-{profile}.properties corresponding configuration file, {label} corresponds to a different branch of git, default is master.
So after we start, we can access the above Application.dev file via Http://localhost:7001/application/dev.
Now that the server is done, how do we get the server-side configuration in the client?
Create a new project, or add dependencies first:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId> spring-boot-starter-web</artifactid>
</dependency>
<dependency>
<groupId> Org.springframework.cloud</groupid>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
Then add the following configuration to the bootstrap configuration file:
Spring.application.name=application
Spring.cloud.config.profile=dev
spring.cloud.config.label=master
spring.cloud.config.uri=http://localhost:7001/
server.port=7002
Need to be configured in bootstrap to take effect, rather than in application configuration files, bootstrap profiles are loaded earlier than application.
The above attribute is one by one corresponding to the URL.
After the configuration is complete, we're going to get the properties, which is obviously obtained using the @value annotation.
@RestController
class TestController {
@Value ("${from}")
private String from;
@RequestMapping ("/from") public
String from () {return
This.from
}
}
When we visit the Localhost:7002/from, we can get the value from in the configuration.
In a micro-service architecture, a highly available service is required, as is the configuration center, so we need to register the configuration center in the registry so that the client can access the configuration information in the way the service is accessed.
What do we need to do?
To register in a registry, it is obvious that you need Eureka dependencies:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId> Spring-cloud-starter-eureka</artifactid>
</dependency>
and register the configuration Center with the registry in the application configuration file.
eureka.client.serviceurl.defaultzone=http://localhost:1111/eureka/
And you need to register the configuration center as a service client, so add the @enablediscoveryclient annotation to the main class.
This allows the configuration center to be registered.
The configuration center is registered in the service registry, so our clients no longer need to use the URL to access him, but instead use the services in the registry to call him.
To invoke a service in the registry, you must first register yourself in the registry:
Join dependencies:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId> Spring-cloud-starter-eureka</artifactid>
</dependency>
and add the following configuration in the bootstrap configuration file:
eureka.client.serviceurl.defaultzone=http://localhost:1111/eureka/
spring.cloud.config.discovery.enabled= True
Spring.cloud.config.discovery.serviceid=config-server
First line: Registering in the registration center
Second line: Start service discovery function, open to call other services
Third line: The name of the service found
Finally, adding @enablediscoveryclient to the application main class is done.
The same access to Localhost:7002/from can be obtained from the value.
Configure Refresh
In the above configuration, how does the client obtain configuration information for the configuration center? is configured in the bootstrap configuration file and loaded at program startup. Obviously, after the load is complete, if the information in the configuration changes, the client information will not be changed, which is not allowed in the actual development process. What should we do?
We need to add a monitoring module: spring-boot-starter-actuator
Add the following dependencies to the client:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId> Spring-boot-starter-actuator</artifactid>
</dependency>
and add @refreshscope annotations to the controller.
Finally, actuator provides us with a/refresh interface, after modifying the Git warehouse information, send a post message to this interface and update the configuration information.