1. 增加服务保障组件,支持限流、熔断等功能

2. 进一步完善文档
This commit is contained in:
YunaiV 2021-02-24 20:59:09 +08:00
parent 53db9de93d
commit 7c8bfa3443
29 changed files with 126 additions and 39 deletions

View File

@ -40,6 +40,7 @@
1. 链路追踪:基于 SkyWalking 实现性能监控,特别是链路的追踪
1. 分布式锁:基于 Redis 实现分布式锁,满足并发场景
1. 幂等组件:基于 Redis 实现幂等组件,解决重复请求问题
1. 服务保障:基于 Resilience4j 实现服务的稳定性,包括限流、熔断等功能
### 研发工具
@ -58,6 +59,37 @@
> 未来会补充文档和视频,方便胖友冲冲冲!
## 技术栈
**后端**
| 框架 | 说明 | 版本 | 学习指南 |
| --- | --- | --- | --- |
| [Spring Boot](https://spring.io/projects/spring-boot) | 应用开发框架 | 2.4.2 | [文档](https://github.com/YunaiV/SpringBoot-Labs) |
| [MySQL](https://www.mysql.com/cn/) | 数据库服务器 | 5.7 | |
| [Druid](https://github.com/alibaba/druid) | JDBC 连接池、监控组件 | 1.2.4 | [文档](http://www.iocoder.cn/Spring-Boot/datasource-pool/?yudao) |
| [MyBatis-Plus](https://mp.baomidou.com/) | MyBatis 增强工具包 | 3.4.1 | [文档](http://www.iocoder.cn/Spring-Boot/MyBatis/?yudao) |
| [Redis](https://redis.io/) | key-value 数据库 | 5.0 | |
| [Redisson](https://github.com/redisson/redisson) | Redis 客户端 | 3.1.46 | [文档](http://www.iocoder.cn/Spring-Boot/Redis/?yudao) |
| [Spring MVC](https://github.com/spring-projects/spring-framework/tree/master/spring-webmvc) | MVC 框架 | 5.4.2 | [文档](http://www.iocoder.cn/SpringMVC/MVC/?yudao) |
| [Spring Security](https://github.com/spring-projects/spring-security) | Spring 安全框架 | 5.4.2 | [文档](http://www.iocoder.cn/Spring-Boot/Spring-Security/?yudao) |
| [Hibernate Validator](https://github.com/hibernate/hibernate-validator) | 参数校验组件 | 6.1.7 | [文档](http://www.iocoder.cn/Spring-Boot/Validation/?yudao) |
| [Quartz](https://github.com/quartz-scheduler) | 任务调度组件 | 2.3.2 | [文档](http://www.iocoder.cn/Spring-Boot/Job/?yudao) |
| [Knife4j](https://gitee.com/xiaoym/knife4j) | Swagger 增强 UI 实现 | 3.0.2 | [文档](http://www.iocoder.cn/Spring-Boot/Swagger/?yudao) |
| [Resilience4j](https://github.com/quartz-scheduler) | 服务保障组件 | 1.7.0 | [文档](http://www.iocoder.cn/Spring-Boot/Resilience4j/?yudao) |
| [SkyWalking](https://skywalking.apache.org/) | 分布式应用追踪系统 | 8.6.0 | [文档](http://www.iocoder.cn/Spring-Boot/SkyWalking/?yudao) |
| [Spring Boot Admin](https://github.com/skywalking) | Spring Boot 监控平台 | 8.6.0 | [文档](http://www.iocoder.cn/Spring-Boot/Admin/?yudao) |
| [Jackson](https://github.com/FasterXML/jackson) | JSON 工具库 | 2.11.4 | |
| [MapStruct](https://mapstruct.org/) | Java Bean 转换 | 1.4.1 | [文档](http://www.iocoder.cn/Spring-Boot/MapStruct/?yudao) |
| [Lombok](https://projectlombok.org/) | 消除冗长的 Java 代码| 1.16.14 | [文档](http://www.iocoder.cn/Spring-Boot/Lombok/?yudao) |
**前端**
| 框架 | 说明 | 版本 |
| --- | --- | --- |
| [Vue](https://cn.vuejs.org/index.html) | JavaScript 框架 | 2.6.12 |
| [Vue Element Admin](https://ant.design/docs/react/introduce-cn) | 后台前端解决方案 | - |
## 演示图
<table>

View File

@ -34,6 +34,7 @@
<apollo.version>1.7.0</apollo.version>
<!-- 服务保障相关 -->
<lock4j.version>2.2.0</lock4j.version>
<resilience4j.version>1.7.0</resilience4j.version>
<!-- 监控相关 -->
<skywalking.version>8.3.0</skywalking.version>
<spring-boot-admin.version>2.3.1</spring-boot-admin.version>
@ -145,6 +146,12 @@
<version>${lock4j.version}</version>
</dependency>
<dependency>
<groupId>io.github.resilience4j</groupId>
<artifactId>resilience4j-spring-boot2</artifactId>
<version>${resilience4j.version}</version>
</dependency>
<!-- 监控相关 -->
<dependency>
<groupId>org.apache.skywalking</groupId>

View File

@ -1,30 +0,0 @@
## 开发
```bash
# 克隆项目
git clone https://github.com/YunaiV/ruoyi-vue-pro
# 进入项目目录
cd ruoyi-ui
# 安装依赖
npm install
# 建议不要直接使用 cnpm 安装依赖,会有各种诡异的 bug。可以通过如下操作解决 npm 下载速度慢的问题
npm install --registry=https://registry.npm.taobao.org
# 启动服务
npm run dev
```
浏览器访问 http://localhost:80
## 发布
```bash
# 构建测试环境
npm run build:stage
# 构建生产环境
npm run build:prod
```

View File

@ -5,7 +5,6 @@ import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@EnableAdminServer // TODO 芋艿需要迁移出去
public class DashboardApplication {
public static void main(String[] args) {

View File

@ -23,6 +23,8 @@ public interface GlobalErrorCodeConstants {
ErrorCode FORBIDDEN = new ErrorCode(403, "没有该操作权限");
ErrorCode NOT_FOUND = new ErrorCode(404, "请求未找到");
ErrorCode METHOD_NOT_ALLOWED = new ErrorCode(405, "请求方法不正确");
ErrorCode LOCKED = new ErrorCode(423, "请求失败,请稍后重试"); // 并发请求不允许
ErrorCode TOO_MANY_REQUESTS = new ErrorCode(429, "请求过于频繁,请稍后重试");
// ========== 服务端错误段 ==========
@ -30,7 +32,6 @@ public interface GlobalErrorCodeConstants {
// ========== 自定义错误段 ==========
ErrorCode REPEATED_REQUESTS = new ErrorCode(900, "重复请求,请稍后重试"); // 重复请求
ErrorCode CONCURRENCY_REQUESTS = new ErrorCode(901, "请求失败,请稍后重试"); // 并发请求不允许
ErrorCode UNKNOWN = new ErrorCode(999, "未知错误");

View File

@ -1 +1 @@
<http://www.iocoder.cn/Spring-Boot/Async-Job/?dashboard>
<http://www.iocoder.cn/Spring-Boot/Async-Job/?yudao>

View File

@ -0,0 +1 @@
<http://www.iocoder.cn/Spring-Boot/dynamic-datasource/?yudao>

View File

@ -0,0 +1 @@
<http://www.iocoder.cn/Spring-Boot/datasource-pool/?yudao>

View File

@ -14,7 +14,7 @@ public class DefaultLockFailureStrategy implements LockFailureStrategy {
@Override
public void onLockFailure(String key, long acquireTimeout, int acquireCount) {
log.debug("[onLockFailure][线程:{} 获取锁失败key:{} 获取超时时长:{} ms]", Thread.currentThread().getName(), key, acquireTimeout);
throw new ServiceException(GlobalErrorCodeConstants.CONCURRENCY_REQUESTS);
throw new ServiceException(GlobalErrorCodeConstants.LOCKED);
}
}

View File

@ -0,0 +1,9 @@
package cn.iocoder.dashboard.framework.monitor;
import de.codecentric.boot.admin.server.config.EnableAdminServer;
import org.springframework.context.annotation.Configuration;
@Configuration
@EnableAdminServer
public class AdminServerConfiguration {
}

View File

@ -0,0 +1,4 @@
/**
* 使用 Spring Boot Admin 实现简单的监控平台
*/
package cn.iocoder.dashboard.framework.monitor;

View File

@ -0,0 +1 @@
<http://www.iocoder.cn/Spring-Boot/Admin/?yudao>

View File

@ -0,0 +1 @@
<http://www.iocoder.cn/Spring-Boot/MyBatis/?yudao>

View File

@ -0,0 +1 @@
<http://www.iocoder.cn/Spring-Boot/Job/?yudao>

View File

@ -0,0 +1,4 @@
/**
* 采用 Spring Data Redis 操作 Redis底层使用 Redisson 作为客户端
*/
package cn.iocoder.dashboard.framework.redis;

View File

@ -0,0 +1 @@
<http://www.iocoder.cn/Spring-Boot/Redis/?yudao>

View File

@ -0,0 +1,9 @@
/**
* 使用 Resilience4j 组件实现服务保障包括
* 1. 熔断器
* 2. 限流器
* 3. 舱壁隔离
* 4. 重试
* 5. 限时器
*/
package cn.iocoder.dashboard.framework.resilience4j;

View File

@ -0,0 +1 @@
<http://www.iocoder.cn/Spring-Boot/Spring-Security/?github>

View File

@ -1,2 +1,2 @@
* 芋道 Spring Security 入门:<http://www.iocoder.cn/Spring-Boot/Spring-Security/?dashboard>
* Spring Security 基本概念:<http://www.iocoder.cn/Fight/Spring-Security-4-1-0-Basic-concept-description/?dashboard>
* 芋道 Spring Security 入门:<http://www.iocoder.cn/Spring-Boot/Spring-Security/?yudao>
* Spring Security 基本概念:<http://www.iocoder.cn/Fight/Spring-Security-4-1-0-Basic-concept-description/?yudao>

View File

@ -1 +1 @@
<http://www.iocoder.cn/Spring-Boot/Swagger/?dashboard>
<http://www.iocoder.cn/Spring-Boot/Swagger/?yudao>

View File

@ -0,0 +1 @@
<http://www.iocoder.cn/Spring-Boot/SkyWalking/?yudao>

View File

@ -0,0 +1,4 @@
/**
* 使用 Hibernate Validator 实现参数校验
*/
package cn.iocoder.dashboard.framework.validator;

View File

@ -0,0 +1 @@
<http://www.iocoder.cn/Spring-Boot/Validation/?yudao>

View File

@ -4,6 +4,7 @@ import cn.iocoder.dashboard.common.exception.GlobalException;
import cn.iocoder.dashboard.common.exception.ServiceException;
import cn.iocoder.dashboard.common.pojo.CommonResult;
import cn.iocoder.dashboard.framework.security.core.util.SecurityUtils;
import io.github.resilience4j.ratelimiter.RequestNotPermitted;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.validation.BindException;
@ -63,6 +64,9 @@ public class GlobalExceptionHandler {
if (ex instanceof HttpRequestMethodNotSupportedException) {
return httpRequestMethodNotSupportedExceptionHandler((HttpRequestMethodNotSupportedException) ex);
}
if (ex instanceof RequestNotPermitted) {
return requestNotPermittedExceptionHandler(request, (RequestNotPermitted) ex);
}
if (ex instanceof ServiceException) {
return serviceExceptionHandler((ServiceException) ex);
}
@ -163,6 +167,15 @@ public class GlobalExceptionHandler {
return CommonResult.error(METHOD_NOT_ALLOWED.getCode(), String.format("请求方法不正确:%s", ex.getMessage()));
}
/**
* 处理 Resilience4j 限流抛出的异常
*/
@ExceptionHandler(value = RequestNotPermitted.class)
public CommonResult<?> requestNotPermittedExceptionHandler(HttpServletRequest req, RequestNotPermitted ex) {
log.warn("[requestNotPermittedExceptionHandler][url({}) 访问过于频繁]", req.getRequestURL(), ex);
return CommonResult.error(TOO_MANY_REQUESTS);
}
/**
* 处理 Spring Security 权限不足的异常
*

View File

@ -1 +1 @@
<http://www.iocoder.cn/Spring-Boot/SpringMVC/?dashboard>
<http://www.iocoder.cn/Spring-Boot/SpringMVC/?yudao>

View File

@ -1,3 +1,7 @@
### 请求 /get-permission-info 接口 => 成功
### 请求 /tool/test-demo/get 接口 => 成功
GET {{baseUrl}}/tool/test-demo/get?id=1
Authorization: Bearer {{token}}
### 请求 /tool/test-demo/list 接口 => 成功
GET {{baseUrl}}/tool/test-demo/list?ids=1
Authorization: Bearer {{token}}

View File

@ -10,6 +10,7 @@ import cn.iocoder.dashboard.modules.tool.convert.test.ToolTestDemoConvert;
import cn.iocoder.dashboard.modules.tool.dal.dataobject.test.ToolTestDemoDO;
import cn.iocoder.dashboard.modules.tool.service.test.ToolTestDemoService;
import com.baomidou.lock.annotation.Lock4j;
import io.github.resilience4j.ratelimiter.annotation.RateLimiter;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
@ -78,6 +79,7 @@ public class ToolTestDemoController {
@ApiOperation("获得测试示例列表")
@ApiImplicitParam(name = "ids", value = "编号列表", required = true, dataTypeClass = List.class)
@PreAuthorize("@ss.hasPermission('tool:test-demo:query')")
@RateLimiter(name = "backendA")
public CommonResult<List<ToolTestDemoRespVO>> getTestDemoList(@RequestParam("ids") Collection<Long> ids) {
List<ToolTestDemoDO> list = testDemoService.getTestDemoList(ids);
return success(ToolTestDemoConvert.INSTANCE.convertList(list));

View File

@ -88,6 +88,16 @@ lock4j:
acquire-timeout: 3000 # 获取分布式锁超时时间,默认为 3000 毫秒
expire: 30000 # 分布式锁的超时时间,默认为 30 毫秒
# Resilience4j 配置项
resilience4j:
ratelimiter:
instances:
backendA:
limit-for-period: 1 # 每个周期内,允许的请求数。默认为 50
limit-refresh-period: 60s # 每个周期的时长,单位:微秒。默认为 500
timeout-duration: 1s # 被限流时,阻塞等待的时长,单位:微秒。默认为 5s
register-health-indicator: true # 是否注册到健康监测
--- #################### 监控相关配置 ####################
# Actuator 监控端点的配置项

View File

@ -88,6 +88,16 @@ lock4j:
acquire-timeout: 3000 # 获取分布式锁超时时间,默认为 3000 毫秒
expire: 30000 # 分布式锁的超时时间,默认为 30 毫秒
# Resilience4j 配置项
resilience4j:
ratelimiter:
instances:
backendA:
limit-for-period: 1 # 每个周期内,允许的请求数。默认为 50
limit-refresh-period: 60s # 每个周期的时长,单位:微秒。默认为 500
timeout-duration: 1s # 被限流时,阻塞等待的时长,单位:微秒。默认为 5s
register-health-indicator: true # 是否注册到健康监测
--- #################### 监控相关配置 ####################
# Actuator 监控端点的配置项