Spring Cloud Eureka
服務治理
是微服務架構中最核心最基本的模塊。用於實現各個微服務實例的自動化注冊與發現。
服務注冊:
在服務治理框架中,都會構建一個注冊中心,每個服務單元向注冊中心登記自己提供的服務,將主機和端口號、版本號、通信協議等一些附加信息告知注冊中心,注冊中心按服務名分類組織服務清單。
服務注冊中心還需要以心跳的方式去監測清單中的服務是否可用,若不可用,需從服務清單中剔除,達到清楚障礙的目的。
服務發現:
服務的調用通過向服務名發起請求調用實現。調用方向服務注冊中心咨詢服務,並獲取所有服務的實例清單,實現對具體服務實例的訪問。
Netflix Eureka
Spring Cloud Eureka 使用Netflix Eureka來實現服務注冊於發現。既包含服務端組件也包含客戶端組件,且均采用java編寫,因此Eureka適用於java實現的分布式系統,或與JVM兼容語言構建的系統。
因為Eureka服務端的服務治理機制提供了完備的RESTful API,也支持非java語言構建的微服務應用納入Eureka的服務治理體系中去。
Eureka服務端:
服務注冊中心,支持高可用配置。依托於強一致性提供良好的服務實例可用性,應用於不同的故障場景。
以集群部署,分片出現故障時,Eureka進入自我保護模式,其他分片提供服務的發現與注冊,當故障分片恢復時,集群中的其他分片會把故障分片的狀態再次同步回來。
Eureka客戶端:
處理服務的注冊於發現。
在程序運行時,Eureka客戶端向注冊中心注冊自身提供的服務並周期性地發送心跳來更新它的服務租約。也可以從注冊中心查詢當前注冊的服務信息並把他們緩存到本地周期性地刷新服務狀態。
搭建服務注冊中心
pom依賴:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
然后在主類上加上注解@EnableEurekaServer

在application.properties上加入:

eureka.client.register-with-eureka:這是向注冊中心注冊自己,由於本身就是注冊中心,所以設置為false
eureka.client.fetch-registry:注冊中心就是維護服務實例,不需要檢索服務,設置為false
eureka.client.service-url.defaultZone這個實際上就是注冊中心的地址。
注冊服務提供者
pom文件:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
在主類上添加注解@EnableDiscoveryClient

然后在配置文件中添加:

即可在配置中心中找到服務:

高可用注冊中心
之前我們通過下面兩個參數讓服務中心不注冊自己:

eureka server 的高可用實際上就是將自己作為服務向其他服務注冊中心注冊自己,這樣就可以形成一組互相注冊的服務注冊中心,以實現服務清單的互相同步,達到高可用的效果。
注:配置文件還需要添加上面兩個參數,以防止服務中心對自己進行注冊。
創建配置文件application-peer1.properties,作為peer1配置中心,且執行peer2配置中心:

創建配置文件application-peer2.properties,作為peer2配置中心,且執行peer1配置中心:

通過命令行分別啟動peer1和peer2:


這時服務注冊的時候,需要在eureka.client.service-url.defaultZone分別注冊peer1與peer2,用逗號分開。
服務發現與消費
通過前文已經完成了服務注冊中心與服務提供者了。
我們現在需要構建一個服務消費者,它主要有兩個目標:服務發現與消費服務。
服務發現是由eureka的客戶端完成,服務消費是由Ribbon完成。
Ribbon是一個基於HTTP和TCP的客戶端負載均衡器。
創建一個SpringBoot的基本工程來實現服務消費者,取名為ribbon-consumer。pom如下:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-ribbon</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
在主類上加@EnableDiscoveryClient注解,讓該應用注冊為Eureka客戶端,獲取發現服務能力。同時在主類中注入RestTemplate的Spring Bean實例。並通過@LoadBalanced注解開啟客戶端負載均衡。

創建對應的接口控制器:
注:可以看到訪問的地址是一個服務名,而不是一個具體的地址,在服務治理框架中,這是非常重要的特性。

在配置文件中配置eureka注冊中心的位置。

