diff --git a/src/main/java/io/jboot/components/gateway/GatewayLoadBalanceStrategy.java b/src/main/java/io/jboot/components/gateway/GatewayLoadBalanceStrategy.java index 70427bce4..a94a95fc1 100644 --- a/src/main/java/io/jboot/components/gateway/GatewayLoadBalanceStrategy.java +++ b/src/main/java/io/jboot/components/gateway/GatewayLoadBalanceStrategy.java @@ -28,7 +28,7 @@ public interface GatewayLoadBalanceStrategy { * 默认的负载均衡策略,随机返回一个 url */ GatewayLoadBalanceStrategy DEFAULT_STRATEGY = (config, request) -> { - String[] urls = config.getHealthUri(); + String[] urls = config.getHealthUris(); if (urls == null || urls.length == 0) { return null; } else if (urls.length == 1) { diff --git a/src/main/java/io/jboot/components/gateway/JbootGatewayConfig.java b/src/main/java/io/jboot/components/gateway/JbootGatewayConfig.java index 67f3a6499..48177021c 100644 --- a/src/main/java/io/jboot/components/gateway/JbootGatewayConfig.java +++ b/src/main/java/io/jboot/components/gateway/JbootGatewayConfig.java @@ -35,7 +35,12 @@ public class JbootGatewayConfig implements Serializable { private String name; private String[] uri; + + // 是否启用健康检查 + private boolean uriHealthCheckEnable; + // URI 健康检查路径,要求服务 statusCode = 200 + // 当配置 uriHealthCheckPath 后,健康检查的 url 地址为 uri + uriHealthCheckPath private String uriHealthCheckPath; // 是否启用 @@ -99,7 +104,7 @@ public void setUri(String[] uri) { this.uri = uri; } - public String[] getHealthUri(){ + public String[] getHealthUris() { Set set = Sets.newHashSet(uri); if (unHealthUris.size() > 0) { set.removeAll(unHealthUris); @@ -117,6 +122,14 @@ public String[] getHealthUri(){ return null; } + public boolean isUriHealthCheckEnable() { + return uriHealthCheckEnable; + } + + public void setUriHealthCheckEnable(boolean uriHealthCheckEnable) { + this.uriHealthCheckEnable = uriHealthCheckEnable; + } + public String getUriHealthCheckPath() { return uriHealthCheckPath; } @@ -468,7 +481,7 @@ public void addUnHealthUri(String uri) { public void removeUnHealthUri(String uri) { - if (unHealthUris.size() > 0 ) { + if (unHealthUris.size() > 0) { unHealthUris.remove(uri); } } diff --git a/src/main/java/io/jboot/components/gateway/JbootGatewayManager.java b/src/main/java/io/jboot/components/gateway/JbootGatewayManager.java index 51acd6a42..c314fde25 100644 --- a/src/main/java/io/jboot/components/gateway/JbootGatewayManager.java +++ b/src/main/java/io/jboot/components/gateway/JbootGatewayManager.java @@ -101,11 +101,10 @@ private void startHealthCheck() { */ private void doHealthCheck() { for (JbootGatewayConfig config : configMap.values()) { - String healthCheckPath = config.getUriHealthCheckPath(); - if (StrUtil.isNotBlank(healthCheckPath)) { + if (config.isUriHealthCheckEnable() && StrUtil.isNotBlank(config.getUriHealthCheckPath())) { String[] uris = config.getUri(); for (String uri : uris) { - String url = uri + healthCheckPath; + String url = uri + config.getUriHealthCheckPath(); if (getHttpCode(url) == 200) { config.removeUnHealthUri(uri); } else { diff --git a/src/test/java/io/jboot/test/gateway/App1Stater.java b/src/test/java/io/jboot/test/gateway/App1Stater.java index 73a3238e8..b418b8910 100644 --- a/src/test/java/io/jboot/test/gateway/App1Stater.java +++ b/src/test/java/io/jboot/test/gateway/App1Stater.java @@ -6,6 +6,7 @@ public class App1Stater { public static void main(String[] args){ JbootApplication.setBootArg("undertow.port",9901); + JbootApplication.setBootArg("jboot.gateway.enable ",false); JbootApplication.run(args); } } diff --git a/src/test/java/io/jboot/test/gateway/App2Stater.java b/src/test/java/io/jboot/test/gateway/App2Stater.java index 314596fd9..f12ed8744 100644 --- a/src/test/java/io/jboot/test/gateway/App2Stater.java +++ b/src/test/java/io/jboot/test/gateway/App2Stater.java @@ -6,6 +6,7 @@ public class App2Stater { public static void main(String[] args){ JbootApplication.setBootArg("undertow.port",9902); + JbootApplication.setBootArg("jboot.gateway.enable ",false); JbootApplication.run(args); } } diff --git a/src/test/java/io/jboot/test/gateway/App3Stater.java b/src/test/java/io/jboot/test/gateway/App3Stater.java index 2d45f9c29..dcb73c8e1 100644 --- a/src/test/java/io/jboot/test/gateway/App3Stater.java +++ b/src/test/java/io/jboot/test/gateway/App3Stater.java @@ -6,6 +6,7 @@ public class App3Stater { public static void main(String[] args){ JbootApplication.setBootArg("undertow.port",9903); + JbootApplication.setBootArg("jboot.gateway.enable ",false); JbootApplication.run(args); } } diff --git a/src/test/resources/jboot.properties b/src/test/resources/jboot.properties index 999199bc9..f39d9bd94 100644 --- a/src/test/resources/jboot.properties +++ b/src/test/resources/jboot.properties @@ -40,6 +40,7 @@ config.test.test.ccc.type = type3 # 配置 gateway ,当访问 /gateway 的时候,自动路由到 /gateway/render jboot.gateway.enable = true jboot.gateway.uri = http://127.0.0.1:9901/gateway/render,http://127.0.0.1:9902/gateway/render,http://127.0.0.1:9903/gateway/render +jboot.gateway.uriHealthCheckEnable = true jboot.gateway.uriHealthCheckPath = / jboot.gateway.pathEquals = /gateway #jboot.gateway.proxyContentType = image/jpeg