diff --git a/sop-admin/sop-admin-server/src/main/java/com/gitee/sop/adminserver/api/service/ServiceApi.java b/sop-admin/sop-admin-server/src/main/java/com/gitee/sop/adminserver/api/service/ServiceApi.java index 8dbf3364..9c7a0b32 100644 --- a/sop-admin/sop-admin-server/src/main/java/com/gitee/sop/adminserver/api/service/ServiceApi.java +++ b/sop-admin/sop-admin-server/src/main/java/com/gitee/sop/adminserver/api/service/ServiceApi.java @@ -211,6 +211,7 @@ public class ServiceApi { registryService.setMetadata(param.buildServiceInstance(), envPre.getKey(), envPre.getValue()); } + String serviceId = param.getServiceId(); String instanceId = param.getInstanceId(); String userKeyContent = param.getUserKeyContent(); String nameVersionContent = param.getNameVersionContent(); @@ -218,6 +219,7 @@ public class ServiceApi { ConfigGrayUserkey configGrayUserkey = configGrayUserkeyMapper.getByColumn("instance_id", instanceId); if (configGrayUserkey == null) { configGrayUserkey = new ConfigGrayUserkey(); + configGrayUserkey.setServiceId(serviceId); configGrayUserkey.setInstanceId(instanceId); configGrayUserkey.setUserKeyContent(userKeyContent); configGrayUserkey.setNameVersionContent(nameVersionContent); diff --git a/sop-admin/sop-admin-server/src/main/java/com/gitee/sop/adminserver/entity/ConfigGrayUserkey.java b/sop-admin/sop-admin-server/src/main/java/com/gitee/sop/adminserver/entity/ConfigGrayUserkey.java index 531a76c6..d822e182 100644 --- a/sop-admin/sop-admin-server/src/main/java/com/gitee/sop/adminserver/entity/ConfigGrayUserkey.java +++ b/sop-admin/sop-admin-server/src/main/java/com/gitee/sop/adminserver/entity/ConfigGrayUserkey.java @@ -25,6 +25,9 @@ public class ConfigGrayUserkey { /** 数据库字段:id */ private Long id; + /** serviceId, 数据库字段:service_id */ + private String serviceId; + /** instanceId, 数据库字段:instance_id */ private String instanceId; diff --git a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/bean/ApiConfig.java b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/bean/ApiConfig.java index 47853f38..f76cd5ea 100644 --- a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/bean/ApiConfig.java +++ b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/bean/ApiConfig.java @@ -10,13 +10,13 @@ import com.gitee.sop.gatewaycommon.manager.DefaultIsvRoutePermissionManager; import com.gitee.sop.gatewaycommon.manager.DefaultLimitConfigManager; import com.gitee.sop.gatewaycommon.manager.DefaultRouteConfigManager; import com.gitee.sop.gatewaycommon.manager.DefaultServiceErrorManager; -import com.gitee.sop.gatewaycommon.manager.DefaultUserKeyManager; +import com.gitee.sop.gatewaycommon.manager.DefaultEnvGrayManager; import com.gitee.sop.gatewaycommon.manager.IPBlacklistManager; import com.gitee.sop.gatewaycommon.manager.IsvRoutePermissionManager; import com.gitee.sop.gatewaycommon.manager.LimitConfigManager; import com.gitee.sop.gatewaycommon.manager.RouteConfigManager; import com.gitee.sop.gatewaycommon.manager.ServiceErrorManager; -import com.gitee.sop.gatewaycommon.manager.UserKeyManager; +import com.gitee.sop.gatewaycommon.manager.EnvGrayManager; import com.gitee.sop.gatewaycommon.param.ParamBuilder; import com.gitee.sop.gatewaycommon.result.DataNameBuilder; import com.gitee.sop.gatewaycommon.result.DefaultDataNameBuilder; @@ -127,7 +127,7 @@ public class ApiConfig { /** * 用户key管理 */ - private UserKeyManager userKeyManager = new DefaultUserKeyManager(); + private EnvGrayManager userKeyManager = new DefaultEnvGrayManager(); /** * 构建数据节点名称 diff --git a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/manager/AbstractConfiguration.java b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/manager/AbstractConfiguration.java index 8e14007a..9b01bd32 100644 --- a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/manager/AbstractConfiguration.java +++ b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/manager/AbstractConfiguration.java @@ -83,7 +83,7 @@ public class AbstractConfiguration implements ApplicationContextAware { @Bean @ConditionalOnMissingBean - UserKeyManager userKeyManager() { + EnvGrayManager userKeyManager() { return ApiConfig.getInstance().getUserKeyManager(); } diff --git a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/manager/DefaultUserKeyManager.java b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/manager/DefaultEnvGrayManager.java similarity index 60% rename from sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/manager/DefaultUserKeyManager.java rename to sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/manager/DefaultEnvGrayManager.java index fabab80b..7e3a95a6 100644 --- a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/manager/DefaultUserKeyManager.java +++ b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/manager/DefaultEnvGrayManager.java @@ -4,18 +4,33 @@ import com.gitee.sop.gatewaycommon.zuul.loadbalancer.ServiceGrayConfig; import com.google.common.collect.Maps; import com.google.common.collect.Sets; +import java.util.ArrayList; +import java.util.List; import java.util.Map; /** * @author tanghc */ -public class DefaultUserKeyManager implements UserKeyManager { +public class DefaultEnvGrayManager implements EnvGrayManager { /** * KEY:instanceId */ private Map serviceUserKeyMap = Maps.newConcurrentMap(); + private Map> serviceInstanceIdMap = Maps.newConcurrentMap(); + + @Override + public void addServiceInstance(String serviceId, String instanceId) { + List instanceIdList = serviceInstanceIdMap.computeIfAbsent(serviceId, key -> new ArrayList<>()); + instanceIdList.add(instanceId); + } + + @Override + public List listGrayInstanceId(String serviceId) { + return serviceInstanceIdMap.get(serviceId); + } + @Override public boolean containsKey(String instanceId, Object userKey) { if (instanceId == null || userKey == null) { @@ -34,14 +49,12 @@ public class DefaultUserKeyManager implements UserKeyManager { @Override public ServiceGrayConfig getServiceGrayConfig(String instanceId) { - ServiceGrayConfig serviceGrayConfig = serviceUserKeyMap.get(instanceId); - if (serviceGrayConfig == null) { - serviceGrayConfig = new ServiceGrayConfig(); + return serviceUserKeyMap.computeIfAbsent(instanceId, key -> { + ServiceGrayConfig serviceGrayConfig = new ServiceGrayConfig(); serviceGrayConfig.setUserKeys(Sets.newConcurrentHashSet()); serviceGrayConfig.setGrayNameVersion(Maps.newConcurrentMap()); - serviceUserKeyMap.put(instanceId, serviceGrayConfig); - } - return serviceGrayConfig; + return serviceGrayConfig; + }); } @Override diff --git a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/manager/UserKeyManager.java b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/manager/EnvGrayManager.java similarity index 65% rename from sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/manager/UserKeyManager.java rename to sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/manager/EnvGrayManager.java index 568cd597..1b294b4c 100644 --- a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/manager/UserKeyManager.java +++ b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/manager/EnvGrayManager.java @@ -3,10 +3,17 @@ package com.gitee.sop.gatewaycommon.manager; import com.gitee.sop.gatewaycommon.bean.BeanInitializer; import com.gitee.sop.gatewaycommon.zuul.loadbalancer.ServiceGrayConfig; +import java.util.List; + /** * @author tanghc */ -public interface UserKeyManager extends BeanInitializer { +public interface EnvGrayManager extends BeanInitializer { + + void addServiceInstance(String serviceId, String instanceId); + + List listGrayInstanceId(String serviceId); + boolean containsKey(String instanceId, Object userKey); String getVersion(String instanceId, String nameVersion); diff --git a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/zuul/filter/PreVersionDecisionFilter.java b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/zuul/filter/PreVersionDecisionFilter.java new file mode 100644 index 00000000..73023a5d --- /dev/null +++ b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/zuul/filter/PreVersionDecisionFilter.java @@ -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 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; + } +} diff --git a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/zuul/loadbalancer/BaseServerChooser.java b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/zuul/loadbalancer/BaseServerChooser.java index bbda7bef..95a9049b 100644 --- a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/zuul/loadbalancer/BaseServerChooser.java +++ b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/zuul/loadbalancer/BaseServerChooser.java @@ -6,11 +6,10 @@ import com.netflix.loadbalancer.Server; import com.netflix.loadbalancer.ZoneAvoidanceRule; import com.netflix.zuul.context.RequestContext; import lombok.extern.slf4j.Slf4j; -import org.springframework.util.CollectionUtils; import javax.servlet.http.HttpServletRequest; -import java.util.ArrayList; import java.util.List; +import java.util.stream.Collectors; /** * 服务实例选择器 @@ -37,31 +36,88 @@ public abstract class BaseServerChooser extends ZoneAvoidanceRule { */ protected abstract boolean canVisit(Server server, HttpServletRequest request); + /** + * 是否是预发布服务器 + * @param server 服务器 + * @return true:是 + */ + protected abstract boolean isPreServer(Server server); + + /** + * 是否是灰度发布服务器 + * @param server 服务器 + * @return true:是 + */ + protected abstract boolean isGrayServer(Server server); + + /** + * 能否访问预发布 + * @param server 预发布服务器 + * @param request request + * @return true:能 + */ + protected abstract boolean canVisitPre(Server server, HttpServletRequest request); + + /** + * 能否访问灰度服务器 + * @param server 灰度服务器 + * @param request request + * @return true:能 + */ + protected abstract boolean canVisitGray(Server server, HttpServletRequest request); + @Override public Server choose(Object key) { ILoadBalancer lb = getLoadBalancer(); // 获取服务实例列表 - List allServers = new ArrayList<>(lb.getAllServers()); - int index = -1; - for (int i = 0; i < allServers.size(); i++) { - Server server = allServers.get(i); - if (match(server)) { - index = i; - if (canVisit(server, RequestContext.getCurrentContext().getRequest())) { - return server; - } - } + List allServers = lb.getAllServers(); + HttpServletRequest request = RequestContext.getCurrentContext().getRequest(); + + List preServers = allServers.stream() + .filter(this::isPreServer) + .filter(server -> canVisitPre(server, request)) + .collect(Collectors.toList()); +// int index = -1; +// for (int i = 0; i < allServers.size(); i++) { +// Server server = allServers.get(i); +// if (isPreServer(server)) { +// preServers.add(server); +// } +// if (match(server)) { +// index = i; +// if (canVisit(server, RequestContext.getCurrentContext().getRequest())) { +// return server; +// } +// } +// } + + if (!preServers.isEmpty()) { + return this.doChoose(preServers, key); } + + List grayServers = allServers.stream() + .filter(this::isGrayServer) + .filter(server -> canVisitGray(server, request)) + .collect(Collectors.toList()); + if (!grayServers.isEmpty()) { + return doChoose(grayServers, key); + } + + return super.choose(key); // 调用默认的算法 // 如果选出了特殊环境服务器,需要移除命中的服务器 - if (index > -1) { - allServers.remove(index); - } - if (CollectionUtils.isEmpty(allServers)) { - log.error("无可用服务实例,key:", key); - return null; - } - Optional server = getPredicate().chooseRoundRobinAfterFiltering(allServers, key); +// if (index > -1) { +// allServers.remove(index); +// } +// if (CollectionUtils.isEmpty(allServers)) { +// log.error("无可用服务实例,key:", key); +// return null; +// } + + } + + protected Server doChoose(List servers, Object key) { + Optional server = getPredicate().chooseRoundRobinAfterFiltering(servers, key); if (server.isPresent()) { return server.get(); } else { diff --git a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/zuul/loadbalancer/EnvHttpRequestInterceptor.java b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/zuul/loadbalancer/EnvHttpRequestInterceptor.java deleted file mode 100644 index 9f924f55..00000000 --- a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/zuul/loadbalancer/EnvHttpRequestInterceptor.java +++ /dev/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); - } -} \ No newline at end of file diff --git a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/zuul/loadbalancer/HystrixRequestVariableContext.java b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/zuul/loadbalancer/HystrixRequestVariableContext.java deleted file mode 100644 index a4d4901c..00000000 --- a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/zuul/loadbalancer/HystrixRequestVariableContext.java +++ /dev/null @@ -1,18 +0,0 @@ -package com.gitee.sop.gatewaycommon.zuul.loadbalancer; - -/** - * @author tanghc - */ -public class HystrixRequestVariableContext { -// private static final HystrixRequestVariableDefault VERSION_HOLDER = new HystrixRequestVariableDefault<>(); - private static final ThreadLocal VERSION_HOLDER = new ThreadLocal<>(); - - public static void setVersion(String version) { - VERSION_HOLDER.set(version); - } - - public static String getVersion() { - return VERSION_HOLDER.get(); - } - -} diff --git a/sop-common/sop-registry-api/src/main/java/com/gitee/sop/registryapi/bean/HttpTool.java b/sop-common/sop-registry-api/src/main/java/com/gitee/sop/registryapi/bean/HttpTool.java index ee280d4f..ddaed44f 100644 --- a/sop-common/sop-registry-api/src/main/java/com/gitee/sop/registryapi/bean/HttpTool.java +++ b/sop-common/sop-registry-api/src/main/java/com/gitee/sop/registryapi/bean/HttpTool.java @@ -1,4 +1,4 @@ -package com.gitee.sop.websiteserver.bean; +package com.gitee.sop.registryapi.bean; import lombok.Data; import lombok.Getter; @@ -110,7 +110,7 @@ public class HttpTool { * @return * @throws IOException */ - public String request(String url, Map form, Map header, HTTPMethod method) throws IOException { + public String request(String url, Map form, Map header, HTTPMethod method) throws IOException { Request.Builder requestBuilder = buildRequestBuilder(url, form, method); // 添加header addHeader(requestBuilder, header); @@ -153,7 +153,7 @@ public class HttpTool { } } - public static Request.Builder buildRequestBuilder(String url, Map form, HTTPMethod method) { + public static Request.Builder buildRequestBuilder(String url, Map form, HTTPMethod method) { switch (method) { case GET: return new Request.Builder() @@ -178,18 +178,18 @@ public class HttpTool { } } - public static HttpUrl buildHttpUrl(String url, Map form) { + public static HttpUrl buildHttpUrl(String url, Map form) { HttpUrl.Builder urlBuilder = HttpUrl.parse(url).newBuilder(); - for (Map.Entry entry : form.entrySet()) { - urlBuilder.addQueryParameter(entry.getKey(), entry.getValue()); + for (Map.Entry entry : form.entrySet()) { + urlBuilder.addQueryParameter(entry.getKey(), String.valueOf(entry.getValue())); } return urlBuilder.build(); } - public static FormBody buildFormBody(Map form) { + public static FormBody buildFormBody(Map form) { FormBody.Builder paramBuilder = new FormBody.Builder(StandardCharsets.UTF_8); - for (Map.Entry entry : form.entrySet()) { - paramBuilder.add(entry.getKey(), entry.getValue()); + for (Map.Entry entry : form.entrySet()) { + paramBuilder.add(entry.getKey(), String.valueOf(entry.getValue())); } return paramBuilder.build(); } diff --git a/sop-common/sop-registry-api/src/main/java/com/gitee/sop/registryapi/service/impl/RegistryServiceNacos.java b/sop-common/sop-registry-api/src/main/java/com/gitee/sop/registryapi/service/impl/RegistryServiceNacos.java index 8d24ad8a..6f7746d2 100644 --- a/sop-common/sop-registry-api/src/main/java/com/gitee/sop/registryapi/service/impl/RegistryServiceNacos.java +++ b/sop-common/sop-registry-api/src/main/java/com/gitee/sop/registryapi/service/impl/RegistryServiceNacos.java @@ -11,7 +11,6 @@ import com.gitee.sop.registryapi.bean.HttpTool; import com.gitee.sop.registryapi.bean.ServiceInfo; import com.gitee.sop.registryapi.bean.ServiceInstance; import com.gitee.sop.registryapi.service.RegistryService; -import okhttp3.OkHttpClient; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Value; import org.springframework.util.CollectionUtils; @@ -29,8 +28,6 @@ import java.util.Map; */ public class RegistryServiceNacos implements RegistryService { - OkHttpClient client = new OkHttpClient(); - static HttpTool httpTool = new HttpTool(); @Value("${registry.nacos-server-addr:}") diff --git a/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/bean/ParamNames.java b/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/bean/ParamNames.java index be46aab4..d2e18d00 100644 --- a/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/bean/ParamNames.java +++ b/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/bean/ParamNames.java @@ -42,8 +42,6 @@ public class ParamNames { /** 请求参数的集合,最大长度不限,除公共参数外所有请求参数都必须放在这个参数中传递,具体参照各产品快速接入文档 */ public static String BIZ_CONTENT_NAME = "biz_content"; - public static String HEADER_VERSION_NAME = "sop_gray_version"; - /** 时间戳格式 */ public static String TIMESTAMP_PATTERN = "yyyy-MM-dd HH:mm:ss"; diff --git a/sop-gateway/src/main/java/com/gitee/sop/gateway/entity/ConfigGrayUserkey.java b/sop-gateway/src/main/java/com/gitee/sop/gateway/entity/ConfigGrayUserkey.java index a69a2f72..9f91de18 100644 --- a/sop-gateway/src/main/java/com/gitee/sop/gateway/entity/ConfigGrayUserkey.java +++ b/sop-gateway/src/main/java/com/gitee/sop/gateway/entity/ConfigGrayUserkey.java @@ -25,6 +25,9 @@ public class ConfigGrayUserkey { /** 数据库字段:id */ private Long id; + /** serviceId, 数据库字段:service_id */ + private String serviceId; + /** instanceId, 数据库字段:instance_id */ private String instanceId; diff --git a/sop-gateway/src/main/java/com/gitee/sop/gateway/loadbalancer/EnvironmentServerChooser.java b/sop-gateway/src/main/java/com/gitee/sop/gateway/loadbalancer/EnvironmentServerChooser.java index 2ff9be85..6908bd78 100644 --- a/sop-gateway/src/main/java/com/gitee/sop/gateway/loadbalancer/EnvironmentServerChooser.java +++ b/sop-gateway/src/main/java/com/gitee/sop/gateway/loadbalancer/EnvironmentServerChooser.java @@ -1,15 +1,13 @@ package com.gitee.sop.gateway.loadbalancer; -import com.gitee.sop.gateway.manager.DbUserKeyManager; +import com.gitee.sop.gateway.manager.DbEnvGrayManager; import com.gitee.sop.gatewaycommon.bean.SpringContext; import com.gitee.sop.gatewaycommon.param.ApiParam; import com.gitee.sop.gatewaycommon.param.Param; -import com.gitee.sop.gatewaycommon.param.ParamNames; import com.gitee.sop.gatewaycommon.zuul.ZuulContext; import com.gitee.sop.gatewaycommon.zuul.loadbalancer.BaseServerChooser; import com.netflix.loadbalancer.Server; import com.netflix.niws.loadbalancer.DiscoveryEnabledServer; -import com.netflix.zuul.context.RequestContext; import org.apache.commons.lang3.StringUtils; import javax.servlet.http.HttpServletRequest; @@ -30,6 +28,24 @@ public class EnvironmentServerChooser extends BaseServerChooser { */ private static final String PRE_DOMAIN = "localhost"; + @Override + protected boolean isPreServer(Server server) { + String env = getEnvValue(server); + return ENV_PRE_VALUE.equals(env); + } + + @Override + protected boolean isGrayServer(Server server) { + String env = getEnvValue(server); + return ENV_GRAY_VALUE.equals(env); + } + + private String getEnvValue(Server server) { + // eureka存储的metadata + Map metadata = ((DiscoveryEnabledServer) server).getInstanceInfo().getMetadata(); + return metadata.get(MEDATA_KEY_ENV); + } + @Override protected boolean match(Server server) { // eureka存储的metadata @@ -70,6 +86,7 @@ public class EnvironmentServerChooser extends BaseServerChooser { * @param request request * @return 返回true:可以进入到预发环境 */ + @Override protected boolean canVisitPre(Server server, HttpServletRequest request) { String serverName = request.getServerName(); return PRE_DOMAIN.equals(serverName); @@ -83,19 +100,19 @@ public class EnvironmentServerChooser extends BaseServerChooser { */ protected boolean canVisitGray(Server server, HttpServletRequest request) { ApiParam apiParam = ZuulContext.getApiParam(); - DbUserKeyManager userKeyManager = SpringContext.getBean(DbUserKeyManager.class); + DbEnvGrayManager userKeyManager = SpringContext.getBean(DbEnvGrayManager.class); boolean canVisit = false; - if (this.isGrayUser(apiParam, userKeyManager, server, request)) { + /*if (this.isGrayUser(apiParam, userKeyManager, server, request)) { // 指定灰度版本号 String instanceId = server.getMetaInfo().getInstanceId(); String newVersion = userKeyManager.getVersion(instanceId, apiParam.fetchNameVersion()); if (newVersion != null) { // 在header中设置新的版本号,然后微服务端先获取这个新版本号 - RequestContext.getCurrentContext().addZuulRequestHeader(ParamNames.GRAY_HEADER_VERSION_NAME, newVersion); + RequestContext.getCurrentContext().addZuulRequestHeader(ParamNames.HEADER_VERSION_NAME, newVersion); canVisit = true; } - } - return canVisit; + }*/ + return this.isGrayUser(apiParam, userKeyManager, server, request); } @@ -108,7 +125,7 @@ public class EnvironmentServerChooser extends BaseServerChooser { * @param request request * @return true:是 */ - protected boolean isGrayUser(Param param, DbUserKeyManager userKeyManager, Server server, HttpServletRequest request) { + protected boolean isGrayUser(Param param, DbEnvGrayManager userKeyManager, Server server, HttpServletRequest request) { String instanceId = server.getMetaInfo().getInstanceId(); // 这里的灰度用户为appKey,包含此appKey则为灰度用户,允许访问 String appKey = param.fetchAppKey(); diff --git a/sop-gateway/src/main/java/com/gitee/sop/gateway/manager/DbUserKeyManager.java b/sop-gateway/src/main/java/com/gitee/sop/gateway/manager/DbEnvGrayManager.java similarity index 94% rename from sop-gateway/src/main/java/com/gitee/sop/gateway/manager/DbUserKeyManager.java rename to sop-gateway/src/main/java/com/gitee/sop/gateway/manager/DbEnvGrayManager.java index a4b12ec6..ec1c6ab1 100644 --- a/sop-gateway/src/main/java/com/gitee/sop/gateway/manager/DbUserKeyManager.java +++ b/sop-gateway/src/main/java/com/gitee/sop/gateway/manager/DbEnvGrayManager.java @@ -6,7 +6,7 @@ import com.gitee.sop.gateway.entity.ConfigGrayUserkey; import com.gitee.sop.gateway.mapper.ConfigGrayUserkeyMapper; import com.gitee.sop.gatewaycommon.bean.ChannelMsg; import com.gitee.sop.gatewaycommon.bean.UserKeyDefinition; -import com.gitee.sop.gatewaycommon.manager.DefaultUserKeyManager; +import com.gitee.sop.gatewaycommon.manager.DefaultEnvGrayManager; import com.gitee.sop.gatewaycommon.manager.ZookeeperContext; import com.gitee.sop.gatewaycommon.zuul.loadbalancer.ServiceGrayConfig; import lombok.extern.slf4j.Slf4j; @@ -29,7 +29,7 @@ import java.util.stream.Stream; */ @Slf4j @Service -public class DbUserKeyManager extends DefaultUserKeyManager { +public class DbEnvGrayManager extends DefaultEnvGrayManager { private static final int STATUS_ENABLE = 1; @@ -46,6 +46,7 @@ public class DbUserKeyManager extends DefaultUserKeyManager { List list = configGrayUserkeyMapper.list(query); for (ConfigGrayUserkey configGrayUserkey : list) { this.setServiceGrayConfig(configGrayUserkey); + this.addServiceInstance(configGrayUserkey.getServiceId(), configGrayUserkey.getInstanceId()); } } diff --git a/sop-gateway/src/main/java/com/gitee/sop/gateway/manager/ManagerInitializer.java b/sop-gateway/src/main/java/com/gitee/sop/gateway/manager/ManagerInitializer.java index d36cd8fc..9f260f1b 100644 --- a/sop-gateway/src/main/java/com/gitee/sop/gateway/manager/ManagerInitializer.java +++ b/sop-gateway/src/main/java/com/gitee/sop/gateway/manager/ManagerInitializer.java @@ -14,6 +14,6 @@ public class ManagerInitializer { apiConfig.setRouteConfigManager(new DbRouteConfigManager()); apiConfig.setLimitConfigManager(new DbLimitConfigManager()); apiConfig.setIpBlacklistManager(new DbIPBlacklistManager()); - apiConfig.setUserKeyManager(new DbUserKeyManager()); + apiConfig.setUserKeyManager(new DbEnvGrayManager()); } } diff --git a/sop-test/src/main/java/com/gitee/sop/test/HttpTool.java b/sop-test/src/main/java/com/gitee/sop/test/HttpTool.java index 2409977d..39025dc0 100644 --- a/sop-test/src/main/java/com/gitee/sop/test/HttpTool.java +++ b/sop-test/src/main/java/com/gitee/sop/test/HttpTool.java @@ -110,7 +110,7 @@ public class HttpTool { * @return * @throws IOException */ - public String request(String url, Map form, Map header, HTTPMethod method) throws IOException { + public String request(String url, Map form, Map header, HTTPMethod method) throws IOException { Request.Builder requestBuilder = buildRequestBuilder(url, form, method); // 添加header addHeader(requestBuilder, header); @@ -153,7 +153,7 @@ public class HttpTool { } } - public static Request.Builder buildRequestBuilder(String url, Map form, HTTPMethod method) { + public static Request.Builder buildRequestBuilder(String url, Map form, HTTPMethod method) { switch (method) { case GET: return new Request.Builder() @@ -178,18 +178,18 @@ public class HttpTool { } } - public static HttpUrl buildHttpUrl(String url, Map form) { + public static HttpUrl buildHttpUrl(String url, Map form) { HttpUrl.Builder urlBuilder = HttpUrl.parse(url).newBuilder(); - for (Map.Entry entry : form.entrySet()) { - urlBuilder.addQueryParameter(entry.getKey(), entry.getValue()); + for (Map.Entry entry : form.entrySet()) { + urlBuilder.addQueryParameter(entry.getKey(), String.valueOf(entry.getValue())); } return urlBuilder.build(); } - public static FormBody buildFormBody(Map form) { + public static FormBody buildFormBody(Map form) { FormBody.Builder paramBuilder = new FormBody.Builder(StandardCharsets.UTF_8); - for (Map.Entry entry : form.entrySet()) { - paramBuilder.add(entry.getKey(), entry.getValue()); + for (Map.Entry entry : form.entrySet()) { + paramBuilder.add(entry.getKey(), String.valueOf(entry.getValue())); } return paramBuilder.build(); }