Commit 55e895d5 by Nepxion

修复Hystrix获取Request的BUG

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