简体   繁体   English

轮循选择服务器

[英]Round Robin for choosing a server

I do have access to few Servers say A, B, C, D and E. I'd like to choose data from these servers one by one in a round robin way. 我确实可以访问很少的服务器,例如A,B,C,D和E。我想以循环方式从这些服务器中一个一个地选择数据。 I am new to Java and threads, It would be a great help if you could help me with this. 我是Java和线程的新手,如果您能帮助我,这将是一个很大的帮助。

What I am trying to do is to load a map in my application, I send HTTP requests to the servers. 我要尝试的是在应用程序中加载地图,然后将HTTP请求发送到服务器。 These Servers revert response in Bitmap format, I arrange these Images (Tiles) and show it in my application, but I am doing it it sequentially. 这些服务器以位图格式还原响应,我排列了这些图像(标题)并将其显示在我的应用程序中,但是我是按顺序进行的。 Eg I request Server A first to get the tiles then Server B and so on..I would like to get the tiles in such a way that Server A downloads one Image, Server B does the other. 例如,我先请求服务器A获取磁贴,然后请求服务器B,依此类推。.我希望以这样的方式获取磁贴:服务器A下载一个映像,服务器B下载另一个映像。 If I if I'd be doing it all alone using one server without using Multithreading it would take a long time to display whole Map. 如果我要单独使用一台服务器而不使用多线程来完成全部操作,则将需要很长时间才能显示整个Map。

Create a url builder which has the base urls of each server in an array and also keeps track of which server was hit last time. 创建一个URL构建器,该构建器具有数组中每个服务器的基本URL,并跟踪上次命中哪个服务器。 Next time you need data, just return the base url of the next server. 下次需要数据时,只需返回下一个服务器的基本URL。

use modulo see example: (used String as the url) 使用模数参见示例:(使用String作为url)

public static final int MAX_SERVER = 4; public static final int MAX_SERVER = 4;

public static void main(String[] args)
{

    String urlarr[] = new String[MAX_SERVER];
    init(urlarr);

    int idx = 0;
    while(idx < 1000){
        String next = urlarr[idx++%urlarr.length];
        System.out.println(next);
    }
}

private static void init(String[] urlarr)
{
    for(int i=0 ; i<urlarr.length ; i++){
        urlarr[i] = "url("+i+")";
    }

}

using module size of array on idx make it iterates over all available indexes 0,1,2,3 in this case. 在idx上使用array的模块大小可以使它遍历所有可用索引0、1、2、3。 part of output: 部分输出:

url(0)
url(1)
url(2)
url(3)
url(0)
url(1)
url(2)
url(3)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM