From 4ddb89f27aca18d9d7b570c76805741842bc21ff Mon Sep 17 00:00:00 2001 From: haoxr <1490493387@qq.com> Date: Sun, 11 Apr 2021 23:53:20 +0800 Subject: [PATCH] =?UTF-8?q?feat:Sentinel=E7=BD=91=E5=85=B3=E6=B5=81?= =?UTF-8?q?=E6=8E=A7=E6=95=B4=E5=90=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../admin/controller/DashboardController.java | 2 +- .../com/youlai/common/result/ResultCode.java | 10 +++++- .../common/web/aspect/LoginLogAspect.java | 6 ++-- .../gateway/config/ResourceServerConfig.java | 1 - .../gateway/config/SentinelConfiguration.java | 31 +++++++++++++++++++ .../src/main/resources/bootstrap-dev.yml | 3 +- 6 files changed, 46 insertions(+), 7 deletions(-) create mode 100644 youlai-gateway/src/main/java/com/youlai/gateway/config/SentinelConfiguration.java diff --git a/youlai-admin/admin-boot/src/main/java/com/youlai/admin/controller/DashboardController.java b/youlai-admin/admin-boot/src/main/java/com/youlai/admin/controller/DashboardController.java index 999f38edc..c54c197ea 100644 --- a/youlai-admin/admin-boot/src/main/java/com/youlai/admin/controller/DashboardController.java +++ b/youlai-admin/admin-boot/src/main/java/com/youlai/admin/controller/DashboardController.java @@ -65,7 +65,7 @@ public class DashboardController { private long getTodayIpCount() { String date = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd")); TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("date", date); - String indexName = ESConstants.LOGIN_INDEX_PATTERN + date; //索引名称 + String indexName = ESConstants.LOGIN_INDEX_PREFIX + date; //索引名称 long todayIpCount = elasticSearchService.countDistinct(termQueryBuilder, "clientIP.keyword", indexName); return todayIpCount; } diff --git a/youlai-common/common-core/src/main/java/com/youlai/common/result/ResultCode.java b/youlai-common/common-core/src/main/java/com/youlai/common/result/ResultCode.java index 37ab7e3fd..96f915d56 100644 --- a/youlai-common/common-core/src/main/java/com/youlai/common/result/ResultCode.java +++ b/youlai-common/common-core/src/main/java/com/youlai/common/result/ResultCode.java @@ -47,7 +47,7 @@ public enum ResultCode implements IResultCode, Serializable { SYSTEM_ORDER_PROCESSING_TIMEOUT("B0100", "系统订单处理超时"), SYSTEM_DISASTER_RECOVERY_TRIGGER("B0200", "系统容灾功能被出发"), - SYSTEM_LIMITING("B0210", "系统限流"), + FLOW_LIMITING("B0210", "系统限流"), SYSTEM_FUNCTION_DEGRADATION("B0220", "系统功能降级"), SYSTEM_RESOURCE_ERROR("B0300", "系统资源异常"), @@ -85,4 +85,12 @@ public enum ResultCode implements IResultCode, Serializable { private String code; private String msg; + + @Override + public String toString() { + return "{" + + "\"code\":\"" + code + '\"' + + ", \"msg\":\"" + msg + '\"' + + '}'; + } } diff --git a/youlai-common/common-web/src/main/java/com/youlai/common/web/aspect/LoginLogAspect.java b/youlai-common/common-web/src/main/java/com/youlai/common/web/aspect/LoginLogAspect.java index ce50914cf..2247284a1 100644 --- a/youlai-common/common-web/src/main/java/com/youlai/common/web/aspect/LoginLogAspect.java +++ b/youlai-common/common-web/src/main/java/com/youlai/common/web/aspect/LoginLogAspect.java @@ -29,11 +29,11 @@ import java.time.format.DateTimeFormatter; * @author hxr * @date 2021-03-01 */ -@Aspect +/*@Aspect @Component @AllArgsConstructor @Slf4j -@ConditionalOnProperty(value = "spring.application.name", havingValue = "youlai-auth") +@ConditionalOnProperty(value = "spring.application.name", havingValue = "youlai-auth")*/ public class LoginLogAspect { @Pointcut("execution(public * com.youlai.auth.controller.AuthController.postAccessToken(..))") @@ -86,7 +86,7 @@ public class LoginLogAspect { MDC.put("token", token); MDC.put("clientIP", clientIP); - log.info("{} 登录,耗费时间 {} 毫秒", username, elapsedTime); // 收集日志这里必须打印一条日志,内容随便吧,记录在message字段,具体看logback-spring.xml文件 + //log.info("{} 登录,耗费时间 {} 毫秒", username, elapsedTime); // 收集日志这里必须打印一条日志,内容随便吧,记录在message字段,具体看logback-spring.xml文件 return result; } } diff --git a/youlai-gateway/src/main/java/com/youlai/gateway/config/ResourceServerConfig.java b/youlai-gateway/src/main/java/com/youlai/gateway/config/ResourceServerConfig.java index e26d44bdf..476a83f48 100644 --- a/youlai-gateway/src/main/java/com/youlai/gateway/config/ResourceServerConfig.java +++ b/youlai-gateway/src/main/java/com/youlai/gateway/config/ResourceServerConfig.java @@ -59,7 +59,6 @@ public class ResourceServerConfig { return (exchange, denied) -> { Mono mono = Mono.defer(() -> Mono.just(exchange.getResponse())) .flatMap(response ->WebUtils.writeFailedToResponse(response,ResultCode.ACCESS_UNAUTHORIZED)); - return mono; }; } diff --git a/youlai-gateway/src/main/java/com/youlai/gateway/config/SentinelConfiguration.java b/youlai-gateway/src/main/java/com/youlai/gateway/config/SentinelConfiguration.java new file mode 100644 index 000000000..8d55160af --- /dev/null +++ b/youlai-gateway/src/main/java/com/youlai/gateway/config/SentinelConfiguration.java @@ -0,0 +1,31 @@ +package com.youlai.gateway.config; + +import com.alibaba.csp.sentinel.adapter.gateway.sc.callback.BlockRequestHandler; +import com.alibaba.csp.sentinel.adapter.gateway.sc.callback.GatewayCallbackManager; +import com.youlai.common.result.ResultCode; +import org.springframework.context.annotation.Configuration; +import org.springframework.http.HttpStatus; +import org.springframework.http.MediaType; +import org.springframework.web.reactive.function.BodyInserters; +import org.springframework.web.reactive.function.server.ServerResponse; + +import javax.annotation.PostConstruct; + +/** + * @author haoxr + * @description 自定义网关流控异常响应 + * @createTime 2021/4/11 19:58 + */ +@Configuration +public class SentinelConfiguration { + + @PostConstruct + private void initBlockHandler() { + BlockRequestHandler blockRequestHandler = (exchange, t) -> + ServerResponse.status(HttpStatus.OK) + .contentType(MediaType.APPLICATION_JSON) + .body(BodyInserters.fromValue(ResultCode.FLOW_LIMITING.toString())); + GatewayCallbackManager.setBlockHandler(blockRequestHandler); + } + +} diff --git a/youlai-gateway/src/main/resources/bootstrap-dev.yml b/youlai-gateway/src/main/resources/bootstrap-dev.yml index b286bbf6c..78a7ea0ec 100644 --- a/youlai-gateway/src/main/resources/bootstrap-dev.yml +++ b/youlai-gateway/src/main/resources/bootstrap-dev.yml @@ -15,8 +15,9 @@ spring: file-extension: yaml sentinel: enabled: false - eager: true + eager: true # 取消控制台懒加载,项目启动即连接Sentinel transport: + client-ip: localhost dashboard: localhost:8080 datasource: # 网关限流规则,gw-flow为key,随便定义