✨ Introducing new features. #2022 自定义 Spring Boot FailureAnalyzer 避免路由没初始化 启动网关导致404

This commit is contained in:
lengleng committed 2020-11-19 15:38:50 +08:00
1 parent 96a5d4c3f5
commit 438acdd56e
6 files changed
+106 -1

No files matched your search

@@ -19,6 +19,8 @@ package com.pig4cloud.pigx.common.gateway.configuration;
import com.pig4cloud.pigx.common.core.constant.CacheConstants;
import com.pig4cloud.pigx.common.core.util.SpringContextHolder;
import com.pig4cloud.pigx.common.gateway.exception.RouteCheckException;
import com.pig4cloud.pigx.common.gateway.support.DynamicRouteHealthIndicator;
import com.pig4cloud.pigx.common.gateway.support.RouteCacheHolder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
@@ -29,8 +31,10 @@ import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.listener.ChannelTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
/**
* @author lengleng
@@ -46,6 +50,7 @@ public class DynamicRouteAutoConfiguration {
/**
* 配置文件设置为空 redis 加载为准
*
* @return
*/
@Bean
@@ -55,6 +60,7 @@ public class DynamicRouteAutoConfiguration {
/**
* redis 监听配置
*
* @param redisConnectionFactory redis 配置
* @return
*/
@@ -71,4 +77,20 @@ public class DynamicRouteAutoConfiguration {
return container;
}
/**
* 动态路由监控检查
*
* @param redisTemplate redis
* @return
*/
@Bean
public DynamicRouteHealthIndicator healthIndicator(RedisTemplate redisTemplate) {
redisTemplate.setKeySerializer(new StringRedisSerializer());
if (!redisTemplate.hasKey(CacheConstants.ROUTE_KEY)) {
throw new RouteCheckException("路由信息未初始化,网关启动失败");
}
return new DynamicRouteHealthIndicator(redisTemplate);
}
}
@@ -0,0 +1,27 @@
package com.pig4cloud.pigx.common.gateway.exception;
/**
* @author lengleng
* @date 2020/11/19
*/
public class RouteCheckException extends RuntimeException {
public RouteCheckException() {
}
public RouteCheckException(String message) {
super(message);
}
public RouteCheckException(String message, Throwable cause) {
super(message, cause);
}
public RouteCheckException(Throwable cause) {
super(cause);
}
public RouteCheckException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
}
@@ -0,0 +1,20 @@
package com.pig4cloud.pigx.common.gateway.exception;
import org.springframework.boot.diagnostics.AbstractFailureAnalyzer;
import org.springframework.boot.diagnostics.FailureAnalysis;
/**
* @author lengleng
* @date 2020/11/19
* <p>
* 格式化异常信息,方便启动时观察。
*/
public class RouteCheckFailureAnalyzer extends AbstractFailureAnalyzer<RouteCheckException> {
@Override
protected FailureAnalysis analyze(Throwable rootFailure, RouteCheckException cause) {
return new FailureAnalysis(cause.getMessage(),
"解决方案参考: http://t.cn/A6Gl3sDJ", cause);
}
}
@@ -0,0 +1,33 @@
package com.pig4cloud.pigx.common.gateway.support;
import com.pig4cloud.pigx.common.core.constant.CacheConstants;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.actuate.health.AbstractHealthIndicator;
import org.springframework.boot.actuate.health.Health;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
/**
* @author lengleng
* @date 2020/11/19
* <p>
* 动态路由检查检查
*/
@Slf4j
@RequiredArgsConstructor
public class DynamicRouteHealthIndicator extends AbstractHealthIndicator {
private final RedisTemplate redisTemplate;
@Override
protected void doHealthCheck(Health.Builder builder) {
redisTemplate.setKeySerializer(new StringRedisSerializer());
if (redisTemplate.hasKey(CacheConstants.ROUTE_KEY)) {
builder.up();
} else {
log.warn("动态路由监控检查失败,当前无路由配置");
builder.down();
}
}
}
@@ -21,6 +21,7 @@ import cn.hutool.core.collection.CollUtil;
import com.pig4cloud.pigx.common.core.constant.CacheConstants;
import com.pig4cloud.pigx.common.gateway.vo.RouteDefinitionVo;
import lombok.AllArgsConstructor;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.cloud.gateway.route.RouteDefinition;
@@ -42,7 +43,7 @@ import java.util.List;
*/
@Slf4j
@Component
@AllArgsConstructor
@RequiredArgsConstructor
public class RedisRouteDefinitionWriter implements RouteDefinitionRepository {
private final RedisTemplate redisTemplate;
@@ -0,0 +1,2 @@
org.springframework.boot.diagnostics.FailureAnalyzer=\
com.pig4cloud.pigx.common.gateway.exception.RouteCheckFailureAnalyzer