Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/391.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 带TCping的JGroup(静态对等列表)_Java_Jgroups - Fatal编程技术网

Java 带TCping的JGroup(静态对等列表)

Java 带TCping的JGroup(静态对等列表),java,jgroups,Java,Jgroups,我想让我的Java应用程序的几个实例相互协作。我尝试使用JGroups来实现这一点,但没有成功。我没有使用JBoss,只是简单的JGroups库4.0.3 我试着用两个相互连接的实例制作一个“最小工作示例”。我在一台机器上进行了测试 有一次,我运行了两个我的应用程序实例,我期望它们同时打印自己和对方的地址。我得到的是他们只是打印了自己的地址。似乎他们还没有设法联系到对方 下面是我的Java代码: import org.jgroups.Address; import org.jgroups.JCh

我想让我的Java应用程序的几个实例相互协作。我尝试使用JGroups来实现这一点,但没有成功。我没有使用JBoss,只是简单的JGroups库4.0.3

我试着用两个相互连接的实例制作一个“最小工作示例”。我在一台机器上进行了测试

有一次,我运行了两个我的应用程序实例,我期望它们同时打印自己和对方的地址。我得到的是他们只是打印了自己的地址。似乎他们还没有设法联系到对方

下面是我的Java代码:

import org.jgroups.Address;
import org.jgroups.JChannel;
import org.jgroups.View;

public class Main {
    public static void main(String[] args) throws Exception {
        JChannel ch = new JChannel("./test.xml");
        ch.connect("TestCluster");

        final int SLEEP_TIME_IN_MILLIS = 1000;
        while (true) {
            checkNeighbors(ch);
            try {
                Thread.sleep(SLEEP_TIME_IN_MILLIS);
            } catch (InterruptedException e) {
                // Ignored
            }
        }
    }

    private static void checkNeighbors(JChannel channel) {
        View view = channel.getView();
        List<Address> addresses = view.getMembers();
        System.out.println("NEIGHBORS:");
        for (Address address : addresses) {
            System.out.println("    " + address);
        }
    }
}
每次我运行5951时,都会更改为不同的数字


有人知道我做错了什么吗?

你需要在
TCP
中定义
bind_addr
,并在
tcping
中列出所有主机,例如,如果你有两个进程在
192.168.1.5::7950
192.168.1.6::7900
上运行,那么第一个成员的配置需要包括:

<TCP bind_addr="192.168.1.5" bind_port="7950">
<TCPPING initial_hosts="192.168.1.5[7950],192.168.1.6[7900]"/>

还可以查看JGroups手册中绑定地址的符号名称,例如,
localhost
site\u local
匹配地址:xxx

,也可以尝试使用IPv4:
-Djava.net.preferIPv4Stack=true
它工作了!我把我的主机名(CAPYBARA-PC)放在bind_addr和initial_hosts中。
-------------------------------------------------------------------
GMS: address=CAPYBARA-PC-5951, cluster=TestCluster, physical address=fd5c:92d6:98b5:0:c5ee:90c9:e7b0:ceb2:7950
-------------------------------------------------------------------
NEIGHBORS:
    CAPYBARA-PC-5951
NEIGHBORS:
    CAPYBARA-PC-5951
NEIGHBORS:
    CAPYBARA-PC-5951
NEIGHBORS:
    CAPYBARA-PC-5951
NEIGHBORS:
    CAPYBARA-PC-5951
<TCP bind_addr="192.168.1.5" bind_port="7950">
<TCPPING initial_hosts="192.168.1.5[7950],192.168.1.6[7900]"/>
<TCP bind_addr="192.168.1.6" bind_port="7900">
<TCPPING initial_hosts="192.168.1.5[7950],192.168.1.6[7900]"/>
<TCP bind_addr="${my.bind_addr:192.168.1.6}" bind_port="${my.bind_port:7900}"> and start a process with `-Dmy.bind_addr=1.2.3.4` and `-Dmy.bind_port=12345`.