当前位置: 移动技术网 > IT编程>开发语言>Java > 开发zeroc ice应用入门(java开发ice应用,python开发ice应用,java与python结合开发ice服务)

开发zeroc ice应用入门(java开发ice应用,python开发ice应用,java与python结合开发ice服务)

2019年08月26日  | 移动技术网IT编程  | 我要评论

 

 ice作为一种rpc框架,为主流平台设计,包括windows和linux,支持广泛的语言,包括c++,java,c#(和其他.net的语言,例如visual basic),python,ruby,php和actionscript。

安装ice   

1.官网下载地址  

2.安装程序,本文安装位置e:\program files\zeroc\ice-3.6.3

3.配置环境变量

计算机->属性->高级系统设置->环境变量

1)新建立一个ice_home,变量值为安装路径

2)在path中添加”%ice_home%\bin“

3)检验

配置完成之后打开cmd

出现版本号即为安装并配置完成

 

 

 

java结合ice开发

创建一个maven管理的java项目

在pom.xml中添加依赖

<!-- https://mvnrepository.com/artifact/com.zeroc/ice -->
		<dependency>
			<groupid>com.zeroc</groupid>
			<artifactid>ice</artifactid>
			<version>3.6.3</version>
		</dependency>

依赖最好对应ice版本

快速查找依赖的方法  直接百度 maven+多需要的依赖名称

如maven ice

 

 

在项目文件夹下创建slice文件夹   创建一个文件**.ice

本项目示例hello.ice

[["java:package:com.test.ice.service"]] // 定义java包名  父结构
module demo   //模块包名
{
	interface hello   //接口服务名称
	{
		string sayhello(string s);   //具体的方法
	};
};

 

1)使用slice2java编译ice文件生成java代码

在ice所在文件夹打开cmd或者shell,使用以下命令                                   ps:--output-dir    输出文件的目录

slice2java .\hello.ice --output-dir ..\src\main\java

如果看不到代码,刷新项目即可

2)使用eclipse插件生成代码

eclipse – help – marketplace 搜索ice,第一个插件install 

安装完成之后重启eclipse

 重启之后,选中项目名称右键选中ice builder      -> add ice builder

则生成了ice的java代码

 

编写程序

在生成的代码中可以看到"_ice文件接口名称disp.java"的文件,例如本次项目为_hellodisp.java

1)接口实现类helloimpl       继承_hellodisp.java

import com.test.ice.service.demo._hellodisp;

import ice.current;

public class helloimpl extends _hellodisp{

	/**
	 * 
	 */
	private static final long serialversionuid = 1l;

	@override
	public string sayhello(string s, current __current) {
		system.out.println(s);
		return "hello,"+s;
	}
	
}

ps:接下来的话可以忽略,最好是与接口不是同一项目,也就是api即接口项目,server项目,client项目,将接口项目以maven的方式打包成jar安装到本地库,具体操作实例,其他项目添加接口项目依赖

2)服务启动类server

import com.test.ice.service.demo.impl.helloimpl;

import ice.communicator;
import ice.objectadapter;

public class server {
 
	public static void main(string[] args) {
		int status = 0;
		communicator ic = null;
		try{
			system.out.println("server starting...");
			ic = ice.util.initialize(args);
			objectadapter adapter = ic.createobjectadapterwithendpoints("icetest", "default -p 10006");
			ice.object object = new helloimpl();
			adapter.add(object, ic.stringtoidentity("hello"));
			adapter.activate();
			system.out.println("server start success.");
			ic.waitforshutdown();
		}catch(ice.localexception e){
			e.printstacktrace();
			status = 1;
		}catch(exception e){
			system.err.println(e.getmessage());
			status = 1;
		}
		if(ic != null){
			try{
				ic.destroy();
			}catch(exception e){
				system.err.println(e.getmessage());
				status = 1;
			}
		}
		system.exit(status);
	}
 
}

3)客户端client

import com.test.ice.service.demo.helloprx;
import com.test.ice.service.demo.helloprxhelper;

public class client {

	public static void main(string[] args) {
		int status = 0;
		ice.communicator ic = null;
		try {
			ic = ice.util.initialize(args);
			ice.objectprx base = ic.stringtoproxy("hello:default -p 10006");
			helloprx hello = helloprxhelper.checkedcast(base);
			if (hello == null) {
				throw new error("invalid proxy");
			}

			string s = hello.sayhello("world!");
			system.out.println(">>" + s);		
		} catch (ice.localexception e) {
			e.printstacktrace();
			status = 1;
		} catch (exception e) {
			system.err.println(e.getmessage());
			status = 1;
		}
		if (ic != null) {
			try {
				ic.destroy();
			} catch (exception e) {
				system.err.println(e.getmessage());
				status = 1;
			}
		}
		system.exit(status);
	}
}

4)运行程序 

首先运行服务启动类server,

启动客户端client

服务端接受到数据

客户端接收到服务端返回的数据

简单的java 结合ice开发到此介绍结束,具体深入后续一起努力哦

 

 

python开发ice项目

 1.安装ice所需的依赖包

pip install zeroc-ice

 2.编写ice文件

module demo
{
	interface hello
	{
		string sayhello(string s);
	};
};

3.编写server启动程序

#!/usr/bin/env python
# coding=utf-8
import sys, ice

# 动态加载slice文件并编译
ice.loadslice("./demo.ice")

#ice文件中的模块名称
import demo
## 实现一个服务类
class helloimpl(demo.hello):
    def sayhello(self, s, current=none):
        print s
        msg ="hello,"+s
        return msg


with ice.initialize(sys.argv) as communicator:
    print "server starting..."
    adapter = communicator.createobjectadapterwithendpoints("simpleprinteradapter", "default -p 10006")
    object = helloimpl()
    adapter.add(object, communicator.stringtoidentity("hello"))
    adapter.activate()
    print "server start success."
    communicator.waitforshutdown()

4.编写客户端启动程序

#!/usr/bin/env python
# coding=utf-8
import sys, ice
ice.loadslice("./demo.ice")
import demo

with ice.initialize(sys.argv) as communicator:
    base = communicator.stringtoproxy("hello:default -p 10006")
    printer = demo.helloprx.checkedcast(base)
    if not printer:
        raise runtimeerror("invalid proxy")
    print  printer.sayhello("world!")

5.运行程序   

1)启动server程序

2)启动client程序

3)server接受到客户端发送来的数据并输出

 

4)client接受到server返回的数据并输出

 

 

 

 java与python服务互相调用

java启动server服务,python客户端程序调用服务

1)启动java server服务

2)启动python client服务

3)server服务接收数据并输出

4)client接收返回数据并输出

 

 到此为止,门外看看而已

 

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网