一、IPing机制

IPing是一个主动探测服务节点存活的机制,通过判断服务节点的当前状态,设置节点的可用状态。只有当节点为可用时候才会作为负载均衡器的选取节点。

IPing有以下几种模式:

  • DummyPing:默认返回true,即认为所有节点都可用,这也是单独使用Ribbon时的默认模式
  • NIWSDiscoveryPing:借助Eureka服务发现机制获取节点状态。节点状态是UP,则认为是可用状态
  • PingUrl:主动向服务节点发起一次http调用,对方有响应则认为节点是可用状态
  • NoOpPing:返回true
  • PingConstant:返回设置的常量值

二、IPing配置

(1)application.yaml配置

#单个服务设置
[service-name]: ribbon: NFLoadBalancerPingClassName: com.netflix.loadbalancer.DummyPing

(2)代码配置

public class MicroRibbonConfig {@Beanpublic IPing microIPing(){return new DummyPing();}
}@RibbonClient(name = "micro-service", configuration = MicroRibbonConfig.class)
public class RibbonClientConfig {}

三、IPing模式实现

(1)DummyPing

public class DummyPing extends AbstractLoadBalancerPing {public DummyPing() {}public boolean isAlive(Server server) {return true;}@Overridepublic void initWithNiwsConfig(IClientConfig clientConfig) {}
}public abstract class AbstractLoadBalancerPing implements IPing, IClientConfigAware{AbstractLoadBalancer lb;@Overridepublic boolean isAlive(Server server) {return true;}public void setLoadBalancer(AbstractLoadBalancer lb){this.lb = lb;}public AbstractLoadBalancer getLoadBalancer(){return lb;}}

(2)PingUrl

public class PingUrl implements IPing {private static final Logger LOGGER = LoggerFactory.getLogger(PingUrl.class);String pingAppendString = "";boolean isSecure = false;String expectedContent = null;/*** Send one ping only.** Well, send what you need to determine whether or not the* server is still alive.  Should return within a "reasonable"* time.*/public PingUrl() {}public PingUrl(boolean isSecure, String pingAppendString) {this.isSecure = isSecure;this.pingAppendString = (pingAppendString != null) ? pingAppendString : "";}public void setPingAppendString(String pingAppendString) {this.pingAppendString = (pingAppendString != null) ? pingAppendString : "";}public String getPingAppendString() {return pingAppendString;}public boolean isSecure() {return isSecure;}/*** Should the Secure protocol be used to Ping* @param isSecure*/public void setSecure(boolean isSecure) {this.isSecure = isSecure;}public String getExpectedContent() {return expectedContent;}/*** Is there a particular content you are hoping to see?* If so -set this here.* for e.g. the WCS server sets the content body to be 'true'* Please be advised that this content should match the actual * content exactly for this to work. Else yo may get false status.* @param expectedContent*/public void setExpectedContent(String expectedContent) {this.expectedContent = expectedContent;}public boolean isAlive(Server server) {String urlStr   = "";if (isSecure){urlStr = "https://";}else{urlStr = "http://";}urlStr += server.getId();urlStr += getPingAppendString();boolean isAlive = false;HttpClient httpClient = new DefaultHttpClient();HttpUriRequest getRequest = new HttpGet(urlStr);String content=null;try {HttpResponse response = httpClient.execute(getRequest);content = EntityUtils.toString(response.getEntity());isAlive = (response.getStatusLine().getStatusCode() == 200);if (getExpectedContent()!=null){LOGGER.debug("content:" + content);if (content == null){isAlive = false;}else{if (content.equals(getExpectedContent())){isAlive = true;}else{isAlive = false;}}}} catch (IOException e) {e.printStackTrace();}finally{// Release the connection.getRequest.abort();}return isAlive;}public static void main(String[] args){PingUrl p = new PingUrl(false,"/cs/hostRunning");p.setExpectedContent("true");Server s = new Server("ec2-75-101-231-85.compute-1.amazonaws.com", 7101);boolean isAlive = p.isAlive(s);System.out.println("isAlive:" + isAlive);}
}

(3)PingConstant

public class PingConstant implements IPing {boolean constant = true;public void setConstant(String constantStr) {constant = (constantStr != null) && (constantStr.toLowerCase().equals("true"));}public void setConstant(boolean constant) {this.constant = constant;}public boolean getConstant() {return constant;}public boolean isAlive(Server server) {return constant;}
}

(4)NoOpPing

public class NoOpPing implements IPing {@Overridepublic boolean isAlive(Server server) {return true;}}

SpringCloud Ribbon(三)之IPing机制相关推荐

