從REST和Instance角度理解Netfix Eureka

前言

最近項(xiàng)目開始使用到spring cloud作為項(xiàng)目的微服架構(gòu),在開發(fā)中遇
到很多問題,相信很多多人也遇到,寫這篇博客也是談?wù)勎谊P(guān)于spring cloud中的eureka的一些認(rèn)識(shí),當(dāng)中有誤的地方還請(qǐng)各位留言指出,多多交流。

首先看看官方對(duì)eureka的定義

Eureka是一個(gè)基于REST(Representational State Transfer)服務(wù),主要是用于云服務(wù)為目的的中間層服務(wù)器的負(fù)載平衡和故障轉(zhuǎn)移。Eureka還附帶了一個(gè)基于java的客戶端組件,Eureka客戶機(jī)使得交互與服務(wù)更加容易。客戶端也有一個(gè)基于循環(huán)負(fù)載平衡內(nèi)置的負(fù)載均衡器。當(dāng)然在Netflix中,還提供很多更加能滿足復(fù)雜要求的負(fù)載均衡器,它們基于交通、資源使用、錯(cuò)誤條件等因素提供更好的彈性。

這個(gè)我根據(jù)官方的介紹翻譯的原文鏈接在這https://github.com/Netflix/eureka/wiki/Eureka-at-a-glance/當(dāng)中有翻譯不對(duì)的還請(qǐng)留言指出。

從官方的文檔中我們能得到這些重要的信息:

  1. Eureka是基于REST服務(wù)的,也就意味著它是基于http協(xié)議的
  2. 它分為服務(wù)端(java語(yǔ)言實(shí)現(xiàn))和 客服端(可以是java也可以是非Java語(yǔ)言,因?yàn)镋ureka提供的是 rest服務(wù),當(dāng)然官方也有提供部分其他語(yǔ)言的官方包)。
  3. Eureka的作用是實(shí)現(xiàn)為服務(wù)間的負(fù)載均衡和故障轉(zhuǎn)移。
Eureka的服務(wù)端和客服端

Eureka的服務(wù)端來管理所有的微服,微服內(nèi)的所有服務(wù)需要依賴Eureka客戶端的依賴包,并在application上添加@EnableEurekaClient注解來實(shí)現(xiàn)Eureka客戶端。

<dependency>
  <groupId>com.netflix.eureka</groupId>
  <artifactId>eureka-client</artifactId>
  <version>1.1.16</version>
</dependency>

@EnableEurekaClient
public class ServiceHiApplication {

public static void main(String[] args) {
    SpringApplication.run(ServiceHiApplication.class, args);
}

}

當(dāng)然還有配置文件需要添加相關(guān)的配置,具體的可以參考官方文檔https://github.com/Netflix/eureka/wiki/Configuring-Eureka/
,現(xiàn)在網(wǎng)上也有很多相關(guān)的博客,這里我就不詳細(xì)寫了。

Eureka的客服端則是通過rest服務(wù)通知Eureka的服務(wù)端該客服端的健康狀況,事實(shí)是每個(gè)客服端的具體信息保存在InstanceInfo類中,通過ConcurrentHashMap<String, Map<String,Lease<InstanceInfo>>>
的雙層Map結(jié)構(gòu)來管理所有的的客服端。更重要的是其實(shí)在每個(gè)客服端在以心跳的模式定時(shí)通知服務(wù)端客戶端的實(shí)時(shí)狀態(tài)時(shí),客戶端也會(huì)獲得到一份最新的服務(wù)端的InstanceInfo列表清單,沒錯(cuò)其實(shí)每個(gè)客服端也能獲得微服的所有客服端的健康狀況。具體客服端獲得服務(wù)信息的方法如下:

@Qualifier("eurekaClient")
@Autowired
private EurekaClient eurekaClient;

public String  serviceUrl(){
    InstanceInfo instance = eurekaClient.getNextServerFromEureka("service-hi", false);
    return instance.getHomePageUrl();
}

@Autowired
private DiscoveryClient discoveryClient;
//獲得對(duì)應(yīng)服務(wù)名下所有的服務(wù)實(shí)力信息列表,服務(wù)如果有做集群,則返回的是該服務(wù)集群下的所有實(shí)力的列表
List<ServiceInstance> list = discoveryClient.getInstances("service-hi");
if (list != null && list.size() > 0 ) {
    String aa = list.get(0).getUri().getAuthority();
}
    
//獲得微服下所有的服務(wù)名
List<String> servicesNames = discoveryClient.getServices();
StringBuilder sb = new StringBuilder();
for (String s:servicesNames) {
    sb.append(s+"\t");
}
Eureka的REST服務(wù)

上面說過服務(wù)端和客戶端是通過rest服務(wù)實(shí)現(xiàn)通信的,也就是說如果我現(xiàn)在想知道服務(wù)端下所有客服端的具體信息和健康狀況只需要通過一個(gè)http請(qǐng)求就可以完成,答案是肯定的,當(dāng)然本身eureka服務(wù)本身就提供了管理界面來查看客戶端的具體信息如下:


