Index
源码链接
ICE
Hello.ice
#pragma once
module Demo
{
interface Hello
{
idempotent void sayHello();
}
}
客户端
Client.cpp
#include <Ice/Ice.h>
#include <Hello.h>
using namespace std;
using namespace Demo;
//
// Global variable for destroyCommunicator
//
Ice::CommunicatorPtr communicator;
//
// Callback for CtrlCHandler
//
void
destroyCommunicator(int)
{
communicator->destroy();
}
int
main(int argc, char* argv[])
{
#ifdef ICE_STATIC_LIBS
Ice::registerIceLocatorDiscovery();
#endif
int status = 0;
try
{
//
// CtrlCHandler must be created before the communicator or any other threads are started
//
Ice::CtrlCHandler ctrlCHandler;
//
// CommunicatorHolder's ctor initializes an Ice communicator,
// and it's dtor destroys this communicator.
//
Ice::CommunicatorHolder ich(argc, argv, "config.client");
communicator = ich.communicator();
//
// Destroy communicator on Ctrl-C
//
ctrlCHandler.setCallback(&destroyCommunicator);
//
// The communicator initialization removes all Ice-related arguments from argc/argv
//
if(argc > 1)
{
cerr << argv[0] << ": too many arguments" << endl;
status = 1;
}
else
{
HelloPrx hello = HelloPrx::uncheckedCast(communicator->propertyToProxy("Hello.Proxy"));
if(!hello)
{
cerr << argv[0] << ": invalid or missing Hello.Proxy property" << endl;
return 1;
}
hello->sayHello();
}
}
catch(const std::exception& ex)
{
cerr << ex.what() << endl;
status = 1;
}
return status;
}
Icebox服务共享库
HelloI.h
#ifndef HELLO_I_H
#define HELLO_I_H
#include <Hello.h>
class HelloI : public Demo::Hello
{
public:
HelloI(const std::string&);
virtual void sayHello(const Ice::Current&);
private:
// Required to prevent compiler warnings with MSVC++
HelloI& operator=(const HelloI&);
const std::string _serviceName;
};
#endif
HelloI.cpp
#ifdef _MSC_VER
// For getenv
# define _CRT_SECURE_NO_WARNINGS
#endif
#include <Ice/Ice.h>
#include <HelloI.h>
using namespace std;
HelloI::HelloI(const string& serviceName) :
_serviceName(serviceName)
{
}
void
HelloI::sayHello(const Ice::Current&)
{
char* val = getenv("LANG");
string lang = val ? string(val) : "en";
string greeting = "Hello, ";
if(lang == "fr")
{
greeting = "Bonjour, ";
}
else if(lang == "zh")
{
greeting = "你好, ";
}
else if(lang == "de")
{
greeting = "Hallo, ";
}
else if(lang == "es")
{
greeting = "Hola, ";
}
else if(lang == "it")
{
greeting = "Ciao, ";
}
cout << greeting << _serviceName << endl;
}
HelloServiceI.h
#ifndef HELLO_SERVICE_I_H
#define HELLO_SERVICE_I_H
#include <IceBox/IceBox.h>
class HelloServiceI : public ::IceBox::Service
{
public:
virtual void start(const ::std::string&,
const ::Ice::CommunicatorPtr&,
const ::Ice::StringSeq&);
virtual void stop();
private:
::Ice::ObjectAdapterPtr _adapter;
};
#endif
HelloServiceI.cpp
#include <Ice/Ice.h>
#include <HelloServiceI.h>
#include <HelloI.h>
using namespace std;
extern "C"
{
//
// Factory function
//
ICE_DECLSPEC_EXPORT IceBox::Service*
create(Ice::CommunicatorPtr)
{
return new HelloServiceI;
}
}
void
HelloServiceI::start(const string& name, const Ice::CommunicatorPtr& communicator, const Ice::StringSeq& /*args*/)
{
_adapter = communicator->createObjectAdapter("Hello-" + name);
string helloIdentity = communicator->getProperties()->getProperty("Hello.Identity");
Demo::HelloPtr hello = new HelloI(name);
_adapter->add(hello, Ice::stringToIdentity(helloIdentity));
_adapter->activate();
}
void
HelloServiceI::stop()
{
_adapter->destroy();
}
客户端配置
config.client
#
# The client reads this property to create the reference to the
# "hello" object in the server.
#
Hello.Proxy=hello
#
# The IceGrid locator proxy.
#
Ice.Default.Locator=DemoIceGrid/Locator:default -h localhost -p 4061
#
# To use the IceGrid discovery plugin comment out the default locator
# property above and un-comment the two properties below. The discovery
# plugin uses multicast to discover IceGrid.
#
#Ice.Plugin.IceLocatorDiscovery=IceLocatorDiscovery:createIceLocatorDiscovery
#IceLocatorDiscovery.InstanceName=DemoIceGrid
icegridnode服务配置
config.grid
#
# The IceGrid instance name.
#
IceGrid.InstanceName=DemoIceGrid
#
# The IceGrid locator proxy.
#
Ice.Default.Locator=DemoIceGrid/Locator:default -h localhost -p 4061
#
# 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
#
# Enable the admin session manager for use with Glacier2.
#
IceGrid.Registry.AdminSessionManager.Endpoints=default -h localhost
IceGrid.Registry.LMDB.Path=db/registry
IceGrid.Registry.PermissionsVerifier=DemoIceGrid/NullPermissionsVerifier
IceGrid.Registry.AdminPermissionsVerifier=DemoIceGrid/NullPermissionsVerifier
IceGrid.Registry.SSLPermissionsVerifier=DemoIceGrid/NullSSLPermissionsVerifier
IceGrid.Registry.AdminSSLPermissionsVerifier=DemoIceGrid/NullSSLPermissionsVerifier
IceGrid.Registry.Discovery.Interface=127.0.0.1
#
# IceGrid node configuration.
#
IceGrid.Node.Name=node1
IceGrid.Node.Endpoints=default -h localhost
IceGrid.Node.Data=db/node
IceGrid.Node.CollocateRegistry=1
#IceGrid.Node.Output=db
#IceGrid.Node.RedirectErrToOut=1
#
# IceMX configuration
#
IceMX.Metrics.Debug.GroupBy=id
IceMX.Metrics.Debug.Disabled=1
IceMX.Metrics.Debug.Reject.parent=Ice\.Admin
IceMX.Metrics.ByParent.GroupBy=parent
IceMX.Metrics.ByParent.Disabled=1
#
# Trace properties.
#
IceGrid.Node.Trace.Activator=1
#IceGrid.Node.Trace.Adapter=2
#IceGrid.Node.Trace.Server=3
#
# Dummy username and password for icegridadmin.
#
IceGridAdmin.Username=foo
IceGridAdmin.Password=bar
application.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!-- This file was written by IceGrid Admin -->
<icegrid>
<application name="HelloSimpsons">
<properties id="mx">
<property name="IceMX.Metrics.Debug.GroupBy" value="id"/>
<property name="IceMX.Metrics.Debug.Disabled" value="1"/>
<property name="IceMX.Metrics.ByParent.GroupBy" value="parent"/>
<property name="IceMX.Metrics.ByParent.Disabled" value="1"/>
</properties>
<service-template id="HelloService">
<parameter name="name"/>
<service name="${name}" entry="./HelloService:create">
<description>A very simple service named after ${name}</description>
<properties>
<properties refid="mx"/>
<property name="Hello.Identity" value="hello"/>
</properties>
<adapter name="Hello-${name}" endpoints="default -h localhost" id="Hello-${name}"
replica-group="HelloGroup" server-lifetime="false"/>
</service>
</service-template>
<server-template id="Glacier2">
<parameter name="instance-name" default="${application}.Glacier2"/>
<parameter name="client-endpoints"/>
<parameter name="server-endpoints"/>
<parameter name="session-timeout" default="0"/>
<server id="${instance-name}" activation="manual" exe="glacier2router">
<properties>
<properties refid="mx"/>
<property name="Glacier2.Client.Endpoints" value="${client-endpoints}"/>
<property name="Glacier2.Server.Endpoints" value="${server-endpoints}"/>
<property name="Glacier2.InstanceName" value="${instance-name}"/>
<property name="Glacier2.SessionTimeout" value="${session-timeout}"/>
</properties>
</server>
</server-template>
<replica-group id="HelloGroup">
<load-balancing type="round-robin" n-replicas="1"/>
<object identity="hello" type="::Demo::Hello"/>
</replica-group>
<node name="node1">
<server-instance template="Glacier2"
instance-name="DemoGlacier2"
client-endpoints="tcp -h localhost -p 4063"
server-endpoints="tcp -h localhost">
<properties>
<property name="Glacier2.SessionManager" value="DemoIceGrid/AdminSessionManager"/>
<property name="Glacier2.PermissionsVerifier" value="DemoGlacier2/NullPermissionsVerifier"/>
</properties>
</server-instance>
<icebox id="IceBox" activation="on-demand" exe="icebox">
<description>A sample IceBox server</description>
<properties>
<properties refid="mx"/>
<property name="IceBox.Trace.ServiceObserver" value="1"/>
</properties>
<service-instance template="HelloService" name="Homer"/>
<service-instance template="HelloService" name="Marge"/>
<service-instance template="HelloService" name="Bart"/>
<service-instance template="HelloService" name="Lisa"/>
<service-instance template="HelloService" name="Maggie"/>
<env>LANG=en</env>
</icebox>
</node>
</application>
</icegrid>
部署
启动icegridnode
icegridnode --Ice.Config=config.grid
部署服务HelloSimpsons
icegridadmin --Ice.Config=config.grid -e "application add application.xml"
测试
./client
icegridgui
- 安装:
sudo apt install zeroc-icegridgui
- 启动:
icegridgui
- 登录(确保icegridnode服务已经启动)
- 创建连接