1. 程式人生 > >Spring Cloud 註冊中心Eureka

Spring Cloud 註冊中心Eureka

ont 允許 包括 enter 1.0 bbb oid pub try

一、簡介

最近在看Spring Cloud微服務,接下來的時間和大家一起分享我所看到的,公司現在用的是dubbo ,之後有時間也去了解了解dubbo的源碼。與dubbo相比較,Spring Cloud 在微服務方面有很多全面的實踐。今天主要和大家簡單介紹一下其中的一個組件Eureka註冊中心。Eureka同其他服務註冊中心一樣,支持高可用配置。如果Eureka以集群模式不熟,當集群中有分片出現故障時,那麽Eureka就轉入自我保護模式。它允許在分片故障期間繼續提供服務的發現和註冊,當故障分片恢復運行時,集群的其他分片會把它們的狀態再次同步回來。

二、實踐

首先我們創建一個Spring Boot的maven工程,名為micro-service-integration。下面有一個子模塊名為registration-center-web,該子模塊就是我們今天介紹的註冊中心。其工程目錄如下:

技術分享

在父工程中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">
    <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 for Spring 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>

  在該父模塊引入 spring-boot-starter-parent作為其父模塊,這樣可以簡單的默認的使用Spring Boot 配置。並且引入Spring Cloud 的版本為Dalston.RELEASE。

  而在子模塊 registration-center-web 的pom.xml 依賴該Spring Cloud的版本。以後的其他服務也依賴該版本的Spring Cloud。下面是該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</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>

  在該pom.xml 中,可以根據profile中來制定不同的服務啟動端口。

  接下來我們來看一下java代碼

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);
	}
}

  非常的簡單,主要通過main函數啟動該工程,2個註解 @SpringBootApplication 和 @EnableEurekaServer。然後我們來看一下我們application.properties文件。

[email protected]@

spring.application.name=registration-center-web

server.register.port1=8881
server.register.port2=8882
server.register.port3=8883

eureka.instance.hostname=register.center.com

#由於該應用為註冊中心,所以設置為false,代表不向註冊中心註冊自己
eureka.client.register-with-eureka=true


#由於註冊中心的職責就是維護服務實例,所以他不需要去檢索服務
eureka.client.fetch-registry=true

eureka.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

  這裏我們是搭建一個高可用的註冊中心,其中有三個註冊中心分別為K1,K2,K3,其中K1的端口為8881 ,K2的端口為8882,K3的端口為8883。接著然後K1向K2,K3註冊中心註冊自己,而K2向K1,K3註冊中心註冊自己,而K3向K1,K2註冊中心註冊自己。由於在啟動K1註冊中心時,K2和K3註冊中心還沒開啟,所以K1會報異常,但是服務還是會正常啟動,同理K2也會由於K3沒有啟動,所以也會報異常,但是啟動K3的時候,K1和K2註冊中心已經正常啟動,所以K3不會報異常。最後在各自的註冊中心可以看到其他2個註冊中心最為服務註冊上去。各自的訪問地址為 http://register.center.com:8881、http://register.center.com:8882、http://register.center.com:8883。

  接著我們啟動三個註冊中心,我們看下如下結果:

技術分享

三、分析

@EnableEurekaServer 該註解啟動一個服務註冊中心提供給其他應用進行對話。
eureka.client.register-with-eureka : 該參數代表該Eureka應用(包括註冊中心)是否註冊到註冊中心中,如果只是一個單一註冊中心,那麽把該參數設置為false,代表不向註冊中心註冊自己。如果是搭建高可用的集群註冊中心,則該屬性設置為true。該屬性值默認true。
eureka.client.fetch-registry : 該參數代表是否需要檢索服務,如果是單一註冊中心則不需要去檢索服務,則設置為false。該參數默認值為true。
eureka.client.serviceUrl.defaultZone : 該參數指定默認註冊中心的註冊地址,其他的微服務應用就是通過該屬性值來註冊服務。
eureka.server.enable-self-preservation :設置為false 代表關閉註冊中心的保護機制,默認為true。

其他的詳細屬性和配置可以查看官方文檔。或者留言大家一起討論。
該工程的gitHub地址為:https://github.com/vOoT/micro-service-integration.git


 
 

Spring Cloud 註冊中心Eureka