【问题标题】:Invoking remote ejb in a 2-node wildfly cluster在 2 节点 Wildfly 集群中调用远程 ejb
【发布时间】:2016-05-06 06:59:41
【问题描述】:

我正在尝试在具有节点 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)

请推荐

【问题讨论】:

  • 可能你的集群只有一个 JNDI 服务。
  • 加布里埃尔,我不明白。如何确保集群中的每个节点都有单独的 JNDI 服务?
  • 您能否在执行 testBean(list>) 之前检查您的列表节点并确保它们在创建时不同。
  • 日志的哪一部分来自客户端和节点?代码说: log.debug("node:"+node+"slsbejbobject:"+slsbRemote.hashCode());您帖子的日志部分说: node: "node1", binddbejb object: 1276422461 似乎它们是不同的版本。

标签: java jakarta-ee remote-access ejb-3.1 wildfly-9


【解决方案1】:

为了使用集群的 EJB Wild fly,需要配置集群,据我所知:

  1. Wildfly 提供有状态 EJB 集群。
  2. Wild fly 文档提供了集群故障转移方案的示例。 (客户端尝试联系服务器 #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,并且应该由集群来决定哪个实例为客户端提供服务。

【讨论】:

  • 这可能会让人们感到困惑:“集群 EJB 仅提供给有状态的 ejb。”。 Wildfly 中的一个集群工作,用于提供故障转移和负载平衡的无状态 EJB。
  • 我更新了文字。你是对的,感谢你的观察,我改进了措辞。
猜你喜欢
  • 2014-08-18
  • 1970-01-01
  • 2015-04-20
  • 2020-09-16
  • 1970-01-01
  • 2014-08-22
  • 2015-08-06
  • 2014-01-16
  • 1970-01-01
相关资源
最近更新 更多