You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@dubbo.apache.org by "houkunlin (via GitHub)" <gi...@apache.org> on 2023/09/12 08:41:38 UTC
[GitHub] [dubbo] houkunlin opened a new issue, #13047: 拦截扩展的类加载器疑问
houkunlin opened a new issue, #13047:
URL: https://github.com/apache/dubbo/issues/13047
<!-- If you need to report a security issue please visit https://github.com/apache/dubbo/security/policy -->
- [ ] I have searched the [issues](https://github.com/apache/dubbo/issues) of this repository and believe that this is not a duplicate.
## Ask your question here
Dubbo版本:`org.apache.dubbo:dubbo-bom:3.2.5`
SpringBoot:3.1.2
根据 https://cn.dubbo.apache.org/zh-cn/overview/mannual/java-sdk/reference-manual/spi/description/filter/ 文档,在使用拦截器时,无法获取 AopAspect 类中 ThreadLocal 数据,经过调试发现:
在 Controller 中 与在 Filter 中,两个 `AopAspect.class` 不是一个对象,两个 `AopAspect.LISTS` 不是同一个 ThreadLocal 对象,导致在 Filter 中无法获取到数据。
```java
@Slf4j
@Aspect
@Component
@RequiredArgsConstructor
public class AopAspect {
/**
* 当前请求的某个数据列表
*/
public static final ThreadLocal<List<?>> LISTS = new ThreadLocal<>();
}
@Slf4j
@Activate(group = CommonConstants.CONSUMER, order = -10000, onClass = {OBJECT_MAPPER_CLASS_NAME})
public class MyConsumerFilter implements Filter {
public static final String KEY = DataScope.class.getName() + "#Dubbo";
private final ObjectMapperCodec mapper;
public MyConsumerFilter(ApplicationModel applicationModel) {
this.mapper = applicationModel.getBeanFactory().getBean(ObjectMapperCodec.class);
}
@Override
public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
log.info("消费者");
Result result;
try {
initContext(invocation);
result = invoker.invoke(invocation);
} finally {
invocation.getObjectAttachments().remove(KEY);
}
return result;
}
private void initContext(Invocation invocation) {
// 在 Controller 中,这个是有数据的,但是在 Filter 中无法获取数据
List<?> lists = AopAspect.LISTS.get();
if (lists == null || lists.isEmpty()) {
return;
}
String context = mapper.serialize(new MyContext(lists));
log.info("消费者:{}", context);
invocation.setObjectAttachment(KEY, context);
}
}
```
在 `META-INF/dubbo/org.apache.dubbo.rpc.Filter` 文本文件中增加
```txt
MyConsumerFilter=com.houkunlin.MyConsumerFilter
```
--
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-unsubscribe@dubbo.apache.org.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org
[GitHub] [dubbo] MingJunDuan commented on issue #13047: 拦截扩展的类加载器疑问
Posted by "MingJunDuan (via GitHub)" <gi...@apache.org>.
MingJunDuan commented on issue #13047:
URL: https://github.com/apache/dubbo/issues/13047#issuecomment-1716004358
threadlocal有跨线程池上下文丢失情况,查看是否存在跨线程池的情况
--
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-unsubscribe@dubbo.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org
[GitHub] [dubbo] houkunlin commented on issue #13047: 拦截扩展的类加载器疑问
Posted by "houkunlin (via GitHub)" <gi...@apache.org>.
houkunlin commented on issue #13047:
URL: https://github.com/apache/dubbo/issues/13047#issuecomment-1716769840
调试执行 `Thread.currentThread()` 看到都是同一个线程对象
下图,在 Filter 里面调试的内容

下图,在 Controller 里面调试的内容

--
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-unsubscribe@dubbo.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org
[GitHub] [dubbo] AlbumenJ commented on issue #13047: 拦截扩展的类加载器疑问
Posted by "AlbumenJ (via GitHub)" <gi...@apache.org>.
AlbumenJ commented on issue #13047:
URL: https://github.com/apache/dubbo/issues/13047#issuecomment-1716831698
在两边分别执行 `System.identityHashCode(AopAspect.LISTS)` 看下结果是不是一样呢
--
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-unsubscribe@dubbo.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org
[GitHub] [dubbo] AlbumenJ commented on issue #13047: 拦截扩展的类加载器疑问
Posted by "AlbumenJ (via GitHub)" <gi...@apache.org>.
AlbumenJ commented on issue #13047:
URL: https://github.com/apache/dubbo/issues/13047#issuecomment-1716821235
是不是环境里面存在多个 `AopAspect.class`,对应的 class loader 是不一样的
--
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-unsubscribe@dubbo.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org
Re: [I] 拦截扩展的类加载器疑问 [dubbo]
Posted by "AlbumenJ (via GitHub)" <gi...@apache.org>.
AlbumenJ commented on issue #13047:
URL: https://github.com/apache/dubbo/issues/13047#issuecomment-1813861243
那这个应该需要你去处理类加载器的差异,Dubbo 只能通过 TCCL 去加载你的类
--
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-unsubscribe@dubbo.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org
[GitHub] [dubbo] houkunlin commented on issue #13047: 拦截扩展的类加载器疑问
Posted by "houkunlin (via GitHub)" <gi...@apache.org>.
houkunlin commented on issue #13047:
URL: https://github.com/apache/dubbo/issues/13047#issuecomment-1716856323
`log.warn("调试 Dubbo 问题 HashCode: {}", System.identityHashCode(DataScopeAop.DATA_SCOPE_LIST));` 执行打印日志截图

--
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-unsubscribe@dubbo.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org
[GitHub] [dubbo] AlbumenJ commented on issue #13047: 拦截扩展的类加载器疑问
Posted by "AlbumenJ (via GitHub)" <gi...@apache.org>.
AlbumenJ commented on issue #13047:
URL: https://github.com/apache/dubbo/issues/13047#issuecomment-1716821391
Dubbo 的 Filter 默认是使用 Dubbo 的 classloader 加载的
--
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-unsubscribe@dubbo.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org
Re: [I] 拦截扩展的类加载器疑问 [dubbo]
Posted by "CuriousRookie (via GitHub)" <gi...@apache.org>.
CuriousRookie commented on issue #13047:
URL: https://github.com/apache/dubbo/issues/13047#issuecomment-1825088155
RestartClassLoader...
把 spring-boot-devtools 去掉,或者参考[官方文档](https://docs.spring.io/spring-boot/docs/current/reference/html/using.html#using.devtools.restart.customizing-the-classload)进行一些自定义?
--
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-unsubscribe@dubbo.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org
Re: [I] 拦截扩展的类加载器疑问 [dubbo]
Posted by "houkunlin (via GitHub)" <gi...@apache.org>.
houkunlin commented on issue #13047:
URL: https://github.com/apache/dubbo/issues/13047#issuecomment-1813694611
在 Controller 获取 AOP 的 classloader

在过滤器中获取 AOP 的 classloader

--
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-unsubscribe@dubbo.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org
Re: [I] 拦截扩展的类加载器疑问 [dubbo]
Posted by "AlbumenJ (via GitHub)" <gi...@apache.org>.
AlbumenJ commented on issue #13047:
URL: https://github.com/apache/dubbo/issues/13047#issuecomment-1987585097
No news is good news. Please feel free to create a new issue if you have any question.
--
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-unsubscribe@dubbo.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org
Re: [I] 拦截扩展的类加载器疑问 [dubbo]
Posted by "AlbumenJ (via GitHub)" <gi...@apache.org>.
AlbumenJ closed issue #13047: 拦截扩展的类加载器疑问
URL: https://github.com/apache/dubbo/issues/13047
--
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-unsubscribe@dubbo.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org
[GitHub] [dubbo] AlbumenJ commented on issue #13047: 拦截扩展的类加载器疑问
Posted by "AlbumenJ (via GitHub)" <gi...@apache.org>.
AlbumenJ commented on issue #13047:
URL: https://github.com/apache/dubbo/issues/13047#issuecomment-1720729670
> `log.warn("调试 Dubbo 问题 HashCode: {}", System.identityHashCode(DataScopeAop.DATA_SCOPE_LIST));` 执行打印日志截图
>
> 
如果这两个对象不一样就意味着环境里面有两个 DataScopeAop 类
--
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-unsubscribe@dubbo.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org
[GitHub] [dubbo] songxiaosheng commented on issue #13047: 拦截扩展的类加载器疑问
Posted by "songxiaosheng (via GitHub)" <gi...@apache.org>.
songxiaosheng commented on issue #13047:
URL: https://github.com/apache/dubbo/issues/13047#issuecomment-1715618623
说明不是一个线程,同一个线程内是一样的,可以贴一下你的线程名字么
--
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-unsubscribe@dubbo.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org
[GitHub] [dubbo] houkunlin commented on issue #13047: 拦截扩展的类加载器疑问
Posted by "houkunlin (via GitHub)" <gi...@apache.org>.
houkunlin commented on issue #13047:
URL: https://github.com/apache/dubbo/issues/13047#issuecomment-1716824566
有且只有一个 `AopAspect.class`
--
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-unsubscribe@dubbo.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org
[GitHub] [dubbo] kimmking commented on issue #13047: 拦截扩展的类加载器疑问
Posted by "kimmking (via GitHub)" <gi...@apache.org>.
kimmking commented on issue #13047:
URL: https://github.com/apache/dubbo/issues/13047#issuecomment-1741818162
最开始的截图就能看出来,两个类的id不一样,一个是22718,一个是7773,说明不是一个类,存在两个不同cl加载的Aop类,导致LISTS静态对象也不是一个。
--
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-unsubscribe@dubbo.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org