2017-06-13 121 views
1

我想有我的Java應用程序的一些實例互相合作。我嘗試過使用JGroups,但沒有成功。我沒有使用JBoss,只是簡單的JGroups庫4.0.3。JGroups與TCPPING(靜態對等列表)

我試圖做兩個實例互相連接的'最小工作示例'。我按照單臺機器進行測試。

一旦我運行了我的應用程序的兩個實例,我期望的是他們將打印他們自己和對方的地址。我得到的是他們只是打印他們自己的地址。看起來他們沒有設法連接到另一個。

這纔是我的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

您需要在TCP中定義bind_addr,並列出TCPPING的所有主機,例如,如果你有2個進程上192.168.1.5::7950192.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]"/> 

和第二構件的配置應包括:

<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的手冊,用於綁定地址,例如符號名localhost,site_localmatch-address:xxx

+1

也許還嘗試使用IPv4:'-Djava.net.preferIPv4Stack = true' –

+0

它的工作!我把我的主機名(CAPYBARA-PC)都放在bind_addr和initial_hosts中。 LordCapybara