IceGrid 服务初次搭建【C++】

1.编写ice接口

#ifndef SIMPLE_ICE
#define SIMPLE_ICE

module DiyModule{
	interface CAbsPrinter{
		void DispStr(string s);
		int Calc(int num);
	};
};

#endif

2.编写服务器,实现接口类

#include 
#include "Printer.h"
using namespace DiyModule;

#include 
using namespace std;

class CPrinter : public CAbsPrinter{
public:
	virtual void DispStr(const string& msg, const Ice::Current& curr){
		cout << msg << endl;
	}
	virtual int Calc(int num, const Ice::Current& curr){
		return num*10;
	}
};


int  main(int argc, char* argv[])  
{  
	int iStatus = 0;//程序的退出时的状态,就是否成功执行  
	Ice::CommunicatorPtr spCommunicator;//来包含Ice run time 的主句柄
	cout << "~~~~~~~~~~服务器开始启动~~~~~~~~~~~~~" << endl;

	try{   
		spCommunicator = Ice::initialize(argc, argv);   
		Ice::ObjectAdapterPtr pAdapter= spCommunicator->createObjectAdapterWithEndpoints("PrinterAdapter", "default -p 10000");    
		Ice::ObjectPtr pObj = new CPrinter;  
		pAdapter->add(pObj, spCommunicator->stringToIdentity("SimplePrinter"));  
		pAdapter->activate();  
		spCommunicator->waitForShutdown();  
	}   
	catch (const Ice::Exception& e){  
		cerr << e << endl;  
		iStatus = 1;  
	}   
	catch (const char* msg) {  
		cerr << msg << endl;  
		iStatus = 1;  
	}  


	if (spCommunicator){  
		try{  
			spCommunicator->destroy();  
		}   
		catch (const Ice::Exception& e) {  
			cerr << e << endl;  
			iStatus = 1;  
		}  
	}  
	return iStatus;  
}  

3.编写客户端,通过接口调用服务

#include 
#include "Printer.h"
using namespace DiyModule;


#include   
#include   
#include 
using namespace std;  

int main(int argc, char * argv[])  
{  
	int status = 0;  
	Ice::CommunicatorPtr spCommunicator;  
	cout << "~~~~~~~~~~~客户端开始测试~~~~~~~~~~~~~~~~~~" << endl;

	try{  
		spCommunicator = Ice::initialize(argc, argv);  
		Ice::ObjectPrx pProxy = spCommunicator->stringToProxy("SimplePrinter:default -p 10000");  
		CAbsPrinterPrx pPrinter = CAbsPrinterPrx::checkedCast(pProxy);      
		if (!pPrinter)			throw "Invalid proxy";   
		for(int i = 0; i < 9; i++){
			stringstream oSS; oSS << i+1;
			pPrinter->DispStr(oSS.str() + " : Hello, Server !");  
			cout << oSS.str() << " * 10 = " << pPrinter->Calc(i+1) << endl;
		}
	}   
	catch (const Ice::Exception & ex){  
		cerr << ex << endl;  
		status = 1;  
	}   
	catch (const char * msg){  
		cerr << msg << endl;  
		status = 1;  
	}  

	if (spCommunicator)         spCommunicator->destroy();  

	system("Pause");  
	return status;  
}  

4.创建cfg文件

D:\Projects\IceUsing\Debug\Server.cfg

PrinterAdapter.AdapterId=PrinterAdapter  
PrinterAdapter.Endpoints=tcp  

D:\Projects\IceUsing\Debug\Client.cfg

Ice.Default.Locator=FirstIceGrid/Locator:tcp -h 127.0.0.1 -p 4061 

D:\Projects\IceUsing\Debug\registry.cfg

IceGrid.InstanceName=FirstIceGrid    
#客户端连接到注册中心的地址    
IceGrid.Registry.Client.Endpoints=tcp -p 4061  
IceGrid.Registry.Server.Endpoints=tcp  
IceGrid.Registry.Internal.Endpoints=tcp  
IceGrid.Registry.PermissionsVerifier=FirstIceGrid/NullPermissionsVerifier  
IceGrid.Registry.AdminPermissionsVerifier=FirstIceGrid/NullPermissionsVerifier  
#注册中心数据保存路径,需要手动创建文件夹  
IceGrid.Registry.Data=D:\Projects\IceUsing\Debug\db\registry 
IceGrid.Registry.DynamicRegistration=1  
Ice.Admin.InstanceName=AdminInstance  
Ice.Admin.ServerId=Admin  

D:\Projects\IceUsing\Debug\node1.cfg

# 注册中心地址    
Ice.Default.Locator=FirstIceGrid/Locator:tcp -h 127.0.0.1 -p 4061    
#node名    
IceGrid.Node.Name=node1    
IceGrid.Node.Endpoints=tcp    
#node存储路径    
IceGrid.Node.Data=D:\Projects\IceUsing\Debug\db\node1 
IceGrid.Node.Output=D:\Projects\IceUsing\Debug\db\node1 
IceGrid.Node.CollocateRegistry=0 

5.编写app.xml文件

  
      
          
              
                  
					  
				    
                  
                    
                        
                    
				  
				  
				  
				                                  
               
          
      
   
  

6.建对应的目录结构

D:\Projects\IceUsing\Debug\db\registry

D:\Projects\IceUsing\Debug\db\node1

7.测试

1、icegridregistry --Ice.Config=registry.cfg   
2、icegridnode --Ice.Config=node1.cfg
3、icegridadmin --Ice.Config=node1.cfg   在node1节点上启动,然后添加app.xml配置文件

application add 文件名,添加配置文件
application remove 文件名,删除配置文件

icegridadmin --Ice.Config=node1.cfg -e "application add 'app.xml'"

使用icegridgui查看服务所需的配置:

FirstIceGrid
tcp -h 127.0.0.1 -p 4061

4、在ice的安装包中,找到 bin\icegridgui.jar,然后以java -jar icegridgui.jar运行,然后就可以查看各节点信息,
也可以开启服务
IceGrid 服务初次搭建【C++】_第1张图片

你可能感兴趣的:(客户端代理))