image

但是這樣獲得的信息并不完全,所有如果你想獲得更加詳細(xì)的信息,或則你想使用其他編程語(yǔ)言實(shí)現(xiàn)客服端的話可以通過Eureka服務(wù)提供的接口文檔實(shí)現(xiàn)注冊(cè)、取消注冊(cè)、更新、獲得所有InstanceInfo信息等操作,文檔如下:

操作 接口地址 描述
注冊(cè)服務(wù) POST /eureka/v2/apps/appID Input: JSON/XML payload HTTP Code: 204 on success
取消服務(wù)注冊(cè) DELETE /eureka/v2/apps/appID/instanceID HTTP Code: 200 on success
發(fā)送一個(gè)服務(wù)的心跳 PUT /eureka/v2/apps/appID/instanceID HTTP Code: 200 on success 404 if instanceID doesn’t exist
查詢所有服務(wù)信息 GET /eureka/v2/apps HTTP Code: 200 on success Output: JSON/XML
查詢所有服務(wù)的服務(wù)id GET /eureka/v2/apps/appID HTTP Code: 200 on success Output: JSON/XML
根據(jù)具體服務(wù)id查詢服務(wù)信息 GET /eureka/v2/apps/appID/instanceID HTTP Code: 200 on success Output: JSON/XML
對(duì)于一個(gè)具體的實(shí)例查詢 GET /eureka/v2/instances/instanceID HTTP Code: 200 on success Output: JSON/XML
停止實(shí)力服務(wù) PUT /eureka/v2/apps/appID/instanceID/status?value=OUT_OF_SERVICE HTTP Code: 200 on success 500 on failure
把實(shí)例重新放入服務(wù)中 DELETE /eureka/v2/apps/appID/instanceID/status?value=UP (The value=UP is optional, it is used as a suggestion for the fallback status due to removal of the override) HTTP Code: 200 on success 500 on failure
更新 metadata中的數(shù)據(jù) PUT /eureka/v2/apps/appID/instanceID/metadata?key=value HTTP Code: 200 on success 500 on failure
查詢特定vipaddress地址下的所有實(shí)例 GET /eureka/v2/vips/vipAddress HTTP Code: 200 on success Output: JSON/XML 404 if the vipAddress does not exist.
查詢特定安全vipaddress下的所有實(shí)例 GET /eureka/v2/svips/svipAddress HTTP Code: 200 on success Output: JSON/XML 404 if the svipAddress does not exist.

官方文檔的鏈接在這:https://github.com/Netflix/eureka/wiki/Eureka-REST-operations/
已獲取所有InstanceInfo列表信息為例,我在postman上的請(qǐng)求結(jié)果如下:

<applications>
<versions__delta>1</versions__delta>
<apps__hashcode>UP_3_</apps__hashcode>
<application>
    <name>SERVICE-HI-APPNAME 8763</name>
    <instance>
        <instanceId>service-hi</instanceId>
        <hostName>DESKTOP-CKLCJ5F</hostName>
        <app>SERVICE-HI-APPNAME 8763</app>
        <ipAddr>192.168.1.8</ipAddr>
        <status>UP</status>
        <overriddenstatus>UNKNOWN</overriddenstatus>
        <port enabled="true">8763</port>
        <securePort enabled="false">443</securePort>
        <countryId>1</countryId>
        <dataCenterInfo class="com.netflix.appinfo.InstanceInfo$DefaultDataCenterInfo">
            <name>MyOwn</name>
        </dataCenterInfo>
        <leaseInfo>
            <renewalIntervalInSecs>30</renewalIntervalInSecs>
            <durationInSecs>90</durationInSecs>
            <registrationTimestamp>1510988885998</registrationTimestamp>
            <lastRenewalTimestamp>1511102494467</lastRenewalTimestamp>
            <evictionTimestamp>0</evictionTimestamp>
            <serviceUpTimestamp>1510988822454</serviceUpTimestamp>
        </leaseInfo>
        <metadata>
            <testkey4>testValue4</testkey4>
            <testkey3>testValue3</testkey3>
            <testkey2>testValue2</testkey2>
            <testkey1>testValue1</testkey1>
        </metadata>
        <homePageUrl>http://DESKTOP-CKLCJ5F:8763/</homePageUrl>
        <statusPageUrl>http://DESKTOP-CKLCJ5F:8763/info</statusPageUrl>
        <healthCheckUrl>http://DESKTOP-CKLCJ5F:8763/health</healthCheckUrl>
        <vipAddress>service-hi</vipAddress>
        <secureVipAddress>service-hi</secureVipAddress>
        <isCoordinatingDiscoveryServer>false</isCoordinatingDiscoveryServer>
        <lastUpdatedTimestamp>1510988885998</lastUpdatedTimestamp>
        <lastDirtyTimestamp>1510988885942</lastDirtyTimestamp>
        <actionType>ADDED</actionType>
    </instance>
