Commit 600333aa by Nepxion

修正Hystrix上下文丢失的Bug

parent 7d6ee9f6
......@@ -16,7 +16,7 @@ import org.apache.commons.lang3.builder.ToStringStyle;
import org.springframework.web.server.ServerWebExchange;
public class GatewayStrategyContext {
private static final ThreadLocal<GatewayStrategyContext> THREAD_LOCAL = new InheritableThreadLocal<GatewayStrategyContext>() {
private static final ThreadLocal<GatewayStrategyContext> THREAD_LOCAL = new ThreadLocal<GatewayStrategyContext>() {
@Override
protected GatewayStrategyContext initialValue() {
return new GatewayStrategyContext();
......
......@@ -16,7 +16,7 @@ import org.apache.commons.lang3.builder.ToStringStyle;
import org.springframework.web.context.request.RequestAttributes;
public class RestStrategyContext {
private static final ThreadLocal<RestStrategyContext> THREAD_LOCAL = new InheritableThreadLocal<RestStrategyContext>() {
private static final ThreadLocal<RestStrategyContext> THREAD_LOCAL = new ThreadLocal<RestStrategyContext>() {
@Override
protected RestStrategyContext initialValue() {
return new RestStrategyContext();
......
......@@ -19,7 +19,7 @@ import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
public class RpcStrategyContext {
private static final ThreadLocal<RpcStrategyContext> THREAD_LOCAL = new InheritableThreadLocal<RpcStrategyContext>() {
private static final ThreadLocal<RpcStrategyContext> THREAD_LOCAL = new ThreadLocal<RpcStrategyContext>() {
@Override
protected RpcStrategyContext initialValue() {
return new RpcStrategyContext();
......
......@@ -18,7 +18,7 @@ import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
public class ZuulStrategyContext {
private static final ThreadLocal<ZuulStrategyContext> THREAD_LOCAL = new InheritableThreadLocal<ZuulStrategyContext>() {
private static final ThreadLocal<ZuulStrategyContext> THREAD_LOCAL = new ThreadLocal<ZuulStrategyContext>() {
@Override
protected ZuulStrategyContext initialValue() {
return new ZuulStrategyContext();
......
......@@ -30,6 +30,7 @@ import com.nepxion.discovery.plugin.strategy.service.aop.RestTemplateStrategyInt
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
// Hystrix测试
// @EnableCircuitBreaker
public class DiscoveryApplicationA1 {
public static void main(String[] args) {
......
......@@ -26,9 +26,18 @@ public class AFeignImpl extends AbstractFeignImpl implements AFeign {
@Autowired
private BFeign bFeign;
// Hystrix测试
// @Autowired
// private HystrixService hystrixService;
@Override
// @HystrixCommand(fallbackMethod = "fallback", commandProperties = { @HystrixProperty(name = "execution.isolation.strategy", value = "THREAD") })
public String invoke(@RequestBody String value) {
// LOG.info("---------- 主方法里获取上下文 RequestContextHolder.getRequestAttributes():{}", RequestContextHolder.getRequestAttributes());
// LOG.info("---------- 主方法里获取上下文 RestStrategyContext.getCurrentContext().getRequestAttributes():{}", RestStrategyContext.getCurrentContext().getRequestAttributes());
// LOG.info("---------- 主方法里获取Token:{}", ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest().getHeader("token"));
// hystrixService.invokeHystrix(value);
value = doInvoke(value);
value = bFeign.invoke(value);
......@@ -36,12 +45,4 @@ public class AFeignImpl extends AbstractFeignImpl implements AFeign {
return value;
}
/*public String fallback(String value, Throwable e) {
if (e != null) {
LOG.error("Fallback error", e);
}
return "Fallback by Hystrix";
}*/
}
\ No newline at end of file
package com.nepxion.discovery.plugin.example.service.hystrix;
/**
* <p>Title: Nepxion Discovery</p>
* <p>Description: Nepxion Discovery</p>
* <p>Copyright: Copyright (c) 2017-2050</p>
* <p>Company: Nepxion</p>
* @author Haojun Ren
* @version 1.0
*/
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.ServletRequestAttributes;
import com.nepxion.discovery.plugin.strategy.service.context.RestStrategyContext;
@Service
public class HystrixService {
private static final Logger LOG = LoggerFactory.getLogger(HystrixService.class);
// Hystrix测试
// @HystrixCommand(fallbackMethod = "invokeFallback", commandProperties = { @HystrixProperty(name = "execution.isolation.strategy", value = "THREAD") })
public String invokeHystrix(String value) {
LOG.info("---------- 熔断方法里获取上下文 RestStrategyContext.getCurrentContext().getRequestAttributes():{}", RestStrategyContext.getCurrentContext().getRequestAttributes());
LOG.info("---------- 熔断方法里获取Token:{}", ((ServletRequestAttributes) RestStrategyContext.getCurrentContext().getRequestAttributes()).getRequest().getHeader("token"));
return "Invoke Hystrix";
}
public String invokeFallback(String value, Throwable e) {
LOG.info("---------- 快速失败方法里获取上下文 RestStrategyContext.getCurrentContext().getRequestAttributes():{}", RestStrategyContext.getCurrentContext().getRequestAttributes());
if (e != null) {
LOG.error("Fallback error", e);
}
return "Fallback by Hystrix";
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment