参考文档
https://blog.csdn.net/u011784767/article/details/74539644
https://doc.zeroc.com/ice/3.6/ice-services/icegrid/getting-started-with-icegrid
在上一章的基础上学习本章
https://blog.csdn.net/huiyanshizhen21/article/details/106455643
1.只使用icebox的问题
只使用IceBox组件来设计和启动Ice服务,客户端必须将服务端的EndPoint写死到代码中。
2.解决方法
Ice设计了服务注册表Registry组件,这是一个以二进制文件形式存储运行期Ice服务注册信息的独立进程,
作为服务的metadata存储数据中心,以供客户端查询。
依托Registry的功能,Ice服务实现了Service Locator组件,这是一个标准的Ice Object服务对象,
我们可以在自己的服务端调用这个寻址服务,从而解决客户端寻址的问题。
3.Service Locator组件
Service Locator组件实现了两个非常实用和重要的功能:
Ⅰ:自动实现了多种可选择的负载均衡算法,客户端代码无需自己再实现
Ⅱ:服务部署位置和部署数量发送变化之后,客户端无需重启,自动感知和适应
4.启动icegridregistry
4.1配置文件
安装ice后默认在/etc下有个icegridregistry.conf可以参考下
mkdir -p /opt/ice_project/config /opt/ice_project/data/registry
cd /opt/ice_project/config && vim icegridregistry.conf
IceGrid.Registry.Client.Endpoints=tcp -p 4061 -h 192.168.1.25
IceGrid.Registry.Server.Endpoints=tcp
IceGrid.Registry.Internal.Endpoints=tcp
IceGrid.Registry.Data=/opt/ice_project/data/registry
IceGrid.Registry.PermissionsVerifier=IceGrid/NullPermissionsVerifier
IceGrid.Registry.AdminPermissionsVerifier=IceGrid/NullPermissionsVerifier
IceGrid.Registry.DynamicRegistration=1
4.2 启动icegridregistry
nohup icegridregistry --Ice.Config=/opt/ice_project/config/icegridregistry.conf >/dev/null 2>&1 &
5.服务端代码不改变
6.修改icebox的配置,添加
#Ice Registry的协议 地址 端口
Ice.Default.Locator=DemoIceGrid/Locator:tcp -h 192.168.1.25 -p 4061
#配置UserServiceServer的适配器Id
UserServiceServer.AdapterId=UserServiceServerAdapter
7.通过配置服务的AdapterId,我们就可以使用
service@adapterId的间接代理的EndPoint方式来寻址服务了。
8.启动icebox
./startIceServer.sh &
9.开端口4061
sudo firewall-cmd --zone=public --add-port=4061/tcp --permanent
systemctl restart firewalld
10.修改客户端代码调用
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 {
String[] initParams = new String[]{"--Ice.Default.Locator=DemoIceGrid/Locator:tcp -h 192.168.1.25 -p 4061"};
// 初始化通信容器
communicator = Ice.Util.initialize(initParams);
//这里使用service@adapterId的形式寻址
Ice.ObjectPrx op = communicator.stringToProxy("UserServiceServer@UserServiceServerAdapter");
// 检查通用客户端代理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);
}
}
}