Traditional practices
It is very simple to achieve high availability on a previously implemented config-server basis, and we do not need to do any additional configuration for these servers, just follow one configuration rule: to point all config servers to the same Git repository, This allows all configuration content to be maintained through a unified shared file system, while the client specifies the config server location, as long as the balanced load outside of config server is configured, as shown in the structure
Register as a service
The Config-server is also registered as a service so that all clients can access it in a service way. In this way, it is only necessary to launch multiple config-server that point to the same Git repository location to achieve high availability.
Config-server Configuration
Pom.xml Dependency
<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId> Spring-cloud-starter-eureka</artifactid> </dependency></dependencies>
application.propertiesconfigure parameters in eureka.client.serviceUrl.defaultZone to specify the location of the service registry
spring.application.name=config-serverserver.port=7001# Configure Service Registry Eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/# Git repository configuration Spring.cloud.config.server.git.uri=xxspring.cloud.config.server.git.searchPaths= Xxspring.cloud.config.server.git.username=Usernamespring.cloud.config.server.git.password= Password
In the application main class, new annotations are added to @EnableDiscoveryClient register the Config-server with the service registry configured above.
@EnableDiscoveryClient @enableconfigserver@springbootapplication Public class Application { publicstaticvoid main (string[] args) { New Springapplicationbuilder (application. Class). Web (true). Run (args); }}
Launch the app and access it http://localhost:1111/ , you can see in the information panel of the Eureka server that Config-server has been registered
Config-client Configuration
Pom.xml Dependency
<dependencies> <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> <dependency> < Groupid>org.springframework.cloud</groupid> <artifactid>spring-cloud-starter-eureka</ Artifactid> </dependency></dependencies>
In bootstrap.properties , configure as follows
Spring.application.name=didispaceserver.port=7002eureka.client.serviceUrl.defaultZone=http: // localhost:1111/eureka/ spring.cloud.config.discovery.enabled=truespring.cloud.config.discovery.serviceId= config-serverspring.cloud.config.profile=dev
eureka.client.serviceUrl.defaultZoneSpecify the service registry with parameters for registration and discovery of services
spring.cloud.config.discovery.enabledparameter is set to True to turn on the ability to access config server through the service
spring.cloud.config.discovery.serviceIdparameter to specify the service name registered by Config server
spring.application.namespring.cloud.config.profileIt is used to locate resources in git, just as it was previously accessed through URIs.
In the application main class, add @EnableDiscoveryClient annotations to discover the Config-server service and use it to load the application configuration
@EnableDiscoveryClient @springbootapplication Public class Application { publicstaticvoid main (string[] args) { New Springapplicationbuilder (application. Class). Web (true). Run (args); }}
Create a controller to load the configuration information in Git
@RefreshScope @restcontroller Public class TestController { @Value ("${from}") private String from; @RequestMapping ("/from") public String from () { return This . from; }}
access the services provided by the client app: http://localhost:7002/from , at which point we will return to the Git repository didispace-dev.properties file configuration from property content: "git-dev-1.0"
Distributed Configuration Center High Availability