mirror of
https://gitee.com/youlaitech/youlai-mall.git
synced 2024-12-23 05:00:25 +08:00
feat:Sentinel网关流控整合
This commit is contained in:
parent
b9ef5c5155
commit
4ddb89f27a
@ -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;
|
||||
}
|
||||
|
@ -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 + '\"' +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
@ -59,7 +59,6 @@ public class ResourceServerConfig {
|
||||
return (exchange, denied) -> {
|
||||
Mono<Void> mono = Mono.defer(() -> Mono.just(exchange.getResponse()))
|
||||
.flatMap(response ->WebUtils.writeFailedToResponse(response,ResultCode.ACCESS_UNAUTHORIZED));
|
||||
|
||||
return mono;
|
||||
};
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
@ -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,随便定义
|
||||
|
Loading…
Reference in New Issue
Block a user