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