參考文檔
https://blog.csdn.net/u011784767/article/details/74459970
https://doc.zeroc.com/ice/3.6/icebox
在上個文章的基礎上看
https://blog.csdn.net/huiyanshizhen21/article/details/106448441
icebox相等於tomcat
1.新建IceBox的UserServiceServer
package com.yinzhen.demo.ice.userservice;
import Ice.Communicator;
import Ice.ObjectAdapter;
import IceBox.Service;
public class UserServiceServerIceBox implements Service{
private ObjectAdapter adapter;
@Override
public void start(String paramString, Communicator communicator, String[] paramArgs) {
//adapter = communicator.createObjectAdapterWithEndpoints("UserServiceAdapter","default -p 10006");
adapter = communicator.createObjectAdapter(paramString);
// 創建服務端代碼實現servant
UserServiceImpl servant = new UserServiceImpl();
// 將servant與ice對象標識符建立映射關係,並添加到ice對象適配器中
adapter.add(servant, Ice.Util.stringToIdentity(paramString));
// 激活對象適配器
adapter.activate();
System.out.println("UserService adapter activate");
}
@Override
public void stop() {
adapter.destroy();
System.out.println("adapter has been destory");
}
}
2.打包上傳到服務器 /opt/ice_project/app/userservice/lib
3.新建配置文件目錄/opt/ice_project/app/userservice/config
mkdir -p /opt/ice_project/app/userservice/config
4.新建icebox配置文件config.properties
IceBox.Service.UserServiceServer=com.yinzhen.demo.ice.userservice.UserServiceServerIceBox
UserServiceServer.Endpoints=tcp -p 10006 -h 192.168.1.25
IceBox.InheritProperties=1
5.啓動腳本修改
echo "$JAVA_HOME/bin/java -Xms512M -Xmx512M -XX:PermSize=128m -XX:MaxPermSize=256m -cp $CLASSPATH IceBox.Server --Ice.Config=$SERVER_HOME/config/config.properties"
$JAVA_HOME/bin/java -Xms512M -Xmx512M -XX:PermSize=128m -XX:MaxPermSize=256m -cp $CLASSPATH IceBox.Server --Ice.Config=$SERVER_HOME/config/config.properties
6.啓動服務
./startIceServer.sh &
7.調用
package com.yinzhen.demo.ice.userservice;
import com.yinzhen.demo.ice.user.UserInfo;
import com.yinzhen.demo.ice.user.UserServicePrx;
import com.yinzhen.demo.ice.user.UserServicePrxHelper;
public class UserServiceClient {
public static void main(String[] args) {
Ice.Communicator communicator = null;
try {
//初始化ice通信器communicator,可以使用args傳入一下ice初始化的參數如超時時間,線程池大小等
communicator = Ice.Util.initialize(args);
// 傳入遠程服務單元的 ice對象標識符 協議默認tcp 主機 已經服務監聽端口 -h 172.16.20.220
Ice.ObjectPrx op = communicator.stringToProxy("UserServiceServer:default -h 192.168.1.25 -p 10006");
// 檢查通用客戶端代理op 是不是queryServer對象標識符所關聯的ice對象的代理
UserServicePrx userServicePrx = UserServicePrxHelper.checkedCast(op);
if(userServicePrx == null){
throw new Exception("qp == null");
}
UserInfo userInfo = userServicePrx.getUserInfoById("id");
if(userInfo == null){
throw new Exception("userInfo == null");
}
// 輸出服務端返回結果
System.out.println(userInfo.remark);
} catch (Exception e) {
// TODO: handle exception
System.out.println(e);
}
}
}