我想有我的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改變到一個不同的數字。
有沒有人有線索我做錯了什麼?
也許還嘗試使用IPv4:'-Djava.net.preferIPv4Stack = true' –
它的工作!我把我的主機名(CAPYBARA-PC)都放在bind_addr和initial_hosts中。 –
LordCapybara