参考文档
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);
}
}
}