Skip to content

Commit

Permalink
add: 新增门户网关的健康检查功能
Browse files Browse the repository at this point in the history
  • Loading branch information
yangfuhai committed Feb 8, 2021
1 parent c5a63ea commit ab49b95
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
17 changes: 15 additions & 2 deletions src/main/java/io/jboot/components/gateway/JbootGatewayConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

// 是否启用
Expand Down Expand Up @@ -99,7 +104,7 @@ public void setUri(String[] uri) {
this.uri = uri;
}

public String[] getHealthUri(){
public String[] getHealthUris() {
Set<String> set = Sets.newHashSet(uri);
if (unHealthUris.size() > 0) {
set.removeAll(unHealthUris);
Expand All @@ -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;
}
Expand Down Expand Up @@ -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);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions src/test/java/io/jboot/test/gateway/App1Stater.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
1 change: 1 addition & 0 deletions src/test/java/io/jboot/test/gateway/App2Stater.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
1 change: 1 addition & 0 deletions src/test/java/io/jboot/test/gateway/App3Stater.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
1 change: 1 addition & 0 deletions src/test/resources/jboot.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit ab49b95

Please sign in to comment.