本文分两个部分:无状态会话Bean集群和无状态会话Bean集群示例
关于无状态会话Bean集群的基本理论参照JBoss 7/WildFly 集群之无状态会话Bean集群 - I(基本理论)。
无状态会话Bean集群示例架构如下图:
如图所示:
public interface StatelessSession { public String getServer(); }
@Stateless @Remote(StatelessSession.class) @Clustered public class StatelessSessionBean implements StatelessSession { public String getServer() { StringBuffer sb = new StringBuffer(); String ip = System.getProperty("jboss.bind.address"); if(null != ip) { sb.append("jboss.bind.address: " + ip); sb.append(", jboss.node.name: "); } String jbossNodeName = System.getProperty("jboss.node.name"); if(null != jbossNodeName) { sb.append(jbossNodeName); } String result = sb.toString(); System.out.println(result); return result; } }
EJB远程调运我们需要创建Application User,具体执行JBOSS_HOME/bin/add_user.sh或JBOSS_HOME/bin/add_user.bat创建Application User democlient/password1!。
我们使用如下命令分别启动两个JBoss节点:
./standalone.sh -c standalone-ha.xml -b 10.66.218.46 -bmanagement=10.66.218.46 -u 239.255.100.100 -Djboss.node.name=node1 ./standalone.sh -c standalone-ha.xml -b 10.66.218.47 -bmanagement=10.66.218.47 -u 239.255.100.100 -Djboss.node.name=node2
EJB客户端配置文件jboss-ejb-client.properties内容如下:
endpoint.name=client-endpoint remote.connectionprovider.create.options.org.xnio.Options.SSL_ENABLED=false remote.connections=node1,node2 remote.connection.node1.host=10.66.218.46 remote.connection.node1.port=4447 remote.connection.node1.connect.timeout=500 remote.connection.node1.connect.options.org.xnio.Options.SASL_POLICY_NOANONYMOUS=false remote.connection.node1.username=democlient remote.connection.node1.password=password1! remote.connection.node2.host=10.66.218.47 remote.connection.node2.port=4447 remote.connection.node2.connect.timeout=500 remote.connection.node2.connect.options.org.xnio.Options.SASL_POLICY_NOANONYMOUS=false remote.connection.node2.username=democlient remote.connection.node2.password=password1! remote.clusters=ejb remote.cluster.ejb.connect.options.org.xnio.Options.SASL_POLICY_NOANONYMOUS=false remote.cluster.ejb.username=democlient remote.cluster.ejb.password=password1!
public class StatelessSessionBeanClient { private String applicationContext = "cluster-demo-slsb"; private String SLSB_JNDI = "ejb:/" + applicationContext + "/StatelessSessionBean!" + StatelessSession.class.getName() ; private void execute() throws Exception { Hashtable<String, String> jndiProps = new Hashtable<String, String>(); jndiProps.put( Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming" ); Context context = new InitialContext( jndiProps ); StatelessSession slsb = (StatelessSession)context.lookup(SLSB_JNDI); for (int i = 0; i < 10; i++){ System.out.println(slsb.getServer()); } } public static void main(String[] args) throws Exception { new StatelessSessionBeanClient().execute(); } }
jboss.bind.address: 10.66.218.47, jboss.node.name: node2 jboss.bind.address: 10.66.218.46, jboss.node.name: node1 jboss.bind.address: 10.66.218.46, jboss.node.name: node1 jboss.bind.address: 10.66.218.47, jboss.node.name: node2 jboss.bind.address: 10.66.218.46, jboss.node.name: node1 jboss.bind.address: 10.66.218.46, jboss.node.name: node1 jboss.bind.address: 10.66.218.47, jboss.node.name: node2 jboss.bind.address: 10.66.218.46, jboss.node.name: node1 jboss.bind.address: 10.66.218.47, jboss.node.name: node2 jboss.bind.address: 10.66.218.47, jboss.node.name: node2