In the previous two introductions, the client calls the server side of the configuration center directly to get the configuration file information. So there is a problem. The coupling between client and server is too high. If the server side wants to cluster, the client can only route in the original way. When the server side changes the IP address, the client also needs to modify the configuration, which does not conform to the concept of spring cloud service governance. Spring cloud provides such a solution. We only need to register the server side as a service in eureka, and the client side goes to Eureka to get the service in the server side of the configuration center.
server end modification
1. Adding dependencies
<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>
More spring-cloud-starter-eureka packages are needed to add support for eureka.
2. Configuration file
server: server: port: 8001 spring: application: name: spring-cloud-config-server cloud: config: server: git: uri: https://github.com/ityouknow/spring-cloud-starter/ # Configure the address of the git repository search-paths: config-repo # Relative addresses under git warehouse address can be configurable, used and divided. username: username # Account number of git warehouse password: password # git warehouse password eureka: client: serviceUrl: defaultZone: http://localhost:8000/eureka/ # registry eurka address
Added configuration of eureka registry
3. Startup class
Start class adds @EnableDiscoveryClient activation support for configuration center
@EnableDiscoveryClient @EnableConfigServer @SpringBootApplication public class ConfigServerApplication { public static void main(String[] args) { SpringApplication.run(ConfigServerApplication.class, args); } }
In this way, the transformation of server end is completed. Start the eureka registry, start the server, access in the browser: http://localhost:8000/ You will see that the server side has registered with the registry.
According to the test steps in the previous chapter, the server side is tested to be normal.
Client transformation
1. Adding dependencies
<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>
More spring-cloud-starter-eureka packages are needed to add support for eureka.
2. Configuration file
spring.application.name=spring-cloud-config-client server.port=8002 spring.cloud.config.name=neo-config spring.cloud.config.profile=dev spring.cloud.config.label=master spring.cloud.config.discovery.enabled=true spring.cloud.config.discovery.serviceId=spring-cloud-config-server eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/
Mainly remove the configuration of spring.cloud.config.uri pointing directly to the server address, and add the last three configurations:
spring.cloud.config.discovery.enabled: Turn on Config service discovery support
spring.cloud.config.discovery.serviceId: Specifies the name on the server side, which is the value of spring.application.name on the server side
eureka.client.serviceUrl.defaultZone: Address to Configuration Center
All three configuration files need to be placed in bootstrap.properties configuration
3. Startup class
Start class adds @EnableDiscoveryClient activation support for configuration center
@EnableDiscoveryClient @SpringBootApplication public class ConfigClientApplication { public static void main(String[] args) { SpringApplication.run(ConfigClientApplication.class, args); } }
Start the client side and access in the browser: http://localhost:8000/ You will see that both server and client are registered in the registry.
High availability
In order to simulate the production cluster environment, we change the port of server to 8003, and then start a server to do the load of the service to provide highly available server-side support.
First, we test the server separately and visit the server separately. http://localhost:8001/neo-config/dev,http://localhost:8003/neo-config/dev Return information:
{ "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" } } ] }
Explains that both server s read the configuration information normally.
Visit again: http://localhost:8002/hello Return: hello im dev update. Explain that the client has read the content of the server side, we stop a server side service at random, and visit again. http://localhost:8002/hello Return: hello im dev update, indicating high availability.
java B2B2C Source Multi-level Distribution Spring MVC mybatis Multi-tenant Electronic Mall System