ice作為一種rpc框架,為主流平台設計,包括Windows和Linux,支持廣泛的語言,包括C++,Java,C#(和其他.Net的語言,例如Visual Basic),Python,Ruby,PHP和ActionScript。
安裝ice
1.官網下載地址 https://zeroc.com/downloads/ice
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安裝到本地庫,具體操作實例https://www.jianshu.com/p/5ce9d1567fee,其他項目添加接口項目依賴
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接收返回數據並輸出
到此為止,門外看看而已