Dubbo 2.7.3 集成Apollo

问题描述

Dubbo 2.7.3支持配置中心外部化配置, 因此只需要定义一个ConfigCenterConfig的Bean。

@EnableDubbo(scanBasePackages = {"com.slankka.cloud.dubbo"})
@Configuration
public class DubboConfig {
@Bean
public ConfigCenterConfig configCenterConfig() {
ConfigCenterConfig configCenterConfig = new ConfigCenterConfig();
configCenterConfig.setAddress("apollo.xxxxx.com:8080");
configCenterConfig.setProtocol("apollo");
configCenterConfig.setNamespace("dubbo");
configCenterConfig.setGroup(null);
return configCenterConfig;
}
}

问题:

  1. Apollo 找不到 meta。
  2. Dubbo 找不到 provider

解决方案

1. Apollo 找不到meta

Apollo的jar 的apollo-core的配置文件明明声明了PRO.meta="apollo.xxxxx.com:8080"。

这个问题出现在

apollo.bootstrap.enabled = false

如果要坚持这样配置,需要增加

apollo.meta=apollo.xxxxx.com:8080

更新:此问题还有更深入的分析Apollo报错找不到apollo.meta的问题解决方案

2. Dubbo 找不到Provider

仔细看日志 Interface: com.xxx.xxx.service,如果后面没有跟着版本号例如: 1.2.0,则说明版本没有定义。

问题是因为定义了占位符,而Dubbo启动的时候,创建ReferenceBean的类是个BeanPostProcessor,启动比较早,而apollo.bootstrap.enabled=false。

则Dubbo创建这个IRExecutionService对应的Bean类的时候,找不到version,但是他catch吃掉异常了。等于没有配置version。

apollo.bootstrap.enabled = false

@Reference(version = "${job.service.version}", retries = 0, lazy = true)
private IRExecutionService executionService;

则原因是Dubbo不能从ConfigCenterConfig读取版本配置,或者太迟了,如果要解决很简单 ,但是太依赖Apollo提前初始化开关。

如果坚持要apollo.bootstrap.enabled = false,强制使用Dubbo自行处理这个变量的解析怎么办?

package io.github.slankka.dubbo-apollo.server.config;

