Zeroc Ice 学习笔记--IceGrid服务部署

Zeroc Ice 学习笔记--IceGrid服务部署

1、Ice Registry(建立registry.cfg文件)

Zeroc Ice 学习笔记--IceGrid服务部署_第1张图片

    命令启动注册表进程:

133440_QpxJ_2517950.png

成功后再Data目录下存在一些二进制文件

Zeroc Ice 学习笔记--IceGrid服务部署_第2张图片

2、IceGrid Node

负责IceBox的装载和启停、采集主机的负载信息、执行控制器IceGrid Admin的指令

建立node.cfg文件:

Zeroc Ice 学习笔记--IceGrid服务部署_第3张图片

命令启动配置:134007_l1N5_2517950.png

首先启动Ice Registry 启动IceGrid Node,这样成功以后再Data目录中产生如下文件:

134150_GYXZ_2517950.png

3、IceGrid

  介绍:IceGrid从组成上来说,就是一个Registry进程+N个Grid Node进程,方便管理和运维其程序包有:icegridadmin、icegridgui、icepatch

   如果Registry在本地,端口为4061,启动icegridadmin的命令:

icegridadmin -u test -p test --Ice.Default.Locator="IceGrid/Locator:tcp -h localhost -p 4061"

134703_xkYv_2517950.png

可以通过help查看支持命令:

Zeroc Ice 学习笔记--IceGrid服务部署_第4张图片

我们添加application需要先创建一个IceGrid应用的xml文件:


    
        
            
            
            
            
            
            
            
            
            
            
            
            
        
        
            
            
        
        
            
            
                
                    
                    
                
                
                CLASSPATH=E:\Program Files (x86)\ZeroC\Ice-3.6.2\lib\*;E:\gwsoftWork\gwsoftProjectFile\light-business-temple\icegrid\lib\*;
                
                    
                
            
        
        
            
            
        
        
            
            
        
    
 
   

创建application命令:

135053_CjH3_2517950.png

成功以后查看服务列表(红线为其他服务):

135153_CuGo_2517950.png

启动服务命令:

135316_YMH4_2517950.png

4、Client调用IceGrid服务

  封装Client(参考Zeroc Ice权威指南):

public class IceClientUtil {

    private static Logger LOG = LoggerFactory.getLogger(IceClientUtil.class);

    private static volatile Ice.Communicator communicator = null;

    private static Map classObjectPrxMap = new HashMap();

    private static volatile long lastAccessTimestamp;
    private static volatile MonitorThread monitorThread;
    private static long idleTimeOutSeconds = 0;
    private static String iceLocator = null;
    private static final String locatorKey = "--Ice.Default.Locator";

    //延迟加载Communicator
    public static Ice.Communicator getIceCommuicator(){
        if(communicator == null){
            synchronized (IceClientUtil.class){
                if(communicator == null){
                    ResourceBundle resourceBundle = ResourceBundle.getBundle("iceclient", Locale.ENGLISH);
                    iceLocator = resourceBundle.getString(locatorKey);
                    idleTimeOutSeconds = Integer.parseInt(resourceBundle.getString("idleTimeOutSeconds"));
                    System.out.println("Ice Client's locator is "+iceLocator + " proxy cache time out seconds :"+idleTimeOutSeconds);
                    String[] initParams = new String[]{locatorKey+"="+iceLocator};
                    communicator = Ice.Util.initialize(initParams);
                    createMonitorThread();
                }
            }
        }
        lastAccessTimestamp = System.currentTimeMillis();
        return communicator;
    }

    private static void createMonitorThread(){
        monitorThread = new MonitorThread();
        monitorThread.setDaemon(true);
        monitorThread.start();
    }

    public static void closeCommunicator(boolean remveServiceCache){
        synchronized (IceClientUtil.class){
            if(communicator != null){
                safeShutdown();
                monitorThread.interrupt();
                if(remveServiceCache && !classObjectPrxMap.isEmpty()){
                    try {
                        classObjectPrxMap.clear();
                    }catch (Exception e){
                        LOG.error("关闭Communicator失败:"+e.getMessage());
                        e.printStackTrace();
                    }
                }
            }
        }
    }

    private static void safeShutdown(){
        try {
            communicator.shutdown();
        }catch (Exception e){
            LOG.error("安全关闭失败:"+e.getMessage());
            e.printStackTrace();
        }finally {
            communicator.destroy();
            communicator = null;
        }
    }

    //反射创建Object Proxy
    private static ObjectPrx createIceProxy(Ice.Communicator communicator,Class serviceCls){
        ObjectPrx objectPrx = null;
        String clsName = serviceCls.getName();
        String serviceName = serviceCls.getSimpleName();
        int pos = serviceName.lastIndexOf("Prx");
        if(pos <= 0){
            throw new IllegalArgumentException("Invalid ObjectPrx class ,class name must end with Prx");
        }
        String realSvName = serviceName.substring(0,pos);
        try {
            ObjectPrx base = communicator.stringToProxy(realSvName);
            objectPrx = (ObjectPrx)Class.forName(clsName+"Helper").newInstance();
            Method method = objectPrx.getClass().getDeclaredMethod("uncheckedCast",ObjectPrx.class);
            objectPrx = (ObjectPrx)method.invoke(objectPrx,base);
            return objectPrx;
        }catch (Exception e){
            LOG.error("加载ObjectPrx失败:"+e.getMessage());
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    //用于客户端Api 获取Ice服务实例
    public static ObjectPrx getServicePrx(Class serviceCls){
        ObjectPrx objectPrx = classObjectPrxMap.get(serviceCls);
        if(objectPrx != null){
            lastAccessTimestamp = System.currentTimeMillis();
            return objectPrx;
        }

        objectPrx = createIceProxy(getIceCommuicator(),serviceCls);
        classObjectPrxMap.put(serviceCls,objectPrx);
        lastAccessTimestamp = System.currentTimeMillis();
        return objectPrx;
    }

    static class MonitorThread extends Thread{
        @Override
        public void run() {
           while (!Thread.currentThread().isInterrupted()){
               try {
                   if(lastAccessTimestamp + idleTimeOutSeconds *1000L < System.currentTimeMillis()){
                       closeCommunicator(true);
                   }
               }catch (Exception e){
                   LOG.error("Client线程错误:"+e.getMessage());
                   e.printStackTrace();
               }
           }
        }
    }
}

调用服务:

public class IceGridClientTest {

    public static void main(String... args) {
        TempleServicesPrx templeServicesPrx = (TempleServicesPrx) IceClientUtil.getServicePrx(TempleServicesPrx.class);
        List lists = templeServicesPrx.findTemplePage(100, "", 1, 50);
        for(SliceTemple sliceTemple : lists){
            System.out.println("sdfffff:"+sliceTemple.id);
            List slicePalacePics = templeServicesPrx.findPalacePicByPalaceId(3,1,100);
            if(slicePalacePics != null){
                System.out.println(slicePalacePics.size());
            }
        }

    }

}

 

转载于:https://my.oschina.net/cxlt216/blog/748501

你可能感兴趣的:(java,运维,网络)