In the previous article, we implemented the function of unified configuration file management, but we can find that we only used one server. If this server hangs up, the entire configuration center will be unavailable. Let’s solve the high availability problem of the configuration center.
Below we integrate Eureka to achieve high availability of the configuration center, because as configuration management within the architecture, it can actually be regarded as a microservice in the architecture. We can also register the config server as a service, so that all clients can access it in the form of a service. In this way, you only need to start multiple config servers pointing to the same Gitlab repository location to achieve high availability.
1. Join Eureka on the Config Server
1. Join Eureka Dependence
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency>
2. Add Eureka support and register the configuration service to Eureka
@EnableEurekaClient
3. Modify the configuration file on the server side
Add Eureka registered configuration, bootstrap.yml
server: port: 8889 eureka: client: service-url: defaultZone: http://localhost:8761/eureka instance: prefer-ip-address: true spring: application: name: foo cloud: config: server: git: uri: <span>https://gitlab.xxx.com/xxxxx/xxxx.git</span> # Configure the address of the gitlab repository search-paths: <span>/config-repo</span> # The relative addresses under the gitlab repository address can be configured with multiple, use, and split. username: your name # gitlab repository account password: your password # gitlab repository password
Start Config Server
4. Change the port number and start a Config Server
5. Check whether the service is registered successfully
2. Join Eureka by Config client
1. Join Eureka's pom dependency and use Config Server
2. Modify the configuration file of the Config client
bootstrap.properties has been tested. The client here can only be bootstrap.properties, otherwise it will be connected to port 8888.
server.port=8890 spring.application.name=configserver spring.cloud.config.name=configserver spring.cloud.config.profile=dev spring.cloud.config.label=master # Enable Config service discovery support spring.cloud.config.discovery.enabled=true # Here you need to set the service name registered by Config Server on Eureka spring.cloud.config.discovery.service-id=foo # Specify the service discovery center eureka.client.service-url.defaultZone=http://localhost:8761/eureka
Among them, use the eureka.client.service-url.defaultZone parameter to specify the service registration center for service registration and discovery, and then set the spring.cloud.config.discovery.enabled parameter to true, enable the function of accessing Config Server through the service, and finally use the spring.cloud.config.discovery.serviceId parameter to specify the service name registered by Config Server. The spring.application.name and spring.cloud.config.profile here are consistent with the meanings in the previous blog.
3. Add Eureka support to the Application main class, which is consistent with Config Server
3. Verification
1. Check the services on Eureka Server
2. Check whether the Config Server is normal
Enter: http://localhost:8888/configserver/dev/master, http://localhost:8889/configserver/dev/master in the browser to see if there is a return result as follows:
{ "name": "configserver", "profiles": [ "dev" ], "label": "master", "version": "8949024814dcb6d61f97dc49db7e9dadcfc724b1", "state": null, "propertySources": [ { "name": "https://gitlab.xxx.com/xxxxx/xxxx/project/config-repo/configserver.properties", "source": { "name": "chhliuxyh", "hello": "i'm the king of the world!!!", "profile": "profile-default" } } ] }3. Check whether the client is normal
Enter http://localhost:8890/hello in the browser to see if there is any return as follows
i'm the king of the world!!!
If the above steps are normal, it means that it is OK.
4. Stop the Config Server corresponding to port 8888, and then re-enter http://localhost:8890/hello to see if it is OK! We found it was still OK, so we completed the high availability of the configuration center!
<pre code_snippet_id="2245130" snippet_file_name="blog_20170306_4_711400"></pre> <pre></pre> <pre></pre>
Summarize
The above is the high-availability implementation method of the spring cloud config distributed configuration center introduced to you by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support to Wulin.com website!