Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fescar and spring cloud integration can not get xid problem when using Feign service call #883

Closed
wenchao523 opened this issue Apr 24, 2019 · 4 comments
Labels
RPC: SpringCloud Relate to seata SpringCloud type: bug Category issues or prs related to bug. type: question Category issues related to questions or problems

Comments

@wenchao523
Copy link

wenchao523 commented Apr 24, 2019

 fescar与springcloud集成时,客户端调用微服务接口时,由于hystrix 做了线程隔离 ,

Hystrix会将请求放入Hystrix的线程池中去执行,此时会新启一个子线程处理请求。
而RootContext采用threadlocal传递数据,无法在子线程中传递,造成在子线程中通过RootContext.getXid()获取为空。
解决办法,在ThreadLocalContextCore类中,用InheritableThreadLocal 替代ThreadLocal。如下:
public class ThreadLocalContextCore implements ContextCore {

//private ThreadLocal<Map<String, String>> threadLocal = new ThreadLocal<Map<String, String>>()
private ThreadLocal<Map<String, String>> threadLocal = new InheritableThreadLocal<Map<String, String>>() {
    @Override
    protected Map<String, String> initialValue() {
        return new HashMap<String, String>();
    }

};
@fescar-robot fescar-robot added the RPC: SpringCloud Relate to seata SpringCloud label Apr 24, 2019
@fescar-robot
Copy link

Hi @wenchao523, we detect non-English characters in the issue. This comment is an auto translation from @fescar-robot to help other users to understand this issue.
We encourage you to describe your issue in English which is more friendly to other users.

Fescar and spring cloud integration can not get xid problem when using Feign service call

When fescar is integrated with springcloud, when the client calls the microservice interface, Hystrix will insert the request into the thread pool of Hystrix to perform the thread isolation, and a new child thread will process the request. The RootContext uses threadlocal to pass data, which cannot be passed in the child thread, resulting in null in the child thread through RootContext.getXid().
The solution is to replace ThreadLocal with InheritableThreadLocal in the ThreadLocalContextCore class. as follows:
public class ThreadLocalContextCore implements ContextCore {

//private ThreadLocal<Map<String, String>> threadLocal = new ThreadLocal<Map<String, String>>()
private ThreadLocal<Map<String, String>> threadLocal = new InheritableThreadLocal<Map<String, String>>() {
    @Override
    protected Map<String, String> initialValue() {
        return new HashMap<String, String>();
    }

};

@fescar-robot fescar-robot changed the title fescar与spring cloud集成是采用Feign服务调用时 无法获取xid问题 Fescar and spring cloud integration can not get xid problem when using Feign service call Apr 24, 2019
@fescar-robot fescar-robot added type: bug Category issues or prs related to bug. type: question Category issues related to questions or problems labels Apr 24, 2019
@CoffeeLatte007
Copy link
Contributor

I don't think is's a bug. Because in many asynchronous scenarios, There is no need for asynchronous transactions to join a distributed transaction,It makes distributed transactions larger.
In your case,I think Seata can provide some mechanism to support cross thread and let consumers choose whether to use it or not

@wangxinpier
Copy link

我也遇到了这个问题,感谢 wenchao523

@chengyan0
Copy link

chengyan0 commented Nov 26, 2019

hystrix 有信号量与线程池两种模式,应该只有使用线程池模式时才会出现这个问题。http://www.itmuch.com/spring-cloud-sum/hystrix-threadlocal/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
RPC: SpringCloud Relate to seata SpringCloud type: bug Category issues or prs related to bug. type: question Category issues related to questions or problems
Projects
None yet
Development

No branches or pull requests

6 participants