Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to configure central service and high availability for java

Shulou Source: shulou.com Published: 2022-06-01 00:30:13 09月11日 Update

This article mainly introduces the relevant knowledge of "how to configure the center service and high availability of java". The editor shows you the operation process through the actual case, the operation method is simple and fast, and the practicality is strong. I hope this article "how to configure the center service and high availability of java" can help you solve the problem.

The coupling between the client and the server is too high. If the server end wants to cluster, the client can only route in the original way. When the server changes the IP address, the client also needs to modify the configuration, which is not in line with the concept of springcloud service governance. Springcloud provides such a solution. We only need to register the server side as a service in eureka, and the client side can go to eureka to get the service on the server side of the configuration center.

Server terminal transformation

1. Add dependencies

Org.springframework.cloud spring-cloud-config-server org.springframework.cloud spring-cloud-starter-eureka

More spring-cloud-starter-eureka packages need to be introduced to add support for eureka.

2. Configuration file

Server:server: port: 8001spring: application: name: spring-cloud-config-server cloud: config: server: git: uri: https://github.com/ityouknow/spring-cloud-starter/ # configure the address of the git warehouse search-paths: config-repo # relative address under the git warehouse address, you can configure multiple, use, partition. Username: username # git warehouse account password: password # git warehouse password eureka: client: serviceUrl: defaultZone: http://localhost:8000/eureka/ # # registry eurka address

Added configuration of eureka registry

3. Startup class

Add @ EnableDiscoveryClient to the startup class to activate support for configuration center

@ EnableDiscoveryClient@EnableConfigServer@SpringBootApplicationpublic class ConfigServerApplication {public static void main (String [] args) {SpringApplication.run (ConfigServerApplication.class, args);}}

In this way, the transformation of the server side is completed. Start the eureka registry first. On the server side, visit: http://localhost:8000/ in the browser and you will see that the server side has registered with the registry.

It is normal to test the server side according to the test steps in the previous section.

Client transformation

1. Add dependencies

Org.springframework.cloud spring-cloud-starter-config org.springframework.boot spring-boot-starter-web org.springframework.cloud spring-cloud-starter-eureka org.springframework.boot spring-boot-starter-test test

More spring-cloud-starter-eureka packages need to be introduced to add support for eureka.

2. Configuration file

Spring.application.name=spring-cloud-config-clientserver.port=8002 spring.cloud.config.name=neo-configspring.cloud.config.profile=devspring.cloud.config.label=masterspring.cloud.config.discovery.enabled=truespring.cloud.config.discovery.serviceId=spring-cloud-config-server eureka.client.serviceUrl.defaultZone= http://localhost:8000/eureka/

It mainly removes the configuration that spring.cloud.config.uri points directly to the server address, and adds the last three configurations:

Spring.cloud.config.discovery.enabled: enable Config service discovery support

Spring.cloud.config.discovery.serviceId: specify the name on the server side, that is, the value of the server side spring.application.name

Eureka.client.serviceUrl.defaultZone: the address pointing to the configuration center

All three configuration files need to be placed in the configuration of bootstrap.properties

3. Startup class

Add @ EnableDiscoveryClient to the startup class to activate support for configuration center

@ EnableDiscoveryClient@SpringBootApplicationpublic class ConfigClientApplication {public static void main (String [] args) {SpringApplication.run (ConfigClientApplication.class, args);}}

Start the client side and visit it in the browser: http://localhost:8000/ will see that both the server side and the client side have registered with the registry.

High availability

In order to simulate the production cluster environment, we change the port of the server side to 8003, and then start a server side to do the load of the service, providing highly available server side support.

As shown in the figure above, it can be found that two server clients will provide configuration center services at the same time to prevent the use of the entire system from being affected when a down is dropped.

Let's test the server first and visit the returned information of http://localhost:8001/neo-config/dev and http://localhost:8003/neo-config/dev respectively:

{"name": "neo-config", "profiles": ["dev"], "label": null, "version": null, "state": null, "propertySources": [{"name": "https://github.com/ityouknow/spring-cloud-starter/config-repo/neo-config-dev.properties"," "source": {"neo.hello": "hello im dev"}]}

It indicates that both server terminals have read the configuration information normally.

Visit: http://localhost:8002/hello again and return: hello im dev update. It indicates that the client has read the content of the server. We randomly stop a service on the server, visit http://localhost:8002/hello again, and return: hello im dev update, indicating that the goal of high availability has been achieved.

This is the end of the content about "how to configure central services and high availability for java". Thank you for your reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

Tags: Configuration service address end support client client warehouse file knowledge modification test three two information content again point browser activate Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB macOS MySQL Huawei Microsoft