Skip to main content

JEAP 6 - 從Server 上Remote 呼叫其他Server 上的 Clustered EJB

老公前幾天才跟我發飆說,如果我晚上回家再不理他,在家裡寫Blog的話
他就要跟我離婚,白天的工作就很忙了,
為了家庭的和睦,所以最近更新會慢一點。。。
=======================================

EJB Server 連結需要在兩邊的Server 做一些基本認證的連接點,
我們把EJB 設定為Cluster, 所以需要有HA 的機制,不會設定的,請到前面幾篇去看(連結)。
再啓動另一台JBoss 去呼叫兩個Cluster 的EJB instance.


呼叫的那個WAR (或是EAR) 請放在我們稱作Client端,被呼叫的EJB Server 我們叫接收端。

接收端
基本上接收端的機器不需要做特別的設定,僅需要建立一個Application user 即可。 所以,請到JBoss_INSTALL_PATH/bin 下,執行 add-user.sh or add-user.bat 如下
(如果你是Domain 的話,僅需要在Domain controller 下執行,如果你是在Standalone的話,請每個Server 都要執行喔!)

Lin-Christinateki-MacBook-Air:bin weimeilin$ ./add-user.sh

What type of user do you wish to add? 
 a) Management User (mgmt-users.properties) 
 b) Application User (application-users.properties) 
(a): (告訴系統是AP在用的)

Enter the details of the new user to add.
Realm (ApplicationRealm) : 
Username : christinaejb (使用者名稱)
Password : (輸入密碼)
Re-enter Password : (輸入密碼)
What roles do you want this user to belong to? (Please enter a comma separated list, or leave blank for none)[  ]: (使用者的角色,在這裡直接空白)
About to add user 'christina' for realm 'ApplicationRealm'
Is this correct yes/no? yes
Added user 'christina' to file '/Applications/demo/jboss-eap-6.0/standalone/configuration/application-users.properties'
Added user 'christina' to file '/Applications/demo/jboss-eap-6.0/domain/configuration/application-users.properties'
Added user 'christina' with roles remoteJMS to file '/Applications/demo/jboss-eap-6.0/standalone/configuration/application-roles.properties'
Added user 'christina' with roles remoteJMS to file '/Applications/demo/jboss-eap-6.0/domain/configuration/application-roles.properties'
Is this new user going to be used for one AS process to connect to another AS process e.g. slave domain controller?
yes/no? yes
To represent the user add the following to the server-identities definition <secret value="Y2hyaXN0aW5hZWpiNzk=" /> (等一下要用到的)


請注意最後這個加密後的值。

Client端

這裡才是需要注意的地方,因為這裡我們要在Server上設定可以對外聯接EJB的Connector, 沒錯,就是在Server上開個洞去連結EJB, 因為是開個連結,所以想當然爾就要一些安全性上的設定拉!請到 standalone-full.xml  or standalone-full-ha.xml 或是 domain.xml 裡面的 full or full-ha 的Profile下設定

1. 設定連接的密碼,在下面紅色的地方,打入你剛剛加密後得到的密碼


<management>
        <security-realms>
            <!--...-->
            <security-realm name="ejb-security-realm">
                <server-identities>
                    <secret value="dsHJfKDhhsjdas=="/>
                </server-identities>
            </security-realm>
        </security-realms>
</management>


2. 設定對外聯接的IP 與Socket 位置,也就是你的EJB Server 對外的remote IP 與port 通常default 都是 4447


<socket-binding-group name="standard-sockets" default-interface="public" port-offset="${jboss.socket.binding.port-offset:0}">
        <!--...-->
        <outbound-socket-binding name="remote-ejb">
            <remote-destination host="IP_ADDRESS" port="4447"/>
        </outbound-socket-binding>
        <outbound-socket-binding name="remote-ejb1">
            <remote-destination host="IP_ADDRESS" port="4547"/>
        </outbound-socket-binding>
</socket-binding-group>



3. 到Remote subsystem 下面設定你對外的連結名稱

<subsystem xmlns="urn:jboss:domain:remoting:1.1">
  <!--....-->
  <outbound-connections>
     <remote-outbound-connection name="remote-ejb-connection" outbound-socket-binding-ref="remote-ejb" security-realm="ejb-security-realm" username="christinaejb">
<properties>
  <property name="SASL_POLICY_NOANONYMOUS" value="false"/>
  <property name="SSL_ENABLED" value="false"/>
</properties>
      </remote-outbound-connection>

      <remote-outbound-connection name="remote-ejb-connection1" outbound-socket-binding-ref="remote-ejb1" security-realm="ejb-security-realm" username="christinaejb">
<properties>
  <property name="SASL_POLICY_NOANONYMOUS" value="false"/>
  <property name="SSL_ENABLED" value="false"/>
</properties>
      </remote-outbound-connection>
   </outbound-connections>
</subsystem>

其中,
1. remote-ejb-connection 就是你給這個remote connection的名字
2. remote-ejb 剛剛我們設定對外對應的socket名稱
3. ejb-security-realm 剛剛設定安全密碼的名稱
4. christinaejb 建立在ejb Server上的使用者名字
又因為這次是兩個server, 所以分別建立的兩次!

這樣Server上的設定都完成了, 接下來就是要到程式裡面去設定,告訴你的WAR 或是 EAR 你需要用哪幾個remote的 EJB connection

建立一個  jboss-ejb-client.xml , 如果是EAR, 請放在 EAR/META-INF, 如果是WAR 請放在WAR/META-INF

