支持https

修改router header 模式 bug
This commit is contained in:
ztzh_xieyun 2024-05-14 13:44:21 +08:00
parent bccdfab4ba
commit 8312736200
7 changed files with 25 additions and 19 deletions

View File

@ -43,6 +43,7 @@ import io.vertx.core.http.HttpClient;
import io.vertx.core.http.HttpMethod; import io.vertx.core.http.HttpMethod;
import io.vertx.core.http.HttpServer; import io.vertx.core.http.HttpServer;
import io.vertx.core.http.HttpServerOptions; import io.vertx.core.http.HttpServerOptions;
import io.vertx.core.net.JksOptions;
import io.vertx.core.spi.cluster.ClusterManager; import io.vertx.core.spi.cluster.ClusterManager;
import io.vertx.ext.web.Route; import io.vertx.ext.web.Route;
import io.vertx.ext.web.Router; import io.vertx.ext.web.Router;
@ -82,12 +83,13 @@ public class AppConfigHandler {
// 负载均衡路由类型 appCode:apiCode - routerType // 负载均衡路由类型 appCode:apiCode - routerType
// 执行流程 routerType= <br/> // 执行流程 routerType= <br/>
// 1serviceNodel="NORMAL", serviceNodel="ROUTE" and RouteType = "WEIGHT_ROUTE" <br/> // 1serviceNodel="NORMAL", serviceNodel="ROUTE" and RouteType = "WEIGHT_ROUTE"
// <br/>
// return LOADBALANCING_MAP // return LOADBALANCING_MAP
// 2serviceNodel="ROUTE", RouteType = "HEADER_ROUTE" <br/> // 2serviceNodel="ROUTE", RouteType = "HEADER_ROUTE" <br/>
// return APICODE_CONFIG_ROUTERCONENT_MAP // return APICODE_CONFIG_ROUTERCONENT_MAP
private static ConcurrentHashMap<String, Integer> APICODE_CONFIG_ROUTERTYPE_MAP = new ConcurrentHashMap<>(); private static ConcurrentHashMap<String, Integer> APICODE_CONFIG_ROUTERTYPE_MAP = new ConcurrentHashMap<>();
private static ConcurrentHashMap<String, List<RouteContent>> APICODE_CONFIG_ROUTERCONENT_MAP = new ConcurrentHashMap<>(); private static ConcurrentHashMap<String, List<RouteContent>> APICODE_CONFIG_ROUTERCONENT_MAP = new ConcurrentHashMap<>();
// apiCode熔断配置 appCode:apiCode - CircuitBreaker // apiCode熔断配置 appCode:apiCode - CircuitBreaker
private static ConcurrentHashMap<String, CircuitBreaker> APICODE_CONFIG_CIRCUIT_BREAKER_MAP = new ConcurrentHashMap<>(); private static ConcurrentHashMap<String, CircuitBreaker> APICODE_CONFIG_CIRCUIT_BREAKER_MAP = new ConcurrentHashMap<>();
@ -95,23 +97,22 @@ public class AppConfigHandler {
// 禁用appCode // 禁用appCode
private static ConcurrentHashSet<String> DISABLED_APPCODE = new ConcurrentHashSet<String>(); private static ConcurrentHashSet<String> DISABLED_APPCODE = new ConcurrentHashSet<String>();
public static Integer routerType(String key) { public static Integer routerType(String key) {
return APICODE_CONFIG_ROUTERTYPE_MAP.get(key) != null ? APICODE_CONFIG_ROUTERTYPE_MAP.get(key) : 1; return APICODE_CONFIG_ROUTERTYPE_MAP.get(key) != null ? APICODE_CONFIG_ROUTERTYPE_MAP.get(key) : 1;
} }
public static List<RouteContent> routerConentList(String key) { public static List<RouteContent> routerConentList(String key) {
return APICODE_CONFIG_ROUTERCONENT_MAP.get(key); return APICODE_CONFIG_ROUTERCONENT_MAP.get(key);
} }
public static Integer requestModel() { public static Integer requestModel() {
return sacVertxConfig.getRequestModel(); return sacVertxConfig.getRequestModel();
} }
public static String sacResponseHeaderKey() { public static String sacResponseHeaderKey() {
return sacVertxConfig.getSacResponseHeaderKey(); return sacVertxConfig.getSacResponseHeaderKey();
} }
public static String rpcUri() { public static String rpcUri() {
return sacVertxConfig.getRpcUri(); return sacVertxConfig.getRpcUri();
} }
@ -299,11 +300,11 @@ public class AppConfigHandler {
if (appConfig.getAppCurrentLimitingConfig() != null) { if (appConfig.getAppCurrentLimitingConfig() != null) {
initRateLimiter(appCode, appConfig.getAppCurrentLimitingConfig(), GLOBAL_APP_CURRENT_LIMITING_MAP); initRateLimiter(appCode, appConfig.getAppCurrentLimitingConfig(), GLOBAL_APP_CURRENT_LIMITING_MAP);
} }
// app router负载均衡 // app router负载均衡
int routerType = 1;
List<RouteContent> routeContentList = null; List<RouteContent> routeContentList = null;
for (SacService sacService : appConfig.getService()) { for (SacService sacService : appConfig.getService()) {
int routerType = 1;
List<Node> nodeList = new ArrayList<>(); List<Node> nodeList = new ArrayList<>();
// 获取service模式 // 获取service模式
if (StringUtils.equals(sacService.getServiceModel(), "NORMAL")) { if (StringUtils.equals(sacService.getServiceModel(), "NORMAL")) {
@ -338,7 +339,7 @@ public class AppConfigHandler {
for (ApiConfig apiConfig : sacService.getApiConfig()) { for (ApiConfig apiConfig : sacService.getApiConfig()) {
String key = appCode + ":" + apiConfig.getApiCode(); String key = appCode + ":" + apiConfig.getApiCode();
APICODE_CONFIG_MAP.put(key, apiConfig); APICODE_CONFIG_MAP.put(key, apiConfig);
// 负载均衡模式 // 负载均衡模式
APICODE_CONFIG_ROUTERTYPE_MAP.put(key, routerType); APICODE_CONFIG_ROUTERTYPE_MAP.put(key, routerType);
switch (routerType) { switch (routerType) {
@ -480,7 +481,7 @@ public class AppConfigHandler {
} }
}); });
} }
/*** /***
* 发布消息订阅消息 * 发布消息订阅消息
* *
@ -501,6 +502,10 @@ public class AppConfigHandler {
// 创建HTTP监听 // 创建HTTP监听
// 所有ip都能访问 // 所有ip都能访问
HttpServerOptions httpServerOptions = new HttpServerOptions().setHost("0.0.0.0"); HttpServerOptions httpServerOptions = new HttpServerOptions().setHost("0.0.0.0");
if (sacVertxConfig.isSSLs()) {
httpServerOptions.setSsl(true)
.setKeyStoreOptions(new JksOptions().setPassword("changeit").setPath("keystore.jks"));
}
HttpServer server = VERTX.createHttpServer(httpServerOptions); HttpServer server = VERTX.createHttpServer(httpServerOptions);
Router mainHttpRouter = Router.router(VERTX); Router mainHttpRouter = Router.router(VERTX);
Integer serverPort = vertxConfig.getPort() == null ? sacVertxConfig.getPort() : vertxConfig.getPort(); Integer serverPort = vertxConfig.getPort() == null ? sacVertxConfig.getPort() : vertxConfig.getPort();
@ -563,10 +568,9 @@ public class AppConfigHandler {
} else { } else {
route = mainHttpRouter.route(); route = mainHttpRouter.route();
} }
route.handler(ParameterCheckHandler.create()) route.handler(ParameterCheckHandler.create()).handler(AppRateLimitHandler.create(rateLimitModel))
.handler(AppRateLimitHandler.create(rateLimitModel)).handler(ApiRateLimitHandler.create(rateLimitModel)) .handler(ApiRateLimitHandler.create(rateLimitModel)).handler(BodyHandler.create())
.handler(BodyHandler.create()).handler(ProxyHandler.create(mainWebClient, proxy)) .handler(ProxyHandler.create(mainWebClient, proxy)).failureHandler(RestfulFailureHandler.create());
.failureHandler(RestfulFailureHandler.create());
// mainHttpRouter.route().handler(ProxyHandler.create(mainWebClient, proxy)); // mainHttpRouter.route().handler(ProxyHandler.create(mainWebClient, proxy));
} }

View File

@ -34,6 +34,9 @@ public class SacVertxConfig {
@Value("${server.vertx.rpcUri:/rpc}") @Value("${server.vertx.rpcUri:/rpc}")
private String rpcUri; private String rpcUri;
@Value("${server.vertx.isSSL:false}")
private boolean isSSLs;
@Value("${server.vertx.deploymentMode:1}") @Value("${server.vertx.deploymentMode:1}")
private Integer deploymentMode; private Integer deploymentMode;

View File

@ -3,8 +3,6 @@ package com.sf.vertx.utils;
import java.util.List; import java.util.List;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import org.apache.commons.lang3.StringUtils;
import com.sf.vertx.api.pojo.Node; import com.sf.vertx.api.pojo.Node;
import com.sf.vertx.api.pojo.RouteContent; import com.sf.vertx.api.pojo.RouteContent;
import com.sf.vertx.arithmetic.roundRobin.SacLoadBalancing; import com.sf.vertx.arithmetic.roundRobin.SacLoadBalancing;

View File

@ -3,12 +3,13 @@ server:
vertx: vertx:
deploymentMode: 1 # 1:单机 2:集群 deploymentMode: 1 # 1:单机 2:集群
requestModel: 2 # 1: 客户端传递uri. 2: uri vertx代理,不对客户端暴露uri requestModel: 2 # 1: 客户端传递uri. 2: uri vertx代理,不对客户端暴露uri
isSSL: false # vertx https或者http启动, ssl 证书有过期时间
rpcUri: /rpc rpcUri: /rpc
sacResponseHeaderKey: sacErrorCode sacResponseHeaderKey: sacErrorCode
environment: dev environment: dev
server: server:
default: default:
port: 80 port: 80 # https端口配置为443
cluster: cluster:
ip: 127.0.0.1 ip: 127.0.0.1
clusterName: sac-dev clusterName: sac-dev

Binary file not shown.

Binary file not shown.

Binary file not shown.