Skip to main content

JEAP 6 - HornetQ Cluster

又是應觀眾要求。好像已經被問了一兩次了,
我其實本來還想要PO 多一點BRMS上來的說,但是再不放上來又要被問N次了..
其實非常簡單,既然是這樣,就花點時間PO上來吧。
================================================

這篇再看這邊之前,請先

A. 學會設定JBoss Cluster (Standalone or Domain), 為了快速與方便,本次使用Standalone Cluster, domain 設定也是一樣的,所以就不再贅述。(連結)
B. 學會設定 HornetQ 的ConnectionFactory 與 Queue。(連結)

先前的JBoss 除了把JBoss Messaging 設定起來後,若要呼叫,就要另外設定HA-JNDI, 新的JBoss EAP 6 已經不需要JNDI 了,只要使用Remote就好了,它自動就會去分配到不同的機器上。


這次是把所有的JBoss 與它的HornetQ 的Cluster 設定起來,之後使用JMS 的單獨的Client 努力的發送訊息(message) 到 Queue 中。然後再寫個Receiver 去接收 Cluster 每一個node queue中的訊息。


1. 先將JBoss 設定為Cluster ,請確認使用 standalone-full-ha.xml ,domain的話請使用 full-ha 的 profile。然後就可以進入正題,請找到HornetQ 設定的subsystem.
先把 Clustered 設定為true, 與互相溝通的 user name與password

   
      true
      clusteruser 
      cluster-secret
      
   


2. 開始設定上面的ConnectionFactory, 這裡我是使用裡面default已經設好的jms/RemoteConnectionFactory。



   
   
   
      
         
      
      
         
      
      true 
      3 
    
    
    


3. 到JBoss Admin console上加上這次要使用的queue,與建立Application Server (請參考連結)

a.建立Queue

b.建立使用者
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 : christina (使用者名稱)
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)[  ]: remoteJMS (使用者的角色,在這裡我自定為remoteJMS)
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? no

b.建立安全性設定





4. 啟動兩個JBoss,
./standalone.sh -c=standalone-full-ha.xml -Djboss.node.name=node1 -b IP_ADDRESS
./standalone.sh -c=standalone-full-ha.xml -Djboss.node.name=node2 -b IP_ADDRESS




5. 寫一個Producer, 會非常快速的把資料放到queue裡面,

若你要Remote Cluster的話,請在URL 的地方放入所有的Server 的位置。
package redhat.jmsclient;

import java.util.Hashtable;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;

public class MsgSender {
 // Set up all the default values
    private static final String DEFAULT_CONNECTION_FACTORY = "jms/RemoteConnectionFactory";
    private static final String DEFAULT_DESTINATION = "jms/queue/sampleQ";
    private static final String DEFAULT_USERNAME = "christina";
    private static final String DEFAULT_PASSWORD = "christina79";
    private static final String INITIAL_CONTEXT_FACTORY = "org.jboss.naming.remote.client.InitialContextFactory";
    private static final String PROVIDER_URL = "remote://127.0.0.1:4447,remote://127.0.0.1:4547";
    
    private Context ic = null;
    private ConnectionFactory cf = null;
    private Connection connection = null;
    public String msg = "Hello Christina at ";
    
    public static void main(String[] args) throws Exception {

     MsgSender sender = new MsgSender();
     if(args != null && args.length>=1 &&args[0] != null){
      sender.msg = args[0];
      }
     sender.send();

 }
    
    public MsgSender(){
     
     
    }

