瑞士滑雪胜地雪崩,吴尊快乐大本营,律政男神解读网贷新规
netflix eureka是spring cloud体系下构建微服务的核心,应用层面的路由、负载均衡、重试、熔断等等功能,全都是以eureka方式作为默认支持的。
官方文档和网上一些学习资料对eureka的分析也比较全面透彻,本文主旨是简化对eureka的理解。
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相关信息。
在eureka中,这个标识为serviceid,在依赖eureka做服务发现的微服务中,访问目标服务的rest接口时,使用http://www.lhsxpumps.com/_serviceid/root/context/target/path这样的url,eureka client会自动为你负载到目标服务的实例上。围绕这个工作原理,简单理解eureka本质:
再简单归结一句话:
eureka的本质是resttemplate的切面,将url中的serviceid按照一定的策略替换为目标服务的ip:port;
为了保证serviceid与ip:port的映射关系正确,做了大量的定时任务和消息通知机制,来同步eureka server、eureka client的缓存。
eureka中默认的替换策略--即负载策略是round ribbon。
下面来个官方的通信架构压压惊。
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,是一个很好的开始。
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复
浅析我对 String、StringBuilder、StringBuffer 的理解
使用IDEA搭建SSM框架的详细教程(spring + springMVC +MyBatis)
Springboot整合freemarker 404问题解决方案
引入mybatis-plus报 Invalid bound statement错误问题的解决方法
网友评论