Nepxion / Discovery

☀️ Nepxion Discovery is a solution for Spring Cloud with blue green, gray, route, limitation, circuit breaker, degrade, isolation, tracing, dye, failover, active 蓝绿灰度发布、路由、限流、熔断、降级、隔离、追踪、流量染色、故障转移、多活
http://www.nepxion.com
Apache License 2.0
5.61k stars 1.42k forks source link

开启feign.hystrix.enabled=true的时候, 多线程feign调用的情况下因RequestContextHolder.getRequestAttributes()获取不到报空指针错误 #131

Closed ZhangNingPegasus closed 3 years ago

ZhangNingPegasus commented 3 years ago

Nepxion版本: 6.5.1 Discovery Agent版本: 1.0.1 IDE: Intellj idea

问题: 在开启feign.hystrix.enabled=true时候,并使用javaagent进行多线程的调用,【RequestContextHolder.getRequestAttributes()】报空指针的错

报错类型: com.nepxion.discovery.plugin.strategy.service.wrapper.DefaultServiceStrategyCallableWrapper 代码行数: 32行

VM options参数: -javaagent:E:\Java\middleware\spring-cloud\service-demo\src\main\resources\agent\discovery-agent-starter-1.0.1.jar -Dthread.scan.packages=org.springframework.aop.interceptor;com.netflix.hystrix;org.wyyt.springcloud.service.demo.feign

以下两个配置都设置为了true feign.hystrix.enabled=true spring.application.strategy.hystrix.threadlocal.supported=true

B6DBD23D-8913-4130-B7CD-A32E93BAB4FA

ZhangNingPegasus commented 3 years ago

纠正一下: 使用的版本不是6.5.1,而是6.5.0

HaojunRen commented 3 years ago

Discovery Agent和Hystrix增强包两者选一个即可,前者功能比后者强大,可以处理几乎所有异步场景,后者仅仅处理Hystrix异步

ZhangNingPegasus commented 3 years ago

您说的Hystrix增强包指的是?

HaojunRen commented 3 years ago
    <!-- 当服务用Hystrix做线程隔离的时候,才需要导入下面的包 -->
    <!-- <dependency>
        <groupId>com.nepxion</groupId>
        <artifactId>discovery-plugin-strategy-starter-hystrix</artifactId>
    </dependency> -->

如下开关跟上面的增强包是配套的: spring.application.strategy.hystrix.threadlocal.supported=true

ZhangNingPegasus commented 3 years ago
   非常感谢您的回复,在使用Discovery Agent,并去掉Hystrix增强包(discovery-plugin-strategy-starter-hystrix)的前提下,不再报空指针的错误,但是在多线程的feign调用下,灰度发布出现失效的情况。

相关配置: feign.hystrix.enabled=true spring.application.strategy.hystrix.threadlocal.supported=false

注册中心:consul

配置中心: Apollo

配置xml (全链路条件灰度发布): `<?xml version="1.0" encoding="UTF-8"?>

{"a":"1.1","b":"1.1"}

`

说明:
网关会调用服务a,服务a会调用服务b, 网关只有一个版本1.0;a服务有1.1和1.2版本; b服务也有1.1和1.2版本。 在上面的配置下,所有请求都应该调用a服务的1.1版本和b服务的1.1版本

现象: 测试用例中,基于feign的invoke接口(同步调用)、invoke-async接口(加了@async注解)都能够正常的按照上面的灰度发布策略运行, 但是同样基于feign下的invoke-thread接口(单独开线程)和invoke-threadpool接口(使用线程池)却不行,具体表现是:服务a始终能走1.1版本, 但是调用到服务b时,却1.1和1.2版本都会运行。

VM options选项: -javaagent:E:\Java\middleware\spring-cloud\service-demo\src\main\resources\agent\discovery-agent-starter-1.0.1.jar -Dthread.scan.packages=org.springframework.aop.interceptor;com.netflix.hystrix;org.wyyt.springcloud.service.demo.feign

服务a和服务b的POM依赖: `

com.nepxion discovery-plugin-register-center-starter-consul
    <!-- 配置中心插件 -->
    <dependency>
        <groupId>com.nepxion</groupId>
        <artifactId>discovery-plugin-config-center-starter-apollo</artifactId>
    </dependency>

    <!-- 管理中心插件 -->
    <dependency>
        <groupId>com.nepxion</groupId>
        <artifactId>discovery-plugin-admin-center-starter</artifactId>
    </dependency>

    <!-- 服务的策略编排插件 -->
    <dependency>
        <groupId>com.nepxion</groupId>
        <artifactId>discovery-plugin-strategy-starter-service</artifactId>
    </dependency>

    <!-- 熔断器 -->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
    </dependency>`

网关pom: `

com.nepxion discovery-plugin-register-center-starter-consul
    <!-- 配置中心插件 -->
    <dependency>
        <groupId>com.nepxion</groupId>
        <artifactId>discovery-plugin-config-center-starter-apollo</artifactId>
    </dependency>

    <!-- 熔断器 -->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
    </dependency>

    <!-- 管理中心插件 -->
    <dependency>
        <groupId>com.nepxion</groupId>
        <artifactId>discovery-plugin-admin-center-starter</artifactId>
    </dependency>

    <!-- 网关的策略编排插件 -->
    <dependency>
        <groupId>com.nepxion</groupId>
        <artifactId>discovery-plugin-strategy-starter-gateway</artifactId>
    </dependency>`

另外: discovery-agent-starter使用的是最新的1.0.1版本,但是控制打印的却是1.0.0版本 1

其他相关截图: 2 3

HaojunRen commented 3 years ago
  1. 不需要配置 spring.application.strategy.hystrix.threadlocal.supported=false
  2. 既然你用了condition,那你的条件呢?条件是可以基于Header/Parameter/Cookie其中一个或者多个来驱动的。请参考如下链接的用法: http://nepxion.gitee.io/discovery/#/?id=%e5%85%a8%e9%93%be%e8%b7%af%e7%89%88%e6%9c%ac%e6%9d%a1%e4%bb%b6%e5%8c%b9%e9%85%8d%e8%93%9d%e7%bb%bf%e5%8f%91%e5%b8%83
ZhangNingPegasus commented 3 years ago

谢谢,问题已解决。