JBoss 系列六十六:JBoss 7/WildFly 集群之无状态会话Bean集群 - II(示例)

概述

本文分两个部分:无状态会话Bean集群和无状态会话Bean集群示例

无状态会话Bean集群

关于无状态会话Bean集群的基本理论参照JBoss 7/WildFly 集群之无状态会话Bean集群 - I(基本理论)

无状态会话Bean集群示例

无状态会话Bean集群示例架构如下图:


如图所示:

  • JBoss node1和node2组成一个集群,node1对应IP地址10.66.218.46,node2对应IP地址10.66.218.47
  • EJB 客户端位于另一台机器
我们通过如下几步部署运行无状态会话Bean集群示例。

定义StatelessSession接口

public interface StatelessSession {
	public String getServer();
}

定义StatelessSession接口实现

扫描二维码关注公众号,回复: 6464177 查看本文章

@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;
	}

}

启动JBoss并部署无状态会话Bean集群应用

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

无状态会话Bean集群应用代码位于: https://github.com/kylinsoong/cluster/tree/master/demo/slsb我们可以使用 软件安装及资料下载 中描述的方法下载,编译生成部署包cluster-demo-slsb.jar。使用使用4种方式部署应用到JBoss7/WildFly中描述的方法分别部署cluster-demo-slsb.jar到两个JBoss节点。

定义EJB客户端

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!

EJB客户端调运类如下:

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();
	}
}

运行StatelessSessionBeanClient输出如下:

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

我们看到同一个EJB Client通过相同的client stub的一系列EJB调运会进行负载均衡,即请求在集群中两个节点中切换。

转载于:https://my.oschina.net/iwuyang/blog/197153

猜你喜欢

转载自blog.csdn.net/weixin_33795833/article/details/91897377
今日推荐