我们讨论一下怎么使用配置文件,这是从SpringCloud Netflix1.0开始的,6.4 Customizing the Ribbon Client by Setting Propertieshttps://cloud.spring.io/spring-cloud-netflix/multi/multi_spring-cloud-ribbon.htmlStarting with version 1.2.0, Spring Cloud Netflix now supports customizing Ribbon clients by setting properties to be compatible with the Ribbon documentation.This lets you change behavior at start up time in different environments.The following list shows the supported properties>:<clientName>.ribbon.NFLoadBalancerClassName: Should implement ILoadBalancer<clientName>.ribbon.NFLoadBalancerRuleClassName: Should implement IRule<clientName>.ribbon.NFLoadBalancerPingClassName: Should implement IPing<clientName>.ribbon.NIWSServerListClassName: Should implement ServerList<clientName>.ribbon.NIWSServerListFilterClassName: Should implement ServerListFilterclientName是我要请求的serviceId,这边有一个注意点Classes defined in these properties have precedence over beans defined by using @RibbonClient(configuration=MyRibbonConfig.class) and the defaults provided by Spring Cloud Netflix.这个配置文件里面定义的类,用JAVA代码的优先级要高,同时高于Spring Cloud Netflix的默认配置,也就是优先级配置文件NO.1,java代码No2,默认的配置No3,这边给了一个demo,如果你想配置IRULE的话To set the IRule for a service name called users, you could set the following properties:负载均衡的规则的话,你可以这么玩users:ribbon:NIWSServerListClassName: com.netflix.loadbalancer.ConfigurationBasedServerListNFLoadBalancerRuleClassName: com.netflix.loadbalancer.WeightedResponseTimeRule非常简单,/*** A loadbalacing strategy that randomly distributes traffic amongst existing* servers.* * @author stonse* */
public class RandomRule extends AbstractLoadBalancerRule {根据响应时间加权,这个是加权,不是优先级,/** * Rule that use the average/percentile response times* to assign dynamic "weights" per Server which is then used in * the "Weighted Round Robin" fashion. * <p>* The basic idea for weighted round robin has been obtained from JCS* The implementation for choosing the endpoint from the list of endpoints* is as follows:Let's assume 4 endpoints:A(wt=10), B(wt=30), C(wt=40), * D(wt=20). * <p>* Using the Random API, generate a random number between 1 and10+30+40+20.* Let's assume that the above list is randomized. Based on the weights, we* have intervals as follows:* <p>* 1-----10 (A's weight)* <br>* 11----40 (A's weight + B's weight)* <br>* 41----80 (A's weight + B's weight + C's weight)* <br>* 81----100(A's weight + B's weight + C's weight + C's weight)* <p>* Here's the psuedo code for deciding where to send the request:* <p>* if (random_number between 1 &amp; 10) {send request to A;}* <br>* else if (random_number between 11 &amp; 40) {send request to B;}* <br>* else if (random_number between 41 &amp; 80) {send request to C;}* <br>* else if (random_number between 81 &amp; 100) {send request to D;}* <p>* When there is not enough statistics gathered for the servers, this rule* will fall back to use {@link RoundRobinRule}. * @author stonse*/
public class WeightedResponseTimeRule extends RoundRobinRule {其实Ribbon支持各种各样的rule,重试的Rule,* @deprecated Use {@link WeightedResponseTimeRule}* * @see WeightedResponseTimeRule* */
public class ResponseTimeWeightedRule extends RoundRobinRule {这两个其实是一样的,只不过之前的名称取得不好,现在改名了,see WeightedResponseTimeRule,我们可以看一下IRURE的实现类,响应时间加权的rule,RoundRobinRule,是继承了RoundRobinRule,还有重试的Rule,RandomRule,最高可用性的Rule,BestAvaliableRule,/*** A rule that skips servers with "tripped" circuit breaker and picks the* server with lowest concurrent requests.* <p>* This rule should typically work with {@link ServerListSubsetFilter} which puts a limit on the * servers that is visible to the rule. This ensure that it only needs to find the minimal * concurrent requests among a small number of servers. Also, each client will get a random list of * servers which avoids the problem that one server with the lowest concurrent requests is * chosen by a large number of clients and immediately gets overwhelmed.* * @author awang**/
public class BestAvailableRule extends ClientConfigEnabledRoundRobinRule {我很难做到一个节点比另一个节点响应时间长,所以我还是使用RandomRule,microservice-simple-provider-user.ribbon.NFLoadBalancerRuleClassName=
com.netflix.loadbalancer.RandomRule随机的规则去负载均衡localhost:8010/movie/1localhost:8010/test
<?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><groupId>com.learn</groupId><artifactId>microservice-consumer-movie-ribbon-properties-customizing</artifactId><version>0.0.1-SNAPSHOT</version><packaging>jar</packaging><name>microservice-simple-consumer-movie</name><description>Demo project for Spring Boot</description><parent><groupId>cn.learn</groupId><artifactId>microcloud02</artifactId><version>0.0.1</version></parent><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><java.version>1.8</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-eureka</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>
#debug=true
server.port=8010eureka.client.serviceUrl.defaultZone=http://admin:1234@10.40.8.152:8761/eurekaspring.application.name=microservice-consumer-movie-ribbon
eureka.instance.prefer-ip-address=true
eureka.instance.instance-id=${spring.application.name}:${spring.cloud.client.ipAddress}:${spring.application.instance_id:${server.port}}
eureka.client.healthcheck.enabled=true
spring.redis.host=10.40.8.152
spring.redis.password=1234
spring.redis.port=6379microservice-simple-provider-user.ribbon.NFLoadBalancerRuleClassName=com.netflix.loadbalancer.RandomRule
package com.learn.cloud.controller;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.RestController;
import org.springframework.web.client.RestTemplate;import com.learn.cloud.entity.User;@RestController
public class MovieController {@Autowiredprivate RestTemplate restTemplate;@Autowiredprivate LoadBalancerClient loadBalancerClient;@GetMapping("/movie/{id}")public User findById(@PathVariable Long id) {// http://localhost:7900/simple/// VIP virtual IP// HAProxy HeartbeatServiceInstance serviceInstance = this.loadBalancerClient.choose("microservice-simple-provider-user");System.out.println("==============" + ":" + serviceInstance.getServiceId() + ":" + serviceInstance.getHost() + ":" + serviceInstance.getPort());return this.restTemplate.getForObject("http://microservice-simple-provider-user/simple/" + id, User.class);}@GetMapping("/test")public String test() {
//    ServiceInstance serviceInstance = this.loadBalancerClient.choose("microservice-simple-provider-user");
//    System.out.println("111" + ":" + serviceInstance.getServiceId() + ":" + serviceInstance.getHost() + ":" + serviceInstance.getPort());ServiceInstance serviceInstance2 = this.loadBalancerClient.choose("microservice-simple-provider-user2");System.out.println("222" + ":" + serviceInstance2.getServiceId() + ":" + serviceInstance2.getHost() + ":" + serviceInstance2.getPort());return "1";}}
package com.learn.cloud;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.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;@SpringBootApplication
@EnableEurekaClient
public class ConsumerMovieRibbonApplication {@Bean@LoadBalancedpublic RestTemplate restTemplate() {return new RestTemplate();}public static void main(String[] args) {SpringApplication.run(ConsumerMovieRibbonApplication.class, args);}
}

Ribbon-3使用配置文件自定义Ribbon Client相关推荐

