JBoss 系列六十八:JBoss 7/WildFly 集羣之有狀態會話Bean集羣 - II(示例)

概述

本文分兩個部分:有狀態會話Bean集羣和有狀態會話Bean集羣示例html

有狀態會話Bean集羣

關於有狀態會話Bean集羣的基本理論參照 JBoss 7/WildFly 集羣之無狀態會話Bean集羣 - I(基本理論)

有狀態會話Bean集羣示例

有狀態會話Bean集羣示例架構以下圖:


如圖:
java

  • JBoss node1和node2組成一個集羣,node1對應IP地址10.66.218.46,node2對應IP地址10.66.218.47
  • EJB 客戶端位於另外一臺機器
咱們經過以下幾步部署運行有狀態會話Bean集羣示例。

定義StatefulSession接口

public interface StatefulSession {
	public String getServer();
	public String getName();
	public void setName(String name);
}

定義StatefulSession接口實現

@Stateful
@Remote(StatefulSession.class)
@Clustered
public class StatefulSessionBean implements StatefulSession {

	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;
	}
	
	private String name;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

}

啓動並部署有狀態會話Bean集羣應用

EJB遠程調運咱們須要建立Application User,具體執行JBOSS_HOME/bin/add_user.sh或JBOSS_HOME/bin/add_user.bat建立Application User democlient/password1!
咱們使用以下命令分別啓動兩個JBoss節點:
node

./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/sfsb

咱們能夠使用軟件安裝及資料下載中描述的方法下載,編譯生成部署包cluster-demo-sfsb.jar。使用使用4種方式部署應用到JBoss7/WildFly中描述的方法分別部署cluster-demo-sfsb.jar到兩個JBoss節點。
git

定義EJB客戶端

EJB客戶端配置文件jboss-ejb-client.properties內容以下:
github

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 StatefulSessionBeanClient {
	
	private String applicationContext = "cluster-demo-sfsb";
	private String SFSB_JNDI = "ejb:/" + applicationContext + "/StatefulSessionBean!" + StatefulSession.class.getName()  + "?stateful";		
	
	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 );
		StatefulSession sfsb = (StatefulSession)context.lookup(SFSB_JNDI);
		
		System.out.println("Sticky routes Test");
		for(int i = 0 ; i < 10 ; i ++){
			System.out.println(sfsb.getServer());
		}
		
		System.out.println("\nFail Over Test");
		for(int i = 0 ; i < 10 ; i ++) {
			System.out.println(sfsb.getName());
			sfsb.setName("sfsb-test-" + i);
			Thread.currentThread().sleep(1000 * 5);
		}
	}

	public static void main(String[] args) throws Exception {
		new StatefulSessionBeanClient().execute();
	}

}

運行StatefulSessionBeanClient輸出以下:

Sticky routes Test
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.46, jboss.node.name: node1
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.46, jboss.node.name: node1
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.46, jboss.node.name: node1
jboss.bind.address: 10.66.218.46, jboss.node.name: node1

Fail Over Test
null
sfsb-test-0
sfsb-test-1
sfsb-test-2
sfsb-test-3
sfsb-test-4
sfsb-test-5
sfsb-test-6
sfsb-test-7
sfsb-test-8

咱們看到同一個EJB Client經過相同的client stub的一系列EJB調運會進行Sticky 轉發,即全部請求都分配到節點1。當 sfsb-test-3輸出後咱們手動關閉節點1,咱們發現sfsb-test-4,sfsb-test-5...輸出,查看JBoss日誌咱們能夠發現有狀態會話Bean集羣中容錯和狀態同步機制。
相關文章
相關標籤/搜索