內容如下:


<jboss-ejb-client xmlns="urn:jboss:ejb-client:1.0">
    <client-context>
        <ejb-receivers>
            <remoting-ejb-receiver outbound-connection-ref="remote-ejb-connection"/>
            <remoting-ejb-receiver outbound-connection-ref="remote-ejb-connection1"/>
        </ejb-receivers>
    </client-context>
</jboss-ejb-client>

這裡很容易看出來,他其實就是告訴EAR/WAR 你有哪幾個remote ejb connection 可以用。

以上所有的設定都完成了,寫在JSP or Servlet中的code 也沒什麼大的驚喜,與EJB Client相同。

protected void doInvoke(){
   Context context=null;
   try{
      final Hashtable jndiProperties = new Hashtable();
      jndiProperties.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");
      context = new InitialContext(jndiProperties);
   }catch (Exception e) {
      e.printStackTrace();
   }

   MyStatusBeanRemote remote;
   try {
      remote = (MyStatusBeanRemote)context.lookup("ejb:MyEJBProjectEAR/MyEJBProject//MyStatusBean!redhat.remotebeans.MyStatusBeanRemote?stateful");
      System.out.println("1."+remote.getState());
      remote.updateState("Christina");
      System.out.println("2."+remote.getState());
   } catch (NamingException e) {
      e.printStackTrace();
   }
}
之後啓動Server, 可以看到Server 去連結兩個ejb的client. (有個lookup 的小bug,目前我還在找patch中,會出error 但是基本上不會影響工作。AS7-4755  )

執行Servelt之後,可以看到回應很正常!以上!






Comments

Popular posts from this blog

Red Hat Fuse - Announcing Fuse 7 Tech preview 3 release.

Red Hat Fuse 7.0 technical preview three is out today! On the pathway to become one of the best cloud-native integration platform, Fuse gives developer freedom to choose how they want to develop the integration solution, where they want to deploy it and capabilities to address new integration personas that do not have development experience.
By supporting the three major runtime, developer is free to work on the runtime of their choice.By supporting standalone and cloud deployment, it simplifies the complexity to distinguish between these environments, allowing application to deploy freely among the environment of your choice. All levels of developers are welcome, you can either dive deep into creating customize complex integration logic, or using the new low code platform to quickly build a simple integration. In this Tech Preview release you get it all.
Fuse StandaloneSpring-boot for microserviceKaraf 4 for OSGi loverJBoss EAP for JavaEE developersFuse on OpenShiftPlugins for easy co…

JBoss EAP 6 - 效能調校 (一) DataSource 的 Connection Pool

效能沒有什麼Best Practice, 反正能調整的就那些。 通常,一個程式的效能大概有70-80% 都跟程式怎麼寫的其實比較有關係。

最近我最疼愛的小貓Puji 因為膀胱結石開刀的時候過世了,心情很差請原諒我的口氣沒有很好,也沒有心情寫部落格。

Puji R.I.P.

=======================正文=======================

這個題目很多人叫我寫,可是這題目好大,這分明就是整死我咩~
所以我會分幾段慢慢寫。

JBoss 的 SubsystemDatasource WebWeb Service EJB Hibernate JMSJCAJVM 調校OS (作業系統)

先來看一下 DataSource Subsystem, DataSource 的部分主要是針對Connection Pool 做調校。

通常,程式都會需要跟資料庫界接,電腦在本機,尤其是在記憶體的運算很快,但是一旦要外部的資源連接,就是會非常的耗資源。所以現在的應用程式伺服器都會有個Pool 放一些先連接好的 資料庫connection,當程式有需要的時候就可以馬上提供,而不用花那些多餘的資源去連接資料庫。

這就是為什麼要針對Connection Pool 去做調校。

以下會討論到的參數,都是跟效能比較有關係,Datasource 還有很多參數,像是檢核connection 是否正確的,我都不會提到。如果你追求的是非常快速的效能,那我建議你一個檢核都不要加。當然,這樣就會為伺服器上面執行的程式帶來風險。這就是你要在效能與正確,安全性上面的取捨了。 (套句我朋友說的話,不可能又要馬兒好,又要馬兒不吃草的..)

最重要的調校參數就是 Connection 的 Pool 數量。(也就是那個Pool 裡面要放幾條的connection.) 這個參數是每一個應用程式都不一樣的。

min-pool-size 

Connection Pool 最少會存留的connection 數量

max-pool-size 

Connection Pool 最多可以開啓的 connection 數量

prefill

事先將connection pool 裡面建立好min-pool-size 的connection.

我的建議是觀察一下平常程式要用到的量設定為 min-pool-size 。
加上…

Fuse - Contract First API Design with Apicurio and Fuse/Camel - Part One

This is part one of my two-article series that demonstrates the approach of implementing contract-first API design using Apicurioand Red Hat Fuse.

It covers how to create an OpenAPI standard document as the contract between API providers and consumers using Apicurio. It also shows how to quickly create mock tests using Red Hat Fuse.

There are two common approaches of creating these APIs.
Code FirstContract First Coming from a old time ESB developer, these are not new. We have been doing this forever. Before, it was the WSDL that define the contract of the service. we were doing a lot more code first, for me it's simply because it's much easier for me to write couple of Java classes and generate the WSDL for my consumer. 

It's often pretty straightforward if the consumer of your application has finalized how they want the service to be like. But you and I all know this is not often the case. So I had to go back to my code, and make the changes accordingly and pray I did not …