1. 程式人生 > >Spring cloud eureka 入門使用及指導說明(單機篇)

Spring cloud eureka 入門使用及指導說明(單機篇)

Spring cloud eureka 入門使用及指導說明(單機篇)

 

基本概念和方案

    Eureka是基於REST(Representational State Transfer,代表性狀態傳輸)的服務,主要用於AWS雲中定位服務,以實現中間層伺服器的負載平衡和故障轉移。我們稱這個服務為Eureka伺服器。Eureka還帶有一個基於Java的客戶端元件,即Eureka客戶端,它使與服務的互動更容易。

 

    客戶端還有一個內建的負載均衡器,可以進行基本的迴圈負載均衡。在Netflix,一個更復雜的負載平衡器包裝Eureka提供加權負載平衡的基礎上的幾個因素,如流量,資源使用情況,錯誤條件等,以提供卓越的彈性。

    叢集方案邏輯架構圖如下:

 

    上面的體系結構描述了Netflix如何部署Eureka,這是通常執行的方式。每個區域一個Eureka叢集區域,負責該區域內的服務例項。每個區域至少有一臺Eureka伺服器來處理區域故障。

 

    服務註冊Eureka,然後傳送心跳每30秒更新一次租約。如果客戶端無法續租幾次,則會在大約90秒內將其從伺服器登錄檔中取出。註冊資訊和更新被複制到群集中的所有Eureka節點。來自任何區域的客戶端可以查詢登錄檔資訊(每30秒發生一次)以找到他們的服務(可能位於任何區域)並進行遠端呼叫。

Eureka-Server

搭建Server端,有三種方式:

 

第一種,可以直接去到Maven庫下載war,放入Tomcat中即可使用,地址:

http://search.maven.org/#search%7Cga%7C1%7Ceureka-server

第二種,可以去到Github下載Netflix/eureka最新的原始碼自己編譯,地址:

https://github.com/Netflix/eureka

第三種,也是本章節主要的講解,通過Spring boot建立一個Eureka Server專案。

1、pom.xml 引入Eureka Server依賴

 
  1. <parent>

  2.     <groupId>org.springframework.boot</groupId>

  3.     <artifactId>spring-boot-starter-parent</artifactId>

  4.   <version>1.5.9.RELEASE</version>

  5. <relativePath />

  6. </parent>

  7. <dependencies>

  8.     <dependency>

  9.         <groupId>org.springframework.cloud</groupId>

  10.     <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>

  11. <version>1.4.2.RELEASE</version>

  12. </dependency>

  13. </dependencies>

  14.  
  15. <dependencyManagement>

  16.     <dependencies>

  17.        <dependency>

  18.     <groupId>org.springframework.cloud</groupId>

  19. <artifactId>spring-cloud-dependencies</artifactId>

  20. <version>Dalston.SR5</version>

  21. <type>pom</type>

  22. <scope>import</scope>

  23. </dependency>

  24.     </dependencies>

  25. </dependencyManagement>

2、配置檔案application.properties

server.port=8765

 #表示是否註冊Eureka伺服器,因為自身作為服務註冊中心,所以為false

eureka.client.registerWithEureka=false

#是否從eureka上獲取註冊資訊,同上

eureka.client.fetchRegistry=false

3、建立一個啟動類EurekaServerApplication,並啟動

執行啟動類,訪問 http://127.0.0.1:8761/,檢視系統狀態,如下圖:

 

Eureka-Client

 

1、pom.xml 引入Eureka Client依賴

 
  1. <parent>

  2.     <groupId>org.springframework.boot</groupId>

  3. <artifactId>spring-boot-starter-parent</artifactId>

  4. <version>1.5.9.RELEASE</version>

  5. <relativePath />

  6. </parent>

  7.  
  8. <dependencies>

  9.     <dependency>

  10.     <groupId>org.springframework.cloud</groupId>

  11. <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>

  12.    <version>1.4.2.RELEASE</version>

  13.    </dependency>

  14. </dependencies>

  15.  
  16. <dependencyManagement>

  17.     <dependencies>

  18.     <dependency>

  19.     <groupId>org.springframework.cloud</groupId>

  20.    <artifactId>spring-cloud-dependencies</artifactId>

  21. <version>Dalston.SR5</version>

  22. <type>pom</type>

  23. <scope>import</scope>

  24. </dependency>

  25. </dependencies>

  26. </dependencyManagement>

 

2、配置檔案application.properties

server.port=18765

  連線註冊中心的服務地址

eureka.client.serviceUrl.defaultZone=http://127.0.0.1:8761/eureka

3、建立一個啟動類EurekaServerApplication,並啟動

 

 

相關說明

 

1、pom.xml引入包的選擇問題

使用最新的 spring-cloud-starter-netflix-eureka-server,而spring-cloud-starter-eureka-server已經過期。

 

2、@EnableEurekaClient和@EnableDiscoveryClient的區別

spring cloud中discovery service有許多種實現(eureka、consul、zookeeper等等),@EnableDiscoveryClient基於spring-cloud-commons,@EnableEurekaClient基於spring-cloud-netflix。

就是如果選用的註冊中心是eureka,那麼就推薦@EnableEurekaClient,如果是其他的註冊中心,那麼推薦使用@EnableDiscoveryClient。