快速入门ice--使用eclipse开发ice



Zeroc ICE Quick Start

1             环境搭建

1.1           

1.1          运行环境配置

1.2          下载

本例下载的版本是Ice-3.5.1-6.msi

https://zeroc.com/download/Ice/3.5/Ice-3.5.1-6.msi

 

1.3          安装ICE运行环境

  1. 建议安装在根目录,如果不安装在根目录,也建议不要装在有空格的目录

本例安装目录为C:\ZeroC\Ice-3.5.1,安装同时会产生一个Ice-3.5.1-demos目录,这里面有很多demo,本例的安装位置为C:\ZeroC\Ice-3.5.1-demos

  1. 配置环境变量

ICE_HOME= C:\ZeroC\Ice-3.5.1

Path= %ICE_HOME%\bin;

CLASSPATH=%ICE_HOME%\lib\ice.jar;%ICE_HOME%\lib\db.jar;%ICE_HOME%\lib\Freeze.jar;%ICE_HOME%\lib\IceBox.jar;

  1. 检查安装是否成功

    cmd窗口执行 slice2cpp –v

    如上显示即表示安装成功

 

1.4          安装ICE 开发插件

本例使用的eclipse版本为4.5jdk版本为1.8.0_60

  1. 打开eclipse,选择help->Eclipse Marketplace

  2. 输入zeroc ice 显示如下

 

  1. 点击Install,选择Ice Builder for Eclipse 4.0.0 并安装

  1. 安装完后eclipse会重启,重点后,点击Windows->Preferemces.配置ICE运行环境

    选择ICE的安装目录即可

 

2             开启ICE之旅

1.1          编写ICE Server

  1. 新建一个Java Project

  1. 为项目添加ICE属性

这时会生成两个目录,如下

  1. slice目录新建一个ice描述文件

    a).HelloWord.ice

代码如下:

module com

{

       modulemyice

       {

              moduletest

              {

                     interfaceHelloWorld

                     {

                            stringsayHello(string s);

                     };

              };

       };

};

 

b).编写servant

HelloWordldI.java

package com.myice.servant;

import com.myice.test._HelloWorldDisp;

import Ice.Current;

public classHelloWordldI extends _HelloWorldDisp{

    @Override

    public StringprintString(String s,Current __current){

       

        System.out.println("client message:"+s);

        return "Hello World:"+s;

    }

}

 

c).使用代码方式发布servant,编写Server

IceServer.java

package com.myice.server;

import com.myice.servant.HelloWordldI;

public classIceServer extends Ice.Application{

    public static void main(String[] a) {

        IceServer app = new IceServer();

        String[]args={"",""};

        app.main("server",args);

    }

 

    public int run(String[] args) {

        int status=0;

        Ice.Communicatoric =null;

        try {

        ic=Ice.Util.initialize(args);

        Ice.ObjectAdapteradapter=ic.createObjectAdapterWithEndpoints("HelloWordldAdapter","default -p 1888");

        Ice.Objectobject= newHelloWordldI();

        adapter.add(object, Ice.Util.stringToIdentity("HelloWordldService"));

       

        adapter.activate();

       

        ic.waitForShutdown();

       

        }catch(Exception e) {

            status=1;

           

        }finally {

            if(ic!=null){

                ic.destroy();

            }

        }

        return status;

       

    }

}

以上代码结构如下:

 

1.2          编写ICE Client

  1. 新建一个java项目 IceClient

  2. 为项目添加ICE属性

  3. ICE描述文件复制到client端的slice目录

  4. 编辑client

IceClient.java

packagecom.myice.client;

 

importcom.myice.test.HelloWorldPrx;

importcom.myice.test.HelloWorldPrxHelper;

 

publicclass IceClient {

         public static void main(String[] args){

 

                   Ice.Communicator ic = null;

 

                   ic =Ice.Util.initialize(args);

 

                   Ice.ObjectPrx base =ic.stringToProxy("HelloWordldService:default -h 127.0.0.1 -p 1888");

 

                   HelloWorldPrx helloworld =HelloWorldPrxHelper.checkedCast(base);

 

                   String s = helloworld.sayHello("huilian");

 

                   System.out.println(s);

         }

}

 

1.3          测试我的Ice

  1. 启动服务端的IceServer

  2. 启动客户端的IceClient

    显示如下即表示你的Hello World成功了,什么?没有,请看步骤3

3.打开IceServer IceClient两个工程所在目录,按Delete键删除,重来一遍

3             ICE进阶

如果你已经成功动行ICE编写的Hello World,那么请你打开IceServer IceClient两个工程所在目录,压缩一下这两个目录为myfirstice.zip,保存起来珍藏,以备多年以后,你成为ICE大牛以后,再打开这两个目录,可以回忆一下也是极好的

1.1            使用配置文件方式发布Ice

ICE Server工程的根目录创建

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

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

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

lib/这里需要引入ice.jarIceGrid.jar IceBox.jar的三个jar

Server引入jar

