Dubbo发起调用前先设一个context,在consumer filter里面覆盖掉这个context,但是provider拿到的还是前一个,这个是dubbo的bug吗?还是设计如此?
哪位老师知道
consumer-side:RpcContext.getContext().setAttachment("demo", "demo001");RpcCall.call();consumer-filter:RpcContext.getContext().setAttachment("demo", "demo002");provider-side://获取到的值是demo001,而不是demo002
RpcContext.getContext().getAttachment("demo");
consumer服务
consumer.java
ApplicationConfig applicationConfig = new ApplicationConfig();
applicationConfig.setName("demo-consumer");
applicationConfig.setQosEnable(false);
applicationConfig.setRegisterMode("instance");
applicationConfig.setMetadataType("remote");
RegistryConfig registryConfig = new RegistryConfig();
registryConfig.setAddress("nacos://xxxxxxxxxxx");
ReferenceConfig<DemoService> demoServiceReference = new ReferenceConfig<>();
demoServiceReference.setInterface(DemoService.class);
demoServiceReference.setCheck(false);
DubboBootstrap bootstrap = DubboBootstrap.newInstance();
bootstrap.application(applicationConfig)
.registry(registryConfig)
.reference(demoServiceReference)
.start();
RpcContext.getContext().setAttachment("demo", "demo001");
DemoService demoService = demoServiceReference.get();
System.out.println(demoService.sayHi(""));
@Activate(group = CommonConstants.CONSUMER)
public class DemoFilter implements Filter {
@Override
public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
RpcContext.getClientAttachment().setAttachment("demo", "demo002");
RpcContext.getServerAttachment().setAttachment("demo", "demo002");
RpcContext.getServerContext().setAttachment("demo", "demo002");
RpcContext.getServiceContext().setAttachment("demo", "demo002");
RpcContext.getContext().setAttachment("demo", "demo002");
// invocation.setAttachment("demo", "demo002");
return invoker.invoke(invocation);
}
}
provider服务
provider.java
ProtocolConfig protocol = new ProtocolConfig();
protocol.setName("dubbo");
ServiceConfig<DemoService> demoServiceConfig = new ServiceConfig<>();
demoServiceConfig.setInterface(DemoService.class);
demoServiceConfig.setRef(new DemoServiceImpl());
DubboBootstrap dubboBootstrap = DubboBootstrap.newInstance();
ApplicationConfig applicationConfig = new ApplicationConfig();
applicationConfig.setName("demo-provider");
applicationConfig.setQosEnable(false);
applicationConfig.setRegisterMode("instance");
RegistryConfig registryConfig = new RegistryConfig();
registryConfig.setAddress("nacos://xxxxxxxxxxxxxxxx");
dubboBootstrap
.application(applicationConfig)
.registry(registryConfig)
.protocol(protocol)
.service(demoServiceConfig)
.start();
System.out.println("start status: " + dubboBootstrap.isStarted());
public class DemoServiceImpl implements DemoService {
@Override
public Object sayHi(String name) {
String d = RpcContext.getContext().getAttachment("demo");
System.out.println(d);
}
}
看着没问题呀,毕竟filter是在后边起作用的,按道理说应该是覆盖了,你filter用spi声明了么,使用invocation.setAttachment();去传递吧,因为dubbo框架内置的ConsumerContextFilter会先于自定义filter执行,里边会将attachment都转到invocation对象里边,这个ClientAttachment中的属性传递是在这完成的,所以你后来在filter里边用是不生效的,直接用invocation加,
这种使用方式属于历史遗留问题了 ,此回答整理自钉群“Apache Dubbo开源讨论群2”
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。