Zeroc Ice简介

"_blank" href="https://zeroc.com/distributions/ice" rel="external nofollow" >Zeroc ICE的文档相应调整。

安装Zeroc Ice

"htmlcode">

sudo apt-key adv --keyserver keyserver.ubuntu.com --recv 5E6DA83306132997
sudo apt-add-repository "deb http://zeroc.com/download/apt/ubuntu$(lsb_release -rs) stable main"
sudo apt-get update
sudo apt-get install zeroc-ice-all-runtime zeroc-ice-all-dev

"htmlcode">

sudo -H pip install zeroc-ice

"htmlcode">

sudo apt-get install python-dev
sudo apt-get install libssl-dev
sudo apt-get install libbz2-dev

开发Server和Client

"htmlcode">

// Printer.ice
module Demo {
  interface Printer {
     string printString(string s);
  };
};

生成指定语言的接口文件

"htmlcode">

import sys, traceback, Ice 
import Demo

# PrinterI是接口实现类,Demo.Printer是slice2py生成的接口
class PrinterI(Demo.Printer):
  def printString(self, s, current=None):
    print(s)
    return "Server Printed: " + s 

status = 0 
ic = None

try:
  # 初始化zeroc ice环境
  ic = Ice.initialize(sys.argv)
  # 生成名为SimplePrinterAdapter的对象适配器,连接方式是缺省的tcp,监听端口10000
  adapter = ic.createObjectAdapterWithEndpoints("SimplePrinterAdapter", "default -p 10000")
  # 生成接口的实现对象,并以指定的名字SimplePrinter添加到对象适配器中
  object = PrinterI()
  adapter.add(object, ic.stringToIdentity("SimplePrinter"))
  # 激活对象适配器
  adapter.activate()
  # 使得本服务器的调用线程在此暂停,直至ice服务结束,或者进程结束
  ic.waitForShutdown()
except:
  traceback.print_exc()
  status = 1 

if ic: 
  # Clean up
  try:
    ic.destroy()
  except:
    traceback.print_exc()
    status = 1 

sys.exit(status)

"htmlcode">

PrinterAdapter.AdapterId=PrinterAdapter
PrinterAdapter.Endpoints=tcp

"htmlcode">

import sys, traceback, Ice 
import Demo

status = 0 
ic = None

try:
  ic = Ice.initialize(sys.argv)
  # 生成名为SimplePrinter代理对象,且通过tcp调用,连接目标机器的10000端口
  base = ic.stringToProxy("SimplePrinter:default -p 10000")
  # 将代理对象转换成目标对象
  printer = Demo.PrinterPrx.checkedCast(base)
  if not printer:
    raise RuntimeError("Invalid proxy")
  # 调用服务器的printString方法,并输出返回结果
  rs = printer.printString("Hello World, I'm talking to you through RPC")
  print(rs)

except:
  traceback.print_exc()
  status = 1 

if ic: 
  # Clean up
  try:
    ic.destroy()
  except:
    traceback.print_exc()
    status = 1 

sys.exit(status)

"htmlcode">

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

客户端直连服务端

"text-align: center">如何用python开发Zeroc Ice应用

配置注册中心

registry.cfg(服务注册中心的配置文件)

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

注意:手工创建文件中的路径。

配置节点

"htmlcode">

# 注册中心地址 
Ice.Default.Locator=SzcIceGrid/Locator:tcp -h 127.0.0.1 -p 4061 
#node名 
IceGrid.Node.Name=node1 
IceGrid.Node.Endpoints=tcp 
#node存储路径 
IceGrid.Node.Data=/home/rocway/test/zerocice/nodes/node1
IceGrid.Node.Output=/home/rocway/test/zerocice/nodes/node1
IceGrid.Node.CollocateRegistry=0

注意:手工创建上述文件中提到的路径。其中服务端程序的输出会保存在Ouput指向路径的*.out文件中。

应用描述文件

"htmlcode">

<icegrid>
  <application name="PrinterApplication">
    <node name="node1">
      <server id="PrinterServer" exe="python" activation="on-demand">
        <adapter name="PrinterAdapter" endpoints="tcp -h 127.0.0.1">
          <object identity="SimplePrinter" type="::Demo::Printer" property="Identity"/>
        </adapter>
        <option>/home/rocway/test/zerocice/Server.py</option>  
        <property name="Ice.Trace.Network" value="1"/>
        <properties> 
          <property name="Ice.ThreadPool.Server.SizeMax" value="1" /> 
        </properties> 
            <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"/>   
      </server>
    </node>
  </application>
</icegrid>

启动icegrid

1.启动icegrid注册中心

icegridregistry --Ice.Config=registry.cfg

2.启动某个节点

icegridnode --Ice.Config=node1.cfg

3.启动节点上的应用管理程序, 并添加应用

icegridadmin --Ice.Config=node1.cfg

application add app.xml

4.查看已经添加的应用

application describe PrinterApplication

5.启动各节点上的应用服务

icegridgui

6.运行客户端程序

python Client.py

实验总结

  此次实验实现了在icegrid上部署服务程序,客户端通过icegrid的服务注册中心调用该服务。实验中服务端和客户端使用的都是Python,有兴趣的同学也可以分别使用不同的语言开发服务端和客户端,尝试一下Zeroc ICE的跨语言RPC调用。
  本次实验就到这里,有关Zeroc ICE的其他内容请关注后续的课程。

以上就是如何用python开发Zeroc Ice应用的详细内容,更多关于python开发Zeroc Ice应用的资料请关注其它相关文章!

标签:
python,开发应用,python,Zeroc,Ice

免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
白云城资源网 Copyright www.dyhadc.com

评论“如何用python开发Zeroc Ice应用”

暂无“如何用python开发Zeroc Ice应用”评论...