1、代理类
public interface IUserCenterOperation
{
UserCenterResponse<BaseUser> findUserByid(Long userId);
}
2、实现
import org.springframework.beans.factory.annotation.Autowired;
import com.alibaba.dubbo.config.annotation.Service;
import com.zto.webUserCenter_userOpera.dto.UserCenterResponse;
import com.zto.webUserCenter_userOpera.dto.BaseUser;
import com.zto.webUserCenter_userOpera.manager.FindUserById;
import com.zto.webUserCenter_userOpera.service.IUserCenterOperation;
@Service(group = "userCenterOperation", timeout = 1000, version = "1.0",filter="dubboServiceFilter")
public class UserCenterOperation implements IUserCenterOperation
{
@Autowired
private FindUserById findUserById;
@Override
public UserCenterResponse<BaseUser> findUserByid(Long userId)
{
UserCenterResponse<BaseUser> res = null;
res = findUserById.exce(userId);
// 业务实现,可替换
return res;
}
}
3、filter
package com.zto.webUserCenter_userOpera.aop;
import com.alibaba.dubbo.rpc.*;
import com.alibaba.fastjson.JSON;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class DubboServiceFilter implements Filter {
private final static Logger logger = LoggerFactory.getLogger(DubboServiceFilter.class);
@Override
public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
Result result = null;
try {
result = invoker.invoke(invocation);
if (result.getException() instanceof Exception)
{
throw new Exception(result.getException());
}
} catch (Exception e) {
logger.error("Exception:{},request{},curr error:{},msg:{}", invocation.getClass(),
JSON.toJSONString(invocation.getArguments()), e.toString(), ExceptionUtils.getRootCause(e));
return result;
} finally {
logger.info("method:[{}],request:{}",
invocation.getMethodName(),JSON.toJSONString(invocation.getArguments()));
}
return result;
}
}
4、在resources中创建文件
META-INF/dubbo/com.alibaba.dubbo.rpc.Filter
内容:dubboServiceFilter=com.zto.webUserCenter_userOpera.aop.DubboServiceFilter
5、application.properties 配置 添加
dubbo.provider.filter =dubboServiceFilter
6、在dubbo-provider.xml添加配置
<dubbo:service interface="com.zto.webUserCenter_userOpera.service.IUserCenterOperation" ref="demoService" filter="dubboServiceFilter" timeout = 1000 version = "1.0"/>
原文链接:https://blog.csdn.net/wangqingqi20005/article/details/78672578