Commit 55e895d5 by Nepxion

修复Hystrix获取Request的BUG

parent 91392344
...@@ -6,29 +6,24 @@ package com.nepxion.discovery.plugin.strategy.service.context; ...@@ -6,29 +6,24 @@ package com.nepxion.discovery.plugin.strategy.service.context;
* <p>Copyright: Copyright (c) 2017-2050</p> * <p>Copyright: Copyright (c) 2017-2050</p>
* <p>Company: Nepxion</p> * <p>Company: Nepxion</p>
* @author Haojun Ren * @author Haojun Ren
* @author Fan Yang
* @version 1.0 * @version 1.0
*/ */
import java.util.Map; import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.context.request.RequestAttributes;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes; import org.springframework.web.context.request.ServletRequestAttributes;
import com.nepxion.discovery.plugin.strategy.constant.StrategyConstant;
public class ServiceStrategyContextHolder { public class ServiceStrategyContextHolder {
@Autowired
private ConfigurableEnvironment environment;
public ServletRequestAttributes getRestAttributes() { public ServletRequestAttributes getRestAttributes() {
Boolean hystrixThreadlocalSupported = environment.getProperty(StrategyConstant.SPRING_APPLICATION_STRATEGY_HYSTRIX_THREADLOCAL_SUPPORTED, Boolean.class, Boolean.FALSE); RequestAttributes requestAttributes = RestStrategyContext.getCurrentContext().getRequestAttributes();
if (hystrixThreadlocalSupported) { if (requestAttributes == null) {
return (ServletRequestAttributes) RestStrategyContext.getCurrentContext().getRequestAttributes(); requestAttributes = RequestContextHolder.getRequestAttributes();
} else {
return (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
} }
return (ServletRequestAttributes) requestAttributes;
} }
public Map<String, Object> getRpcAttributes() { public Map<String, Object> getRpcAttributes() {
......
...@@ -6,27 +6,21 @@ package com.nepxion.discovery.plugin.strategy.zuul.context; ...@@ -6,27 +6,21 @@ package com.nepxion.discovery.plugin.strategy.zuul.context;
* <p>Copyright: Copyright (c) 2017-2050</p> * <p>Copyright: Copyright (c) 2017-2050</p>
* <p>Company: Nepxion</p> * <p>Company: Nepxion</p>
* @author Haojun Ren * @author Haojun Ren
* @author Fan Yang
* @version 1.0 * @version 1.0
*/ */
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.ConfigurableEnvironment;
import com.nepxion.discovery.plugin.strategy.constant.StrategyConstant;
import com.netflix.zuul.context.RequestContext; import com.netflix.zuul.context.RequestContext;
public class ZuulStrategyContextHolder { public class ZuulStrategyContextHolder {
@Autowired
private ConfigurableEnvironment environment;
public HttpServletRequest getRequest() { public HttpServletRequest getRequest() {
Boolean hystrixThreadlocalSupported = environment.getProperty(StrategyConstant.SPRING_APPLICATION_STRATEGY_HYSTRIX_THREADLOCAL_SUPPORTED, Boolean.class, Boolean.FALSE); HttpServletRequest request = ZuulStrategyContext.getCurrentContext().getRequest();
if (hystrixThreadlocalSupported) { if (request == null) {
return ZuulStrategyContext.getCurrentContext().getRequest(); request = RequestContext.getCurrentContext().getRequest();
} else {
return RequestContext.getCurrentContext().getRequest();
} }
return request;
} }
} }
\ No newline at end of file
...@@ -12,5 +12,5 @@ package com.nepxion.discovery.plugin.strategy.constant; ...@@ -12,5 +12,5 @@ package com.nepxion.discovery.plugin.strategy.constant;
public class StrategyConstant { public class StrategyConstant {
public static final String SPRING_APPLICATION_STRATEGY_CONTROL_ENABLED = "spring.application.strategy.control.enabled"; public static final String SPRING_APPLICATION_STRATEGY_CONTROL_ENABLED = "spring.application.strategy.control.enabled";
public static final String SPRING_APPLICATION_STRATEGY_ZONE_AVOIDANCE_RULE_ENABLED = "spring.application.strategy.zone.avoidance.rule.enabled"; public static final String SPRING_APPLICATION_STRATEGY_ZONE_AVOIDANCE_RULE_ENABLED = "spring.application.strategy.zone.avoidance.rule.enabled";
public static final String SPRING_APPLICATION_STRATEGY_HYSTRIX_THREADLOCAL_SUPPORTED = "spring.application.strategy.hystrix.threadlocal.supported"; public static final String SPRING_APPLICATION_STRATEGY_HYSTRIX_THREADLOCAL_SUPPORTED = "spring.application.strategy.hystrix.threadlocal.supported";
} }
\ 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