Skip to main content

JBoss Data Grid - 獨立的HotRod Server

新年快樂!差點被日本的大雪困住,但是還好平安回到台灣來了。
是說我在日本看到這個很酷的茶,有買一罐來喝~老實說... 真的很普通..
(我是在早上去築地市場吃壽司的那個車站買的.)


我還是喜歡Starbucks 的 星冰樂+Java Chip .. 什麼都要給他Java 一下就對了.. 哈哈
======================================================

這次的範例跟先前其實差不了多少,只是之前是使用JBoss Data Grid Server。JDG 其實是很有彈性的,它可以內嵌在伺服器裡面像是JBoss EAP 本身就內建了Infinispan,可以很像之前獨立幫你寫好的Server,也可以獨立寫一支程式去啓動(看你的需求再把它放到Server上)。

這次的範例的Cache 存取就簡單一點了。

1. 老樣子,先建立一個Maven的Project,


選擇Create a simple project.


輸入你的Group,Artifact ID,Version 還有Name... 
  • Group ID: org.christina.blogdemo
  • Artifact IDhotRodServerSample
  • Packaging: JAR 
  • NameHotRodServerSample

按下完成。左方就會出現以下的專案。



2. 在寫程式之前,先去到Red Hat 網站下載把maven 的程式庫下載下來,
 

下載之後,請將檔案解壓縮到你的電腦的某處,(神奇的 Windows 使用者們,路徑請不要放有空格,特殊符號像是 _,-, 下喔...)

3. 設定POM, 我們要先把檔案指到我們剛剛安裝好的Maven程式庫,並且把需要用到的程式庫設定上去。pom.xml 內容如下:

  4.0.0
  org.christina.blogdemo
  hotRodServerSample
  0.0.1-SNAPSHOT
  HotRodServerSample
  
  
        UTF-8
        6.0.0.CR1
        
        1.7
        1.7
    
  
  
   
    org.infinispan
    infinispan-core
    ${version.org.infinispan}
   
   
  org.infinispan
       infinispan-server-hotrod
       ${version.org.infinispan}
 
 
    org.infinispan
    infinispan-client-hotrod
    ${version.org.infinispan}
   
  
  


裡面只有一個要注意的地方,就是程式庫的位置,請修改到你剛剛解壓縮下載回來Data Grid 程式庫的位置。
<repositories>
  <repository>
    <id>JBoss-Data-Grid</id>
    <url>file:///Applications/jbossrepo/jboss-datagrid-6.2.0-maven-repository</url>
  </repository>
</repositories>

4. 在寫程式之前,有幾個設定的xml 檔案需要先放到src/main/resource 下
A. christinaCacheDemo.xml 這個檔案是主要的JDG 設定檔. 
   
   
   
      
       
        
        
      
      
   

   
      
      
         
          
      
   
   
   
    
    

這個XML 主要是對整個Cache 的設定,它可以在這個XML 設定,或是寫成程式..看你自己喜歡。這次我是用xml.  JDG 的 XML 設定可以分成以下三大部份,




像是<global> 的XML 部分,我們可以寫成
GlobalConfigurationBuilder gcb = new GlobalConfigurationBuilder().clusteredDefault();
       gcb.globalJmxStatistics().enable()
       .transport().addProperty("configurationFile", "jgroups-cht.xml")
       .nodeName("Node1");
<default> 的XML 部分,我們可以寫成
ConfigurationBuilder builder = new ConfigurationBuilder();
builder.dataContainer()   
             .jmxStatistics().enable()
             .clustering().cacheMode(CacheMode.DIST_SYNC).hash().numOwners(2);
      
B. jgroups-demo.xml 如果有Cluster 的話,這個很重要,因為JBoss Data Grid 的Cluster 底層就是靠jGroups 互相發現每個node的. 這個demo 的設定是靠UDP. 所以如果你需要TCP 的話,請留個言吧。很多人問我再做。

   

   
   
   
   
   
   
   
   
   
   
   
   
C. log4j.xml. 這沒啥好多說的。純粹log 設定用. 



    
        
        

        
            
            
        
    


    
    
    

    
        
    

    
        
    
    
    
        
    

    
        
    

    
    
    

    
        
        
    



以上三個設定就完成了。

5.開始寫Server 的程式了。
DemoServer.java
package org.christinademo.hotRodServerSample;

import java.io.IOException;

import org.infinispan.manager.DefaultCacheManager;
import org.infinispan.server.core.configuration.ProtocolServerConfiguration;
import org.infinispan.server.hotrod.HotRodServer;
import org.infinispan.server.hotrod.configuration.HotRodServerConfigurationBuilder;


public class DemoServer {

 DefaultCacheManager cacheManager;
 HotRodServer server;
 private static final String HOST_IP = "127.0.0.1";
 private static final int HOST_PORT = 11222;
 
 public DemoServer(){
  try {
   //
   cacheManager = new DefaultCacheManager("christinaCacheDemo.xml");
   
   // Set up Hot Rod Config
   ProtocolServerConfiguration serverConfig = new HotRodServerConfigurationBuilder().host(HOST_IP).port(HOST_PORT).build();
   server = new HotRodServer();
   
   //Start the server
   server.start(serverConfig, cacheManager);
   
  } catch (IOException e) {
   e.printStackTrace();
  }
 }
 
 public static void main(String[] args){
  DemoServer demoServer = new DemoServer();
  
 }
 
 
 
}

發現它非常的簡單吧,第一是去呼叫我們剛剛的JDG 的Cache 設定檔,christinaCacheDemo.xml,然後在機器上啓動一個Hot Rod Server, IP 是 127.0.0.1 port 是 11222 . 如果你喜歡,也可以更改它。

請到Project 的 pom.xml 的路徑下執行

