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

被折叠的 条评论
为什么被折叠?



