带有TCPPING的JGroups(静态对等列表)

时间:2017-06-13 13:05:54

标签: java jgroups

我希望我的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);
        }
    }
}

第一个过程出现'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更改为其他数字。

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

1 个答案:

答案 0 :(得分:1)

您需要在bind_addr中定义TCP并列出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