mvn exec:java -Dexec.mainClass="org.christinademo.hotRodServerSample.DemoServer"


7. 為了確認可以work, 也寫一隻Client 程式確認一下。


DemoClient.java (很單純的小程式,可以add, rm , find 資料進Cache. Cache 也很單純,Key 是 String, Value 也是 String)

package org.christinademo.hotRodServerSample;


import java.io.Console;


import org.infinispan.client.hotrod.RemoteCache;
import org.infinispan.client.hotrod.RemoteCacheManager;
import org.infinispan.client.hotrod.configuration.ConfigurationBuilder;

public class DemoClient {
 
 private Console con;
 private RemoteCacheManager cacheManager;
    private RemoteCache cacheTest;
    private static final String initialPrompt = "Choose action:\n" + "============= \n" + "add  -  add a String\n"
            + "rm  -  remove by Key \n"
            + "q   -  quit\n";
    public DemoClient(Console con){
     this.con = con;
     
     ConfigurationBuilder builder = new ConfigurationBuilder();
     builder.addServer().host("localhost").port(11222);//.addServer().host("localhost").port(11322);
     
     this.cacheManager = new RemoteCacheManager(builder.build());
     cacheTest = cacheManager.getCache("remoteCache"); 
    }
    
    public void addString(){

     String key = con.readLine("type key to add:");
     String value = cacheTest.get("remoteCache");
     if(value != null){
      String action = con.readLine("key already existed, type q to exit. or others to override:");
      
      if ("q".equals(action)) {
             return;
            } else{
             value = action;
            }
      
     }else {
      value = con.readLine("type in value of key:");
     }
     
     
     cacheTest.put(key,value);
     
     System.out.println("String Added ===>key:["+key+"] value:["+cacheTest.get(key)+"]");
     
    }
    
    public void findString(){
     String key = con.readLine("type in key to find value:");
     System.out.println("key :["+key+"] has value:["+cacheTest.get(key)+"]");
    }
    public void size(){
     
     System.out.println("Cache Size:["+cacheTest.size()+"]");
    }
    
    public void removeString(){
     cacheTest.remove(con.readLine("type in key to remove value:"));
    }
    public void close(){
     cacheManager.stop();
    }
    
    public static void main(String[] args) {
     try{
         Console con = System.console();
         con.printf(initialPrompt);
         DemoClient client = new DemoClient(con);
 
         while (true) {
             String action = con.readLine(">");
             if ("add".equals(action)) {
              client.addString();
             } else if ("rm".equals(action)) {
              client.removeString();
             } else if ("find".equals(action)) {
              client.findString();
             } else if ("size".equals(action)) {
              client.size();
             } else if ("q".equals(action)) {
              client.close();
                 break;
             }
         }
     }catch(Exception e){
      e.printStackTrace();
     }
    }
    
}

請到Project 的 pom.xml 的路徑下執行
mvn exec:java -Dexec.mainClass="org.christinademo.hotRodServerSample.DemoClient"



以上。

Comments

謝克群 said…
原來年假是跑去日本玩喔
真羨慕

Popular posts from this blog

Red Hat JBoss Fuse/A-MQ - Fuse and A-MQ Version 6.3 GA is released!

Fuse and A-MQ 6.3 GA has just went out. Maybe, you would think this is just only a minor version release why should I care? Hold your thoughts on that! Because they have done a lot of improvements and also added many new features into this release.

Besides various bug fixes and making sure Fuse Fabric is much more stable. There are two major change in this version update:

New Tooling in JBoss Developer Studio (JBDS) 9.1 GA. Newer Apache Camel version – Camel v2.17. I was really impressed by the work put in to make developing Camel application much simpler. First is the installation of tooling itself. Now it has a all-in-one installer so you don't need to worry about which plugins you need to check. See the videos below to see the new "Getting Started" of Fuse 6.3.



And If you notice from the above video, the presentation of camel route in JBDS has also updated. It fixed some of the miss representation of logic and making it easier to read.

Old Camel Route
New Camel Route
On …

Fuse Integration Service - Setup JBDS and create first quickstart application

Before we go and start creating our first application, I want to show you how to setup your JBoss Developer Studio, create a small application from the quickstart example and then running it on Fuse Integration Service.

I am using JBoss Developer Studio version 9, you can find it here.
After download the

jboss-devstudio-9.0.0.GA-installer-eap.jar
double-click it, and start installing with default values.

After successful installation, we will need install the plugins for Fuse, on JBoss Central view, select software update, select enable early access.


And select JBoss Fuse Development for the plugin,


Click on install, and we are all set to go!

First thing first, we want to create a Fuse project to deploy on the base of Fuse Integration Service, which is OpenShift. If you have not installed it, please go back to my previous post for instructions. So on your JBDS, right click and start creating the project. Select new, maven project, if you have installed the plugin correctly, you should …

Red Hat JBoss Fuse - Getting Started with Fuse Integration Service 2.0 Tech preview

I just realized that I did not do a getting started for Fuse Integration Service 2.0 Tech preview before I did the pipeline demo, thanks for those of you who reminded me! :)

To get started with FIS 2.0, for people who has just getting to know the technology, here is how I interpret it. Basically, it's divide into two aspect,

1. Integration development, FIS uses Apache Camel as the core technology that creates, orchestrate, compose microservices into a super lightweight thin integration layer, and become the API provider and service orchestrator through exposing RESTful or messaging service endpoints. And you can choose to either package and run it with Spring-Boot or Karaf.


2. Application Deployment and Management, FIS takes advantages of OpenShift platform, and allows you to separately deploy the micro-integration service among distributed environment, at the same time takes care of the failover, high availability, load balancing and service lookup problem for you.


So, now we know …