目前公司需要对当前的业务进行微服务化改造,经过讨论选择使用用dubbo进行服务治理,并在我的业务功能模块中进行试点,由于之前写过dubbo相关的demo,基础知识就不再重复了,demo地址:
https://github.com/liujifei/dubbo.git
工程的基本搭建参照了另一篇博客,写的十分详尽,推荐一下:
Maven多模块,Dubbo分布式服务框架,SpringMVC,前后端分离项目,基础搭建,搭建过程出现的问题
========================================================================================================================
基本配置完成之后,当使用组播的方式时没有发现问题,服务提供方和调用方都正常,当使用zk集群作为注册中心时,项目启动报错:
java.lang.NoClassDefFoundError: org/apache/curator/shaded/com/google/common/cache/CacheBuilder
处理方法:
在parent的pom.xml中dependencyManagement加入
子module的pom.xml中加入
问题解决。
另附zk集群配置方法:
<
dubbo:registry
address
=
"zookeeper://10.20.153.10:2181?backup=10.20.153.11:2181,10.20.153.12:2181"
/>
或
<
dubbo:registry
protocol
=
"zookeeper"
address
=
"10.20.153.10:2181,10.20.153.11:2181,10.20.153.12:2181"
/>
同中心多组的配置方法:
<
dubbo:registry
id
=
"chinaRegistry"
protocol
=
"zookeeper"
address
=
"10.20.153.10:2181"
group
=
"china"
/>
<
dubbo:registry
id
=
"intlRegistry"
protocol
=
"zookeeper"
address
=
"10.20.153.10:2181"
group
=
"intl"
/>
若遇到其他问题再记录
2018-04-12 11:05:16
====================================================================================
上面的provider搞定了,之后就是consumer。之前的调用方式是简单的sdk调用,服务化之后,需要将原web工程改造为dubbo调用方。
具体改造内容很简单主要包括两个文件:
1、pom.xml
<dubbo.version>2.6.1</dubbo.version>
另外添加服务提供方api的依赖
2、spring-context.xml
beans的namespace添加 xmlns:dubbo="http://code.alibabatech.com/schema/dubbo";xsi:schemaLocation中添加
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd文件内容中添加
<dubbo:application name="***" />
<dubbo:registry address="***" />
<dubbo:reference id="***" check="false" interface="***"/>
回调:
当provider需要调用consumer中的回调方法时,首先需要在provider的配置文件中修改如下:
<dubbo:service interface="com.***.quality.api.QualityService" ref="qualityService" protocol="dubbo">
<dubbo:method name="excuteJob" >
<dubbo:argument index="1" callback="true" type="com.inspur.quality.api.CallBack"/>
另外需要注意的是,callback作为调用方实现的回调方法,其中不要包含需要dubbo来序列化的对象以及调用其方法,否则会报如下错误:
com.alibaba.dubbo.remoting.RemotingException: Fail to decode request due to: RpcInvocation
如:
qualityService.excuteJob(job);
// 生产报告
qualityService.productReport(new CallBack() {
@Override
public void productReport(String dataBaseName, RuleResult[] result, String jobId, String instanceId) {
Long time = System.currentTimeMillis();
for(RuleResult single : result){
StringBuilder sqlB = new StringBuilder();
sqlB.append("insert into ");
sqlB.append(RESULT_TABLE);
sqlB.append(" (RESULTID,JOBID,INSTANCEID,CHECKID,RESULT) VALUES ( '");
sqlB.append(UUID.randomUUID().toString());
sqlB.append("','");
sqlB.append(jobId);
sqlB.append("','");
sqlB.append(instanceId);
sqlB.append("','");
sqlB.append(single.getRule().getRuleName());
sqlB.append("','");
sqlB.append(single.getResultNumber());
sqlB.append("')");
JdbcUtil.exeSql(dataBaseName, sqlB.toString());
}
}
}, dataSources[2]);
View Code
需要改成
qualityService.excuteJob(job, new CallBack() {
/**
* @Field @serialVersionUID : TODO
*/
private static final long serialVersionUID = -5914504806295102148L;
@Override
public String productReport(String dataBaseName, RuleResult[] result, String jobId, String instanceId) {
Long time = System.currentTimeMillis();
StringBuilder sqlB = new StringBuilder();
for(RuleResult single : result){
sqlB.append("insert into ");
sqlB.append("QUALITY_RESULT");
sqlB.append(" (RESULTID,JOBID,INSTANCEID,CHECKID,RESULT) VALUES ( '");
sqlB.append(UUID.randomUUID().toString());
sqlB.append("','");
sqlB.append(jobId);
sqlB.append("','");
sqlB.append(instanceId);
sqlB.append("','");
sqlB.append(single.getRule().getRuleName());
sqlB.append("','");
sqlB.append(single.getResultNumber());
sqlB.append("')");
}
return sqlB.toString();
}
}, dataSources[2]);
View Code
2018-04-18 15:14:13
=====================================================================================
java.lang.NoSuchMethodError: org.apache.curator.utils.ZKPaths.fixForNamespace
问题原因:curator-framework版本为2.11.0与原项目中的包有冲突,改为原项目中的版本2.6.0后,问题解决。
2018-04-23 15:46:31
=====================================================================================
代码的改造进行完之后,调用时发现报错如下:
服务提供方报错:
Caused by: com.alibaba.dubbo.remoting.RemotingException: com.alibaba.com.caucho.hessian.io.HessianProtocolException: 'com.***.quality.rule.MultiCountRule' could not be instantiated
com.alibaba.com.caucho.hessian.io.HessianProtocolException: 'com.***.quality.rule.MultiCountRule' could not be instantiated
Caused by: java.lang.reflect.InvocationTargetException
Caused by: java.lang.NullPointerException
at com.***.quality.rule.MultiCountRule.
服务调用方报错:
com.alibaba.dubbo.rpc.RpcException: Failed to invoke the method excuteJob in the service com.***.quality.api.QualityService. Tried 3 times of the providers [192.168.138.1:20880] (1/1) from the registry 172.22.5.2:2181 on the consumer 192.168.138.1 using the dubbo version 2.6.1. Last error is: Failed to invoke remote method: excuteJob, provider: dubbo://192.168.138.1:20880/com.***.quality.api.QualityService?anyhost=true&application=crawlManage&check=false&dubbo=2.6.1&excuteJob.1.callback=true&generic=false&interface=com.***.quality.api.QualityService&methods=newExpectCountRule,registDataSource,newNotNullRule,newMultiCountRule,newPatternRule,newUniqueRule,excuteJob,newIntegrityRule,newDataCountRule&pid=115308®ister.ip=192.168.138.1&remote.timestamp=1524468008733&revision=0.0.1-SNAPSHOT&side=consumer×tamp=1524468416127, cause: Fail to decode request due to: RpcInvocation [methodName=excuteJob, parameterTypes=[class com.***.quality.common.QualityCheckJob, interface com.***.quality.api.CallBack, class java.lang.String], arguments=null, attachments={path=com.***.quality.api.QualityService, input=3355, dubbo=2.6.1, version=0.0.0}]
com.alibaba.dubbo.rpc.RpcException: Failed to invoke the method newMultiCountRule in the service com.***.quality.api.QualityService. Tried 3 times of the providers ......
Caused by: com.alibaba.dubbo.remoting.RemotingException: Fail to decode request due to: RpcInvocation [methodName=excuteJob, parameterTypes=[class com.***.quality.common.QualityCheckJob, interface com.***.quality.api.CallBack, class java.lang.String], arguments=null, attachments={path=com.***.quality.api.QualityService, input=3355, dubbo=2.6.1, version=0.0.0}]
问题是由于参数excuteJob中的属性MultiCountRule的类中没有默认构造方法,导致hessian不能正确序列化。
qualityService.productReport(new CallBack() {
/** * @Field @serialVersionUID : TODO */ private static final long serialVersionUID = -5914504806295102148L;
@Override public void productReport(String dataBaseName, RuleResult[] result, String jobId, String instanceId) { Long time = System.currentTimeMillis(); for(RuleResult single : result){ StringBuilder sqlB = new StringBuilder(); sqlB.append("insert into "); sqlB.append("QUALITY_RESULT"); sqlB.append(" (RESULTID,JOBID,INSTANCEID,CHECKID,RESULT) VALUES ( '"); sqlB.append(UUID.randomUUID().toString()); sqlB.append("','"); sqlB.append(jobId); sqlB.append("','"); sqlB.append(instanceId); sqlB.append("','"); sqlB.append(single.getRule().getRuleName()); sqlB.append("','"); sqlB.append(single.getResultNumber()); sqlB.append("')"); JdbcUtil.exeSql(dataBaseName, sqlB.toString()); } } }, dataSources[2]);