ICE进阶,使用配置文件进行配置--java

1.前一篇回顾

首先没看过前一篇的,可以先去从上一篇看起。

使用zeroc ice框架java快速入门

最后也成功调用了另一个工程的方法。但是这种方式耦合太强,代码都写死,所以需要使用到配置文件配置。

2. 进阶配置

从上一篇完成的工程的基础上进行修改
在ICE Server工程的根目录创建

resource目录,这里放配置文件,养成配置文件和代码分开的好习惯

data/registry目录 这里保存grid生成的临时文件

data/node目录 这里保存grid生成的临时文件

lib/这里需要引入ice.jar,IceGrid.jar, IceBox.jar的三个jar包

2.1 打开服务端的HelloI,重新编辑, 集成了Service接口,实现了start方法和Destroy方法

package com.zgd.ice.service.impl;

import java.util.logging.Level;
import java.util.logging.Logger;

import com.zgd.ice.service._HelloServiceDisp;

import Ice.Communicator;
import Ice.Current;
import Ice.Object;
import Ice.ObjectAdapter;
import IceBox.Service;

/**
 * @author Admin
 * 服务接口的具体实现类---servant伺服者
 */
public class HelloI extends _HelloServiceDisp implements Service {

    private Logger log = Logger.getLogger(this.getClass().getSimpleName());

    private ObjectAdapter _adapter;

    /**
    * 实现具体的服务接口中的方法
    * @param str
    * @param __current
    * @return 
    */
    @Override
    public String sayHello(String str, Current __current) {
        log.info("具体的服务接口实现类中的sayHello方法被调用了。。。");

        return "Hello world :"+str;
    }

    /**
    * @param arg0
    * @param arg1
    * @param arg2 
    */
    @Override
    public void start(String name, Communicator communicator, String[] arg2) {
        log.info("name:"+name);
        // 通讯器创建适配器 
        _adapter = communicator.createObjectAdapter(name);
         // ice对象
         Object object = this;
         // 适配器添加ice对象
         _adapter.add(object, communicator.stringToIdentity(name));
         // 激活适配器
         _adapter.activate();
         log.info(name + "-- adapter 激活成功");
    }

    /**
    * <p>Title: stop</p> 
    * <p>Description: </p>  
    */
    @Override
    public void stop() {
        log.info("adapter 销毁成功");
        _adapter.destroy();

    }

}

2.2 grid配置文件

resource/registry.cfg

#registry configfor icegrid

# 注册调用的相关信息
# 设置用于监听客户端连接的协议类型及端口
IceGrid.Registry.Client.Endpoints=tcp -p 7061

# 设置与服务器节点之间通讯的协议类型,不指定端口则由系统自动分配
IceGrid.Registry.Server.Endpoints=tcp

# 内部访问端点信息,通常是default,节点用这个端口和注册服务通信
IceGrid.Registry.Internal.Endpoints=tcp

# 指定进入管理系统的安全验证方式
IceGrid.Registry.AdminPermissionsVerifier=IceGrid/NullPermissionsVerifier

# 设置主注册节点的数据存储目录
IceGrid.Registry.Data=../data/registry

# 动态注册
IceGrid.Registry.DynamicRegistration=1

2.2 node配置文件

resource/node1.cfg

#指定主注册节点的位置
Ice.Default.Locator=IceGrid/Locator:tcp -h 127.0.0.1 -p 7061

#设置节点1相关数据的存储目录
IceGrid.Node.Data=..\data\node 

#指定节点1用于监听客户端连接的端口号
IceGrid.Node.Endpoints=tcp -p 5061 

#指定节点1的名称
IceGrid.Node.Name=node1

#指定错误日志文件
Ice.StdErr=..\data\node\node.stderr.log 

Ice.StdOut=..\data\node\node.stdout.log

2.3 grid.xml

resource /grid.xml
重点注意< env >标签

<icegrid>

       <application name="HelloApp">

        <properties id="MultiThreaded">

            <property name="Ice.ThreadPool.Server.Size" value="50"/>

            <property name="Ice.ThreadPool.Server.SizeWarn" value="150"/>

            <property name="Ice.ThreadPool.Server.SizeMax" value="200"/>

             <property name="IceBox.InheritProperties" value="1"/>

            <property name="Ice.Override.ConnectTimeout" value="5000" />

            <property name="Ice.Override.Timeout" value="10000" />

            <property name="Ice.Default.LocatorCacheTimeout" value="300" />

            <property name="Ice.BackgroundLocatorCacheUpdates" value="1" />

        </properties>

              <server-template id="HelloServerTemp">

                     <parameter name="index" />

                     <icebox id="icebox-${index}" exe="java" activation="on-demand">

                <properties>

                   <properties refid="MultiThreaded" />

                </properties>

                <option>-Xms2G</option>

                            <option>-Xmx2G</option>

                            <option>IceBox.Server</option>
                    <!-- Ice安装目录的lib文件夹,以及eclipse工作空间ice_hello工程目录中的的bin -->
                     <env>CLASSPATH=C:\Program Files (x86)\ZeroC\Ice-3.6.4\lib\*;E:\no-workspace\ice_hello\bin</env>
                            <!--entry是我们自己写的实现类-->
                            <service name="HelloService" entry="com.zgd.ice.service.impl.HelloI">

                                   <adapter name="HelloService" id="HelloAdapter-${index}" endpoints="default"

                                          replica-group="ReplicatedAdapter" />

                            </service>

                     </icebox>

              </server-template>

              <replica-group id="ReplicatedAdapter">

                     <load-balancing type="round-robin"/>
                    <!--注意这里修改成自己的接口的路径-->
                     <object identity="HelloService" type="::com::zgd::ice::service::HelloService" />

              </replica-group>

              <node name="node1" >

                 <server-instance template="HelloServerTemp"  index="1"/>

              </node>

       </application>

</icegrid>

3. 发布

使用cmd进入到service工程的resource目录

发布Grid

执行如下命令,没返回且没有退出就对了
icegridregistry --Ice.Config=registry.cfg

发布node

执行如下命令,同样没返回且没有退出就对了
icegridnode --Ice.Config=node1.cfg

将node部署到grid中

执行如下命令

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

这时会进到icegridadmin管理的工具中

执行以下命令布署node

application add grid.xml

查看当前部署的的app

application list

查看当前部署的Servant

server list

4. client调用方法

/**
 * 
 */
package com.zgd.ice.client;

import java.util.logging.Logger;

import com.zgd.ice.service.HelloServicePrx;
import com.zgd.ice.service.HelloServicePrxHelper;

import Ice.Communicator;
import Ice.ObjectPrx;
import Ice.Util;

/**
 * @author Admin 客户端
 */
public class HelloClient {

    private static Logger log = Logger.getLogger(HelloClient.class.getSimpleName());


    public static void main(String[] args) {

        log.info("客户端启动...");

        // 通信器
        Communicator ic = null;
        String[] initParams = new String[] { "--Ice.Default.Locator=IceGrid/Locator:tcp -h 127.0.0.1 -p 7061" };

        ic =Ice.Util.initialize(initParams);

        Ice.ObjectPrx base = ic.stringToProxy("HelloService");

        // 转换成HelloService类型的代理服务
        HelloServicePrx helloServicePrx = HelloServicePrxHelper.checkedCast(base);
        //调用方法
        String str = helloServicePrx.sayHello("zgd");
        System.out.println(str);

    }
}

结果:
这里写图片描述

猜你喜欢

转载自blog.csdn.net/zzzgd_666/article/details/80338175
今日推荐