记录一次dubbo 接入 nacos 死活报 user not found

文章介绍了DubboUtils类中如何使用ApacheDubbo框架获取服务实例,包括设置版本、tag以及连接到不同的注册中心。方法中涉及到了服务查找、消费配置和注册中心管理。

package com.nwei.hangame.utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.ApplicationConfig;
import org.apache.dubbo.config.ConsumerConfig;
import org.apache.dubbo.config.ReferenceConfig;
import org.apache.dubbo.config.RegistryConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.budwk.app.player.providers.IPlayerUserProvider;

public class DubboUtils {
    private static final Logger logger = LoggerFactory.getLogger(DubboUtils.class);
    public static final Map<String, Object> serviceMap = new HashMap<>();

    /**
     * 取得Dubbo bean
     * 
     * @param clazz
     * @param version
     * @param tag     可以不传 不传的话取system环境变量
     * @param <T>
     * @return
     */
    public static <T> T getService(Class<T> clazz, String version, String... tag) {
        if (clazz == null || StringUtils.isBlank(version)) {
            throw new IllegalArgumentException("DubboUtils#getService():参数不能为空.");
        }
        String key = clazz.getName() + ":" + version;
        Object objService = serviceMap.get(key);
        if (objService != null && !StringUtils.equals(objService.getClass().getName(), clazz.getName())) {
            throw new IllegalArgumentException(
                    "DubboUtils#getService():返回值类型不符.期望:" + clazz.getName() + ",实际:" + objService.getClass().getName());
        }
        if (serviceMap.containsKey(key)) {
            return (T) objService;
        }

        ReferenceConfig reference = createReferenceConfig();

        // 设置tag:用于灰度发布、线上程序调试
        String tagEnv = getTag(tag);
        if (StringUtils.isNotBlank(tagEnv)) {
            ConsumerConfig consumerConfig = new ConsumerConfig();
            consumerConfig.setId(tagEnv);
            reference.setConsumer(consumerConfig);
        }

        reference.setVersion(version);
        // 弱类型接口名
        reference.setInterface(clazz);
        // 声明为泛化接口
//        reference.setGeneric("fasle");

        objService = reference.get();
        serviceMap.put(key, objService);
        return (T) objService;
    }

    private static ReferenceConfig createReferenceConfig() {
        RegistryConfig registry1 = new RegistryConfig();
        registry1.setAddress("nacos://127.0.0.1:8848?namespace=dev&contextPath=/nacos&username=test&password=123");
//        registry1.setUsername("test");
//        registry1.setPassword("123");
//        registry1.setProtocol("dubbo");
//        registry1.setGroup("DEFAULT_GROUP");

        // 普通编码配置方式
        ApplicationConfig application = new ApplicationConfig();
        // 客户端应用名:可以任意取名
        application.setName("consumer11");
        application.setLogger("slf4j");
        application.setOwner("dongshidaddy");
        application.setRegistry(registry1);

        // 连接注册中心配置
        List<RegistryConfig> registries = new ArrayList<>();
        registries.add(registry1);

        System.out.println(registry1);
//        RegistryConfig registry2 = new RegistryConfig();
//        registry2.setAddress("zookeeper://vr-zk-2.189read.com:2181");
//        registries.add(registry2);

        // 引用远程服务
        // 该实例很重量,里面封装了所有与注册中心及服务提供方连接,请缓存
        ReferenceConfig reference = new ReferenceConfig();
        reference.setApplication(application);
        reference.setRegistries(registries);
        reference.setCheck(false);
        return reference;
    }

    public static void main(String[] args) {
        IPlayerUserProvider p=getService(IPlayerUserProvider.class, "1.0.0");
        System.out.println(p);
    }

    private static String getTag(String... tag) {
        String tagEnv = System.getProperty("dubbo.provider.tag");
        if (tag != null && tag.length > 0 && StringUtils.isNotBlank(tag[0])) {
            tagEnv = tag[0];
        }
        return tagEnv;
    }
}
 

最后确认是pom里的包是老的。。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值