 public void send(){

     final Hashtable env = new Hashtable();
     env.put(Context.INITIAL_CONTEXT_FACTORY, INITIAL_CONTEXT_FACTORY);
     env.put(Context.PROVIDER_URL, PROVIDER_URL);
     
    
     
     try {         
      ic = new InitialContext(env);
      cf = (ConnectionFactory)ic.lookup(DEFAULT_CONNECTION_FACTORY);
      Queue queue = (Queue)ic.lookup(DEFAULT_DESTINATION);

      connection = cf.createConnection(DEFAULT_USERNAME,DEFAULT_PASSWORD);
      Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
      MessageProducer publisher = session.createProducer(queue);
  
      connection.start();
      msg = "Hello Christina at "+System.currentTimeMillis();
      TextMessage message = session.createTextMessage(msg);
      
      publisher.send(message);

      System.out.println("Message:["+msg+"] sento to the JMS Provider");

     }
      catch (Exception exc) {
        exc.printStackTrace();
      }
     finally {         
       if (connection != null)   {
         try {
            connection.close();
         } catch (JMSException e) {                    
           e.printStackTrace();
         }
     } 
  
 }
 }
}




發送快速發送個10000次後,

可以看見, 兩個Server上都有很多的訊息,JBoss 會自動幫你平分兩邊的負載,達到負載平衡(Load Balance)



6. 寫個 Consumer ,去抓上面所有的訊息。

package redhat.jmsclient;

import java.util.Hashtable;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.ExceptionListener;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.MessageProducer;
import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;


public class MsgReceiver implements MessageListener {
 
 private static final String DEFAULT_CONNECTION_FACTORY = "jms/RemoteConnectionFactory";
    private static final String DEFAULT_DESTINATION = "jms/queue/sampleQ";
    private static final String DEFAULT_USERNAME = "christina";
    private static final String DEFAULT_PASSWORD = "christina79";
    private static final String INITIAL_CONTEXT_FACTORY = "org.jboss.naming.remote.client.InitialContextFactory";
    private static final String PROVIDER_URL = "remote://127.0.0.1:4447,remote://127.0.0.1:4547";

 private ConnectionFactory connectionFactory;

 private Queue queue;
 private Context ic;
 

 public static void main(String[] args) throws Exception {

  MsgReceiver reciever = new MsgReceiver();
  
  
  reciever.consum();

  while (true) {
   Thread.sleep(1000);
  }

 }

 public MsgReceiver() throws Exception {
  final Hashtable env = new Hashtable();
   env.put(Context.INITIAL_CONTEXT_FACTORY, INITIAL_CONTEXT_FACTORY);
   env.put(Context.PROVIDER_URL, PROVIDER_URL);
  
   
   ic = new InitialContext(env);
   connectionFactory = (ConnectionFactory)ic.lookup(DEFAULT_CONNECTION_FACTORY);
      queue = (Queue)ic.lookup(DEFAULT_DESTINATION);
 }
 
 
 

 private void consum() throws Exception {
  final Connection connection;;

  
  connection = connectionFactory.createConnection(DEFAULT_USERNAME, DEFAULT_PASSWORD);
  
  connection.setExceptionListener(new ExceptionListener() {
   
   @Override
   public void onException(JMSException exception) {
    exception.printStackTrace();
    
   }
  });
  
  final Session session = connection.createSession(false,
    Session.AUTO_ACKNOWLEDGE);

  final MessageConsumer consumer = session.createConsumer(queue);

  consumer.setMessageListener(this);

  connection.start();

  Runtime.getRuntime().addShutdownHook(new Thread() {
   @Override
   public void run() {
    try {
     consumer.close();
     session.close();
     connection.close();
    } catch (JMSException e) {
     e.printStackTrace();
    }
   }
  });
 }

 @Override
 public void onMessage(Message message) {

  if (message instanceof TextMessage) {
   try {
    System.out.println("Received: "
      + ((TextMessage) message).getText());
   } catch (JMSException e) {
    e.printStackTrace();
   }
  } else {
   System.out.println("Received: " + message);
  }
 }

}




以上。超簡單的吧!


Comments

Unknown said…
想問一下JBoss clustering 跟 hornetq cluster 有沒有關係。可以只用hornetq cluster 嗎?
Unknown said…
This comment has been removed by the author.

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 …