Spring Cloud Registration Center Eureka

Source: Internet
Author: User

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:

12345678910111213141516171819202122232425262728293031323334353637383940414243 <?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-center-web</module>    </modules>    <name>micro-service-integration</name>    <description>Demo project forSpring Boot</description>    <parent>        <groupId>org.springframework.boot</groupId>        <artifactId>spring-boot-starter-parent</artifactId>        <version>1.5.2.RELEASE</version>    </parent>    <dependencyManagement>        <dependencies>            <dependency>                <groupId>org.springframework.cloud</groupId>                <artifactId>spring-cloud-dependencies</artifactId>                <version>Dalston.RELEASE</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</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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263 <?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</artifactId>        <groupId>spring.cloud</groupId>        <version>1.0-SNAPSHOT</version>    </parent>    <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>            <id>register-first</id>            <properties>                <final.project.name>registration-center-first</final.project.name>                <server.port>8881</server.port>            </properties>            <activation>                <activeByDefault>true</activeByDefault>            </activation>        </profile>        <profile>            <id>register-second</id>            <properties>                <final.project.name>registration-center-second</final.project.name>                <server.port>8882</server.port>            </properties>        </profile>        <profile>            <id>register-third</id>            <properties>                <final.project.name>registration-center-third</final.project.name>                <server.port>8883</server.port>            </properties>        </profile>    </profiles>    <build>        <finalName>${final.project.name}</finalName>        <resources>            <resource>                <directory>src/main/resources</directory>                <filtering>true</filtering>            </resource>        </resources>    </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

1234567891011121314 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 @EnableEurekaServer public   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.

1234567891011121314151617181920212223242526 server.port=@server[email protected]spring.application.name=registration-center-webserver.register.port1=8881server.register.port2=8882server.register.port3=8883eureka.instance.hostname=register.center.com#由于该应用为注册中心,所以设置为false,代表不向注册中心注册自己eureka.client.register-with-eureka=true#由于注册中心的职责就是维护服务实例,所以他不需要去检索服务eureka.client.fetch-registry=trueeureka.server.enable-self-preservation=false#默认的注册域#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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.