  1. spring cloud中通过配置文件自定义Ribbon负载均衡策略

    2019独角兽企业重金招聘Python工程师标准>>> spring cloud中通过配置文件自定义Ribbon负载均衡策略 博客分类: 微服务 一.Ribbon中的负载均衡策略 1 ...

  2. Spring Cloud【Finchley】-05使用配置文件自定义Ribbon Client

    文章目录 概述 示例 新建子模块 子模块加入依赖 配置文件增加配置 验证 源码 概述 Finchley.SR2版本的官方文档: https://cloud.spring.io/spring-cloud ...

  3. SpringCloud系列五:Ribbon 负载均衡(Ribbon 基本使用、Ribbon 负载均衡、自定义 Ribbon 配置、禁用 Eureka 实现 Ribbon 调用)...

    声明:本文来源于MLDN培训视频的课堂笔记,写在这里只是为了方便查阅. 1.概念:Ribbon 负载均衡 2.具体内容 现在所有的服务已经通过了 Eureka 进行了注册,那么使用 Eureka 注册 ...

  4. SharePoint 2010 自定义Ribbon实现文档批量下载为Zip文件

    在SharePoint 2010文档库中,结合单选框,在Ribbon中提供了批量处理文档的功能,比如,批量删除.批量签出.批量签入等,但是,很遗憾,没有提供批量下载,默认的只能一个个下载,当选择多个文 ...

