解决Dubbo 2.7.3版本使用ConfigCenterConfig集成Apollo No Provider found的问题
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;
}
}
问题:
- Apollo 找不到 meta。
- 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的问题的更多相关文章
- Myeclipse解决dubbo标签不识别问题
Myeclipse解决dubbo标签不识别问题,引入dubbo.xsd文件,即可:
- 如何解决pytorch 编译时CUDA版本与运行时CUDA版本不对应
转载请注明: 仰望高端玩家的小清新 http://www.cnblogs.com/luruiyuan/ 如何解决pytorch 编译时CUDA版本与运行时CUDA版本不对应 如果pytorch的编译时 ...
- phalcon: 解决php7/phalcon3.2以上版本,不支持oracle数据库的方法
解决php7/phalcon3.2以上版本,不支持oracle数据库的方法 phalcon3.2(3.0以上)版本不支持oracle的方法. https://github.com/phalcon/in ...
- 解决在安装Fiddler4.6版本后,在手机上安装证书出现的问题解决方法
解决在安装Fiddler4.6版本后,在手机上安装证书出现的问题解决方法 设置fiddler抓手机包后,在手机上访问http://ip:port,出现如下问题: 问题:creation of the ...
- 解决VS+opencv中Debug版本与Release版本lib切换的问题
Author: Maddock Date: 2015-03-26 09:34:48 问题来源:http://bbs.csdn.net/topics/390733725 PS: 按照上述方法做的时候,在 ...
- 解决本机安装多版本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 ...
- 使用Docker解决同一服务器运行不同版本PHP方案。
前言: 最近公司有两个站点,分别是两种系统进行二次开发,基于LNMP架构的网站.一般想PHP这种非编译型语言想要对外出售源码都会进行加密,加密方法有很多种,大部分都是使用Zend Guard来进行加密 ...
- 解决python2.7.9以下版本requests访问https的问题
在python2.7.9以下版本requests访问https连接后,总会报一些关于SSL warning. 解决法子可以参考:https://urllib3.readthedocs.io/en/la ...
- 解决WebSocket兼容ie浏览器版本问题
转载请注明出处:http://www.cnblogs.com/Joanna-Yan/p/7942323.html 在使用Netty进行WebSocket开发时,测试发现:ie 11系列个别低版本连接W ...
随机推荐
- 2018年蓝桥杯java b组第二题
2.标题:方格计数 如图p1.png所示,在二维平面上有无数个1x1的小方格. 我们以某个小方格的一个顶点为圆心画一个半径为1000的圆.你能计算出这个圆里有多少个完整的小方格吗? 注意:需要提交的是 ...
- Audio Bit Depth Super-Resolution with Neural Networks
Audio Bit Depth Super-Resolution with Neural Networks 作者:Thomas Liu.Taylor Lundy.William Qi 摘要 Audio ...
- 基于python的scrapy环境搭建
0.1安装python软件 32位机的电脑安装python-3.6.3.exe 64位机的电脑安装python-3.6.3-amd64.exe 0.1.1 python环境搭建 执行安装程序 选择Ad ...
- github- 优秀资源总结
权限控制篇: SpringMVC-Mybatis-Shiro-redis-0.2-master:https://www.sojson.com/shiro / https://m.imooc.com/a ...
- java8 两个时间比较
比如在15:30:30之前: LocalTime.now().isBefore(LocalTime.of(15, 30,30)) 或15:30:30之后 LocalTime.now().isAfter ...
- 在创建activiti5..22所需的25张表时 ,所用的方法和遇到的问题。
最近在学习关于activiti流程设计的相关内容,首先第一步就需要了解25张activiti相关的表,具体的每张表的含义 请自行百度. 这里讲一下 用java代码生成所需要的25张表,很简单: pub ...
- C++——多文件结构和编译预处理命令
[toc] 一.多文件结构 1.一个工程可以划分为多个源文件 类声明文件(.h文件) 类实现文件(.cpp文件) 类的使用文件(main函数所在的.cpp文件) 2.利用工程来组合各个文件 //Poi ...
- git clone 解决Permission Denied (publickey)问题
本地git bash 使用git clone git@github.com:***.git方式下载github代码至本地时需要依赖ssh key,遇到权限不足问题时一般都是SSH key失效或者SSH ...
- ping通谷歌后发送QQ邮件通知
前言 国庆期间,据说是为了防止有人在重大节日发表不正当言论,很多可以kxsw的ip都被封了,可是什么时候才会解封呢,不能没事就去ping一下吧,所以我写了个定时任务,定时ping谷歌服务器,如果p ...
- mysql库复制
一.使用navicate复制mysql库 二.使用命令 通过命令:1.创建新数据库CREATE DATABASE `newdb` DEFAULT CHARACTER SET UTF8 COLLATE ...