1. Introduction
When you want to deploy a configuration center to a production environment, like the Service Registry, we also want it to be a highly available application. Spring Cloud Config is very simple to implement high availability on the server side, and there are mainly two ways to do it.
Traditional mode: There is no need to make any additional configuration for these servers. You only need to comply with one configuration rule and point all Config Servers to the same Git repository, so that all configuration content is maintained through a unified shared file system. When the client specifies the location of the Config Server, it only needs to configure the load balancing device address of the upper layer of the Config Server, which is the structure shown in the figure below.
Service model: In addition to the above traditional implementation model, we can also incorporate Config Server as an ordinary microservice application into Eureka's service governance system. In this way, our microservice applications can obtain configuration information through the service name of the configuration center. This method is more conducive to maintenance than the traditional implementation model, because the load balancing configuration of the server and the configuration center specification of the client are solved through the service governance mechanism, which not only achieves high availability but also realizes self-maintenance. Since the implementation of this part requires the cooperation of the client, readers of specific examples can read the "Service Configuration Center" section in the "Client Explanation" section in detail.
2. Preparation
A service registration center, EUREKASERVER, port 5555;
3. Transform Config-Server
(1) pom.xml, add spring-cloud-starter-eureka 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> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies>
(2) application.yml, configure the parameter eureka.client.serviceUrl.defaultZone to specify the location of the service registration center
server: port: 5588 spring: application: name: config-server eureka: client: serviceUrl: defaultZone: http://localhost:5555/eureka/ #Configure service registration center cloud: config: server: git: uri: https://gitee.com/smartdt/springcloudconfig.git #Configure the location of the Git repository. searchPaths: config-repo #Configure the relative search location under the repository path, and you can configure multiple. username: username #Username to access the Git repository. password: password #User password to access the Git repository. label: master #Configure the branch of the repository###If the Git repository is a public repository, you can not fill in the user name and password. If it is a private repository, you need to fill in it.
(3) Entry class, add the @EnableDiscoveryC giant ent annotation to register the config-server to the service registration center configured above.
@EnableDiscoveryClient @EnableConfigServer @SpringBootApplication public class SpringcloudconfigserverApplication { public static void main(String[] args) { SpringApplication.run(SpringcloudconfigserverApplication.class, args); } }(4) Start config-server and view through Eureka-Server
4. Renovate Config-Client
(1) pom.xml, add spring-cloud-starter-eureka dependency
<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies>
(2) bootstrap.properties, add configuration service center information
spring.application.name=configspace spring.cloud.config.label=master spring.cloud.config.profile=dev spring.cloud.config.uri= http://localhost:5588/ server.port=5589 eureka.client.serviceUrl.defaultZone=http://localhost:5555/eureka/
(3) Entry class, add @EnableDiscoveryClient
@EnableDiscoveryClient @SpringBootApplication public class SpringcloudconfigclientApplication { public static void main(String[] args) { SpringApplication.run(SpringcloudconfigclientApplication.class, args); } }(4) The test class remains unchanged
@RefreshScope @RestController public class ConfigController { @Value("${from}") private String from; @Value("${username}") private String username; @Value("${password}") private String password; @RequestMapping("/from") public String from() { return this.from + "~user:" + this.username + "~pass:" + this.password; } }(5) Start the test and view it through Eureka-Server
(6) Browser testing, visit http://localhost:5589/from
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.