  1. SpringCloud进阶-Hystrix的熔断机制+Hystrix的工作流程

    提示:本文主要对SpringCloud中的Hystrix的熔断机制和工作流程进行总结 文章目录 前言 一.简介 1.熔断是什么 二.实操 1.实现 2.测试 三.总结 1.大神结论 2.熔断类型 3. ...

  2. JDK/Dubbo/Spring 三种 SPI 机制,谁更好?

    点击关注公众号,Java干货及时送达 来源:juejin.cn/post/6950266942875779108 SPI 全称为 Service Provider Interface,是一种服务发现机 ...

  3. java同步异步调用_详解java 三种调用机制(同步、回调、异步)

    1:同步调用:一种阻塞式调用,调用方要等待对方执行完毕才返回,jsPwwCe它是一种单向调用 2:回调:一种双向调用模式,也就是说,被调用方在接口被调用时也会调用对方的接口: 3:异步调用:一种类似消 ...

  4. JDK/Dubbo/Spring 三种 SPI 机制,谁更好呢?

    JDK/Dubbo/Spring 三种 SPI 机制,谁更好? SPI 全称为 Service Provider Interface,是一种服务发现机制.SPI 的本质是将接口实现类的全限定名配置在文 ...

  5. flink 三种时间机制_Flink1.10入门:时间机制简介

    一.概述 上篇文章介绍了Window窗口机制的相关知识,这里我们介绍下Flink的另外一个核心概念"Event Time机制",本篇文章只介绍相关概念不讲实战,实战会结合Windo ...

  6. Yarn的三种资源调度机制

    在企业中并不是只有一个人来执行MapReduce程序单独使用Yarn的资源,实际开发中,会有很多人一起使用Yarn这个资源,如果每个人都提交了job,这个时候Yarn就需要进行调度去分配资源给job, ...

  7. Android Glide图片加载框架(三)缓存机制

    文章目录 一.缓存简介 二.缓存用法 内存缓存方式 磁盘缓存方式 三.缓存KEY 四.内存缓存 内存缓存流程 五.磁盘缓存 磁盘缓存流程 Android Glide图片加载框架系列文章 Android ...

  8. 【JVM】第三章 垃圾收集机制

    第三章 垃圾收集机制 文章目录 第三章 垃圾收集机制 一.垃圾标记算法 1.引用计数算法 2.根可达性分析算法 二.垃圾收集算法 1.标记 - 清除算法 2.复制算法 3.标记 - 压缩算法 4.对比 ...

  9. Application应用框架思考(三) 之[插件机制]

    Application应用框架思考(三) 之[插件机制] /// <summary> /// 单例类 /// //为子功能提供主框架信息接口 类 /// </summary> ...

最新文章

  1. android 上传pdf文件,Android 加载PDF文件
  2. python中字典和集合的区别_Python中字典和集合学习小结
  3. python3精要(27)-*与**解包
  4. update yum 到指定版本_linux yum安装指定版本mysql或php
  5. 404. Sum of Left Leaves
  6. mysql viewlog_Mysql心路历程:两个”log”引发的”血案”
  7. 支付宝五福开奖!几个亿的项目你分到几块?
  8. 2018.08.22 NOIP模拟 string(模拟)
  9. android无线投屏到电视盒子,【沙发管家】如何把电脑视频投屏到智能电视/ 电视盒子上?...
  10. 解决依赖包引入后重复问题Duplicate zip entry
  11. 数字功放芯片品牌大全
  12. Python之Pandas文本处理
  13. 键盘切换不出中文输入法的解决方法
  14. 计算机工程学院迎新生,计算机工程学院举办“青春相约,梦想同行”迎新晚会...
  15. PCL中点云关键点提取
  16. IDEA javadoc快捷键
  17. linux 下glog的安装
  18. 【Html标签学习】表单标签
  19. 你的气象图何必如此枯燥
  20. 电压空间矢量的白话准确表述

热门文章

  1. 算法设计与分析——递归与分治策略——棋盘覆盖
  2. leetcode225. 用队列实现栈
  3. TypeScript,从0到入门带你进入类型的世界
  4. [Redis6]常用数据类型_Zset有序集合
  5. Rx2.0后台开发分享
  6. 2019-03-22-算法-进化(回文链表)
  7. 2019-02-22-算法-进化
  8. Trie:hdu 4825、1251、1247、Poj 3764
  9. 【CF1189D】Add on a Tree【结论】【构造】
  10. 剑指 Offer 43. 1~n 整数中 1 出现的次数(数位dp)