我必须在 WAN 上创建一个具有单个接收器和多个发送器的集群。问题是,在每个成员中使用相同的集群名称调用后JChannel.connect()
,没有创建公共视图的公共集群,但每个组件都看到自己的。结果是,当向接收者发送消息时,无法确定后者的物理地址,并且所有消息都被掺杂。
发件人 :
package mydemo;
import org.jgroups.Address;
import org.jgroups.JChannel;
import org.jgroups.ReceiverAdapter;
public class Sender {
JChannel channel;
Address receiver;
String group;
String props;
String name;
public Sender(Address rcv, String group, String props, String name) {
receiver = rcv;
this.group = group;
this.props = props;
this.name = name;
try {
channel = new JChannel(props);
//channel.setName(group);
channel.connect(group);
System.out.println("View size(sender): " + channel.getView().getMembers().size());
System.out.println("Sender address: " + channel.getView().getMembers().get(0));
} catch (Exception e) {
e.printStackTrace();
}
}
public void sendMessage() throws Exception {
channel.send(receiver,"Hello, I am "+name);
}
public void setReceiverAdapter(ReceiverAdapter adapter) {
channel.setReceiver(adapter);
}
}
收件人:
package mydemo;
import java.util.LinkedList;
import java.util.List;
import org.jgroups.JChannel;
import org.jgroups.Message;
import org.jgroups.ReceiverAdapter;
import org.jgroups.View;
public class ClusterTest extends ReceiverAdapter {
JChannel channel;
List<Sender> senders;
public ClusterTest(int num, String props, String name)
throws Exception {
senders = new LinkedList<Sender>();
channel = new JChannel(props);
channel.setReceiver(this);
//channel.setName(name);
channel.connect(name);
System.out.println("Rcv address: " + channel.getView().getMembers().get(0));
for (int i = 0; i < num; i++) {
/* senders.add(new Sender(channel.getView().getMembers().get(0), name,
props, "Sender" + i));*/
senders.add(new Sender(channel.getAddress(), name,
props, "Sender" + i));
senders.get(i).setReceiverAdapter(this);
System.out.println("View size(rcv): " + channel.getView().getMembers().size());
}
}
public void receive(Message msg) {
System.out.println("received message " + msg);
}
public void viewAccepted(View view) {
System.out.println("received view " + view.toString());
}
public void start() throws Exception {
for (Sender sender : senders) {
Thread.sleep(3000);
sender.sendMessage();
}
channel.close();
}
public static void main(String[] args) throws Exception {
new ClusterTest(3, "d:/sth/MyDemo/src/conf/tcp.xml", "TestGroup").start();
}
}
jgroups 提供的 tcp.xml 用于 TCPPING 的小改动:
<TCPPING timeout="3000"
initial_hosts="${jgroups.tcpping.initial_hosts:localhost[7800],localhost[7801],localhost[7802],
localhost[7803],localhost[7804],localhost[7805],localhost[7806],localhost[7807],localhost[7808],
localhost[7809],localhost[7810],localhost[7811],localhost[7812],localhost[7813],localhost[7814],
localhost[7815],localhost[7816]}"
port_range="10"
num_initial_members="11"/>
输出:
GMS: address=myhost-42336, cluster=TestGroup, physical address=192.168.3.1:7806
received view [myhost-42336|0] [myhost-42336]
Rcv address: myhost-42336
GMS: address=myhost-48774, cluster=TestGroup, physical address=192.168.3.1:7807
View size(sender): 1
Sender address: myhost-48774
View size(rcv): 1
GMS: address=myhost-38597, cluster=TestGroup, physical address=192.168.3.1:7808
View size(sender): 1
Sender address: myhost-38597
View size(rcv): 1
GMS: address=myhost-55548, cluster=TestGroup, physical address=192.168.3.1:7809
View size(sender): 1
Sender address: myhost-55548
View size(rcv): 1
WARNING: myhost-48774: no physical address for myhost-42336, dropping message
WARNING: myhost-48774: no physical address for myhost-42336, dropping message
WARNING: myhost-38597: no physical address for myhost-42336, dropping message
WARNING: myhost-38597: no physical address for myhost-42336, dropping message
WARNING: myhost-48774: no physical address for myhost-42336, dropping message
...
请你帮助我好吗?也许这个概念是错误的,但由于目标是在 WAN 上工作,我必须使用单播和 TCP。