Spring Cloud 注冊中心Eureka


一、簡介

      最近在看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文件。

server.port=@server.port@

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
 
        
 
        
 
 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM