springcloud-04-自定义ribbon的配置方式
在dubbo项目中, zookeeper即注册中心帮我们实现了调度和负载均衡的能力, 这种方式被称为服务器端的负载均衡, springcloud中, 使用ribben实现的客户端负载均衡
什么是ribbon?
Ribbon是Netflix发布的云中间层服务开源项目,其主要功能是提供客户端侧负载均衡算法。Ribbon客户端组件提供一系列完善的配置项如连接超时,重试等。简单的说,Ribbon是一个客户端负载均衡器,我们可以在配置文件中列出Load Balancer后面所有的机器,Ribbon会自动的帮助你基于某种规则(如简单轮询,随机连接等)去连接这些机器,我们也很容易使用Ribbon实现自定义的负载均衡算法。
eureka使用ribbon的大致架构

Ribbon工作时分为两步:第一步先选择 Eureka Server, 它优先选择在同一个Zone且负载较少的Server;第二步再根据用户指定的策略,在从Server取到的服务注册列表中选择一个地址。其中Ribbon提供了多种策略,例如轮询round robin、随机Random、根据响应时间加权等。
通过代码实现:
将刚才的cosumer-movie改造成ribbon的项目
添加一个注解类, 需要放置在mainClass扫描不到的包下, 或者在mainClass中通过@COmponentScan进行过滤
这儿使用的是注解过滤, 所以首先需要一个注解:
package com.wenbronk.cosumer.ribben.annotation; /**
* 自定义注解, 排除不想被扫描到的内容
* Created by wenbronk on 2017/5/18.
*/
public @interface ExcudeAnnotation {
}
然后是一个ribbon的注解声明类:
package com.wenbronk.cosumer.ribben.config; import com.netflix.loadbalancer.IRule;
import com.netflix.loadbalancer.RandomRule;
import com.wenbronk.cosumer.ribben.annotation.ExcudeAnnotation;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; /**
* 默认规则为轮询, 这个为随机规则
* 该类必须放置在扫描不到的包下, 或者添加排除, 虽然他必须有注解
* Created by wenbronk on 2017/5/18.
*/
@Configuration
@ExcudeAnnotation
public class MyRibbonConfig { @Bean
public IRule ribbonRule() {
return new RandomRule();
} }
在mainClass中进行过滤
package com.wenbronk.cosumer.ribben; import com.wenbronk.cosumer.ribben.annotation.ExcudeAnnotation;
import com.wenbronk.cosumer.ribben.config.MyRibbonConfig;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.ribbon.RibbonClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.FilterType;
import org.springframework.web.client.RestTemplate; /**
* 通过RIbbonClient自定义ribbon客户端
* Created by root on 2017/5/18.
*/
@SpringBootApplication
@EnableEurekaClient
// 使用此规则不可放在 可扫描的路径下, 如果非要放置, 需要加自定义注解
@RibbonClient(name = "microserver-provider-user", configuration = MyRibbonConfig.class)
@ComponentScan(excludeFilters = {@ComponentScan.Filter(type = FilterType.ANNOTATION, value = {ExcudeAnnotation.class})})
public class MovieRibbenApplication { /**
* 使用LoadBalanced开启客户端负载均衡的功能
* @return
*/
@Bean
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
} public static void main(String[] args) {
SpringApplication.run(MovieRibbenApplication.class, args);
} }
验证:
通过配置端口, 将user服务启动4个实例, 并注册为2个服务, 在movie-ribbon的controller中加入如下代码
package com.wenbronk.cosumer.ribben.controller; import com.wenbronk.cosumer.ribben.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate; /**
* Created by root on 2017/5/18.
*/
@RestController
public class MovieRibbenController { @Autowired
private RestTemplate restTemplate; @Autowired
private LoadBalancerClient loadBalancerClient; @RequestMapping("/movie/{id}")
public User findById(@PathVariable Long id) {
return restTemplate.getForObject("http://microservice-provider-user/simple/" + id, User.class);
} @GetMapping("/test")
public void test() {
ServiceInstance instance = this.loadBalancerClient.choose("MICROSERVICE-PROVIDER-USER");
System.out.println("111: " + instance.getServiceId() + ": " + instance.getHost() + ": " + instance.getPort());
// System.out.println(instance.toString()); ServiceInstance instance1 = this.loadBalancerClient.choose("MICROSERVICE-PROVIDER-USER-1");
System.out.println("222: " + instance1.getServiceId() + ": " + instance1.getHost() + ": " + instance1.getPort());
// System.out.println(instance1);
// System.out.println(instance == instance1);
}
}
通过浏览器重复的发送请求, 在控制台可看到只有provide-user 使用了我们自定义的规则, user-1 仍然使用的ribbon的默认规则轮询
通过配置文件的方式, 自定义ribbonclient:
从版本1.2.0开始,Spring Cloud Netflix现在支持使用属性与Ribbon文档兼容来自定义Ribbon客户端。
配置的优先级: 配置文件 > java代码 > springcloud default
配置文件配置, 只需要在yml'中添加如下内容
users:
ribbon:
NFLoadBalancerRuleClassName: com.netflix.loadbalancer.WeightedResponseTimeRule
NFLoadBalancerRuleClassName 是规则, 除了此规则 springcloud还有
每个规则可添加的类也有很多