</application>
<application>
    <name>SERVICE-RIBBON</name>
    <instance>
        <instanceId>DESKTOP-CKLCJ5F:service-ribbon:8764</instanceId>
        <hostName>DESKTOP-CKLCJ5F</hostName>
        <app>SERVICE-RIBBON</app>
        <ipAddr>192.168.1.8</ipAddr>
        <status>UP</status>
        <overriddenstatus>UNKNOWN</overriddenstatus>
        <port enabled="true">8764</port>
        <securePort enabled="false">443</securePort>
        <countryId>1</countryId>
        <dataCenterInfo class="com.netflix.appinfo.InstanceInfo$DefaultDataCenterInfo">
            <name>MyOwn</name>
        </dataCenterInfo>
        <leaseInfo>
            <renewalIntervalInSecs>30</renewalIntervalInSecs>
            <durationInSecs>90</durationInSecs>
            <registrationTimestamp>1510993263471</registrationTimestamp>
            <lastRenewalTimestamp>1511102494632</lastRenewalTimestamp>
            <evictionTimestamp>0</evictionTimestamp>
            <serviceUpTimestamp>1510988934957</serviceUpTimestamp>
        </leaseInfo>
        <metadata class="java.util.Collections$EmptyMap"/>
        <homePageUrl>http://DESKTOP-CKLCJ5F:8764/</homePageUrl>
        <statusPageUrl>http://DESKTOP-CKLCJ5F:8764/info</statusPageUrl>
        <healthCheckUrl>http://DESKTOP-CKLCJ5F:8764/health</healthCheckUrl>
        <vipAddress>service-ribbon</vipAddress>
        <secureVipAddress>service-ribbon</secureVipAddress>
        <isCoordinatingDiscoveryServer>false</isCoordinatingDiscoveryServer>
        <lastUpdatedTimestamp>1510993263471</lastUpdatedTimestamp>
        <lastDirtyTimestamp>1510993263422</lastDirtyTimestamp>
        <actionType>ADDED</actionType>
    </instance>
</application>
<application>
    <name>SERVICE-HI-APPNAME 8765</name>
    <instance>
        <instanceId>service-hi</instanceId>
        <hostName>DESKTOP-CKLCJ5F</hostName>
        <app>SERVICE-HI-APPNAME 8765</app>
        <ipAddr>192.168.1.8</ipAddr>
        <status>UP</status>
        <overriddenstatus>UNKNOWN</overriddenstatus>
        <port enabled="true">8765</port>
        <securePort enabled="false">443</securePort>
        <countryId>1</countryId>
        <dataCenterInfo class="com.netflix.appinfo.InstanceInfo$DefaultDataCenterInfo">
            <name>MyOwn</name>
        </dataCenterInfo>
        <leaseInfo>
            <renewalIntervalInSecs>30</renewalIntervalInSecs>
            <durationInSecs>90</durationInSecs>
            <registrationTimestamp>1511100242138</registrationTimestamp>
            <lastRenewalTimestamp>1511102492656</lastRenewalTimestamp>
            <evictionTimestamp>0</evictionTimestamp>
            <serviceUpTimestamp>1511100242138</serviceUpTimestamp>
        </leaseInfo>
        <metadata>
            <testkey4>testValue4</testkey4>
            <testkey3>testValue3</testkey3>
            <testkey2>testValue2</testkey2>
            <testkey1>testValue1</testkey1>
        </metadata>
        <homePageUrl>http://DESKTOP-CKLCJ5F:8765/</homePageUrl>
        <statusPageUrl>http://DESKTOP-CKLCJ5F:8765/info</statusPageUrl>
        <healthCheckUrl>http://DESKTOP-CKLCJ5F:8765/health</healthCheckUrl>
        <vipAddress>service-hi</vipAddress>
        <secureVipAddress>service-hi</secureVipAddress>
        <isCoordinatingDiscoveryServer>false</isCoordinatingDiscoveryServer>
        <lastUpdatedTimestamp>1511100242138</lastUpdatedTimestamp>
        <lastDirtyTimestamp>1511100242050</lastDirtyTimestamp>
        <actionType>ADDED</actionType>
    </instance>
</application>
</applications>

上面的信息更加詳細(xì),如IP地址、服務(wù)的健康狀況、instanceId、hostName等信息都可以查到,方便我們?cè)陂_發(fā)中發(fā)現(xiàn)問題。

總結(jié)

相信講到這大家對(duì)Eureka是如何管理微服務(wù)已經(jīng)有了一定的認(rèn)識(shí),管理Eureka下的服務(wù)其實(shí)是管理每個(gè)InstanceInfo,而負(fù)載均衡,錯(cuò)誤故障轉(zhuǎn)移都是基于每個(gè)InstanceInfo實(shí)現(xiàn)的。后面我會(huì)單獨(dú)對(duì)InstanceInfo做更具體的講解,歡迎大家評(píng)論。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容