Wang
Clay
Dubbo 指定调用固定ip+port dubbo调用指定服务 Clay
    欢迎来到唐僧洗头艹飘柔的博客!

Dubbo 指定调用固定ip+port dubbo调用指定服务

Java odliken 2周前 (11-19) 7次浏览 0个评论 扫描二维码

官方使用方式

Dubbo动态指定 IP 调用

  • 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)
[彳亍]
分享 (0)
发表我的评论
取消评论
表情 贴图 加粗 删除线 居中 斜体 签到