Spring Cloud 系列之注冊中心 Eureka詳解
1.1 簡介
1.1.1 概述
Netflix Eureka 是由 Netflix 開源的一款基于 REST 的服務(wù)發(fā)現(xiàn)組件,包括 Eureka Server 及 Eureka Client。2012 年 9 月在 GitHub 上發(fā)布 1.1.2 版本,目前 Netflix 以宣布閉源,所以市面上還是以 1.x 版本為主。Eureka 提供基于 REST 的服務(wù),在集群中主要用于服務(wù)管理。Eureka 提供了基于 Java 語言的客戶端組件,客戶端組件實現(xiàn)了負(fù)載均衡的功能,為業(yè)務(wù)組件的集群部署創(chuàng)造了條件。使用該框架,可以將業(yè)務(wù)組件注冊到 Eureka 容器中,這些組件可進行集群部署,Eureka 主要維護這些服務(wù)的列表并自動檢查它們的狀態(tài)。Spring Cloud Netflix Eureka 是 Pivotal 公司為了將 Netflix Eureka 整合于 Spring Cloud 生態(tài)系統(tǒng)提供的版本。
Eureka 包含兩個組件:Eureka Server 和 Eureka Client, Eureka Server 提供服務(wù)注冊服務(wù)。各個微服務(wù)節(jié)點通過配置啟動后,會在 EurekaServer 中進行注冊,這樣 EurekaServer 中的服務(wù)注冊表中將會存儲所有可用服務(wù)節(jié)點的信息,服務(wù)節(jié)點的信息可以在界面中直觀看到。EurekaClient 通過注冊中心進行訪問。它是一個 Java 客戶端,用于簡化 Eureka Server 的交互,客戶端同時也具備一個內(nèi)置的、使用輪詢(round-robin)負(fù)載算法的負(fù)載均衡器。在應(yīng)用啟動后,將會向 Eureka Server 發(fā)送心跳(默認(rèn)周期為30秒)。如果 Eureka Server 在多個心跳周期內(nèi)沒有接收到某個節(jié)點的心跳,EurekaServer 將會從服務(wù)注冊表中把這個服務(wù)節(jié)點移除(默認(rèn)90秒)

1.1.2 原理圖
一個簡單的 Eureka 集群,需要一個 Eureka 服務(wù)器、若干個服務(wù)提供者。我們可以將業(yè)務(wù)組件注冊到 Eureka 服務(wù)器中,其他客戶端組件可以向服務(wù)器獲取服務(wù)并且進行遠(yuǎn)程調(diào)用。Eureka:就是服務(wù)注冊中心(可以是一個集群),對外暴露自己的地址;提供者:啟動后向 Eureka 注冊自己信息(地址,提供什么服務(wù));消費者:向 Eureka 訂閱服務(wù),Eureka 會將對應(yīng)服務(wù)的所有提供者地址列表發(fā)送給消費者,并且定期更新;心跳(續(xù)約):提供者定期通過 http 方式向 Eureka 刷新自己的狀態(tài)。

1.1.3 相關(guān)依賴
<!-- 需要確定 Spring Cloud 版本 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency>
1.2 搭建 EurekaServer
1.2.1 相關(guān)依賴
現(xiàn)在都是子父工程,我們將子模塊中都需要用的依賴放到父工程的 pom 文件中
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<packaging>pom</packaging>
<modules>
<module>eureka</module>
</modules>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.10.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.software</groupId>
<artifactId>spring-cloud</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-cloud</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Hoxton.SR8</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- 演示就不使用數(shù)據(jù)庫了 -->
<!--<dependency>-->
<!--<groupId>org.springframework.boot</groupId>-->
<!--<artifactId>spring-boot-starter-data-jpa</artifactId>-->
<!--</dependency>-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- H 版 Spring Cloud 將 server 與 client 分開了,需要導(dǎo)入兩個坐標(biāo) -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
1.2.2 聲明為 Eureka Server
在 eureka 服務(wù)的啟動類上使用 @EnableEurekaServer,聲明當(dāng)前應(yīng)用為 Eureka 服務(wù)。
/**
* Created with IntelliJ IDEA.
*
* @author Demo_Null
* @date 2020/10/29
* @description Eureka 啟動類
*/
@SpringBootApplication
@EnableEurekaServer
public class EurekaApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaApplication.class, args);
}
}
1.2.3 配置文件
server: port: 8081 spring: application: name: eurekaServer # 應(yīng)用名稱,在 Eureka 中作為 id 標(biāo)識 eureka: client: register-with-eureka: false # 不注冊自己 fetch-registry: false # 不拉取自己 service-url: defaultZone: http://127.0.0.1:8081/eureka/ # EurekaServer 的地址,如果是集群,需要加上其它 Server 的地址
1.2.4 啟動服務(wù)
啟動服務(wù)訪問對應(yīng)的端口就可以看到以下界面,現(xiàn)在是一個服務(wù)都沒有注冊上來。可以把 register-with-eureka,fetch-registry 兩個配置取消就可以看到 eureka 自己了。


