JGroups 与 TCPPING(静态对等列表)

JGroups with TCPPING (static peer list)

我想让 Java 应用程序的几个实例相互协作。我尝试为此使用 JGroups,但没有成功。我没有使用 JBoss,只是普通的 JGroups 库 4.0.3.

我试图用两个相互连接的实例制作一个 'minimal working example'。我按照单机描述的进行测试。

一旦我 运行 我的应用程序的两个实例,我期望的是它们会打印自己的地址和彼此的地址。我得到的是他们只是打印了他们自己的地址。他们似乎没有成功连接到彼此。

这是我的 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);
        }
    }
}

第一个过程'test.xml'来了:

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns="urn:org:jgroups"
        xsi:schemaLocation="urn:org:jgroups http://www.jgroups.org/schema/jgroups.xsd">
    <TCP bind_port="7950"
         recv_buf_size="${tcp.recv_buf_size:130k}"
         send_buf_size="${tcp.send_buf_size:130k}"
         max_bundle_size="64K"
         sock_conn_timeout="300"

         thread_pool.min_threads="0"
         thread_pool.max_threads="20"
         thread_pool.keep_alive_time="30000"/>

    <TCPPING async_discovery="true"
             initial_hosts="${jgroups.tcpping.initial_hosts:localhost[7900],localhost[7950]}"
             port_range="2"/>
    <MERGE3  min_interval="10000"
             max_interval="30000"/>
    <FD_SOCK/>
    <FD timeout="3000" max_tries="3" />
    <VERIFY_SUSPECT timeout="1500"  />
    <BARRIER />
    <pbcast.NAKACK2 use_mcast_xmit="false"
                   discard_delivered_msgs="true"/>
    <UNICAST3 />
    <pbcast.STABLE desired_avg_gossip="50000"
                   max_bytes="4M"/>
    <pbcast.GMS print_local_addr="true" join_timeout="2000"
                view_bundling="true"/>
    <MFC max_credits="2M"
         min_threshold="0.4"/>
    <FRAG2 frag_size="60K"  />
    <!--RSVP resend_interval="2000" timeout="10000"/-->
    <pbcast.STATE_TRANSFER/>
</config>

对于第二个进程,我只是将绑定端口更改为 7900。

我的输出只是每个进程打印自己的地址,如下所示:

-------------------------------------------------------------------
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

每次我运行5951都换成不同的号码

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

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

<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]"/>

如您所见,TCPPING列出了所有成员的绑定地址及其端口。

您可以通过系统属性使用 相同的 配置,例如

<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`.

同时查看 JGroups 手册以了解绑定地址的符号名称,例如localhostsite_localmatch-address:xxx.