在一个2节点的Wildfly集群中调用远程EJB

8

我正在尝试在群集的每个节点上调用远程ejb,其中节点是node1和node2,但我总是得到node1。在这两个节点上部署了EJB和客户端代码的EAR文件。应用程序在Wildfly 9应用服务器上运行。从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)

请提出建议。

可能你的集群只有一个JNDI服务。 - Gabriel Aramburu
Gabriel,我不明白。如何确保集群中的每个节点都有单独的JNDI服务? - Sumanth
在执行testBean(list<?>)之前,您能否检查一下列表节点并确保它们在创建时是不同的? - user2024778
日志的哪一部分来自客户端和节点? 代码如下: log.debug("node:"+node+"slsbejbobject:"+slsbRemote.hashCode()); 你发布的日志部分如下: node: "node1", binddbejb object: 1276422461看起来它们是不同的版本。 - Spyros K
1个回答

2

为了使用集群EJB,需要对Wildfly进行集群配置,据我所知:

  1. Wildfly提供有状态EJB的集群。
  2. Wildfly文档提供了一个故障转移场景的示例,用于集群。 (客户端尝试联系服务器#1上的ejb,如果不可用,则客户端联系服务器#2上的ejb。)
  3. 集群化的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,而由集群决定哪个实例为客户端提供服务。

1
这可能会让人感到困惑:“仅为有状态的EJB提供聚类”。在Wildfly中,群集可用于无状态EJB,提供故障转移和负载平衡。 - Dherik
1
我更新了文本。你是对的,我根据你的观察改进了措辞。 - Spyros K

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接