  5. VS2010/MFC编程入门之五十三(Ribbon界面开发:为Ribbon Bar添加控件)

    前面一节中鸡啄米为大家简单介绍了如何创建Ribbon样式的应用程序框架,本节教程就来初步讲讲怎样为Ribbon Bar添加Ribbon控件. VS2010为Ribbon界面开发提供了Ribbon De ...

  6. springCloud入门学习(七):通过属性自定义Ribbon配置

    很多场景下,可能需要根据自定义的Riboon的配置,例如修改ribbon的负载均衡规则等. 配置前缀是:<clientName>.ribbon. NFLoadBalancerClassNa ...

  7. 自定义Ribbon负载均衡策略

    官方文档指出:自定义的负载均衡配置类不能放在 @componentScan 所扫描的当前包下及其子包下,否则我们自定义的这个配置类就会被所有的Ribbon客户端所共享,也就是说我们达不到特殊化定制的目 ...

  8. 白话Elasticsearch29-IK中文分词之IK分词器配置文件+自定义词库

    文章目录 概述 ik配置文件 IK自定义词库 自定义词库 Step1 : 新建自定义分词库 Step2 : 添加到ik的配置文件中 Step3 :重启es ,查看分词 自定义停用词库 Step1 : ...

  9. .Net之配置文件自定义

    前文讲获取配置文件内容的时候,是获取默认的appsettings.json配置文件的配置,下面说明下如何进行自定义配置文件获取 1. Json Provider 1.1 构建独立的IConfigura ...

最新文章

  1. eclipse环境下配置tomcat服务器
  2. Self Service Password (SSP)
  3. CentOS7 安装NFS SSH免密码登陆
  4. java 生成.sh文件,Java 生成Bat或SH文件,调用Sqlldr安插数据到Oracle
  5. js正则表达exec和match的区别(转)
  6. Redis 6.0 正式版终于发布了!除了多线程还有什么新功能?
  7. Java判断一个数是不是快乐数
  8. Photon Release 4.8.0汉化(附图教程)
  9. Arduino 硬件开发 教程收集
  10. 印度SaaS创企Whatfix获370万美元A轮融资
  11. 学习《华为基本法》(6):公司的成长
  12. declaration of 'com.afollestad.materialdialogs.R$id' appears in /data/app/xxx/split_lib_slice_3_apk
  13. 数据增强神器 SimpleCopyPaste 支持全流程
  14. 小刘的http状态码整理
  15. iexplore应用程序错误
  16. SAP 如何在选择画面中显示图片 <转载> cl_gui_docking_container
  17. 【Python】 plt.savefig保存图片时一片空白
  18. DNS主从域名解析服务器
  19. 12306自动抢票及自动识别验证码功能(一)
  20. 《软件过程管理》 第四章 软件过程需求管理

热门文章

  1. 读《构建之法》第 8、9、10 章有感
  2. 异步调用代码嵌套问题
  3. PHP文件上传类及其使用实例教程
  4. WinForm加载外部类库项目的集成开发模式
  5. 浅谈对JavaScript闭包的理解
  6. 洛谷P1561 [USACO12JAN]爬山Mountain Climbing 贪心 数学
  7. Android Bundle类,通过bundle实现在两个activity之间的通讯
  8. SQL2005实现全文检索的步骤 停止数据库的用户连接
  9. 耗尽您CPU资源的Explored病毒清除法
  10. Spring quartz 并发性研究