parent
8718284dc3
commit
5d23eeb1d1
@ -0,0 +1,53 @@ |
||||
package com.gitee.sop.gatewaycommon.zuul.filter; |
||||
|
||||
import com.gitee.sop.gatewaycommon.bean.TargetRoute; |
||||
import com.gitee.sop.gatewaycommon.manager.EnvGrayManager; |
||||
import com.gitee.sop.gatewaycommon.manager.RouteRepositoryContext; |
||||
import com.gitee.sop.gatewaycommon.param.ApiParam; |
||||
import com.gitee.sop.gatewaycommon.param.ParamNames; |
||||
import com.gitee.sop.gatewaycommon.zuul.ZuulContext; |
||||
import com.netflix.zuul.context.RequestContext; |
||||
import com.netflix.zuul.exception.ZuulException; |
||||
import org.springframework.beans.factory.annotation.Autowired; |
||||
|
||||
import java.util.List; |
||||
|
||||
/** |
||||
* @author tanghc |
||||
*/ |
||||
public class PreVersionDecisionFilter extends BaseZuulFilter { |
||||
|
||||
@Autowired |
||||
private EnvGrayManager envGrayManager; |
||||
|
||||
@Override |
||||
protected FilterType getFilterType() { |
||||
return FilterType.PRE; |
||||
} |
||||
|
||||
@Override |
||||
protected int getFilterOrder() { |
||||
return PRE_LIMIT_FILTER_ORDER + 1; |
||||
} |
||||
|
||||
@Override |
||||
protected Object doRun(RequestContext requestContext) throws ZuulException { |
||||
ApiParam apiParam = ZuulContext.getApiParam(); |
||||
String nameVersion = apiParam.fetchNameVersion(); |
||||
TargetRoute targetRoute = RouteRepositoryContext.getRouteRepository().get(nameVersion); |
||||
if (targetRoute == null) { |
||||
return null; |
||||
} |
||||
String serviceId = targetRoute.getServiceRouteInfo().getServiceId(); |
||||
List<String> instanceIdList = envGrayManager.listGrayInstanceId(serviceId); |
||||
|
||||
for (String instanceId : instanceIdList) { |
||||
String version = envGrayManager.getVersion(instanceId, nameVersion); |
||||
if (version != null) { |
||||
requestContext.addZuulRequestHeader(ParamNames.HEADER_VERSION_NAME, version); |
||||
break; |
||||
} |
||||
} |
||||
return null; |
||||
} |
||||
} |
@ -1,23 +0,0 @@ |
||||
package com.gitee.sop.gatewaycommon.zuul.loadbalancer; |
||||
|
||||
import com.gitee.sop.gatewaycommon.param.ParamNames; |
||||
import org.springframework.http.HttpRequest; |
||||
import org.springframework.http.client.ClientHttpRequestExecution; |
||||
import org.springframework.http.client.ClientHttpRequestInterceptor; |
||||
import org.springframework.http.client.ClientHttpResponse; |
||||
import org.springframework.http.client.support.HttpRequestWrapper; |
||||
|
||||
import java.io.IOException; |
||||
|
||||
/** |
||||
* @author tanghc |
||||
*/ |
||||
public class EnvHttpRequestInterceptor implements ClientHttpRequestInterceptor { |
||||
@Override |
||||
public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException { |
||||
HttpRequestWrapper requestWrapper = new HttpRequestWrapper(request); |
||||
String newVersion = HystrixRequestVariableContext.getVersion(); |
||||
requestWrapper.getHeaders().add(ParamNames.GRAY_HEADER_VERSION_NAME, newVersion); |
||||
return execution.execute(requestWrapper, body); |
||||
} |
||||
} |
@ -1,18 +0,0 @@ |
||||
package com.gitee.sop.gatewaycommon.zuul.loadbalancer; |
||||
|
||||
/** |
||||
* @author tanghc |
||||
*/ |
||||
public class HystrixRequestVariableContext { |
||||
// private static final HystrixRequestVariableDefault<String> VERSION_HOLDER = new HystrixRequestVariableDefault<>();
|
||||
private static final ThreadLocal<String> VERSION_HOLDER = new ThreadLocal<>(); |
||||
|
||||
public static void setVersion(String version) { |
||||
VERSION_HOLDER.set(version); |
||||
} |
||||
|
||||
public static String getVersion() { |
||||
return VERSION_HOLDER.get(); |
||||
} |
||||
|
||||
} |
Loading…
Reference in new issue