栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

自定义 实现类

自定义 实现类

自定义实现类几种方式
  • dubbo 实现
    • 扩展包 ext

dubbo 实现
private ThirdApiService initDubboService() throws Exception {
        ApplicationConfig applicationConfig = new ApplicationConfig();
        applicationConfig.setName(applicationContext.getId());
        RegistryConfig registry = getRegistryConfig();
        ReferenceConfig referenceConfig = new ReferenceConfig<>();
        referenceConfig.setInterface(ThirdMeetingApiService.class);
        if(StringUtils.isNotBlank(dubboGroup)){
            referenceConfig.setGroup(dubboGroup);
        }
        referenceConfig.setRegistry(registry);
        referenceConfig.setApplication(applicationConfig);
        //ReferenceConfig实例很重,封装了与注册中心的连接以及与提供者的连接,
        //需要缓存,否则重复生成ReferenceConfig可能造成性能问题并且会有内存和连接泄漏。
        //API方式编程时,容易忽略此问题。
        //这里使用dubbo内置的简单缓存工具类进行缓存

        ReferenceConfigCache cache = ReferenceConfigCache.getCache();
        ThirdApiService genericService = cache.get(referenceConfig);
        // 用com.alibaba.dubbo.rpc.service.GenericService可以替代所有接口引用
        if (genericService == null) {
            genericService = referenceConfig.get();
        }
        return genericService;
    }
public interface ThirdApiService  {
}
接入代码

maven引入依赖:

        
            com.meicloud.mp
            vmm-service-third-interface
            1.2.0
        
实现dubbo接口:com.meicloud.mp.vmm.third.service.ThirdApiService

@Service("customThirdApiService")
public class CustomThirdService implements ThirdApiService {

//TODO 具体接入实现

}
配置dubbo接口




扩展包 ext
@Configuration
@Slf4j
public class ThirdApiServiceConfiguration {

    private static final String CUSTOM_EXTRENSION_SERVICE = "custom";

    @Value("${common.vmm.thirdapi.service:com.meicloud.mp.ne.meeting.service.NeMeetingService}")
    private String thirdApiServiceClassName;

    @Autowired
    private ApplicationContext applicationContext;

    @Value("${common.zookeeper.hosts}")
    private String zookeeperAddress;

    @Value("${dubbo.registry.protocol}")
    private String zookeeperProtocol;

    @Value("${vmm.dubbo.timeout.seconds:60000}")
    private Integer vmmDubboTimeout;

    @Value("${vmm.dubbo.group:}")
    private String dubboGroup;

    private boolean isCustom() {
        return Objects.equals(CUSTOM_EXTRENSION_SERVICE, thirdApiServiceClassName);
    }

    private RegistryConfig getRegistryConfig() {
        RegistryConfig registry = new RegistryConfig();
        registry.setAddress(zookeeperAddress);
        registry.setProtocol(zookeeperProtocol);
        return registry;
    }

    @Bean
    public ThirdMeetingApiService thirdMeetingApiService() {
        log.info("load thirdApiServiceClassName:{} ", thirdApiServiceClassName);
        if (StringUtils.isBlank(thirdApiServiceClassName)) {
            throw new baseException(VmmErrorCode.MEETING_THIRD_CONFIG_ERROR, "common.vmm.thirdapi.service配置不能为空");
        }
        if (isCustom()) {
            try {
                return initDubboService();
            } catch (Exception e) {
                throw new baseException(VmmErrorCode.MEETING_THIRD_CONFIG_ERROR, "common.vmm.thirdapi.service配置自定义扩展初始化错误,无法实例化,msg:" + e.getMessage(), e);
            }
        }
        try {
            Class clazz = Class.forName(thirdApiServiceClassName);
            return (ThirdMeetingApiService) clazz.getDeclaredConstructor().newInstance();
        } catch (ClassNotFoundException e) {
            throw new baseException(VmmErrorCode.MEETING_THIRD_CONFIG_ERROR, "common.vmm.thirdapi.service配置错误,class[" + thirdApiServiceClassName + "]不存在", e);
        } catch (NoSuchMethodException | InvocationTargetException e) {
            throw new baseException(VmmErrorCode.MEETING_THIRD_CONFIG_ERROR, "common.vmm.thirdapi.service配置错误,class[" + thirdApiServiceClassName + "]需要无参public构造方法", e);
        } catch (IllegalAccessException | InstantiationException e) {
            throw new baseException(VmmErrorCode.MEETING_THIRD_CONFIG_ERROR, "common.vmm.thirdapi.service配置错误,class[" + thirdApiServiceClassName + "]无法实例化", e);
        }
    }

    private ThirdMeetingApiService initDubboService() throws Exception {
        ApplicationConfig applicationConfig = new ApplicationConfig();
        applicationConfig.setName(applicationContext.getId());
        RegistryConfig registry = getRegistryConfig();
        ReferenceConfig referenceConfig = new ReferenceConfig<>();
        referenceConfig.setInterface(ThirdMeetingApiService.class);
        if(StringUtils.isNotBlank(dubboGroup)){
            referenceConfig.setGroup(dubboGroup);
        }
        referenceConfig.setRegistry(registry);
        referenceConfig.setApplication(applicationConfig);
        //ReferenceConfig实例很重,封装了与注册中心的连接以及与提供者的连接,
        //需要缓存,否则重复生成ReferenceConfig可能造成性能问题并且会有内存和连接泄漏。
        //API方式编程时,容易忽略此问题。
        //这里使用dubbo内置的简单缓存工具类进行缓存

        ReferenceConfigCache cache = ReferenceConfigCache.getCache();
        ThirdMeetingApiService genericService = cache.get(referenceConfig);
        // 用com.alibaba.dubbo.rpc.service.GenericService可以替代所有接口引用
        if (genericService == null) {
            genericService = referenceConfig.get();
        }
        return genericService;
    }
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/605066.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号