mainClass.java
package com.wenbronk.cosumer.ribbon.yml; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate; /**
* Created by wenbronk on 2017/5/20.
*/
@SpringBootApplication
@EnableEurekaClient
public class MoveiRibbonYmlApplicatoin { @Bean
public RestTemplate restTemplate() {
return new RestTemplate();
} public static void main(String[] args) {
SpringApplication.run(MoveiRibbonYmlApplicatoin.class, args);
}
}
MovieRibbonController
package com.wenbronk.cosumer.ribbon.yml.controller; import com.wenbronk.cosumer.ribbon.yml.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate; /**
* Created by root on 2017/5/18.
*/
@RestController
public class MovieRibbenController { @Autowired
private RestTemplate restTemplate; @Autowired
private LoadBalancerClient loadBalancerClient; @RequestMapping("/movie/{id}")
public User findById(@PathVariable Long id) {
return restTemplate.getForObject("http://microservice-provider-user/simple/" + id, User.class);
} @GetMapping("/test")
public void test() {
ServiceInstance instance = this.loadBalancerClient.choose("MICROSERVICE-PROVIDER-USER");
System.out.println("111: " + instance.getServiceId() + ": " + instance.getHost() + ": " + instance.getPort());
// System.out.println(instance.toString()); ServiceInstance instance1 = this.loadBalancerClient.choose("MICROSERVICE-PROVIDER-USER-1");
System.out.println("222: " + instance1.getServiceId() + ": " + instance1.getHost() + ": " + instance1.getPort());
// System.out.println(instance1);
// System.out.println(instance == instance1);
}
}
springcloud-04-自定义ribbon的配置方式的更多相关文章
- 0404-服务注册与发现-客户端负载均衡-两种自定义方式-Ribbon通过代码自定义配置、使用配置文件自定义Ribbon Client
一.官方文档解读 官方地址:https://cloud.spring.io/spring-cloud-static/Edgware.SR3/single/spring-cloud.html#_cust ...
- SpringCloud系列八:自定义Ribbon配置
1. 回顾 上文使用Ribbon实现了客户端侧的负载均衡.但是很多场景下,我们可能需要自定义Ribbon的配置,比如修改Ribbon的负载均衡规则. Spring Cloud允许使用Java代码或属性 ...
- 使用Java代码自定义Ribbon配置
很多场景下,需要实现不同的微服务采用不同的策略,例如修改Ribbon的负载均衡规则等.Spring Cloud允许使用Java代码自定义Ribbon的配置. 在Spring Cloud中,Ribbon ...
- spring cloud 自定义ribbon客户端
一.自定义Ribbon客户端-[方式一]配置类 1.1.自定义负载规则 增加RibbonConfiguration.java配置类 public class RibbonConfiguration { ...
- SpringCloud系列五:Ribbon 负载均衡(Ribbon 基本使用、Ribbon 负载均衡、自定义 Ribbon 配置、禁用 Eureka 实现 Ribbon 调用)
1.概念:Ribbon 负载均衡 2.具体内容 现在所有的服务已经通过了 Eureka 进行了注册,那么使用 Eureka 注册的目的是希望所有的服务都统一归属到 Eureka 之中进 行处理,但是现 ...
- 表单配置项写法,表单写成JSON数组套对象,一行是一个数组单位,一列是一个对象单位,然后再写一个公共组件读取这个配置,循环加载slot,外层载入slot的自定义部分,比如input select等,这种写法就是把组件嵌套改为配置方式
表单配置项写法,表单写成JSON数组套对象,一行是一个数组单位,一列是一个对象单位,然后再写一个公共组件读取这个配置,循环加载slot,外层载入slot的自定义部分,比如input select等,这 ...
- Springboot中以配置类方式自定义Mybatis的配置规则(如开启驼峰映射等)
什么是自定义Mybatis的配置规则? 答:即原来在mybatis配置文件中中我们配置到<settings>标签中的内容,如下第6-10行内容: 1 <?xml version=&q ...
- SpringCloud | FeignClient和Ribbon重试机制区别与联系
在spring cloud体系项目中,引入的重试机制保证了高可用的同时,也会带来一些其它的问题,如幂等操作或一些没必要的重试. 今天就来分别分析一下 FeignClient 和 Ribbon 重试机制 ...
- SpringCloud学习(4)——Ribbon负载均衡
Ribbon概述 SpringCloud Ribbon是基于Netflix Ribbon实现的一套客户端负载均衡工具. 简单的说, Ribbon是Netflix发布的开源项目, 主要功能是提供客户端软 ...
随机推荐
- 公开课 之 tony 电子时钟 (课堂笔记)
# tony 之电子时钟from PyQt5.QtWidgets import QApplication, QWidget, QLCDNumber, QDesktopWidget, QVBoxLayo ...
- Android摘抄总结
著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处.作者:葛小葛链接:http://www.zhihu.com/question/27069588/answer/38417756来源: ...
- BZOJ4221 : JOI2012 kangaroo
将袋鼠大小和口袋大小分别从小到大排序. 枚举从左往右第一只没有被放入任何口袋的袋鼠$x$,那么$x$之前的所有袋鼠.以及$x$能装入的所有口袋都应该在匹配边上. 按这只袋鼠将上下两个序列分为两部分,设 ...
- (Tcp协议)linux上netstat -atunlp后出现的数据的意思(socket状态)
https://zhidao.baidu.com/question/486077599.html 1.netstat命令的-t参数指的是 tcp的简写,意思是仅显示tcp相关选项2.示例:列出所有 t ...
- 安装 jenkins
1. 将jenkins.war包放在 tomcat 的 webapps 目录下即可 2 重启 tomcat 3. 通过浏览器访问 IP:8080/jenkins
- redis 在 php 中的应用(Connection [ 连接] 篇)
本文为我阅读了 redis参考手册 之后编写,注意 php_redis 和 redis-cli 的区别(主要是返回值类型和参数用法) 目录: Connection(连接) AUTH ECHO PING ...
- mysql 跨表更新
update hhs_goods as g INNER JOIN (select SUM(goods_number) as goods_number,goods_id from hhs_order_g ...
- 机器学习数据集,主数据集不能通过,人脸数据集介绍,从r包中获取数据集,中国河流数据集
机器学习数据集,主数据集不能通过,人脸数据集介绍,从r包中获取数据集,中国河流数据集 选自Microsoft www.tz365.Cn 作者:Lee Scott 机器之心编译 参与:李亚洲.吴攀. ...
- wifipineapple获取用户上网信息
ssh连接到wifipineapple: 输入连接信息:ssh root@172.16.42.1 输入密码:pineapplesareyummy 安装依赖基本环境: opkg update opkg ...
- 微软BI 之SSRS 系列 - 基于时间段参数的 MDX 查询以及时间日历 Date Picker 的时间类型参数化
今天在天善问答里看到一个问题,如果我没有理解错的话,它应该是指比如在一个报表中选取一个时间段,然后求出这个时间段的某个 Measure 的 SUM 和.并且同时求出这两个时间点对应的上一年的时间点之间 ...
