我试图在具有节点 node1 和 node2 的集群的每个节点上调用远程 ejb,但我总是得到 node1。在两个节点中将 EJB 和客户端代码部署为 EAR 文件。应用程序正在 Wildfly 9 ApplicationServer 上运行。从 node1 调用客户端代码。
EJB 代码:
@Remote
public interface SLSBRemote {
public void test();
}
@Stateless(mappedName="SLSBEJB")
public class SLSBEJB implements SLSBRemote {
@Override
public void test()
{
try {
String nodeName = System.getProperty("jboss.node.name");
if(nodeName == null) {
nodeName = InetAddress.getLocalHost().getHostName();
}
log.debug("nodename: "+nodeName);
} catch (Exception e) {
log.error("Error",e);
}
}
}
客户端代码:
public class testEjb
{
//invoking this method from other class. Able to pass node address
public void testBean(List<String> nodes)
{
Properties properties = new Properties();
properties.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.naming.remote.client.InitialContextFactory");
Context context;
for (String node : nodes) {
properties.put(Context.PROVIDER_URL,"http-remoting://" + node);
try {
context = new InitialContext(properties);
SLSBRemote slsbRemote=(SLSBRemote)context.lookup(getLookupName());
log.debug("node:"+node+"slsbejbobject:"+slsbRemote.hashCode());
slsbRemote.test();
} catch (NamingException e) {
log.error("Error ", e);
}
}
}
}
在日志中,
node: "node1", binddbejb object: 1276422461
node: "node2", binddbejb object: 1276422461
nodename: "node1_address"
nodename: "node1_address" (instead of node2_address)
请推荐
最佳答案
为了使用集群 EJB,wild fly 需要配置集群,据我所知:
- Wildfly 提供有状态 EJB 的集群。
- Wild fly 文档提供了集群故障转移场景的示例。 (客户端尝试联系服务器 #1 上的 ejb,如果不可用,则客户端联系服务器 #2 上的 ejb。)
- 集群 Ejb 需要根据需要进行配置并正确注释。
import org.jboss.ejb3.annotation.Clustered; import javax.ejb.Stateful; @Stateful @Clustered public class MyStatefulBean { ... }
文档的这一页给出了示例,其中详细描述了必须完成的操作。 https://docs.jboss.org/author/display/WFLY8/EJB+Services
如果您应用此配置,则来自集群所有节点的 EJB 都可以为客户端提供服务。
但是请注意,客户端通常应该完全不知道集群。客户端需要调用 ejb,这应该由集群来决定哪个实例为客户端服务。
关于java - 在 2 节点 wildfly 集群中调用远程 ejb,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35069328/