import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.config.Configuration;
import org.apache.dubbo.common.config.Environment;
import org.apache.dubbo.common.extension.ExtensionLoader;
import org.apache.dubbo.common.utils.StringUtils;
import org.apache.dubbo.config.ApplicationConfig;
import org.apache.dubbo.config.ConfigCenterConfig;
import org.apache.dubbo.config.spring.beans.factory.annotation.ReferenceAnnotationBeanPostProcessor;
import org.apache.dubbo.configcenter.DynamicConfiguration;
import org.apache.dubbo.configcenter.DynamicConfigurationFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.InjectionMetadata;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.bind.PropertySourcesPlaceholdersResolver;
import org.springframework.context.ApplicationContext;
import org.springframework.core.annotation.AnnotationAttributes;
import org.springframework.core.env.PropertySource;
import org.springframework.stereotype.Component; import java.io.IOException;
import java.util.ArrayList;
import java.util.List; import static org.apache.dubbo.common.config.ConfigurationUtils.parseProperties; /**
* Project: dubbo-apollo
*
* @author slankka on 2019/8/29.
*/
@ConditionalOnProperty(name = "apollo.bootstrap.enabled", havingValue = "false", matchIfMissing = true)
@Component(value = ReferenceAnnotationBeanPostProcessor.BEAN_NAME)
public class ReferencedAnnotationPatch extends ReferenceAnnotationBeanPostProcessor { private ApplicationContext myContext; @Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
myContext = applicationContext;
super.setApplicationContext(applicationContext);
} @Override
protected Object doGetInjectedBean(AnnotationAttributes attributes, Object bean, String beanName,
Class<?> injectedType,
InjectionMetadata.InjectedElement injectedElement) throws Exception {
eagerInitConfigCenter();
Configuration configuration = Environment.getInstance().getConfiguration(); List<PropertySource<?>> propertySources = new ArrayList<>();
propertySources.add(new PropertySource<Configuration>("dubboConfigCenter", configuration) {
@Override
public Object getProperty(String name) {
return configuration.getProperty(name);
}
});
PropertySourcesPlaceholdersResolver propertySourcesPlaceholdersResolver = new PropertySourcesPlaceholdersResolver(propertySources); for (String attribute : attributes.keySet()) {
Object stringAttr = attributes.get(attribute);
if (stringAttr instanceof String) {
Object value = propertySourcesPlaceholdersResolver.resolvePlaceholders(attributes.getString(attribute));
attributes.put(attribute, value);
}
} return super.doGetInjectedBean(attributes, bean, beanName, injectedType, injectedElement);
} private void eagerInitConfigCenter() {
ConfigCenterConfig configCenter = myContext.getBean(ConfigCenterConfig.class);
if (configCenter.isValid()) {
if (configCenter.checkOrUpdateInited()) {
configCenter.refresh(); URL url = configCenter.toUrl();
DynamicConfigurationFactory factories = ExtensionLoader
.getExtensionLoader(DynamicConfigurationFactory.class)
.getExtension(url.getProtocol());
DynamicConfiguration dynamicConfiguration = factories.getDynamicConfiguration(url);
String configContent = dynamicConfiguration.getProperties(configCenter.getConfigFile(), configCenter.getGroup()); ApplicationConfig application = myContext.getBean(ApplicationConfig.class);
String appGroup = application.getName();
String appConfigContent = null;
if (StringUtils.isNotEmpty(appGroup)) {
appConfigContent = dynamicConfiguration.getProperties
(StringUtils.isNotEmpty(configCenter.getAppConfigFile()) ? configCenter.getAppConfigFile() : configCenter.getConfigFile(),
appGroup
);
}
try {
Environment.getInstance().setConfigCenterFirst(configCenter.isHighestPriority());
Environment.getInstance().updateExternalConfigurationMap(parseProperties(configContent));
Environment.getInstance().updateAppExternalConfigurationMap(parseProperties(appConfigContent));
} catch (IOException e) {
throw new IllegalStateException("Failed to parse configurations from Config Center.", e);
}
}
}
}
}

则能纠正Dubbo 的ReferenceAnnotationBeanPostProcessor 行为,因为这个时候,已经有ConfigCenterConfig这个Bean了,所以让ConfigCenter提前启动,从而使得@Reference注解的占位符能够被解析。

注意,这个占位符是配置在Namespace("dubbo");内的。

最后一个小问题

Dubbo 会默认读取 dubbo这个 Apollo的namespace,如果用自定义的namespace,他也会读取,因为不存在而启动减慢,所以为了加快启动速度,建议创建Apollo的 一个空dubbo的namespace。

解决Dubbo 2.7.3版本使用ConfigCenterConfig集成Apollo No Provider found的问题的更多相关文章

  1. Myeclipse解决dubbo标签不识别问题

    Myeclipse解决dubbo标签不识别问题,引入dubbo.xsd文件,即可:              

  2. 如何解决pytorch 编译时CUDA版本与运行时CUDA版本不对应

    转载请注明: 仰望高端玩家的小清新 http://www.cnblogs.com/luruiyuan/ 如何解决pytorch 编译时CUDA版本与运行时CUDA版本不对应 如果pytorch的编译时 ...

  3. phalcon: 解决php7/phalcon3.2以上版本,不支持oracle数据库的方法

    解决php7/phalcon3.2以上版本,不支持oracle数据库的方法 phalcon3.2(3.0以上)版本不支持oracle的方法. https://github.com/phalcon/in ...

  4. 解决在安装Fiddler4.6版本后,在手机上安装证书出现的问题解决方法

    解决在安装Fiddler4.6版本后,在手机上安装证书出现的问题解决方法 设置fiddler抓手机包后,在手机上访问http://ip:port,出现如下问题: 问题:creation of the ...

  5. 解决VS+opencv中Debug版本与Release版本lib切换的问题

    Author: Maddock Date: 2015-03-26 09:34:48 问题来源:http://bbs.csdn.net/topics/390733725 PS: 按照上述方法做的时候,在 ...

  6. 解决本机安装多版本jdk导致The type java.lang.Object cannot be resolved It is indirectly referenced ...

    本机开始安装了jdk1.6,然后安装了jdk1.8 当在调自动化的时候,发现传入函数传参String类型,报错The type java.lang.Object cannot be resolved ...

  7. 使用Docker解决同一服务器运行不同版本PHP方案。

    前言: 最近公司有两个站点,分别是两种系统进行二次开发,基于LNMP架构的网站.一般想PHP这种非编译型语言想要对外出售源码都会进行加密,加密方法有很多种,大部分都是使用Zend Guard来进行加密 ...

  8. 解决python2.7.9以下版本requests访问https的问题

    在python2.7.9以下版本requests访问https连接后,总会报一些关于SSL warning. 解决法子可以参考:https://urllib3.readthedocs.io/en/la ...

  9. 解决WebSocket兼容ie浏览器版本问题

    转载请注明出处:http://www.cnblogs.com/Joanna-Yan/p/7942323.html 在使用Netty进行WebSocket开发时,测试发现:ie 11系列个别低版本连接W ...

