When there are many instances of the service, the files are read from the configuration center, which can be considered as a micro-service to cluster the configuration center to achieve high availability
Create a new registration center
Pom as follows
<?xml version= "1.0" encoding= "UTF-8"? ><project xmlns= "http://maven.apache.org/POM/4.0.0" xmlns:xsi= "http ://www.w3.org/2001/XMLSchema-instance "xsi:schemalocation=" http://maven.apache.org/POM/4.0.0/http Maven.apache.org/xsd/maven-4.0.0.xsd "><modelVersion>4.0.0</modelVersion><groupId> Com.cloud.config</groupid><artifactid>eureka-server</artifactid><version>0.0.1- snapshot</version><packaging>jar</packaging><name>eureka-server</name>< description>eureka-server-desc</description><parent><groupid>org.springframework.boot< /groupid><artifactid>spring-boot-starter-parent</artifactid><version>1.5.13.release</ Version><relativepath/> <!--lookup parent from repository--></parent><properties>< Project.build.sourceencoding>utf-8</project.build.sourceencoding><project.reporting.outputencoding >utf-8</project.reporting.outputencoding><java.version>1.8</java.version><spring-cloud.version>edgware.sr3</ Spring-cloud.version></properties><dependencies><dependency><groupid> org.springframework.boot</groupid><artifactid>spring-boot-starter-web</artifactid></ Dependency><dependency><groupid>org.springframework.cloud</groupid><artifactid> Spring-cloud-starter-eureka-server</artifactid></dependency><dependency><groupid> Org.springframework.boot</groupid><artifactid>spring-boot-starter-test</artifactid><scope >test</scope></dependency></dependencies><dependencyManagement><dependencies> <dependency><groupId>org.springframework.cloud</groupId><artifactId> Spring-cloud-dependencies</artifactid><version>${spring-cloud.version}</version><type> Pom</type><scope>import</scope></dependency></dependencies></dependencymanagement><build><plugins><plugin><groupid >org.springframework.boot</groupid><artifactid>spring-boot-maven-plugin</artifactid></ Plugin></plugins></build></project>
On the profile application.yml, specify a service port of 8889, plus the basic configuration as the service registry
Server:port:8889eureka:instance:hostname:localhostclient:registerwitheureka:falsefetchregistry:falseserviceurl :d efaultzone:http://${eureka.instance.hostname}:${server.port}/eureka/
Entry class
@EnableEurekaServer @springbootapplicationpublic class Eurekaserverapplication {public static void main (string[] args) {Springapplication.run (eurekaserverapplication.class, args);}}
Retrofit Config-server
Pom.xml plus Eureka client Start-up dependency Spring-cloud-starter-eureka
<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 >
Configure APPLICATION.YML to specify the service registration address as Http://localhost:8889/eureka
server:port:8888spring:application:name:config-servercloud:config:server:git:uri:https://github.com/enzofeng/ Springcloudconfig.git #如果git仓库为公开仓库, can not fill in the user name and password Search-paths:respousername:enzofengpassword: ********label: Mastereureka:client:serviceurl:defaultzone:http://localhost:8889/eureka
In the program's startup class application plus @enableeurekaclient annotations
Retrofit Config-client
Introduction of Eureka starting dependency, Pom as follows
<?xml version= "1.0" encoding= "UTF-8"? ><project xmlns= "http://maven.apache.org/POM/4.0.0" xmlns:xsi= "http ://www.w3.org/2001/XMLSchema-instance "xsi:schemalocation=" http://maven.apache.org/POM/4.0.0/http Maven.apache.org/xsd/maven-4.0.0.xsd "><modelversion>4.0.0</modelversion><groupid>com. springcloud</groupid><artifactid>config-client</artifactid><version>0.0.1-snapshot< /version><packaging>jar</packaging><name>config-client</name><description> Config-client-desc</description><parent><groupid>org.springframework.boot</groupid> <artifactId>spring-boot-starter-parent</artifactId><version>1.5.13.RELEASE</version> <relativePath/> <!--lookup parent from repository--></parent><properties>< Project.build.sourceencoding>utf-8</project.build.sourceencoding><project.reporting.outputencoding >utf-8</project.reporting.outputencoding><java.version>1.8</java.version><spring-cloud.version>edgware.sr3</ Spring-cloud.version></properties><dependencies><dependency><groupid> org.springframework.cloud</groupid><artifactid>spring-cloud-starter-config</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-web</artifactid></ Dependency><dependency><groupid>org.springframework.boot</groupid><artifactid> Spring-boot-starter-test</artifactid><scope>test</scope></dependency></dependencies ><dependencymanagement><dependencies><dependency><groupid>org.springframework.cloud </groupid><artifactid>spring-cloud-dependencies</artifactid><version>${spring-cloud.version}</version><type>pom</type><scope >import</scope></dependency></dependencies></dependencyManagement><build>< Plugins><plugin><groupid>org.springframework.boot</groupid><artifactid> Spring-boot-maven-plugin</artifactid></plugin></plugins></build></project>
Configuration file bootstrap.yml, plus service registration address
Spring:application:name:config-clientcloud:config:label:masterprofile:dev#uri:http://localhost:8888/discovery: enabled:trueservice-id:config-serverserver:port:8881eureka:client:serviceurl:defaultzone:http://localhost:8889 /eureka
Spring.cloud.config.discovery.enabled is reading files from the configuration center
Spring.cloud.config.discovery.serviceId Configuration Center ServiceId, which is the service name
It is found that the read configuration file no longer writes the IP address, but the service name, at this time if the configuration service deployed multiple copies, through load balancing, thereby high availability.
Start Eureka-server, Config-server, config-client
Visit http://localhost:8889/
springcloud-high-Availability Distributed configuration Center (config)