直接上代码,服务提供方
package com.weifor.xx.api.dubbo;
import com.weifor.util.WeiFor;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.common.constants.CommonConstants;
import org.apache.dubbo.common.extension.Activate;
import org.apache.dubbo.rpc.*;
import java.util.concurrent.CompletableFuture;
/**
*
* Dubbo过滤器,执行者
*
* Copyright http://www.weifor.com/
* Author PangShuqiang
* Blog https://blog.csdn.net/PangSir
* Email pangshuqiang@163.com
* QQ 127015919
* Date: 2021/6/25
*/
@Slf4j
@Activate(group = CommonConstants.PROVIDER)
public class DubboProviderFilter implements Filter {
@Override
public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
String appName = RpcContext.getContext().getAttachment(WebConstEnum.Model.SERVER_NAME);
String currentPath = invoker.getUrl().getPath().concat("." + invocation.getMethodName());
log.info("调用Dubbo服务,项目名称:{}", appName);
if (true) {
RpcContext.getContext().getAttachment("appName");
return invoker.invoke(invocation);
} else {
log.error("调用Dubbo服务失败!Path:".concat(currentPath));
CompletableFuture b = new CompletableFuture ();
AsyncRpcResult asyncRpcResult = new AsyncRpcResult(b,invocation);
asyncRpcResult.setValue("Provider 服务失败,Dubbo Provider 停止执行!Path:" + currentPath);
return asyncRpcResult;
}
}
}
package com.weifor.xx.api.dubbo;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.common.constants.CommonConstants;
import org.apache.dubbo.common.extension.Activate;
import org.apache.dubbo.rpc.*;
import java.util.Arrays;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
/**
*
* Dubbo过滤器,调用者
*
* Copyright http://www.weifor.com/
* Author PangShuqiang
* Blog https://blog.csdn.net/PangSir
* Email pangshuqiang@163.com
* QQ 127015919
* Date: 2021/6/25
*/
@Slf4j
@Activate(group = CommonConstants.CONSUMER)
public class DubboConsumerFilter implements Filter {
@Override
public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
// Result result = invoker.invoke(invocation);
if (invoker.getUrl() == null) {
log.error("调用Dubbo服务时,准备动作处理失败!");
throw new RpcException("调用Dubbo服务时,准备动作处理失败!");
}
Class<?> serviceType = invoker.getInterface();
Map attachments = invocation.getAttachments();
log.info("[{}], [{}], [{}]", invoker.getInterface(), invocation.getMethodName(), Arrays.toString(invocation.getArguments()));
String currentPath = invoker.getUrl().getPath().concat("." + invocation.getMethodName());
if (true) {
RpcContext.getContext().setAttachment("appName", "这个是你给我的!");
return invoker.invoke(invocation);
} else {
log.error("调用Dubbo服务时失败!Path:".concat(currentPath));
CompletableFuture b = new CompletableFuture ();
AsyncRpcResult asyncRpcResult = new AsyncRpcResult(b,invocation);
asyncRpcResult.setValue("Consumer 服务失败,Dubbo Consumer 停止调用!Path:" + currentPath);
return asyncRpcResult;
}
}
}
关键配置,官方说明:https://dubbo.apache.org/docs/v2.7/dev/impls/filter/#m-docsv27devimplsfilter
无论是SpringCloud还是SpringBoot,均可按照Spring方式进行配置,具体可在项目Resource下执行:
1、建文件夹及文件:META-INF/dubbo/org.apache.dubbo.rpc.Filter,其中META-INF/dubbo是文件夹,org.apache.dubbo.rpc.Filter是整个文件名。
2、在org.apache.dubbo.rpc.Filter加入自定义类:
dubboConsumerFilter=com.weifor.xx.api.dubbo.DubboConsumerFilter
dubboProviderFilter=com.weifor.xx.api.dubbo.DubboProviderFilter
3、在配置文件application.yml、bootstrap.yml或application.property里加入:
dubbo:
scan:
base-packages: com.weifor
consumer:
check: false #作为消费者时启动不检查
#generic: false #是否缺省泛化接口,默认false
#timeout: 3000
#retries: 0
filter: dubboConsumerFilter
provider:
#timeout: 3000
#retries: 0
filter: dubboProviderFilter
#cloud:
#subscribed-services: xxx
为什么设计得这么啰嗦,实现自定义过滤器后,还要增加org.apache.dubbo.rpc.Filter
加了文件定义文件还没得,还要配置dubbo.consumer.filter和dubbo.provider.filter
吐槽一下:Apache Dubbo 你能不能设计得更啰嗦点?就不能直接扫描找到项目里的自定义FIlter文件??然后,就没有然后了,这么简单的事还要搞配置。。。。。。