SpringCloud:Ribbon与Feign
127.0.0.1 eureka1.com127.0.0.1 eureka2.com
spring:application:name: spring-cloud-learn-eurekaserver:port: 8761eureka:instance:hostname: eureka1.comclient:#表示是否将自己注册到Eureka Server,默认为true。registerWithEureka: false#表示是否从Eureka Server获取注册信息,默认为true。fetchRegistry: falseserviceUrl:defaultZone: http://eureka2.com:8762/eureka/
  spring:application:name: spring-cloud-learn-eurekaserver:port: 8762eureka:instance:hostname: eureka2.comclient:#表示是否将自己注册到Eureka Server,默认为true。registerWithEureka: false#表示是否从Eureka Server获取注册信息,默认为true。fetchRegistry: falseserviceUrl:defaultZone: http://eureka1.com:8761/eureka/
  spring:application:name: spring-cloud-learn-provider-deptserver:port: 8763eureka:client:serviceUrl:defaultZone: http://eureka1.com:8761/eureka/,http://eureka2.com:8762/eureka/
- 
    
一致性(Consistency,C):在分布式系统中的所有数据备份,在同一时刻是否同样的值。(等同于所有节点访问同一份最新的数据副本)。  - 
    
可用性(Availability,A):在一个分布式系统的集群中一部分节点故障后,该集群是否还能够正常响应客户端的读写请求。(对数据更新具备高可用性)。  - 
    
分区容错性(Partition tolerance,P):大多数的分布式系统都分布在多个子网络中,而每个子网络就叫做一个区(partition)。分区容错的意思是,区间通信可能失败。在一个分布式系统中一般分区容错是无法避免的,因此可以认为 CAP 中的 P 总是成立的。CAP 理论告诉我们,在 C 和 A 之间是无法同时做到。  
简单的说,Ribbon是Netflix发布的开源项目,主要功能是提供客户端的软件负载均衡算法,将Netflix的中间层服务连接在一起。Ribbon客户端组件提供一系列完善的配置项如连接超时,重试等。简单的说,就是在配置文件中列出Load Balancer(简称LB)后面所有的机器,Ribbon会自动的帮助你基于某种规则(如简单轮询,随机连接等)去连接这些机器。我们也很容易使用Ribbon实现自定义的负载均衡算法。
Ribbon的配置在上一篇中已经给出,实现也是非常的简单,主要看几种负载均衡算法:
| 策略名 | 策略描述 | 实现说明 | 
|---|---|---|
| BestAvailableRule | 选择一个最小的并发请求的server | 逐个考察Server,如果Server被tripped了,则忽略,在选择其中ActiveRequestsCount最小的server | 
| AvailabilityFilteringRule | 过滤掉那些因为一直连接失败的被标记为circuit tripped的后端server,并过滤掉那些高并发的的后端server(active connections 超过配置的阈值) | 使用一个AvailabilityPredicate来包含过滤server的逻辑,其实就就是检查status里记录的各个server的运行状态 | 
| WeightedResponseTimeRule | 根据相应时间分配一个weight,相应时间越长,weight越小,被选中的可能性越低。 | 一个后台线程定期的从status里面读取评价响应时间,为每个server计算一个weight。Weight的计算也比较简单responsetime 减去每个server自己平均的responsetime是server的权重。当刚开始运行,没有形成statas时,使用roubine策略选择server。 | 
| RetryRule | 对选定的负载均衡策略机上重试机制。 | 在一个配置时间段内当选择server不成功,则一直尝试使用subRule的方式选择一个可用的server | 
| RoundRobinRule | roundRobin方式轮询选择server | 轮询index,选择index对应位置的server | 
| RandomRule | 随机选择一个server | 在index上随机,选择index对应位置的server | 
| ZoneAvoidanceRule | 复合判断server所在区域的性能和server的可用性选择server | 使用ZoneAvoidancePredicate和AvailabilityPredicate来判断是否选择某个server,前一个判断判定一个zone的运行性能是否可用,剔除不可用的zone(的所有server),AvailabilityPredicate用于过滤掉连接数过多的Server。 | 
当想要修改负载均衡的策略时,直接返回IRule实现即可,例:
@Beanpublic IRule myRule(){return new RandomRule();}
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>com.yuanqinnan</groupId><artifactId>spring-cloud-learn-parent</artifactId><version>1.0.0-SNAPSHOT</version></parent><artifactId>spring-cloud-learn-consumer-dept-feign</artifactId><packaging>jar</packaging><dependencies><!-- Spring Boot Begin --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-tomcat</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><!-- Spring Boot End --><!-- Spring Cloud Begin --><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-openfeign</artifactId></dependency><!-- Spring Cloud End --></dependencies></project>
@SpringBootApplication@EnableDiscoveryClient@EnableFeignClientspublic class ConsumerDeptFeignApplication {public static void main(String[] args) {SpringApplication.run(ConsumerDeptFeignApplication.class, args);}}
spring:application:name: spring-cloud-learn-consumer-dept-feignserver:port: 8765eureka:client:serviceUrl:defaultZone: http://localhost:8761/eureka/
@FeignClient(value = "spring-cloud-learn-provider-dept")public interface DeptService {@RequestMapping(value = "hi", method = RequestMethod.GET)String sayHi(@RequestParam(value = "message") String message);}
@RestControllerpublic class DeptController {@Autowiredprivate DeptService deptService;@RequestMapping(value = "hi", method = RequestMethod.GET)public String sayHi(@RequestParam String message) {return deptService.sayHi(message);}}
  
剩下的就不会给大家一展出来了,以上资料按照一下操作即可获得
——将文章进行转发和评论,关注公众号【Java烤猪皮】,关注后继续后台回复领取口令“ 666 ”即可免费领文章取中所提供的资料。
       腾讯、阿里、滴滴后台试题汇集总结 — (含答案)
面试:史上最全多线程序面试题!
最新阿里内推Java后端试题
JVM难学?那是因为你没有真正看完整这篇文章
       关注作者微信公众号 — 《JAVA烤猪皮》
了解了更多java后端架构知识以及最新面试宝典
看完本文记得给作者点赞+在看哦~~~大家的支持,是作者来源不断出文的动力~
评论
