First, Introduction
Recently looking at Spring Cloud microservices, the next time to share what I saw, the company is now using Dubbo, then have time to understand the source of Dubbo. Compared with Dubbo, Spring Cloud has a lot of comprehensive practices in microservices. Today, we will briefly introduce one of the components of Eureka Registration Center. Eureka supports highly available configurations like any other service registration center. If the Eureka in cluster mode is not ripe, when there are shards in the cluster failure, then eureka into self-protection mode. It allows the discovery and registration of services to continue during a shard failure, and when a failed shard resumes running, the other shards of the cluster will synchronize their status back again.
Second, practice
First we create a spring boot maven project called Micro-service-integration. Here is a submodule named Registration-center-web, which is the registry we are introducing today. The project catalogue is as follows:
The contents of the Pom.xml file in the parent project are 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> Spring.cloud</groupid> <artifactId>micro-service-integration</artifactId> <packaging>pom </packaging> <version>1.0-SNAPSHOT</version> <modules> <module>registration-cen ter-web</module> </modules> <name>micro-service-integration</name> <description>dem o Project for Spring boot</description> <parent> <groupid>org.springframework.boot</groupid > <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.2.release</versio N> </parent> <dependencymanagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>dalston.relea Se</version> <type>pom</type> <scope>import</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <VERSION>1.5.2.RELEASE&L t;/version> <scope>test</scope> </dependency> </dependencies> </dependencyManagement></project>
The parent module introduces Spring-boot-starter-parent as its parent module, which can be simply used by default with the spring boot configuration. And the version of Spring Cloud is introduced as Dalston.release.
The pom.xml of the Submodule Registration-center-web relies on the version of the spring cloud. Other future services also rely on this version of Spring Cloud. The following is the contents of the Pom.xml
<?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 "> <parent> <ARTIFACTID>MICRO-SERVICE-INTEGRATION</ARTIFAC tid> <groupId>spring.cloud</groupId> <version>1.0-SNAPSHOT</version> </pare Nt> <modelVersion>4.0.0</modelVersion> <artifactid>registration-center-web</artifactid > <dependencies> <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> <profiles> <profile& Gt <id>register-first</id> <properties> <final.project.name>registration-ce Nter-first</final.project.name> <server.port>8881</server.port> </propertie s> <activation> <activeByDefault>true</activeByDefault> </act ivation> </profile> <profile> <id>register-second</id> < Properties> <final.project.name>registration-center-second</final.project.name> <server.port>8882</server.port> </properties> </profile> <PROFILE&G T <id>register-third</id> <properties> <final.project.name>registratiOn-center-third</final.project.name> <server.port>8883</server.port> </prop erties> </profile> </profiles> <build> <finalName>${final.project.name}< /finalname> <resources> <resource> <directory>src/main/resources< /directory> <filtering>true</filtering> </resource> </RESOURCES&G T </build></project>
In this pom.xml, different service boot ports can be developed according to the profile.
Now let's take a look at the Java code
Package Com.qee.registrationcenter.app;import Org.springframework.boot.springapplication;import Org.springframework.boot.autoconfigure.springbootapplication;import org.springframework.cloud.netflix.eureka.server.enableeurekaserver;@ Springbootapplication@enableeurekaserverpublic class Registrationcenterapplication {public static void main (String[] args) {Springapplication.run (registrationcenterapplication.class, args);}}
Very simple, mainly through the main function to start the project, 2 annotations @SpringBootApplication and @EnableEurekaServer. Then let's take a look at our application.properties file.
[email protected] @spring. application.name= registration-center-webserver.register.port1=8881server.register.port2=8882server.register.port3= 8883eureka.instance.hostname=register.center.com# because the app is a registry, set to False, The representative does not register himself with the registry eureka.client.register-with-eureka=true# since the Registry is responsible for maintaining the service instance, he does not need to retrieve the service eureka.client.fetch-registry= trueeureka.server.enable-self-preservation=false# The default registration domain #eureka.client.serviceurl.defaultzone=http://${ eureka.instance.hostname}:${server.port}/eureka/eureka.client.serviceurl.defaultzone=http://${ eureka.instance.hostname}:${server.register.port1}/eureka/,http://${eureka.instance.hostname}:${ server.register.port2}/eureka/#控制台彩色输出spring. Output.ansi.enabled=always
Here we are building a high-availability registry, where three registries are K1,K2,K3, where K1 ports are 8881 and K2 ports are 8882,K3 ports of 8883. Then K1 registers itself with the K2,k3 registry, and K2 registers himself with the K1,k3 registry, and K3 registers itself with the K1,K2 registry. Due to the launch of the K1 Registration Center, K2 and K3 Registration Center is not open, so K1 will be reported abnormal, but the service will still start normally, the same K2 will also be reported abnormal because K3 did not start, so will also report the exception, but when the K3, K1 and K2 Registry has started normally, so K3 will not report abnormal. Finally in their respective registries can see the other 2 registries most services registered up. The respective access addresses are http://register.center.com:8881, http://register.center.com:8882, http://register.center.com:8883.
Then we started three registries, and we looked at the following results:
Third, analysis
@EnableEurekaServer The note initiates a service registry to provide a conversation to other apps.
Eureka.client.register-with-eureka: This parameter indicates whether the Eureka app (including the registry) is registered with the registry, and if it is a single registry, set this parameter to False, which means that it does not register itself with the registry. If you are building a highly available cluster registry, this property is set to true. The property value is true by default.
Eureka.client.fetch-registry: This parameter indicates whether the service needs to be retrieved, or False if the single registry does not need to retrieve the service. The default value of this parameter is true.
Eureka.client.serviceUrl.defaultZone: This parameter specifies the registered address of the default registry, and other MicroServices applications register the service with that attribute value.
Eureka.server.enable-self-preservation: Set to false is the protection mechanism for shutting down the registry, which defaults to true.
Additional detailed properties and configurations can be viewed in the official documentation. or leave a message for us to discuss together.
The GitHub address for the project is: Https://github.com/vOoT/micro-service-integration.git
Spring Cloud Registration Center Eureka