当前位置: 移动技术网 > IT编程>开发语言>Java > Eureka之简单理解

Eureka之简单理解

2018年08月31日  | 移动技术网IT编程  | 我要评论

瑞士滑雪胜地雪崩,吴尊快乐大本营,律政男神解读网贷新规

netflix eureka是spring cloud体系下构建微服务的核心,应用层面的路由、负载均衡、重试、熔断等等功能,全都是以eureka方式作为默认支持的。

官方文档和网上一些学习资料对eureka的分析也比较全面透彻,本文主旨是简化对eureka的理解。

1. 服务发现的本质

eureka是spring cloud体系微服务下的注册发现组件,我们首先得理解服务发现的本质,ngnix官方有篇讲的很好:

let’s imagine that you are writing some code that invokes a service that has a rest api or thrift api.
in order to make a request, your code needs to know the network location (ip address and port) of a
service instance.
service instances have dynamically assigned network locations. moreover, the set of service instances
changes dynamically because of autoscaling, failures, and upgrades. consequently, your client code
needs to use a more elaborate service discovery mechanism.

你在代码中访问微服务的时候,需要知道目标服务的ip:port,但是当目标服务频繁的变更宿主机、弹性伸缩、升级时,你如何管理ip:port?

服务发现通常是用一个标识来代表一个微服务,管理该微服务的所有实例及对应的ip:port相关信息。

2. eureka的本质

在eureka中,这个标识为serviceid,在依赖eureka做服务发现的微服务中,访问目标服务的rest接口时,使用http://www.lhsxpumps.com/_serviceid/root/context/target/path这样的url,eureka client会自动为你负载到目标服务的实例上。围绕这个工作原理,简单理解eureka本质:

  • resttemplate的切面,将访问url中的serviceid解析为相应的ip:port;
  • 为了获取serviceid与ip:port的映射关系:要求微服务注册register的时候将这些信息带上;
  • 为了使各client端能够解析serviceid,因此需要定期任务执行fetchregister,拉取和更新server端注册的所有微服务信息;
  • 为了保证分发到各client端的注册信息可用,server端要求各微服务在定时任务中发出心跳
  • 为了使server端能够剔除长时间未发送心跳的失活实例,需要定时任务eviction去做剔除
  • 为了保证server端的高可用,多个eureka server之间做replication保证信息同步。

再简单归结一句话:

eureka的本质是resttemplate的切面,将url中的serviceid按照一定的策略替换为目标服务的ip:port;

为了保证serviceid与ip:port的映射关系正确,做了大量的定时任务和消息通知机制,来同步eureka server、eureka client的缓存。

 eureka中默认的替换策略--即负载策略是round ribbon。

下面来个官方的通信架构压压惊。

3.研读springcloud各组件代码的建议

spring cloud各组件的代码架构师比较统一的,初学者最快捷的研读方法是从spring.factories文件入手,如eureka-client.jar中的该文件:

 1 org.springframework.boot.autoconfigure.enableautoconfiguration=\
 2 org.springframework.cloud.netflix.eureka.config.eurekaclientconfigserverautoconfiguration,\
 3 org.springframework.cloud.netflix.eureka.config.eurekadiscoveryclientconfigserviceautoconfiguration,\
 4 org.springframework.cloud.netflix.eureka.eurekaclientautoconfiguration,\
 5 org.springframework.cloud.netflix.ribbon.eureka.ribboneurekaautoconfiguration
 6 
 7 org.springframework.cloud.bootstrap.bootstrapconfiguration=\
 8 org.springframework.cloud.netflix.eureka.config.eurekadiscoveryclientconfigservicebootstrapconfiguration
 9 
10 org.springframework.cloud.client.discovery.enablediscoveryclient=\
11 org.springframework.cloud.netflix.eureka.eurekadiscoveryclientconfiguration

以这些autoconfiguration类为入口,分析内部定义的bean,是一个很好的开始。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网