本文主要介绍构建ZeroC IceGrid demo的方法,该demo的构建过程中使用到了Ice提供的Ice::Application类。
1 概述
继承Ice::Application类构建IceGrid demo时,实际上是对外提供接口的Ice服务(即server)继承了Ice::Application类。在IceGrid框架工作时,直接调用了该server程序。而使用IceBox搭建IceGrid时,则是通过icebox命令间接地调用了Ice服务。
2 demo构建
说明:本文介绍的demo程序是在Centos 7上、使用C++编程语言开发的,对应的ice、icegrid版本为“3.6.4”。
沿用博客《ZeroC Ice中继承Ice::Application类构建demo的方法》中已编写的继承Ice::Application类的基础框架文件(即:Hello.ice、Hello.h、Hello.cpp、HelloI.h、HelloI.cpp、server.cpp、config.server),我们继续编写IceGrid需要的其他文件。
2.6 编写注册器配置文件
说明:本节继承了博客《ZeroC Ice中继承Ice::Application类构建demo的方法》的部分内容,所以本节直接以2.6小节开始计数。
注册器配置文件(config.registry)的内容如下:
#
# The IceGrid instance name.
#
IceGrid.InstanceName=DemoIceGrid
#
# IceGrid registry configuration.
#
IceGrid.Registry.Client.Endpoints=default -h localhost -p 4061
IceGrid.Registry.Server.Endpoints=default -h localhost
IceGrid.Registry.Internal.Endpoints=default -h localhost
IceGrid.Registry.Data=db/registry
IceGrid.Registry.PermissionsVerifier=DemoIceGrid/NullPermissionsVerifier
IceGrid.Registry.AdminPermissionsVerifier=DemoIceGrid/NullPermissionsVerifier
IceGrid.Registry.SSLPermissionsVerifier=DemoIceGrid/NullSSLPermissionsVerifier
IceGrid.Registry.AdminSSLPermissionsVerifier=DemoIceGrid/NullSSLPermissionsVerifier
#
# Dummy username and password for icegridadmin.
#
IceGridAdmin.Username=foo
IceGridAdmin.Password=bar
注意:
- 注册器存放数据的目录(IceGrid.Registry.Data)需要用户手动创建好。
节点配置文件(config.node1)的内容如下:
#
# The IceGrid locator proxy.
#
Ice.Default.Locator=DemoIceGrid/Locator:default -h localhost -p 4061
#
# IceGrid node configuration.
#
IceGrid.Node.Name=node1
IceGrid.Node.Endpoints=default -h localhost
IceGrid.Node.Data=db/node1
注意:
- 节点存放数据的目录(IceGrid.Node.Data)需要用户手动创建好。
应用配置文件(application.xml)的内容如下:
<icegrid>
<application name="Simple">
<node name="node1">
<server id="SimpleServer" exe="./server" activation="on-demand">
<adapter name="HelloAdapter" endpoints="tcp -h localhost">
<object identity="hello" type="::Demo::Hello" property="Identity"/>
</adapter>
</server>
</node>
</application>
</icegrid>
2.7 编写客户端代码及配置文件
客户端代码(client.cpp)如下:
#include <Ice/Ice.h>
#include <Hello.h>
using namespace std;
using namespace Demo;
int main(int argc, char* argv[])
{
int status = 0;
Ice::CommunicatorPtr ic;
try
{
ic = Ice::initialize(argc, argv);
Ice::ObjectPrx base = ic->stringToProxy("hello");
HelloPrx hello = HelloPrx::checkedCast(base);
if (!hello)
{
throw "Invalid proxy";
}
string result = "";
result = hello->SayHello("liitdar");
cout << "[icegrid_with_application] client's result: " << result << endl;
}
catch (const Ice::Exception& ex)
{
cerr << ex << endl;
status = 1;
}
catch (const char* msg)
{
cerr << msg << endl;
status = 1;
}
if (ic)
{
ic->destroy();
}
return status;
}
客户端配置文件(config.client)的内容如下:
#
# The IceGrid locator proxy.
#
Ice.Default.Locator=DemoIceGrid/Locator:default -h localhost -p 4061
2.8 编译生成客户端和服务端
编译生成客户端应用程序(client),命令如下:
g++ -o client -I. client.cpp Hello.cpp -lIce -lIceUtil -lpthread
编译生成服务端程序(server),命令如下:
g++ -o server -I. server.cpp Hello.cpp HelloI.cpp -lIce -lIceUtil -lpthread
2.9 运行IceGrid组件
说明:本文为了便于讲述,对于下面的每一步操作都会单独新建一个终端,运行相应的命令。在实际工作中,用户可根据具体情况,对这些操作使用后台模式运行。
运行IceGrid组件的步骤如下:
1. 运行注册器,命令如下:
icegridregistry --Ice.Config=config.registry
2. 运行节点服务器,命令如下:
icegridnode --Ice.Config=config.node1
3. 运行IceGrid命令行管理工具,添加应用配置文件application.xml并查看应用配置信息,命令如下:
[root@liitdar /opt/liitdar/ice-3.6/icegrid_with_app]# icegridadmin --Ice.Default.Locator="DemoIceGrid/Locator:default -h localhost -p 4061"
user id: foo
password:
Ice 3.6.4 Copyright (c) 2003-2017 ZeroC, Inc.
>>> application list
>>>
>>> application add /opt/liitdar/ice-3.6/icegrid_with_app/application.xml
>>>
>>> application list
Simple
>>>
>>> server list
SimpleServer
>>>
说明:icegridadmin后接参数为注册器的定位器地址。
4. 新建一个终端,运行客户端,命令如下:
./client --Ice.Config=config.client
正常情况下,我们能够在上面的“运行节点服务器终端”和“运行客户端终端”两个终端中,看到节点服务器(中的Ice服务)与客户端的信息交互情况,如下:
【节点服务器终端】:
【客户端终端】:
如果两个终端中出现了上述信息,说明继承Ice::Application类的IceGrid的demo构建成功了。