随机推荐

  1. easyui-datetimebox 控件绑定双击事件实现自动选中当前日期时间

    本方法是在不改变原 js 的情况下,通过扩展方法来实现本目的 首先在 datetimebox 控件中扩展一个 绑定双击事件 的方法 $.extend($.fn.datetimebox.methods, ...

  2. cp -rf 操作时依然会提示覆盖

    在linux上经常会使用cp -rf命令,该命令就是强制覆盖指定目录下的文件,但有时使用该命令依然会提示是否覆盖,cp命令的参数有如下一些: 参数说明: -a:此选项通常在复制目录时使用,它保留链接. ...

  3. IDEA 导入 Maven 项目后报错 cannot resolve symbol 解决办法

    这两天整理项目,导入新的 Maven 项目时出现 cannot resolve symbol ,即使 rebuild 也没有用.解决办法如下: 1. File -> Close Project: ...

  4. Spring Boot 整合视图层技术

    这一节我们主要学习如何整合视图层技术: Jsp Freemarker Thymeleaf 在之前的案例中,我们都是通过 @RestController 来处理请求,所以返回的内容为json对象.那么如 ...

  5. Java 从入门到进阶之路(七)

    在之前的文章中我们介绍了一下 java 中的对象和类,接下来我们来看一下 Java 中的方法重载. 在显示生活中,我们肯定会遇到这样一个问题,那就是我们再商场买东西的时候在付账时的选择.如下 A:在收 ...

  6. WebGL简易教程(十):光照

    目录 1. 概述 2. 原理 2.1. 光源类型 2.2. 反射类型 2.2.1. 环境反射(enviroment/ambient reflection) 2.2.2. 漫反射(diffuse ref ...

  7. JDK-基于Windows环境搭建

    JDK安装: 毋庸置疑你要跑java程序,肯定少不了JDK,如jemter还有还有~ 下载jdk地址1:https://pan.baidu.com/s/1FIvGNvZSy0EpCBxHCz07nA  ...

  8. BZOJ 2535: [Noi2010]Plane 航空管制2

    Description 世博期间,上海的航空客运量大大超过了平时,随之而来的航空管制也频频发生.最近,小X就因为航空管制,连续两次在机场被延误超过了两小时.对此,小X表示很不满意. 在这次来烟台的路上 ...

  9. HDU 1428漫步校园

    漫步校园 Problem Description LL最近沉迷于AC不能自拔,每天寝室.机房两点一线.由于长时间坐在电脑边,缺乏运动.他决定充分利用每次从寝室到机房的时间,在校园里散散步.整个HDU校 ...

  10. requests模块(代理)篇

    - 用户验证 - 代理验证 #可能需要使用HTTP basic Auth, 可以这样 # 格式为 用户名:密码@代理地址:端口地址 proxy = { "http": " ...