Dubbo 指定调用固定ip+port dubbo调用指定服务
内容纲要
官方使用方式
坑
dubbo-cluster-specify-address-dubbo3
中的Dubbo版本是3.0.6版,所以项目中使用的Dubbo版本也需要相同- 如果其他映入了其他类型的Dubbo,比如start-dubbo,则需要排除掉
dubbo-cluster-specify-address-dubbo3
的dubbo依赖
扩展使用 nacos+dubbo
NamingService
类可以主动获取到dubbo中注册到的服务,其中就有ip+port,配合dubbo-cluster-specify-address-dubbo3
则可以实现指定nacos中注册成功的dubbo服务发起调用
public class DubboTest {
private final NamingService namingService;
@DubboReference
private DubboUserService dubboUserService;
public DubboTest(NacosDiscoveryProperties nacosDiscoveryProperties, Environment environment) {
NacosServiceManager nacosServiceManager = new NacosServiceManager();
Properties nacosProperties = nacosDiscoveryProperties.getNacosProperties();
String namespace = environment.getProperty("dubbo.registry.parameters.namespace");
nacosProperties.setProperty(PropertyKeyConst.NAMESPACE, namespace);
this.namingService = nacosServiceManager.getNamingService(nacosProperties);
}
public String test() throws NacosException {
List<Instance> allInstances = namingService.getAllInstances("dubbo-admin");
Instance instance = allInstances.get(0);
UserSpecifiedAddressUtil.setAddress(new Address(instance.getIp(), instance.getPort(), true));
List<Long> longs = dubboUserService.searchAllUserIds();
return longs.toString();
}
}
共有 0 条评论