将安装目录C:\ZeroC\Ice-3.5.1\lib下的Ice.jar IceGrid.jar IceBox.jar三个jar包复制到 lib目录,并

改写Servant以支持IceBox

HelloWordldI.java

package com.myice.servant;

 

importcom.myice.test._HelloWorldDisp;

 

import Ice.Communicator;

import Ice.Current;

import Ice.Logger;

import Ice.ObjectAdapter;

import IceBox.Service;

 

public classHelloWordldI extends _HelloWorldDisp   implements Service {

        /**

        *

        */

       private static final long serialVersionUID = 1L;

       private Loggerlog;

        private ObjectAdapter _adapter;

       @Override

       public StringsayHello(String s, Current __current) {

             

              System.out.println("clientmessage:"+s);

              return "HelloWorld:"+s;

       }

         @Override

           public voidstart(String name, Communicator communicator,String[] args) {

               this.log = communicator.getLogger().cloneWithPrefix(name);

               _adapter = communicator.createObjectAdapter(name);

               Ice.Object object = this;

               _adapter.add(object, communicator.stringToIdentity(name));

               _adapter.activate();

               log.trace("control", "started ");

           }

 

           @Override

           public void stop(){

               log.trace("control", "stoped ");

               _adapter.destroy();

 

           }

}

编写Grid配置文件

resource/registry.cfg

#registry configfor icegrid

IceGrid.Registry.Client.Endpoints=tcp-p 7061

IceGrid.Registry.Server.Endpoints=tcp

IceGrid.Registry.Internal.Endpoints=tcp

IceGrid.Registry.AdminPermissionsVerifier=IceGrid/NullPermissionsVerifier

IceGrid.Registry.Data=../data/registry

IceGrid.Registry.DynamicRegistration=1

编写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 

IceGrid.Node.Name=node1

#指定节点1的名称       

Ice.StdErr=..\data\node\node.stderr.log 

#指定错误日志文件

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

编写服务描述文件

resource /1.xml

为什么要改成1.xml 这个习惯不太好,但是你后面会体会到我为什么要这么做

请注意以下文件的红色部分

      <icegrid>

       <application name="HelloWorldApp">

        <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>

                     <env>CLASSPATH=C:\ZeroC\Ice-3.5.1\lib\*;D:\workspace\IceServer\bin</env>

                            <service name="HelloWorld" entry="com.myice.servant.HelloWordldI">

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

                                          replica-group="ReplicatedAdapter" />

                            </service>

                     </icebox>

              </server-template>

              <replica-group id="ReplicatedAdapter">

                     <load-balancing type="round-robin"/>

                     <object identity="HelloWorld" type="::com::myice::test::HelloWorld" />

              </replica-group>

              <node name="node1" >

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

              </node>

             

       </application>

</icegrid>

 

以上文件的目录如下

发布

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

  1. 发布Grid

执行如下命令,没返回且没有退出就对了

icegridregistry --Ice.Config=registry.cfg

  1. 发布node

执行如下命令,同样没返回且没有退出就对了

icegridnode --Ice.Config=node1.cfg

  1. node部署到grid

执行如下命令

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

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

执行以下命令布署node

application add 1.xml

查看当前部署的的app

application list

查看当前部署的Servant

server list

以下命令截图如下

 

使用client测试发布的servant

改写IceClient.java如下,

package com.myice.client;

 

importcom.myice.test.HelloWorldPrx;

importcom.myice.test.HelloWorldPrxHelper;

 

public classIceClient {

       public static voidmain(String[] args) {

              Ice.Communicatoric = null;

               String[] initParams = newString[] { "--Ice.Default.Locator=IceGrid/Locator:tcp-h 127.0.0.1 -p 7061" };

              ic =Ice.Util.initialize(initParams);

              Ice.ObjectPrxbase = ic.stringToProxy("HelloWorld");

              HelloWorldPrxhelloworld =HelloWorldPrxHelper.checkedCast(base);

              Strings = helloworld.sayHello("huilian");

              System.out.println(s);

       }

}

执行这个client 如果返回如下信息即表示成功了,如果没有,请自觉把之前备份的myfirstice.zip 解压出来再来一遍

 

当然我们也可以使用配置文件的方式启动client 这个就留着大家自己研究了~~~

 

 

1.2            IceGrid Admin查看发布的服务状态

  1. 打开安装的IceGridAdmin

  2. 点击 File->Login->New connection

    Instance Name填写 IceGrid

  3. 选择 An Endpint String?

    IceGrid Registry Endpoint(s)填写tcp -h 127.0.0.1 -p 7061

     

  4. 默认密码是test test

就可以看到我们刚才发布的ICE Grid node Servant 如下图

 

 

 

同样请大家关掉前面打开的所有cmd窗口及iceGrid Admin窗口

打开IceServer IceClient两个工程所在目录,压缩一下这两个目录为mysecondice.zip,保存起来备用.

 

大家肯定会觉得这么多配置文件没有一个说明怎么用

请大家出门左拐参考一下这个文档《02.IceGrid应用配置手册》

4             ICE远航

未完待续

 

 

 

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值