AmosWong1998 commented on issue #11876: URL: https://github.com/apache/dubbo/issues/11876#issuecomment-1727015486
> > > > 不知道你的和我的问题对不对,我的问题是消费者setAttachment但是提供者get不到,看源码发现它走的是已废弃的代码getAttachment()方法。其中会if (value instanceof String),判断成立强转为string,不成立直接return null。 > > > > > > > > > 如果是 set 的 string 类型的这里不应该有问题。 另外 attachment 是提供给业务编程侧使用的,如果是在 Filter 内使用的话需要直接基于 invocation 对象操作 > > > > > > 【如果是在 Filter 内使用的话需要直接基于 invocation 对象操作】大佬这句话是什么意思没有理解?Filter里面不应该使用RpcContext或者attachment去传递业务标识吗? @AlbumenJ > > 比如我想在dubbo服务间透传tag标识,是不是直接在filter里面使用 invocation.setAttachment("dubbo.tag", "green");就可以了 > >  attachments会在ConsumerContextFilter(ClusterFilter的实现类)中给invocation加上。对业务编程侧来说是比较友好的,不需要扩展组件。  > > 但如果你是想要通过设置"dubbo.tag"来完成流量的打标,则需要扩展ClusterFiter(不能是filter),在invoke方法中直接使用 invocation.setAttachment("dubbo.tag", "green")。不能使用attachments的形式,因为我们拓展的ClusterFilter默认是会在内置的ClusterFilter链的最后,也就是会在ConsumerContexFilter之后,所以这些attachments不会被自动加到invocation中。ClusterFilter是Dubbo3.0中新加入,可以参考#7382 > > 而TagStateRouter.doRoute是拿的invocation.getAttachment("dubbo.tag")来做标签路由的:  > > 不能通过扩展Filter的方式来进行流量的打标的原因是因为Dubbo的调用链中到Filter这里就已经完成了路由选址和负载均衡,Filter只是对已确定的provider实例做一些其他操作 具体可以参考官网文档: https://cn.dubbo.apache.org/zh-cn/overview/mannual/java-sdk/reference-manual/architecture/service-invocation/ 所以针对于你的问题,在dubbo服务间透传tag标识,其实还是看你的用途,如果要为了做路由,则扩展ClusterFilter,用invocation.setAttachment;如果只是为了传一个上下文参数,则可以不用扩展ClusterFilter,直接在业务侧编程时使用Rpc.getClientAttachment.setAttachments就可以 跪谢大佬,这两天一直被这个问题困扰。原因就是扩展了filter,传递了dubbo.tag 但是没办法做tag路由。非常感谢大佬的耐心解答!我去试试 @robocanic -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: notifications-unsubscr...@dubbo.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: notifications-unsubscr...@dubbo.apache.org For additional commands, e-mail: notifications-h...@dubbo.apache.org