feat: 重新整合Sentinel网关和普通流控

This commit is contained in:
郝先瑞 2022-07-25 00:53:06 +08:00
parent c4eb9fa5b4
commit 17626703bb
4 changed files with 51 additions and 10 deletions

View File

@ -11,7 +11,7 @@ VOLUME /tmp
# 复制jar至镜像
ADD target/youlai-gateway.jar app.jar
ENTRYPOINT ["java", "-Xmx128m", "-Djava.security.egd=file:/dev/./urandom", "-jar", "/app.jar"]
ENTRYPOINT ["java", "-Xmx128m", "-Djava.security.egd=file:/dev/./urandom","-Dcsp.sentinel.app.type=1", "-jar", "/app.jar"]
EXPOSE 9999

View File

@ -60,11 +60,12 @@
<artifactId>spring-security-oauth2-jose</artifactId>
</dependency>
<!-- Sentinel流量控制、熔断降级 -->
<!-- Sentinel -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-sentinel-gateway</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>

View File

@ -0,0 +1,32 @@
package com.youlai.gateway.sentinel;
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
* @date 2022/7/24
*/
@Configuration
public class SentinelConfiguration {
@PostConstruct
private void initBlockHandler() {
BlockRequestHandler blockRequestHandler = (exchange, t) ->
ServerResponse.status(HttpStatus.TOO_MANY_REQUESTS)
.contentType(MediaType.APPLICATION_JSON)
.body(BodyInserters.fromValue(ResultCode.FLOW_LIMITING.toString())
);
GatewayCallbackManager.setBlockHandler(blockRequestHandler);
}
}

View File

@ -9,9 +9,6 @@ import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Arrays;
import java.util.List;
@Api(tags = "「实验室」Sentinel接口")
@RestController
@RequestMapping("/api/v1/sentinel")
@ -19,11 +16,22 @@ import java.util.List;
@Slf4j
public class SentinelController {
@ApiOperation("【限流】获取订单列表")
@GetMapping("/orders/_limit")
public Result listOrders() {
List<String> orderList = Arrays.asList("订单1", "订单2", "订单3");
return Result.success(orderList);
@ApiOperation("【普通流控】获取数据")
@GetMapping("/flow_limiting/data")
public Result getFlowLimitingData() {
return Result.success("正常数据");
}
@ApiOperation("【网关流控-RouteID】获取数据")
@GetMapping("/gateway_route_flow_limiting/data")
public Result getGatewayRouteFlowLimitingData() {
return Result.success("正常数据");
}
@ApiOperation("【网关流控-API分组】获取数据")
@GetMapping("/gateway_api_flow_limiting/data")
public Result getGatewayApiFlowLimitingData() {
return Result.success("正常数据");
}
}