黑松山资源网 Design By www.paidiu.com

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应用的资料请关注其它相关文章!

黑松山资源网 Design By www.paidiu.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
黑松山资源网 Design By www.paidiu.com

RTX 5090要首发 性能要翻倍!三星展示GDDR7显存

三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。

首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。

据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。