框架中dubbo配置说明:
Provider端配置如下:
<dubbo:protocol name="dubbo" host="${dubbo.host}" port="${dubbo.port}" />
<!--
服务提供者filter,在Provider上尽量多配置Consumer端属性, 配置的覆盖规则:1) 方法级配置别优于接口级别,即小Scope优先 2) Consumer端配置 优于 Provider配置 优于 全局配置
-->
<dubbo:provider retries="0" filter="resFilter,channelFilter" timeout="${dubbo.timeout}" group="${dubbo.group}" />
<dubbo:annotation />
<context:annotation-config />
<!-- 扫描包路径 -->
<context:component-scan base-package="org.go,com.yc" />
重要属性说明
属性
说明
dubbo.host
可以控制dubbo访问的网络权限,设置为空则以内网地址发布服务
dubbo.port
dubbo服务的端口号
dubbo.group
dubbo组名,只有同组的服务才可相互调用
dubbo.timeout
dubbo调用的默认超时时间
Consumer端配置如下
<dubbo:annotation/>
<context:annotation-config/>
<dubbo:consumer retries="0" filter="reqFilter" check="false" timeout="${dubbo.timeout}" group="${dubbo.group}" />
<!-- 扫描包路径 -->
<context:component-scan base-package="org.go,com.yc"/>
重要属性说明
属性
说明
dubbo.group
dubbo组名,只有同组的服务才可相互调用
dubbo.timeout
dubbo调用的默认超时时间
服务消费端Filter
负责MsgId的生产,以及将部分信息注入到RpcContext中去。
@Override
public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
Result result = null;
boolean flag = false;
try {
flag = Context.getMsgId() == null ? flag : true;
Stopwatch started = Stopwatch.createStarted();// 开始计时
RpcContext rpcContext = RpcContext.getContext(); // 获取上下文
Context.initialMsgId();// 初始化msgId
rpcContext.setAttachment(MSG_ID, Context.getMsgId());
String tokenId = setRpcAttachment(rpcContext);// 设置设置传递的RPC参数
result = invoker.invoke(invocation);// 调用RPC接口
String serverIP = RpcContext.getContext().getRemoteHost();// 获取远程RPC地址
Stopwatch stop = started.stop();
logInfo(serverIP, Context.getMsgId(), tokenId, String.valueOf(stop.elapsed(TimeUnit.MILLISECONDS)));
} catch (RpcException rpcException) {
throw rpcException;
} finally {
if (!flag) {
Context.clearAllLocal();
}
}
return result;
}
服务提供端Filter
处理消费端的RpcContext信息,并在业务方法调用完成后进行异常处理,以及输出日志
public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
Result result = null;
try {
// 初始化当前上下文
RpcContext rpcContext = RpcContext.getContext();
String tokenId = rpcContext.getAttachment(TOKENID);
String msgId = rpcContext.getAttachment(MSG_ID);
String clientIP = RpcContext.getContext().getRemoteHost();// 请求IP信息
String application = rpcContext.getUrl().getParameter(APPLICATION); // demo-provider服务提供者名称
String reqUrl = rpcContext.getUrl().getParameter(CLASS_NAME); // 服务响应者的具体类
String version = rpcContext.getUrl().getParameter(VERSION);// 服务提供者给予的版本
Context.initialRpcLocal(rpcContext, msgId);
logInfo(clientIP, msgId, tokenId, application, reqUrl, version);
// 真正的业务方法调用
result = invoker.invoke(invocation);
RPCLogger.info("接口名称为" + RpcContext.getContext().getUrl().getServiceInterface());
RPCLogger.info("方法名称为" + RpcContext.getContext().getMethodName());
if (result.getException() != null && result.getException() instanceof GoException) {
GoException goException = (GoException) result.getException();
Context.getRequestInfo().setMsgCd(goException.getCode());
ResponseDto responseDto = new ResponseDto();
responseDto.setRspCd(goException.getCode());
responseDto.setRspInf(goException.getMessage());
try {
Context.getRequestInfo().setMsgCd(goException.getCode());
Context.getRequestInfo().setMsgInf(goException.getMessage());
Context.getRequestInfo().setResponseData(MAPPER.writeValueAsBytes(responseDto));
} catch (JsonProcessingException e) {
RPCLogger.info("GoException format json was failed");
}
} else {
if (result.getException() != null) {
RPCLogger.info("rpc invoke exception => " + ExceptionUtils.getStackTrace(result.getException()));
}
Object reObj = result.getValue();
if (reObj instanceof ResponseDto) {
ResponseDto rd = (ResponseDto) reObj;
String rspCode = rd.getRspCd();
if (StringUtils.isNotBlank(rspCode)) {
Context.getRequestInfo().setMsgCd(rspCode);
}
} else if (reObj != null) {
Context.getRequestInfo().setMsgCd(SysCode.SUCCESS);
}
try {
Context.getRequestInfo().setResponseData(MAPPER.writeValueAsBytes(reObj));
} catch (JsonProcessingException e) {
RPCLogger.info("Dubbo result format json was failed");
}
}
afterCompletion(clientIP, tokenId);
} catch (RpcException e) {
throw e;
} finally {
Context.clearAllLocal();
}
return result;
}