1.3 提供者
1.3.1 聲明為 Eureka Client
在服務(wù)提供者啟動類中使用 @EnableDiscoveryClient,讓 Eureka 能夠發(fā)現(xiàn),掃描到該服務(wù)。@EnableEurekaClient 注解也能實現(xiàn)但是該注解只支持 Eureka 作為注冊中心,@EnableDiscoveryClient 可以是其他注冊中心,建議使用 @EnableDiscoveryClient
/**
* Created with IntelliJ IDEA.
*
* @author Demo_Null
* @date 2020/10/29
* @description 服務(wù)提供者啟動類
*/
@SpringBootApplication
@EnableDiscoveryClient
public class ProviderApplication {
public static void main(String[] args) {
SpringApplication.run(ProviderApplication.class, args);
}
}
1.3.2 配置文件
server: port: 8082 spring: application: name: ProviderServer # 應(yīng)用名稱,在 Eureka 中作為 id 標(biāo)識 eureka: client: service-url: defaultZone: http://127.0.0.1:8081/eurake/
1.3.3 提供服務(wù)
/**
* Created with IntelliJ IDEA.
*
* @author Demo_Null
* @date 2020/10/29
* @description
*/
@RestController
@RequestMapping("/provider")
public class ProviderController {
@GetMapping("/get")
public Object get() {
return "你已經(jīng)消費了";
}
}
1.3.4 啟動服務(wù)
啟動服務(wù)之后,會自動將自己注冊到 Eureka 中

1.4 消費者
1.4.1 聲明為 Eureka Client
/**
* Created with IntelliJ IDEA.
*
* @author Demo_Null
* @date 2020/10/29
* @description 消費者啟動類
*/
@SpringBootApplication
@EnableDiscoveryClient
public class ConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class, args);
}
// 將 RestTemplate 交由容器管理
@Bean
public RestTemplate getRestTemplate() {
return new RestTemplate();
}
}
1.4.2 配置文件
server: port: 8083 spring: application: name: ConsumerServer # 應(yīng)用名稱,在 Eureka 中作為 id 標(biāo)識 eureka: client: service-url: defaultZone: http://127.0.0.1:8081/eureka
1.4.3 消費服務(wù)
我們之前使用 RestTemplate 需要自己寫 URI,這樣很不利于維護,而且容易出錯,現(xiàn)在只需要確定應(yīng)用名稱,利用應(yīng)用名稱從 Eureka 中就可以獲取到詳細(xì)信息。
/**
* Created with IntelliJ IDEA.
*
* @author gaohu9712@163.com
* @date 2020/10/29
* @description
*/
@RestController
@RequestMapping("/consumer")
public class ConsumerController {
@Autowired
private DiscoveryClient discoveryClient;
@GetMapping("/go")
public void go() {
List<ServiceInstance> providerServer = discoveryClient.getInstances("ProviderServer");
if (0 == providerServer.size()) {
return;
}
ServiceInstance serviceInstance = providerServer.get(0);
String host = serviceInstance.getHost();
int port = serviceInstance.getPort();
URI uri = serviceInstance.getUri();
System.out.println("主機:" + host);
System.out.println("端口:" + port);
System.out.println("uri:" + uri);
RestTemplate restTemplate = new RestTemplate();
String str = restTemplate.getForObject(uri + "/provider/get", String.class);
System.out.println(str);
}
}
1.4.4 啟動服務(wù)

1.4.5 請求服務(wù)

