In the first two introductions, the client directly invokes the server side of the configuration Center to get profile information. So there is a problem, the client and server coupling is too high, if the servers to do the cluster, the client can only be routed in the original way, the server side changes the IP address, the client also needs to modify the configuration, not in line with the Springcloud service governance concept. Springcloud provides such a solution, we only need to register the server side as a service to Eureka, the client side to Eureka to get the configuration Center server side of the service can be.
This article is based on the configuration center git version of the content to transform
Server-side retrofit 1, adding dependencies
1 <dependencies> 2 <dependency> 3 <groupId> Org.springframework.cloud</groupid> 4 <artifactid>spring-cloud-config-server</ Artifactid> 5 </dependency> 6 <dependency> 7 < Groupid>org.springframework.cloud</groupid> 8 <artifactId> Spring-cloud-starter-eureka</artifactid> 9 </dependency> </ Dependencies>
You need to introduce more spring-cloud-starter-eureka
packages to add support for Eureka.
2. Configuration files
1 Server:2 Server:3Port80014 Spring:5 Application:6name:spring-cloud-config-Server7 Cloud:8 Config:9 Server:Ten git: OneUri:https://github.com/ityouknow/spring-cloud-starter/# Configuring the address of the GIT repository Asearch-paths:config-Repo # git repository address relative address, can be configured multiple, with, split. - Username:username # git repository account - Password:password # git repository password the Eureka: - Client: - serviceurl: -Defaultzone:http://localhost:8000/eureka/# # Registration Center Eurka Address
Added configuration of the Eureka Registration Center
3. Startup class
Launch class add @EnableDiscoveryClient
activation support for configuration Center
1 @EnableDiscoveryClient2 @EnableConfigServer3 @SpringBootApplication4 Public classconfigserverapplication {5 6 Public Static voidMain (string[] args) {7Springapplication.run (configserverapplication.class, args);8 }9}
This completes the server-side transformation. Start the Eureka Registry first, on the server side, and in the browser: http://localhost:8000/
you will see that the server side is registered to the registry.
Follow the test steps in the previous article to test the server side for normal service.
Client retrofit 1, adding dependencies
1<dependencies>2<dependency>3<groupId>org.springframework.cloud</groupId>4<artifactId>spring-cloud-starter-config</artifactId>5</dependency>6<dependency>7<groupId>org.springframework.boot</groupId>8<artifactId>spring-boot-starter-web</artifactId>9</dependency>Ten<dependency> One<groupId>org.springframework.cloud</groupId> A<artifactId>spring-cloud-starter-eureka</artifactId> -</dependency> -<dependency> the<groupId>org.springframework.boot</groupId> -<artifactId>spring-boot-starter-test</artifactId> -<scope>test</scope> -</dependency> +</dependencies>
You need to introduce more spring-cloud-starter-eureka
packages to add support for Eureka.
2. Configuration files
1spring.application.name=spring-cloud-config-Client2server.port=80023 4spring.cloud.config.name=neo-Config5spring.cloud.config.profile=Dev6Spring.cloud.config.label=Master7Spring.cloud.config.discovery.enabled=true8spring.cloud.config.discovery.serviceid=spring-cloud-config-Server9 TenEureka.client.serviceurl.defaultzone=http://localhost:8000/eureka/
The main point is spring.cloud.config.uri
to remove the configuration directly pointing to the server-side address, adding the last three configurations:
spring.cloud.config.discovery.enabled
: Open Config Service discovery support
spring.cloud.config.discovery.serviceId
: Specifies the server-side name, which is the server-side spring.application.name
value
eureka.client.serviceUrl.defaultZone
: Address to Configuration Center
These three configuration files need to be placed bootstrap.properties
in the configuration
3. Startup class
Launch class add @EnableDiscoveryClient
activation support for configuration Center
1 @EnableDiscoveryClient 2 @SpringBootApplication 3 Public class configclientapplication {45 Public Static void Main (string[] args) {6 Springapplication.run (configclientapplication. Class, args); 7 }8 }
Start client side, access in browser: http://localhost:8000/
you will see that both the server side and client side are registered to the registry.
Highly Available
In order to simulate the production cluster environment, we changed the server port to 8003, and then started a server side to do the service load, providing high availability server-side support.
It can be found that there will be two servers at the same time to provide configuration center services, to prevent one down after the impact of the entire system to use.
We first test the server side separately, Access: http://localhost:8001/neo-config/dev
, http://localhost:8003/neo-config/dev
return information:
1 {2 "name":"Neo-config", 3 "Profiles": [4 "Dev"5 ], 6 "label":NULL, 7 "version":NULL, 8 " State":NULL, 9 "propertysources": [Ten { One "name":"https://github.com/ityouknow/spring-cloud-starter/config-repo/neo-config-dev.properties", A "Source": { - "Neo.hello":"Hello im dev" - } the } - ] -}
Note The configuration information is read correctly on both server sides.
Visit again: http://localhost:8002/hello
, return: hello im dev update
. Stating that the client has read the contents of the server side, we randomly stop a server-side service, revisit http://localhost:8002/hello
, return: hello im dev update
, the description reached the purpose of high availability.
Sample code
Springcloud (eight): Configure hub service and high availability