为了账号安全,请及时绑定邮箱和手机立即绑定

从REST和Instance角度理解Netfix Eureka

标签:
Spring Cloud

前言

最近项目开始使用到spring cloud作为项目的微服架构,在开发中遇
到很多问题,相信很多多人也遇到,写这篇博客也是谈谈我关于spring cloud中的eureka的一些认识,当中有误的地方还请各位留言指出,多多交流。

首先看看官方对eureka的定义

Eureka是一个基于REST(Representational State Transfer)服务,主要是用于云服务为目的的中间层服务器的负载平衡和故障转移。Eureka还附带了一个基于java的客户端组件,Eureka客户机使得交互与服务更加容易。客户端也有一个基于循环负载平衡内置的负载均衡器。当然在Netflix中,还提供很多更加能满足复杂要求的负载均衡器,它们基于交通、资源使用、错误条件等因素提供更好的弹性。

这个我根据官方的介绍翻译的原文链接在这https://github.com/Netflix/eureka/wiki/Eureka-at-a-glance/当中有翻译不对的还请留言指出。

从官方的文档中我们能得到这些重要的信息:

  1. Eureka是基于REST服务的,也就意味着它是基于http协议的

  2. 它分为服务端(java语言实现)和 客服端(可以是java也可以是非Java语言,因为Eureka提供的是 rest服务,当然官方也有提供部分其他语言的官方包)。

  3. Eureka的作用是实现为服务间的负载均衡和故障转移。

Eureka的服务端和客服端

Eureka的服务端来管理所有的微服,微服内的所有服务需要依赖Eureka客户端的依赖包,并在application上添加@EnableEurekaClient注解来实现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);
}

}

当然还有配置文件需要添加相关的配置,具体的可以参考官方文档https://github.com/Netflix/eureka/wiki/Configuring-Eureka/
,现在网上也有很多相关的博客,这里我就不详细写了。

Eureka的客服端则是通过rest服务通知Eureka的服务端该客服端的健康状况,事实是每个客服端的具体信息保存在InstanceInfo类中,通过ConcurrentHashMap<String, Map<String,Lease<InstanceInfo>>>
的双层Map结构来管理所有的的客服端。更重要的是其实在每个客服端在以心跳的模式定时通知服务端客户端的实时状态时,客户端也会获得到一份最新的服务端的InstanceInfo列表清单,没错其实每个客服端也能获得微服的所有客服端的健康状况。具体客服端获得服务信息的方法如下:

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

@Autowiredprivate DiscoveryClient discoveryClient;//获得对应服务名下所有的服务实力信息列表,服务如果有做集群,则返回的是该服务集群下的所有实力的列表List<ServiceInstance> list = discoveryClient.getInstances("service-hi");if (list != null && list.size() > 0 ) {
    String aa = list.get(0).getUri().getAuthority();
}    
//获得微服下所有的服务名List<String> servicesNames = discoveryClient.getServices();
StringBuilder sb = new StringBuilder();for (String s:servicesNames) {
    sb.append(s+"\t");
}
Eureka的REST服务

上面说过服务端和客户端是通过rest服务实现通信的,也就是说如果我现在想知道服务端下所有客服端的具体信息和健康状况只需要通过一个http请求就可以完成,答案是肯定的,当然本身eureka服务本身就提供了管理界面来查看客户端的具体信息如下:


webp

image


但是这样获得的信息并不完全,所有如果你想获得更加详细的信息,或则你想使用其他编程语言实现客服端的话可以通过Eureka服务提供的接口文档实现注册、取消注册、更新、获得所有InstanceInfo信息等操作,文档如下:

操作接口地址描述
注册服务POST /eureka/v2/apps/appIDInput: JSON/XML payload  HTTP Code: 204 on success
取消服务注册DELETE /eureka/v2/apps/appID/instanceIDHTTP Code: 200 on success
发送一个服务的心跳PUT /eureka/v2/apps/appID/instanceIDHTTP Code: 200 on success 404 if instanceID doesn’t exist
查询所有服务信息GET /eureka/v2/appsHTTP Code: 200 on success Output: JSON/XML
查询所有服务的服务idGET /eureka/v2/apps/appIDHTTP Code: 200 on success  Output: JSON/XML
根据具体服务id查询服务信息GET /eureka/v2/apps/appID/instanceIDHTTP Code: 200 on success  Output: JSON/XML
对于一个具体的实例查询GET /eureka/v2/instances/instanceIDHTTP Code: 200 on success Output: JSON/XML
停止实力服务PUT /eureka/v2/apps/appID/instanceID/status?value=OUT_OF_SERVICEHTTP Code: 200 on success 500 on failure
把实例重新放入服务中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中的数据PUT /eureka/v2/apps/appID/instanceID/metadata?key=valueHTTP Code: 200 on success 500 on failure
查询特定vipaddress地址下的所有实例GET /eureka/v2/vips/vipAddressHTTP Code: 200 on success  Output: JSON/XML  404 if the vipAddress does not exist.
查询特定安全vipaddress下的所有实例GET /eureka/v2/svips/svipAddressHTTP 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上的请求结果如下:

<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>

上面的信息更加详细,如IP地址、服务的健康状况、instanceId、hostName等信息都可以查到,方便我们在开发中发现问题。

总结

相信讲到这大家对Eureka是如何管理微服务已经有了一定的认识,管理Eureka下的服务其实是管理每个InstanceInfo,而负载均衡,错误故障转移都是基于每个InstanceInfo实现的。后面我会单独对InstanceInfo做更具体的讲解,欢迎大家评论。



作者:瘦竹竿
链接:https://www.jianshu.com/p/5aa2bef6667e


点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
JAVA开发工程师
手记
粉丝
50
获赞与收藏
175

关注作者,订阅最新文章

阅读免费教程

  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消