1.4.6 執(zhí)行流程
♞ 先啟動 eureka 注冊中心
♞ 啟動服務(wù)提供者 provider
♞ 服務(wù)提供者啟動后會把自身信息(比如服務(wù)地址以別名方式注冊進 eureka)
♞ 消費者 consumer 服務(wù)在需要調(diào)用接口時,使用服務(wù)別名去注冊中心獲取實際的 RPC 遠(yuǎn)程調(diào)用地址
♞ 消費者獲得調(diào)用地址后,底層實際是利用 HttpClient 技術(shù)實現(xiàn)遠(yuǎn)程調(diào)用
♞ 消費者獲得服務(wù)地址后會緩存在本地 jvm 內(nèi)存中,默認(rèn)每間隔 30 秒更新一次服務(wù)調(diào)用地址
1.5 補充配置
1.5.1 actuator 信息完善
我們現(xiàn)在的服務(wù)注冊到 Eureka 上面是沒有 ip 地址的,以后等服務(wù)搭建集群是很不方便的,所以我們需要讓他顯示自己的 ip 地址;第二個就是服務(wù)名稱為主機 + 服務(wù)名 + 端口,這樣就暴露了主機名,我們可以指定顯示的名稱。

在配置文件中添加如下配置,通過健康檢查(http://ip:port/actuator/health)查看是否修改成功。
eureka: instance: # 實例名稱 instance-id: consumer-01 # 地址中顯示 ip prefer-ip-address: true

1.5.2 自我保護機制 ☞ 概述
我們可以看到 Eureka 上有一行紅色的英文 EMERGENCY! EUREKA MAY BE INCORRECTLY CLAIMING INSTANCES ARE UP WHEN THEY'RE NOT. RENEWALS ARE LESSER THAN THRESHOLD AND HENCE THE INSTANCES ARE NOT BEING EXPIRED JUST TO BE SAFE.它代表了 Eureka 保護模式的開啟。一旦進入保護模式,Eureka Server 將會嘗試保護其服務(wù)注冊表中的信息,不再刪除服務(wù)注冊表中的數(shù)據(jù),也就是不會注銷任何微服務(wù)。
默認(rèn)情況下,如果 Eureka Server 在一定時間內(nèi)沒有接收到某個微服務(wù)實例的心跳,Eureka Server 將會注銷該實例(默認(rèn)90秒)。但是當(dāng)網(wǎng)絡(luò)分區(qū)故障發(fā)生(延時、卡頓、擁擠)時,微服務(wù)與 Eureka Server 之間無法正常通信,以上行為可能變得非常危險了——因為微服務(wù)本身其實是健康的,此時本不應(yīng)該注銷這個微服務(wù)。為了防止 Eureka Client 可以正常運行,但是與 Eureka Server 網(wǎng)絡(luò)不通情況下,Eureka Server 不會立刻將 Eureka Client 服務(wù)剔除。
☞ 關(guān)閉自我保護機制
我們之前的 Eureka 截圖中可以看到 DESKTOP-GL7GS52:ConsumerServer:8083,consumer-01 兩個同時存在,這明明是一個服務(wù),修改完配置之后前面的沒有剔除,這就是因為自我保護機制打開了。
# Eureka Server 配置 eureka: server: # 關(guān)閉自我保護模式, 默認(rèn)為打開 enable-self-preservation: false # 續(xù)期時間,即掃描失效服務(wù)的間隔時間 eviction-interval-timer-in-ms: 5000 # Eureka Client 配置 eureka: instance: # Eureka Client 給 Eureka Server 發(fā)送心跳的時間間隔,默認(rèn) 30 單位是 s lease-renewal-interval-in-seconds: 1 # Eureka Server 最后一次收到心跳的等待上限,超時剔除服務(wù),默認(rèn) 90 單位是 s lease-expiration-duration-in-seconds: 2
1.6 Eureka 高可用
1.6.1 Eureka 集群搭建
在之前的單體中我們的端口是隨意的,但是搭建集群我們需要對端口進行規(guī)劃,例如將 808X 端口作為 Eureka 集群的端口。先來看下配置有什么區(qū)別,起初我們是將自己注冊到自己上,現(xiàn)在我們需要將自己注冊到其他 Eureka 上,有多個則用 , 隔開。
server: port: 8081 spring: application: name: eurekaServer eureka: client: register-with-eureka: false fetch-registry: false service-url: defaultZone: http://127.0.0.1:8082/eureka server: enable-self-preservation: false eviction-interval-timer-in-ms: 5000
server: port: 8082 spring: application: name: eurekaServer_back eureka: client: register-with-eureka: false fetch-registry: false service-url: defaultZone: http://127.0.0.1:8081/eureka server: enable-self-preservation: false eviction-interval-timer-in-ms: 5000


1.6.2 Privoder 集群
服務(wù)提供者的集群配置了多個 Eureka 地址,會將自己同時注冊到多個 Eureka 上,除了配置文件以外其他的服務(wù)代碼完全一致,也可以加以區(qū)分是哪個提供的服務(wù)。需要注意的是 Eureka 集群的應(yīng)用名稱可以不一致甚至不寫,但是服務(wù)提供者的應(yīng)用名稱必須保持一致,否則會被認(rèn)為不是一個服務(wù)。
server: port: 8091 spring: application: name: ProviderServer # 應(yīng)用名稱,在 Eureka 中作為 id 標(biāo)識 eureka: client: service-url: defaultZone: http://127.0.0.1:8081/eureka, http://127.0.0.1:8082/eureka instance: instance-id: provider-prim prefer-ip-address: true lease-renewal-interval-in-seconds: 1 lease-expiration-duration-in-seconds: 2
server: port: 8092 spring: application: name: ProviderServer eureka: client: service-url: defaultZone: http://127.0.0.1:8081/eureka, http://127.0.0.1:8082/eureka instance: instance-id: provider-back prefer-ip-address: true lease-renewal-interval-in-seconds: 1 lease-expiration-duration-in-seconds: 2

1.6.3 遠(yuǎn)程調(diào)用
/**
* Created with IntelliJ IDEA.
*
* @author Demo_Null
* @date 2020/10/29
* @description
*/
@RestController
@RequestMapping("/consumer")
public class ConsumerController {
@Autowired
private DiscoveryClient discoveryClient;
@GetMapping("/go")
public void go() {
List<ServiceInstance> providerServer = discoveryClient.getInstances("ProviderServer");
if (0 == providerServer.size()) {
return;
}
RestTemplate restTemplate = new RestTemplate();
for (ServiceInstance instance : providerServer) {
System.out.print(instance.getUri() + "---");
String url = instance.getUri() + "/provider/get";
System.out.println(restTemplate.getForObject(url, String.class));
}
}
}

咱們可以使用服務(wù)發(fā)現(xiàn) DiscoveryClient 來獲取服務(wù)信息,但是無法自動選擇使用那個服務(wù),這里就涉及到 Ribbon 負(fù)載均衡了。我們可以將 RestTemplate 交由 Ioc 管理,在注入時使用 @LoadBalanced 注解進行負(fù)載均衡。
到此這篇關(guān)于Spring Cloud 系列之注冊中心 Eureka的文章就介紹到這了,更多相關(guān)Spring Cloud 注冊中心Eureka內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
springboot整合cxf發(fā)布webservice以及調(diào)用的方法
這篇文章主要介紹了springboot整合cxf發(fā)布webservice以及調(diào)用的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-08-08
IDEA中已配置阿里鏡像但maven無法下載jar包的問題及解決方法
這篇文章主要介紹了IDEA中已配置阿里鏡像但maven無法下載jar包的問題,本文給大家分享解決方法,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-08-08
利用Java多線程技術(shù)導(dǎo)入數(shù)據(jù)到Elasticsearch的方法步驟
這篇文章主要介紹了利用Java多線程技術(shù)導(dǎo)入數(shù)據(jù)到Elasticsearch的方法步驟,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07
JavaCV與FFmpeg音視頻流處理技巧總結(jié)大全
JavaCV是一個開源的Java接口,它為幾個著名的計算機視覺庫(如OpenCV、FFmpeg)提供了Java封裝,這篇文章主要給大家介紹了關(guān)于JavaCV與FFmpeg音視頻流處理技巧總結(jié)的相關(guān)資料,需要的朋友可以參考下2024-05-05
ehcache開源緩存框架_動力節(jié)點Java學(xué)院整理
Ehcache是現(xiàn)在最流行的純Java開源緩存框架,這篇文章主要介紹了ehcache框架的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-07-07
使用IDEA異常斷點來定位java.lang.ArrayStoreException的問題
這篇文章主要介紹了使用IDEA異常斷點來定位java.lang.ArrayStoreException的問題,平常開發(fā)過程中面對這種描述不夠清楚,無法定位具體原因的問題該如何處理,下面我們來一起學(xué)習(xí)一下吧2019-06-06
Java實現(xiàn)的簡單數(shù)字處理類及用法示例
這篇文章主要介紹了Java實現(xiàn)的簡單數(shù)字處理類及用法,涉及java數(shù)字運算相關(guān)操作技巧,需要的朋友可以參考下2018-01-01

