zoukankan      html  css  js  c++  java
  • 7-3 Ribbon之初体验



    新建一个配置文件




    准备好的配置文件

    # Max number of retries on the same server (excluding the first try)
    jiangzh-client.ribbon.MaxAutoRetries=1
    
    # Max number of next servers to retry (excluding the first server)
    jiangzh-client.ribbon.MaxAutoRetriesNextServer=1
    
    # Whether all operations can be retried for this client
    jiangzh-client.ribbon.OkToRetryOnAllOperations=true
    
    # Interval to refresh the server list from the source
    jiangzh-client.ribbon.ServerListRefreshInterval=2000
    
    # Connect timeout used by Apache HttpClient
    jiangzh-client.ribbon.ConnectTimeout=3000
    
    # Read timeout used by Apache HttpClient
    jiangzh-client.ribbon.ReadTimeout=3000
    
    # Initial list of servers, can be changed via Archaius dynamic property at runtime
    jiangzh-client.ribbon.listOfServers=www.baidu.com:80,www.jd.com:80



    复制到新建的配置文件里


    主要的是listOfServer这个配置节点。

    创建包ribbon

    在下面创建App的类


    准备好的测试类


    先把包都导入到那里。这些在官方的演示里面其实都有。

    Ribbon官方文档有段时间没有更新了。它的官方文档和它的版本已经不一致了。 
    下面复制过来并且抛出了异常。

    读取的是自己创建的配置文件

    在再制下面一段代码,并抛出URI的异常

    executeWidthLoadBalancer需要抛出ClientException的异常。





    执行测试

    默认以这种轮询的方式在访问。

    另外一种方式


    抛出异常

    上面的代码先注释掉

    for循环代码注释掉。保留RestClient和HttpRequest





    把上面的代码放开,不注释了,一起输出看下效果



    下面是演示的动态修改serverList

    很明显动态修改服务的地址比固定的好。
    动态修改也存在一个问题,serverList从哪里来。没有任何情况比从注册中来是更好的。

    下面就看着正式环境Ribbon怎么和eureka做整合。

    package com.mooc.meetingfilm.consumer.ribbon;
    
    import com.netflix.client.ClientException;
    import com.netflix.client.ClientFactory;
    import com.netflix.client.http.HttpRequest;
    import com.netflix.client.http.HttpResponse;
    import com.netflix.config.ConfigurationManager;
    import com.netflix.loadbalancer.ZoneAwareLoadBalancer;
    import com.netflix.niws.client.http.HttpClientRequest;
    import com.netflix.niws.client.http.RestClient;
    
    import java.io.IOException;
    import java.net.URI;
    import java.net.URISyntaxException;
    
    public class App {
        public static void main(String[] args) throws IOException, ClientException, URISyntaxException, InterruptedException {
            ConfigurationManager.loadPropertiesFromResources("jiangzh.properties");  // 1
            System.err.println(ConfigurationManager.getConfigInstance().getProperty("jiangzh-client.ribbon.listOfServers"));
            //构建一个HttpClient
            RestClient client = (RestClient) ClientFactory.getNamedClient("jiangzh-client");  // 2
            HttpRequest request = HttpRequest.newBuilder().uri(new URI("/")).build(); // 3
            for (int i = 0; i < 5; i++)  {
                HttpResponse response = client.executeWithLoadBalancer(request); // 4
                System.err.println("Status code for " + response.getRequestedURI() + "  :" + response.getStatus());
            }
    
            //另外一种方式
            ZoneAwareLoadBalancer lb = (ZoneAwareLoadBalancer) client.getLoadBalancer();
            System.err.println(lb.getLoadBalancerStats());
            ConfigurationManager.getConfigInstance().setProperty(
                    "jiangzh-client.ribbon.listOfServers", "www.qq.com:80,www.taobao.com:80"); // 5
            System.err.println("changing servers ...");
            Thread.sleep(3000); // 6
            for (int i = 0; i < 5; i++)  {
                HttpResponse response = client.executeWithLoadBalancer(request);
                System.err.println("Status code for " + response.getRequestedURI() + "  : " + response.getStatus());
            }
            System.out.println(lb.getLoadBalancerStats()); // 7
    
        }
    }

    结束


  • 相关阅读:
    socket架构
    异常处理
    类的装饰器
    with&as上下文管理协议
    软件开发规范
    面向对象-描述符
    面向对象-迭代器
    面向对象编程多种特性
    体验Visual Studio 2015 之 MVC
    MVC 好记星不如烂笔头之 ---> 全局异常捕获以及ACTION捕获
  • 原文地址:https://www.cnblogs.com/wangjunwei/p/12776463.html
Copyright © 2011-2022 走看看