zoukankan      html  css  js  c++  java
  • Ribbon Ping机制

    在负载均衡器中,提供了 Ping 机制,每隔一段时间,会去 Ping 服务器,判断服务器是否存活,该工作由 com.netflix.loadbalancer.IPing 接口的实现类负责,如果单独使用 Ribbon 默认情况下不会激活 Ping 机制,默认的实现类为 DummyPing(不验证),下面实现自定义的Ping 类,代码如下:

    package org.lixue.ribbon.client;

       

    import com.netflix.loadbalancer.IPing;

    import com.netflix.loadbalancer.Server;

       

    public class MyPing implements IPing{

    public boolean isAlive(Serverserver){

    System.out.println("isAlive"+server.getHostPort());

    return true;

    }

    }

    修改 src/main/resources 目录下的 ribbon-client.properties 配置如下:

    #配置服务器列表

    MyRibbonClient.ribbon.listOfServers=localhost:8080,localhost:8002

    #配置负载均衡规则IRule的实现类

    MyRibbonClient.ribbon.NFLoadBalancerRuleClassName=com.netflix.loadbalancer.WeightedResponseTimeRule

    #配置负载均衡实现类

    MyRibbonClient.ribbon.NFLoadBalancerClassName=com.netflix.loadbalancer.ZoneAwareLoadBalancer

    #配置IPing的实现类

    MyRibbonClient.ribbon.NFLoadBalancerPingClassName=org.lixue.ribbon.client.MyPing

    #配置Ping操作的间隔

    MyRibbonClient.ribbon.NFLoadBalancerPingInterval=2

       

    启动项目可以看到输出如下:

    isAlive localhost:8080

    isAlive localhost:8002

    request localhost:8002 active true

    request localhost:8080 active true

    request localhost:8002 active true

    request localhost:8080 active true

    request localhost:8002 active true

    request localhost:8080 active true

    request localhost:8002 active true

    request localhost:8080 active true

    request localhost:8002 active true

    request localhost:8080 active true

       

  • 相关阅读:
    补习系列(4)-springboot 参数校验详解
    华为鲁勇:5G+云+AI三大核心引擎将驱动广州数字经济发展
    selenium firefox
    徐翔之后新生代“敢死队”浮出水面:八年一万倍
    ActiveMQ讯息传送机制以及ACK机制
    一元线性回归分析及java实现
    jsoup -- xml文档解析
    HDU 1020 Encoding 字符串
    PHP缓存之文件缓存
    Eclipse PHPEclipse 配置
  • 原文地址:https://www.cnblogs.com/li3807/p/8889612.html
Copyright © 2011-2022 走看看