服務治理java
SpringCloud Eureka是SpringCloud Netflix微服務套件的一部分,它基於Netflix Eureka作了二次封裝,主要完成微服務的服務治理功能,SpringCloud經過爲Eureka增長了SpringBoot自動化配置,只須要簡單的依賴和配置就可完成Eureka整合搭建。node
服務治理能夠說是微服務中作核心的模塊,主要負責服務的自動化註冊與發現,在最初開始構建微服務的時候,可能服務並不會不少,咱們能夠經過一些靜態配置進行相互調用,可是隨着業務的更新迭代,會愈來愈複雜,就會發現靜態配置已經難以知足咱們的需求了,咱們的集羣、服務位置、服務命名均可能發生變化,若是是人工進行維護的話將消耗不少的人力,爲了解決微服務中的服務實例維護問題,產生了大量的服務治理的框架,這些框架都圍繞服務註冊和發現及服務實例的自動化管理。web
話很少說上代碼,搭建一個單機的Eureka,首先須要建立SpringBoot項目,建立SpringBoot這裏就不講解了,不會能夠看一下我搭建SpringBoot的文章,下圖爲目錄:spring
導入Eureka相關依賴信息,SpringBoot我用的是1.5.3-RELEASE版本:安全
1 <parent> 2 <groupId>org.springframework.boot</groupId> 3 <artifactId>spring-boot-starter-parent</artifactId> 4 <version>1.5.3.RELEASE</version> 5 </parent> 6 7 <properties> 8 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 9 <java.version>1.8</java.version> 10 <spring.cloud.version>Dalston.SR2</spring.cloud.version> 11 </properties> 12 13 <dependencyManagement> 14 <dependencies> 15 <dependency> 16 <groupId>org.springframework.cloud</groupId> 17 <artifactId>spring-cloud-dependencies</artifactId> 18 <version>${spring.cloud.version}</version> 19 <type>pom</type> 20 <scope>import</scope> 21 </dependency> 22 </dependencies> 23 </dependencyManagement> 24 25 <dependencies> 26 <dependency> 27 <groupId>org.springframework.cloud</groupId> 28 <artifactId>spring-cloud-starter-eureka-server</artifactId> 29 </dependency> 30 <dependency> 31 <groupId>org.springframework.boot</groupId> 32 <artifactId>spring-boot-starter-actuator</artifactId> 33 </dependency> 34 </dependencies>
EurekaServer.java代碼以下,也就是EurekaServer啓動入口:網絡
1 package cloud.eureka.server; 2 3 import org.springframework.boot.SpringApplication; 4 import org.springframework.boot.autoconfigure.SpringBootApplication; 5 import org.springframework.boot.web.support.SpringBootServletInitializer; 6 import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; 7 8 @SpringBootApplication 9 @EnableEurekaServer 10 public class EurekaServer extends SpringBootServletInitializer { 11 public static void main(String[] args) { 12 SpringApplication.run(EurekaServer.class, args); 13 } 14 }
核心部分application.yml:app
#公共配置信息 server: port: 9901 spring: application: name: eureka-server profiles: active: node1 eureka: instance: prefer-ip-address: true instance-id: ${spring.cloud.client.ipAddress}:${server.port} server: enableSelfPreservation: true #關閉保護機制,以確保註冊中心能夠將不可用的實例剔除.(注意:自我保護模式是一種應對網絡異常的安全保護措施,使用自我保護模式,可讓Eureka集羣更加的健壯、穩定) evictionIntervalTimerInMs: 5000 #eureka server清理無效節點的時間間隔,默認60000毫秒,即60秒 client: registerWithEureka: true fetchRegistry: true #用---分割不一樣的環境對應的配置信息(YML以"---"做爲文檔分割符,以"..."做爲結束標誌) --- spring: profiles: node1 eureka: client: serviceUrl: defaultZone: http://10.200.159.22:${server.port:9902}/eureka/ ...
搭建結束,是否是很簡單想火燒眉毛的運行一下了:框架
運行方式:maven
1.IDE直接配置啓動類spring-boot
2.maven install以後執行java -jar eureka-server.jar --spring.profiles.active=node1&
3.mvn spring-boot:run -Dspring.profiles.active=node1
啓動以後訪問測試地址:http://註冊中心IP:端口,訪問以後就會出現下面的界面
註冊成功以後,一塊兒來看下原理:
服務註冊:
服務提供在啓動時候會發送一個REST請求將本身註冊到Eureka Server,同時會帶上自身的元數據信息(主機、端口、URL等),Eureka接收到REST請求以後,將元數據信息存儲到一個雙層Map中,第一層的Key是服務名,第二層的Key是服務的實例名,在服務註冊的時候須要確認一下eureka.client.register-with-eureka=true參數是否正確,若是爲false則不啓動註冊操做。
服務同步:
服務提供者將本身註冊到不一樣的服務註冊中心上,也就是說他們的信息被兩個服務註冊中心所維護,因爲服務註冊中心之間因相互註冊爲服務,當服務提供者發送註冊請求到其中一個服務註冊中心的時候,它會將該請求轉發給集羣中相連的其餘註冊中心,從而實現註冊中心之間的服務同步。
服務續約:
註冊成功以後,服務提供者會維護一個心跳來持續告訴Eureka Server 「我還活着」,防止Eureka的失效剔除將該服務實例從服務列表中排出出去。
eureka.instance.lease-renewal-interval-in-seconds=30
eureka.instance.lease-expiration-duration-in-seconds=90
服務下線:
系統運行中必然會面臨關閉和重啓服務的某個實例狀況,在服務關閉期間咱們天然不但願客戶端能夠繼續調用關閉的實例,因此在客戶端程序中,當服務實例進行正常關閉操做的時候,會觸發一個服務下線的REST請求給Eureka,告訴它「我要下線了」,服務端接受到請求以後將狀態置爲DOWN,而且把該事件傳播出去。
服務失效剔除:
有些時候,微服務並不必定是正常下線的,可能內存溢出、網絡故障等問題的出現不能讓微服務正常使用,可是服務註冊中心並無收到服務下線的請求,爲了將這些出問題的微服務實例從服務列表中剔除掉,Eureka在啓動的時候會建立一個定時任務,默認每隔60秒將清單中默認90秒沒有服務續約的微服務剔除。
自我保護:
Eureka在運行期間會統計服務發送心跳失敗的比例在15分鐘內是否低於85%,若是出現低於的狀況,Eureka就會將當前的實例信息保護起來,讓這些實例不會過時。可是,在保護的這段期間若是出現問題,客戶端很容易拿到實際已經不存在的服務實例,會出現調用失敗的狀況,因此客戶端必需要有容錯機制,好比請求重試、斷路由等。
以後會講解如何搭建Eureka集羣,敬請期待。。。