开发者社区 > 云原生 > 中间件 > 正文

Dubbo发起调用前先设一个context,这个是dubbo的bug吗?

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);
}
}

展开
收起
夹心789 2024-06-04 08:18:39 43 0
1 条回答
写回答
取消 提交回答
  • 看着没问题呀,毕竟filter是在后边起作用的,按道理说应该是覆盖了,你filter用spi声明了么,使用invocation.setAttachment();去传递吧,因为dubbo框架内置的ConsumerContextFilter会先于自定义filter执行,里边会将attachment都转到invocation对象里边,这个ClientAttachment中的属性传递是在这完成的,所以你后来在filter里边用是不生效的,直接用invocation加,image.png
    这种使用方式属于历史遗留问题了 ,此回答整理自钉群“Apache Dubbo开源讨论群2”

    2024-06-04 21:03:14
    赞同 展开评论 打赏

为企业提供高效、稳定、易扩展的中间件产品。

相关电子书

更多
Dubbo开源现状与2.7规划 立即下载
Dubbo分布式服务治理实战 立即下载
《Dubbo 3.0 前瞻》 立即下载