├── .gitignore
├── README.md
├── byapi-common
├── pom.xml
└── src
│ └── main
│ └── java
│ └── com
│ └── example
│ └── common
│ ├── annotation
│ ├── LoginCheck.java
│ └── MustAdmin.java
│ ├── aop
│ ├── AuthAspect.java
│ └── LoginAspect.java
│ ├── config
│ ├── CorsConfig.java
│ ├── Knife4jConfig.java
│ └── MybatisPlusConfig.java
│ ├── constant
│ ├── CommonConsts.java
│ ├── InterfaceConsts.java
│ └── UserConsts.java
│ ├── enums
│ ├── ErrorCode.java
│ └── RoleEnum.java
│ ├── exception
│ ├── BusinessException.java
│ └── GlobalExceptionHandler.java
│ ├── model
│ ├── dto
│ │ ├── EmailDto.java
│ │ ├── InterfaceAddDto.java
│ │ ├── InterfaceInvokeDto.java
│ │ ├── InterfacePageDto.java
│ │ ├── InterfaceUpdateDto.java
│ │ ├── LoginDto.java
│ │ ├── RegisterDto.java
│ │ ├── UserInterfacePageDto.java
│ │ ├── UserInterfaceUpdateDto.java
│ │ ├── UserPageDto.java
│ │ └── UserUpdateDto.java
│ ├── entity
│ │ ├── InterfaceInfo.java
│ │ ├── User.java
│ │ └── UserInterfaceInfo.java
│ └── vo
│ │ ├── InterfaceVo.java
│ │ ├── InvokeCountVo.java
│ │ ├── KeyVo.java
│ │ └── UserVo.java
│ ├── service
│ ├── DubboInterfaceService.java
│ ├── DubboUserInterfaceService.java
│ └── DubboUserService.java
│ └── utils
│ ├── PageBean.java
│ ├── PageRequest.java
│ ├── Result.java
│ ├── SignUtil.java
│ └── TokenBucketLimiter.java
├── byapi-gateway
├── pom.xml
└── src
│ ├── main
│ ├── java
│ │ └── com
│ │ │ └── example
│ │ │ └── gateway
│ │ │ ├── ByapiGatewayApplication.java
│ │ │ ├── config
│ │ │ └── RedisConfig.java
│ │ │ └── filter
│ │ │ └── CustomGlobalFilter.java
│ └── resources
│ │ └── application.yml
│ └── test
│ └── java
│ └── com
│ └── example
│ └── ByapiGatewayApplicationTests.java
├── byapi-interface
├── pom.xml
└── src
│ ├── main
│ ├── java
│ │ └── com
│ │ │ └── example
│ │ │ ├── ByapiInterfaceApplication.java
│ │ │ └── interfaceinfo
│ │ │ ├── controller
│ │ │ └── InterfaceController.java
│ │ │ └── model
│ │ │ ├── ImgRes.java
│ │ │ └── TalkRes.java
│ └── resources
│ │ └── application.yml
│ └── test
│ └── java
│ └── com
│ └── example
│ └── ByapiInterfaceApplicationTests.java
├── byapi-sdk
├── pom.xml
└── src
│ └── main
│ ├── java
│ └── com
│ │ └── example
│ │ ├── client
│ │ └── ByApiClient.java
│ │ ├── config
│ │ └── ByApiConfig.java
│ │ └── util
│ │ └── SignUtil.java
│ └── resources
│ └── META-INF
│ └── spring.factories
├── byapi-server
├── pom.xml
└── src
│ ├── main
│ ├── java
│ │ └── com
│ │ │ └── example
│ │ │ └── server
│ │ │ ├── ByapiServerApplication.java
│ │ │ ├── config
│ │ │ └── RedisConfig.java
│ │ │ ├── controller
│ │ │ ├── InterfaceController.java
│ │ │ ├── UserController.java
│ │ │ └── UserInterfaceController.java
│ │ │ ├── mapper
│ │ │ ├── InterfaceMapper.java
│ │ │ ├── UserInterfaceMapper.java
│ │ │ └── UserMapper.java
│ │ │ ├── service
│ │ │ ├── InterfaceService.java
│ │ │ ├── UserInterfaceService.java
│ │ │ ├── UserService.java
│ │ │ ├── dubbo
│ │ │ │ ├── DubboInterfaceServiceImpl.java
│ │ │ │ ├── DubboUserInterfaceServiceImpl.java
│ │ │ │ └── DubboUserServiceImpl.java
│ │ │ └── impl
│ │ │ │ ├── InterfaceServiceImpl.java
│ │ │ │ ├── UserInterfaceServiceImpl.java
│ │ │ │ └── UserServiceImpl.java
│ │ │ └── task
│ │ │ └── CacheTask.java
│ └── resources
│ │ ├── application.yml
│ │ ├── images
│ │ ├── 2e6b1467dae840a1ba650e682652e07d.png
│ │ ├── 7b3b41a5fe5041c78b6fe619a8910373.jpg
│ │ └── ebcae2156c3c4489bc1a2cc94c2c5956.png
│ │ └── mapper
│ │ ├── InterfaceInfoMapper.xml
│ │ ├── UserInterfaceMapper.xml
│ │ └── UserMapper.xml
│ └── test
│ └── java
│ └── com
│ └── example
│ └── ByapiServerApplicationTests.java
├── pom.xml
└── sql
└── byapi.sql
/.gitignore:
--------------------------------------------------------------------------------
1 | HELP.md
2 | target/
3 | !.mvn/wrapper/maven-wrapper.jar
4 | !**/src/main/**/target/
5 | !**/src/test/**/target/
6 |
7 | ### STS ###
8 | .apt_generated
9 | .classpath
10 | .factorypath
11 | .project
12 | .settings
13 | .springBeans
14 | .sts4-cache
15 |
16 | ### IntelliJ IDEA ###
17 | .idea
18 | *.iws
19 | *.iml
20 | *.ipr
21 |
22 | ### NetBeans ###
23 | /nbproject/private/
24 | /nbbuild/
25 | /dist/
26 | /nbdist/
27 | /.nb-gradle/
28 | build/
29 | !**/src/main/**/build/
30 | !**/src/test/**/build/
31 |
32 | ### VS Code ###
33 | .vscode/
34 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | ### By API
2 |
3 | 本项目是一个API接口开放平台,用户可以在线调用接口进行调试,还可以下载SDK,在自己的项目中引入接口代码,实现对接口的远程调用。
4 |
5 |
6 |
7 | 涉及技术:
8 |
9 | + 常用技术框架SpringBoot、MyBatis-Plus
10 | + 数据库MySQL
11 | + Java技术 反射
12 | + 面向切面编程 Spring AOP
13 | + 在线接口文档 Knife4j
14 | + 网关 Gateway
15 | + RPC框架 Dubbo
16 | + 自定义SDK
--------------------------------------------------------------------------------
/byapi-common/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 4.0.0
6 |
7 | org.example
8 | byapi-backend
9 | 1.0-SNAPSHOT
10 |
11 |
12 | byapi-common
13 |
14 |
15 | 8
16 | 8
17 | UTF-8
18 |
19 |
20 |
21 |
22 | com.google.code.gson
23 | gson
24 | 2.10.1
25 |
26 |
27 | com.github.xiaoymin
28 | knife4j-spring-boot-starter
29 | 3.0.3
30 |
31 |
32 | org.springframework.boot
33 | spring-boot-starter-aop
34 | 3.0.2
35 |
36 |
37 | org.springframework.boot
38 | spring-boot-starter-web
39 | 2.6.13
40 |
41 |
42 | com.mysql
43 | mysql-connector-j
44 | 8.0.31
45 |
46 |
47 | com.baomidou
48 | mybatis-plus-boot-starter
49 | 3.5.4
50 |
51 |
52 | org.projectlombok
53 | lombok
54 | 1.18.28
55 |
56 |
57 | cn.hutool
58 | hutool-all
59 | 5.8.16
60 |
61 |
62 | org.apache.commons
63 | commons-lang3
64 | 3.12.0
65 |
66 |
67 |
--------------------------------------------------------------------------------
/byapi-common/src/main/java/com/example/common/annotation/LoginCheck.java:
--------------------------------------------------------------------------------
1 | package com.example.common.annotation;
2 |
3 | import java.lang.annotation.ElementType;
4 | import java.lang.annotation.Retention;
5 | import java.lang.annotation.RetentionPolicy;
6 | import java.lang.annotation.Target;
7 |
8 | /**
9 | * 自定义注解,校验用户是否登录
10 | *
11 | * @author by
12 | */
13 | @Target(ElementType.METHOD)
14 | @Retention(RetentionPolicy.RUNTIME)
15 | public @interface LoginCheck {
16 | }
17 |
--------------------------------------------------------------------------------
/byapi-common/src/main/java/com/example/common/annotation/MustAdmin.java:
--------------------------------------------------------------------------------
1 | package com.example.common.annotation;
2 |
3 | import java.lang.annotation.ElementType;
4 | import java.lang.annotation.Retention;
5 | import java.lang.annotation.RetentionPolicy;
6 | import java.lang.annotation.Target;
7 |
8 | /**
9 | * 自定义注解,用于标识需要管理员权限的接口
10 | *
11 | * @author by
12 | */
13 | @Retention(RetentionPolicy.RUNTIME)
14 | @Target(ElementType.METHOD)
15 | public @interface MustAdmin {
16 | }
17 |
--------------------------------------------------------------------------------
/byapi-common/src/main/java/com/example/common/aop/AuthAspect.java:
--------------------------------------------------------------------------------
1 | package com.example.common.aop;
2 |
3 | import com.example.common.constant.UserConsts;
4 | import com.example.common.enums.ErrorCode;
5 | import com.example.common.enums.RoleEnum;
6 | import com.example.common.exception.BusinessException;
7 | import com.example.common.model.vo.UserVo;
8 | import org.aspectj.lang.ProceedingJoinPoint;
9 | import org.aspectj.lang.annotation.Around;
10 | import org.aspectj.lang.annotation.Aspect;
11 | import org.springframework.stereotype.Component;
12 | import org.springframework.web.context.request.RequestAttributes;
13 | import org.springframework.web.context.request.RequestContextHolder;
14 | import org.springframework.web.context.request.ServletRequestAttributes;
15 |
16 | import javax.servlet.http.HttpServletRequest;
17 |
18 | /**
19 | * 权限校验切面类
20 | *
21 | * @author by
22 | */
23 | @Component
24 | @Aspect
25 | public class AuthAspect {
26 | @Around("@annotation(com.example.common.annotation.MustAdmin))")
27 | public Object authCheck(ProceedingJoinPoint joinPoint) throws Throwable {
28 | //获取请求对象
29 | RequestAttributes requestAttributes = RequestContextHolder.currentRequestAttributes();
30 | HttpServletRequest request = ((ServletRequestAttributes) requestAttributes).getRequest();
31 | //获取用户登录态
32 | Object object = request.getSession().getAttribute(UserConsts.USER_LOGIN_STATE);
33 | UserVo userVo = (UserVo) object;
34 | //判断是否为空
35 | if (userVo == null) {
36 | throw new BusinessException(ErrorCode.NOT_LOGIN_ERROR);
37 | }
38 | //判断是否为管理员
39 | String role = userVo.getUserRole();
40 | if (role.equals(RoleEnum.USER.getRole())) {
41 | throw new BusinessException(ErrorCode.NO_AUTH_ERROR);
42 | }
43 | //放行
44 | return joinPoint.proceed();
45 | }
46 | }
47 |
--------------------------------------------------------------------------------
/byapi-common/src/main/java/com/example/common/aop/LoginAspect.java:
--------------------------------------------------------------------------------
1 | package com.example.common.aop;
2 |
3 | import com.example.common.constant.UserConsts;
4 | import com.example.common.enums.ErrorCode;
5 | import com.example.common.exception.BusinessException;
6 | import com.example.common.model.vo.UserVo;
7 | import org.aspectj.lang.ProceedingJoinPoint;
8 | import org.aspectj.lang.annotation.Around;
9 | import org.aspectj.lang.annotation.Aspect;
10 | import org.springframework.stereotype.Component;
11 | import org.springframework.web.context.request.RequestAttributes;
12 | import org.springframework.web.context.request.RequestContextHolder;
13 | import org.springframework.web.context.request.ServletRequestAttributes;
14 |
15 | import javax.servlet.http.HttpServletRequest;
16 |
17 | /**
18 | * 登录校验切面类
19 | *
20 | * @author by
21 | */
22 | @Component
23 | @Aspect
24 | public class LoginAspect {
25 | @Around("@annotation(com.example.common.annotation.LoginCheck)")
26 | public Object loginCheck(ProceedingJoinPoint joinPoint) throws Throwable {
27 | //获取请求对象
28 | RequestAttributes requestAttributes = RequestContextHolder.currentRequestAttributes();
29 | HttpServletRequest request = ((ServletRequestAttributes) requestAttributes).getRequest();
30 | //获取登录用户
31 | Object object = request.getSession().getAttribute(UserConsts.USER_LOGIN_STATE);
32 | UserVo userVo = (UserVo) object;
33 | //判断用户是否登录
34 | if (userVo == null) {
35 | throw new BusinessException(ErrorCode.NOT_LOGIN_ERROR);
36 | }
37 | //放行
38 | return joinPoint.proceed();
39 | }
40 | }
41 |
--------------------------------------------------------------------------------
/byapi-common/src/main/java/com/example/common/config/CorsConfig.java:
--------------------------------------------------------------------------------
1 | package com.example.common.config;
2 |
3 | import org.springframework.context.annotation.Configuration;
4 | import org.springframework.web.servlet.config.annotation.CorsRegistry;
5 | import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
6 |
7 | /**
8 | * @author by
9 | *
10 | * 跨域配置类
11 | */
12 | @Configuration
13 | public class CorsConfig implements WebMvcConfigurer {
14 | @Override
15 | public void addCorsMappings(CorsRegistry registry) {
16 | // 覆盖所有请求
17 | registry.addMapping("/**")
18 | // 允许发送 Cookie
19 | .allowCredentials(true)
20 | // 放行哪些域名(必须用 patterns,否则 * 会和 allowCredentials 冲突)
21 | .allowedOriginPatterns("*")
22 | .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
23 | .allowedHeaders("*")
24 | .exposedHeaders("*");
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/byapi-common/src/main/java/com/example/common/config/Knife4jConfig.java:
--------------------------------------------------------------------------------
1 | package com.example.common.config;
2 |
3 | import org.springframework.context.annotation.Bean;
4 | import org.springframework.context.annotation.Configuration;
5 | import springfox.documentation.builders.ApiInfoBuilder;
6 | import springfox.documentation.builders.PathSelectors;
7 | import springfox.documentation.builders.RequestHandlerSelectors;
8 | import springfox.documentation.spi.DocumentationType;
9 | import springfox.documentation.spring.web.plugins.Docket;
10 | import springfox.documentation.swagger2.annotations.EnableSwagger2;
11 |
12 | /**
13 | * Knife4j 接口文档配置
14 | *
15 | * @author yupi
16 | */
17 | @Configuration
18 | @EnableSwagger2
19 | public class Knife4jConfig {
20 |
21 | @Bean
22 | public Docket defaultApi2() {
23 | return new Docket(DocumentationType.SWAGGER_2)
24 | .apiInfo(new ApiInfoBuilder()
25 | .title("API接口开放平台")
26 | .description("API接口开放平台")
27 | .version("1.0")
28 | .build())
29 | .select()
30 | // 指定 Controller 扫描包路径
31 | .apis(RequestHandlerSelectors.basePackage("com.example.server.controller"))
32 | .paths(PathSelectors.any())
33 | .build();
34 | }
35 | }
--------------------------------------------------------------------------------
/byapi-common/src/main/java/com/example/common/config/MybatisPlusConfig.java:
--------------------------------------------------------------------------------
1 | package com.example.common.config;
2 |
3 | import com.baomidou.mybatisplus.annotation.DbType;
4 | import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
5 | import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
6 | import org.springframework.context.annotation.Bean;
7 | import org.springframework.context.annotation.Configuration;
8 |
9 | /**
10 | * 分页插件
11 | *
12 | * @author by
13 | */
14 | @Configuration
15 | public class MybatisPlusConfig {
16 |
17 | @Bean
18 | public MybatisPlusInterceptor mybatisPlusInterceptor() {
19 | MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
20 | //如果配置多个插件,切记分页最后添加
21 | interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
22 | return interceptor;
23 | }
24 | }
--------------------------------------------------------------------------------
/byapi-common/src/main/java/com/example/common/constant/CommonConsts.java:
--------------------------------------------------------------------------------
1 | package com.example.common.constant;
2 |
3 | /**
4 | * 通用常量
5 | *
6 | * @author by
7 | */
8 | public interface CommonConsts {
9 |
10 | String IMAGE_UPLOAD_ERROR = "文件上传错误!";
11 | String IMAGE_FORMAT_ERROR = "文件格式错误!";
12 | String IMAGE_READ_ERROR = "文件读取失败!";
13 | String PAGE_PARAMS_ERROR = "分页参数异常!";
14 | String EXIST_ERROR = "已开通接口调用权限!无需再次开通!";
15 | String BODY_KEY = "body-key";
16 | String SDK_DOWNLOAD_ERROR = "SDK下载失败!";
17 | String GET_INTERFACE_BY_ID_KEY = "byapi:server:getInterfaceById:%s:%s";
18 | String LIST_INVOKE_RECORDS_KEY = "byapi:server:listInvokeRecords:%s";
19 | Integer TIME_GAP = 10 * 60 * 1000;
20 | String NONCE_KEY = "byapi:gateway:filter:nonce";
21 | String CACHE_SET_ERROR = "缓存设置失败!";
22 | String CACHE_DEL_ERROR = "缓存删除失败!";
23 | }
24 |
--------------------------------------------------------------------------------
/byapi-common/src/main/java/com/example/common/constant/InterfaceConsts.java:
--------------------------------------------------------------------------------
1 | package com.example.common.constant;
2 |
3 | /**
4 | * @author by
5 | */
6 | public interface InterfaceConsts {
7 |
8 | String INTERFACE_ONLINE_ERROR = "接口上线中,不能删除!";
9 |
10 | String INTERFACE_HOST = "http://localhost:9020";
11 |
12 | String ACCESS_KEY = "accessKey";
13 |
14 | String SIGN = "sign";
15 | String NOT_EXIST_ERROR = "接口不存在!";
16 | String INVOKE_COUNT_ERROR = "接口调用次数不足!";
17 | String INTERFACE_CLOSE = "接口已关闭!";
18 | }
19 |
--------------------------------------------------------------------------------
/byapi-common/src/main/java/com/example/common/constant/UserConsts.java:
--------------------------------------------------------------------------------
1 | package com.example.common.constant;
2 |
3 | /**
4 | * 用户常量类
5 | *
6 | * @author by
7 | */
8 | public interface UserConsts {
9 | /**
10 | * 用户登录态
11 | */
12 | String USER_LOGIN_STATE = "user_login_state";
13 | Integer USER_NAME_LENGTH = 4;
14 | Integer USER_PASSWORD_LENGTH = 8;
15 | String USER_NAME_ERROR = "用户名长度不能小于4位!";
16 | String USER_PASSWORD_ERROR = "密码长度不能小于8位!";
17 | String USER_PARAMS_ERROR = "用户名或密码错误!";
18 | String PASSWORD_NOT_EQUAL = "密码不一致!";
19 | String USER_NAME_EXIST = "用户名已经存在!";
20 | String CODE = "code";
21 | String EMAIL = "email";
22 | String VER_CODE = "verCode";
23 | String DELAY_TASK_ERROR = "延迟任务失败!";
24 | String SEND_MAIL_ERROR = "发送邮件失败!";
25 | String EMAIL_PARAMS_ERROR = "邮箱或验证码错误!";
26 | String EMAIL_EXIST = "邮箱已经存在!";
27 | String ACCOUNT_FORBIDDEN = "账号已被封禁!";
28 | }
29 |
--------------------------------------------------------------------------------
/byapi-common/src/main/java/com/example/common/enums/ErrorCode.java:
--------------------------------------------------------------------------------
1 | package com.example.common.enums;
2 |
3 | import lombok.Getter;
4 |
5 | /**
6 | * 错误信息枚举类
7 | *
8 | * @author by
9 | */
10 | @Getter
11 | public enum ErrorCode {
12 |
13 | /**
14 | * 基本错误信息枚举
15 | */
16 | PARAMS_ERROR(40000, "请求参数错误"),
17 | NOT_LOGIN_ERROR(40100, "未登录"),
18 | NO_AUTH_ERROR(40101, "无权限"),
19 | FORBIDDEN_ERROR(40300, "禁止访问"),
20 | NOT_FOUND_ERROR(40400, "请求数据不存在"),
21 | SYSTEM_ERROR(50000, "系统内部异常");
22 |
23 | /**
24 | * 状态码
25 | */
26 | private final int code;
27 |
28 | /**
29 | * 错误信息
30 | */
31 | private final String message;
32 |
33 | ErrorCode(int code, String message) {
34 | this.code = code;
35 | this.message = message;
36 | }
37 | }
38 |
--------------------------------------------------------------------------------
/byapi-common/src/main/java/com/example/common/enums/RoleEnum.java:
--------------------------------------------------------------------------------
1 | package com.example.common.enums;
2 |
3 | import lombok.Getter;
4 |
5 | /**
6 | * 用户角色枚举
7 | *
8 | * @author by
9 | */
10 | @Getter
11 | public enum RoleEnum {
12 |
13 | /**
14 | * 管理员
15 | */
16 | ADMIN("admin"),
17 | /**
18 | * 普通用户
19 | */
20 | USER("user");
21 |
22 | private final String role;
23 |
24 | RoleEnum(String role) {
25 | this.role = role;
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/byapi-common/src/main/java/com/example/common/exception/BusinessException.java:
--------------------------------------------------------------------------------
1 | package com.example.common.exception;
2 |
3 | import com.example.common.enums.ErrorCode;
4 | import lombok.Getter;
5 |
6 | /**
7 | * 自定义异常
8 | *
9 | * @author by
10 | */
11 | @Getter
12 | public class BusinessException extends RuntimeException {
13 |
14 | private final Integer code;
15 |
16 | public BusinessException(Integer code, String message) {
17 | super(message);
18 | this.code = code;
19 | }
20 |
21 | public BusinessException(ErrorCode errorCode) {
22 | super(errorCode.getMessage());
23 | this.code = errorCode.getCode();
24 | }
25 |
26 | public BusinessException(ErrorCode errorCode, String message) {
27 | super(message);
28 | this.code = errorCode.getCode();
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/byapi-common/src/main/java/com/example/common/exception/GlobalExceptionHandler.java:
--------------------------------------------------------------------------------
1 | package com.example.common.exception;
2 |
3 | import org.springframework.web.bind.annotation.ExceptionHandler;
4 | import org.springframework.web.bind.annotation.RestControllerAdvice;
5 | import com.example.common.utils.Result;
6 |
7 | /**
8 | * 自定义异常处理类
9 | *
10 | * @author by
11 | */
12 | @RestControllerAdvice
13 | public class GlobalExceptionHandler {
14 |
15 | @ExceptionHandler(BusinessException.class)
16 | public static Result error(BusinessException be) {
17 | return Result.error(be.getCode(), be.getMessage());
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/byapi-common/src/main/java/com/example/common/model/dto/EmailDto.java:
--------------------------------------------------------------------------------
1 | package com.example.common.model.dto;
2 |
3 | import lombok.Data;
4 |
5 | import java.io.Serializable;
6 |
7 | /**
8 | * 邮箱登录请求体
9 | *
10 | * @author by
11 | */
12 | @Data
13 | public class EmailDto implements Serializable {
14 | /**
15 | * 邮箱
16 | */
17 | private String email;
18 | /**
19 | * 验证码
20 | */
21 | private String verCode;
22 | }
23 |
--------------------------------------------------------------------------------
/byapi-common/src/main/java/com/example/common/model/dto/InterfaceAddDto.java:
--------------------------------------------------------------------------------
1 | package com.example.common.model.dto;
2 |
3 | import lombok.Data;
4 |
5 | import java.io.Serializable;
6 |
7 | /**
8 | * @author by
9 | */
10 | @Data
11 | public class InterfaceAddDto implements Serializable {
12 | /**
13 | * 名称
14 | */
15 | private String name;
16 |
17 | /**
18 | * 描述
19 | */
20 | private String description;
21 |
22 | /**
23 | * 接口地址
24 | */
25 | private String url;
26 |
27 | /**
28 | * 请求类型
29 | */
30 | private String method;
31 |
32 | /**
33 | * 请求参数
34 | */
35 | private String requestParams;
36 |
37 | /**
38 | * 请求头
39 | */
40 | private String requestHeader;
41 |
42 | /**
43 | * 响应头
44 | */
45 | private String responseHeader;
46 |
47 | /**
48 | * 接口状态(0-关闭,1-开启)
49 | */
50 | private Integer status;
51 | }
52 |
--------------------------------------------------------------------------------
/byapi-common/src/main/java/com/example/common/model/dto/InterfaceInvokeDto.java:
--------------------------------------------------------------------------------
1 | package com.example.common.model.dto;
2 |
3 | import lombok.Data;
4 |
5 | import java.io.Serializable;
6 |
7 | /**
8 | * 接口调用请求体
9 | *
10 | * @author by
11 | */
12 | @Data
13 | public class InterfaceInvokeDto implements Serializable {
14 |
15 | /**
16 | * 接口ID
17 | */
18 | private Long id;
19 | /**
20 | * 请求参数,不必需
21 | */
22 | private String userRequestParams;
23 | }
24 |
--------------------------------------------------------------------------------
/byapi-common/src/main/java/com/example/common/model/dto/InterfacePageDto.java:
--------------------------------------------------------------------------------
1 | package com.example.common.model.dto;
2 |
3 | import com.example.common.utils.PageRequest;
4 | import lombok.Data;
5 | import lombok.EqualsAndHashCode;
6 |
7 | /**
8 | * 分页条件请求体
9 | *
10 | * @author by
11 | */
12 | @EqualsAndHashCode(callSuper = true)
13 | @Data
14 | public class InterfacePageDto extends PageRequest {
15 | /**
16 | * 主键
17 | */
18 | private Long id;
19 |
20 | /**
21 | * 名称
22 | */
23 | private String name;
24 |
25 | /**
26 | * 接口地址
27 | */
28 | private String url;
29 |
30 | /**
31 | * 请求类型
32 | */
33 | private String method;
34 |
35 | /**
36 | * 接口状态(0-关闭,1-开启)
37 | */
38 | private Integer status;
39 | }
40 |
--------------------------------------------------------------------------------
/byapi-common/src/main/java/com/example/common/model/dto/InterfaceUpdateDto.java:
--------------------------------------------------------------------------------
1 | package com.example.common.model.dto;
2 |
3 | import lombok.Data;
4 |
5 | import java.io.Serializable;
6 |
7 | /**
8 | * @author by
9 | */
10 | @Data
11 | public class InterfaceUpdateDto implements Serializable {
12 | /**
13 | * 主键
14 | */
15 | private Long id;
16 |
17 | /**
18 | * 名称
19 | */
20 | private String name;
21 |
22 | /**
23 | * 描述
24 | */
25 | private String description;
26 |
27 | /**
28 | * 接口地址
29 | */
30 | private String url;
31 |
32 | /**
33 | * 请求类型
34 | */
35 | private String method;
36 |
37 | /**
38 | * 请求参数
39 | */
40 | private String requestParams;
41 |
42 | /**
43 | * 请求头
44 | */
45 | private String requestHeader;
46 |
47 | /**
48 | * 响应头
49 | */
50 | private String responseHeader;
51 |
52 | /**
53 | * 示例代码
54 | */
55 | private String codeExample;
56 | }
57 |
--------------------------------------------------------------------------------
/byapi-common/src/main/java/com/example/common/model/dto/LoginDto.java:
--------------------------------------------------------------------------------
1 | package com.example.common.model.dto;
2 |
3 | import lombok.Data;
4 |
5 | import java.io.Serializable;
6 |
7 | /**
8 | * 登录请求体
9 | *
10 | * @author by
11 | */
12 | @Data
13 | public class LoginDto implements Serializable {
14 |
15 | /**
16 | * 账号
17 | */
18 | private String userAccount;
19 | /**
20 | * 密码
21 | */
22 | private String userPassword;
23 | }
24 |
--------------------------------------------------------------------------------
/byapi-common/src/main/java/com/example/common/model/dto/RegisterDto.java:
--------------------------------------------------------------------------------
1 | package com.example.common.model.dto;
2 |
3 | import lombok.Data;
4 |
5 | import java.io.Serializable;
6 |
7 | /**
8 | * 注册请求体
9 | *
10 | * @author by
11 | */
12 | @Data
13 | public class RegisterDto implements Serializable {
14 | /**
15 | * 账号
16 | */
17 | private String userAccount;
18 | /**
19 | * 密码
20 | */
21 | private String userPassword;
22 | /**
23 | * 确认密码
24 | */
25 | private String confirmPassword;
26 | }
27 |
--------------------------------------------------------------------------------
/byapi-common/src/main/java/com/example/common/model/dto/UserInterfacePageDto.java:
--------------------------------------------------------------------------------
1 | package com.example.common.model.dto;
2 |
3 | import com.example.common.utils.PageRequest;
4 | import lombok.Data;
5 | import lombok.EqualsAndHashCode;
6 |
7 | /**
8 | * @author by
9 | */
10 | @EqualsAndHashCode(callSuper = true)
11 | @Data
12 | public class UserInterfacePageDto extends PageRequest {
13 | /**
14 | * 主键
15 | */
16 | private Long id;
17 |
18 | /**
19 | * 调用接口用户ID
20 | */
21 | private Long userId;
22 |
23 | /**
24 | * 接口ID
25 | */
26 | private Long interfaceInfoId;
27 | }
28 |
--------------------------------------------------------------------------------
/byapi-common/src/main/java/com/example/common/model/dto/UserInterfaceUpdateDto.java:
--------------------------------------------------------------------------------
1 | package com.example.common.model.dto;
2 |
3 | import lombok.Data;
4 |
5 | import java.io.Serializable;
6 |
7 | /**
8 | * 用户接口关系更新请求
9 | *
10 | * @author by
11 | */
12 | @Data
13 | public class UserInterfaceUpdateDto implements Serializable {
14 | /**
15 | * 主键
16 | */
17 | private Long id;
18 |
19 | /**
20 | * 总调用次数
21 | */
22 | private Integer totalNum;
23 |
24 | /**
25 | * 剩余调用次数
26 | */
27 | private Integer leftNum;
28 | }
29 |
--------------------------------------------------------------------------------
/byapi-common/src/main/java/com/example/common/model/dto/UserPageDto.java:
--------------------------------------------------------------------------------
1 | package com.example.common.model.dto;
2 |
3 | import com.example.common.utils.PageRequest;
4 | import lombok.Data;
5 | import lombok.EqualsAndHashCode;
6 |
7 | /**
8 | * 分页请求体
9 | *
10 | * @author by
11 | */
12 | @EqualsAndHashCode(callSuper = true)
13 | @Data
14 | public class UserPageDto extends PageRequest {
15 | /**
16 | * id
17 | */
18 | private Long id;
19 |
20 | /**
21 | * 用户昵称
22 | */
23 | private String userName;
24 |
25 | /**
26 | * 账号
27 | */
28 | private String userAccount;
29 |
30 | /**
31 | * 性别(0-男,1-女)
32 | */
33 | private Integer gender;
34 |
35 | /**
36 | * 用户状态(0-启用,1-禁用)
37 | */
38 | private Integer status;
39 | }
40 |
--------------------------------------------------------------------------------
/byapi-common/src/main/java/com/example/common/model/dto/UserUpdateDto.java:
--------------------------------------------------------------------------------
1 | package com.example.common.model.dto;
2 |
3 | import lombok.Data;
4 |
5 | import java.io.Serializable;
6 |
7 | /**
8 | * 更新用户请求体
9 | *
10 | * @author by
11 | */
12 | @Data
13 | public class UserUpdateDto implements Serializable {
14 | /**
15 | * id
16 | */
17 | private Long id;
18 |
19 | /**
20 | * 用户昵称
21 | */
22 | private String userName;
23 |
24 | /**
25 | * 账号
26 | */
27 | private String userAccount;
28 |
29 | /**
30 | * 用户头像
31 | */
32 | private String userAvatar;
33 |
34 | /**
35 | * 邮箱
36 | */
37 | private String email;
38 |
39 | /**
40 | * 性别(0-男,1-女)
41 | */
42 | private Integer gender;
43 |
44 | /**
45 | * 用户状态(0-启用,1-禁用)
46 | */
47 | private Integer status;
48 |
49 | /**
50 | * 用户角色:user / admin
51 | */
52 | private String userRole;
53 | }
54 |
--------------------------------------------------------------------------------
/byapi-common/src/main/java/com/example/common/model/entity/InterfaceInfo.java:
--------------------------------------------------------------------------------
1 | package com.example.common.model.entity;
2 |
3 | import com.baomidou.mybatisplus.annotation.IdType;
4 | import com.baomidou.mybatisplus.annotation.TableField;
5 | import com.baomidou.mybatisplus.annotation.TableId;
6 | import com.baomidou.mybatisplus.annotation.TableLogic;
7 | import com.baomidou.mybatisplus.annotation.TableName;
8 | import lombok.Data;
9 |
10 | import java.io.Serializable;
11 | import java.util.Date;
12 |
13 | /**
14 | * 接口信息
15 | *
16 | * @author by
17 | */
18 | @TableName(value = "interface")
19 | @Data
20 | public class InterfaceInfo implements Serializable {
21 | /**
22 | * 主键
23 | */
24 | @TableId(type = IdType.AUTO)
25 | private Long id;
26 |
27 | /**
28 | * 名称
29 | */
30 | private String name;
31 |
32 | /**
33 | * 描述
34 | */
35 | private String description;
36 |
37 | /**
38 | * 接口地址
39 | */
40 | private String url;
41 |
42 | /**
43 | * 请求类型
44 | */
45 | private String method;
46 |
47 | /**
48 | * 请求参数
49 | */
50 | private String requestParams;
51 |
52 | /**
53 | * 请求头
54 | */
55 | private String requestHeader;
56 |
57 | /**
58 | * 响应头
59 | */
60 | private String responseHeader;
61 |
62 | /**
63 | * 接口状态(0-关闭,1-开启)
64 | */
65 | private Integer status;
66 |
67 | /**
68 | * 示例代码
69 | */
70 | private String codeExample;
71 |
72 | /**
73 | * 创建时间
74 | */
75 | private Date createTime;
76 |
77 | /**
78 | * 更新时间
79 | */
80 | private Date updateTime;
81 |
82 | /**
83 | * 是否删除(0-未删, 1-已删)
84 | */
85 | @TableLogic
86 | private Integer isDeleted;
87 |
88 | @TableField(exist = false)
89 | private static final long serialVersionUID = 1L;
90 | }
--------------------------------------------------------------------------------
/byapi-common/src/main/java/com/example/common/model/entity/User.java:
--------------------------------------------------------------------------------
1 | package com.example.common.model.entity;
2 |
3 | import com.baomidou.mybatisplus.annotation.IdType;
4 | import com.baomidou.mybatisplus.annotation.TableField;
5 | import com.baomidou.mybatisplus.annotation.TableId;
6 | import com.baomidou.mybatisplus.annotation.TableLogic;
7 | import com.baomidou.mybatisplus.annotation.TableName;
8 |
9 | import java.io.Serializable;
10 | import java.util.Date;
11 |
12 | import lombok.Data;
13 |
14 | /**
15 | * 用户表
16 | *
17 | * @author by
18 | */
19 | @TableName(value = "user")
20 | @Data
21 | public class User implements Serializable {
22 | /**
23 | * id
24 | */
25 | @TableId(type = IdType.AUTO)
26 | private Long id;
27 |
28 | /**
29 | * 用户昵称
30 | */
31 | private String userName;
32 |
33 | /**
34 | * 账号
35 | */
36 | private String userAccount;
37 |
38 | /**
39 | * 用户头像
40 | */
41 | private String userAvatar;
42 |
43 | /**
44 | * 邮箱,用于邮箱登录
45 | */
46 | private String email;
47 |
48 | /**
49 | * 性别(0-男,1-女)
50 | */
51 | private Integer gender;
52 |
53 | /**
54 | * 用户状态(0-启用,1-禁用)
55 | */
56 | private Integer status;
57 |
58 | /**
59 | * 用户角色:user / admin
60 | */
61 | private String userRole;
62 |
63 | /**
64 | * 密码
65 | */
66 | private String userPassword;
67 |
68 | /**
69 | * 盐,用于加密
70 | */
71 | private String salt;
72 |
73 | /**
74 | * accessKey
75 | */
76 | private String accessKey;
77 |
78 | /**
79 | * secretKey
80 | */
81 | private String secretKey;
82 |
83 | /**
84 | * 创建时间
85 | */
86 | private Date createTime;
87 |
88 | /**
89 | * 更新时间
90 | */
91 | private Date updateTime;
92 |
93 | /**
94 | * 是否删除
95 | */
96 | @TableLogic
97 | private Integer isDeleted;
98 |
99 | @TableField(exist = false)
100 | private static final long serialVersionUID = 1L;
101 | }
--------------------------------------------------------------------------------
/byapi-common/src/main/java/com/example/common/model/entity/UserInterfaceInfo.java:
--------------------------------------------------------------------------------
1 | package com.example.common.model.entity;
2 |
3 | import com.baomidou.mybatisplus.annotation.IdType;
4 | import com.baomidou.mybatisplus.annotation.TableField;
5 | import com.baomidou.mybatisplus.annotation.TableId;
6 | import com.baomidou.mybatisplus.annotation.TableLogic;
7 | import com.baomidou.mybatisplus.annotation.TableName;
8 | import lombok.Data;
9 |
10 | import java.io.Serializable;
11 | import java.util.Date;
12 |
13 | /**
14 | * 用户调用接口关系
15 | *
16 | * @author by
17 | */
18 | @TableName(value = "user_interface")
19 | @Data
20 | public class UserInterfaceInfo implements Serializable {
21 | /**
22 | * 主键
23 | */
24 | @TableId(type = IdType.AUTO)
25 | private Long id;
26 |
27 | /**
28 | * 调用接口用户ID
29 | */
30 | private Long userId;
31 |
32 | /**
33 | * 接口ID
34 | */
35 | private Long interfaceId;
36 |
37 | /**
38 | * 总调用次数
39 | */
40 | private Integer totalNum;
41 |
42 | /**
43 | * 剩余调用次数
44 | */
45 | private Integer leftNum;
46 |
47 | /**
48 | * 创建时间
49 | */
50 | private Date createTime;
51 |
52 | /**
53 | * 更新时间
54 | */
55 | private Date updateTime;
56 |
57 | /**
58 | * 是否删除(0-未删, 1-已删)
59 | */
60 | @TableLogic
61 | private Integer isDeleted;
62 |
63 | @TableField(exist = false)
64 | private static final long serialVersionUID = 1L;
65 | }
--------------------------------------------------------------------------------
/byapi-common/src/main/java/com/example/common/model/vo/InterfaceVo.java:
--------------------------------------------------------------------------------
1 | package com.example.common.model.vo;
2 |
3 | import com.example.common.model.entity.InterfaceInfo;
4 | import lombok.Data;
5 | import lombok.EqualsAndHashCode;
6 |
7 | /**
8 | * 接口信息返回类
9 | *
10 | * @author by
11 | */
12 | @EqualsAndHashCode(callSuper = true)
13 | @Data
14 | public class InterfaceVo extends InterfaceInfo {
15 |
16 | /**
17 | * 总调用次数
18 | */
19 | private Integer totalNum;
20 |
21 | /**
22 | * 剩余调用次数
23 | */
24 | private Integer leftNum;
25 | }
26 |
--------------------------------------------------------------------------------
/byapi-common/src/main/java/com/example/common/model/vo/InvokeCountVo.java:
--------------------------------------------------------------------------------
1 | package com.example.common.model.vo;
2 |
3 | import lombok.Data;
4 |
5 | /**
6 | * 接口调用次数返回类
7 | *
8 | * @author by
9 | */
10 | @Data
11 | public class InvokeCountVo {
12 | /**
13 | * 接口名称
14 | */
15 | private String name;
16 | /**
17 | * 调用次数
18 | */
19 | private Integer count;
20 | }
21 |
--------------------------------------------------------------------------------
/byapi-common/src/main/java/com/example/common/model/vo/KeyVo.java:
--------------------------------------------------------------------------------
1 | package com.example.common.model.vo;
2 |
3 | import lombok.Data;
4 |
5 | /**
6 | * 密钥视图对象
7 | *
8 | * @author by
9 | */
10 | @Data
11 | public class KeyVo {
12 | /**
13 | * 访问密钥
14 | */
15 | private String accessKey;
16 | /**
17 | * 私钥
18 | */
19 | private String secretKey;
20 | }
21 |
--------------------------------------------------------------------------------
/byapi-common/src/main/java/com/example/common/model/vo/UserVo.java:
--------------------------------------------------------------------------------
1 | package com.example.common.model.vo;
2 |
3 | import lombok.Data;
4 |
5 | import java.util.Date;
6 |
7 | /**
8 | * @author by
9 | */
10 | @Data
11 | public class UserVo {
12 | /**
13 | * id
14 | */
15 | private Long id;
16 |
17 | /**
18 | * 用户昵称
19 | */
20 | private String userName;
21 |
22 | /**
23 | * 账号
24 | */
25 | private String userAccount;
26 |
27 | /**
28 | * 用户头像
29 | */
30 | private String userAvatar;
31 |
32 | /**
33 | * 邮箱
34 | */
35 | private String email;
36 |
37 | /**
38 | * 性别(0-男,1-女)
39 | */
40 | private Integer gender;
41 |
42 | /**
43 | * 用户角色:user / admin
44 | */
45 | private String userRole;
46 |
47 | /**
48 | * 创建时间
49 | */
50 | private Date createTime;
51 |
52 | /**
53 | * 更新时间
54 | */
55 | private Date updateTime;
56 | }
57 |
--------------------------------------------------------------------------------
/byapi-common/src/main/java/com/example/common/service/DubboInterfaceService.java:
--------------------------------------------------------------------------------
1 | package com.example.common.service;
2 |
3 | import com.example.common.model.entity.InterfaceInfo;
4 |
5 | /**
6 | * @author by
7 | */
8 | public interface DubboInterfaceService {
9 |
10 | /**
11 | * 根据url和method获取接口信息
12 | *
13 | * @param url 接口地址
14 | * @param method 接口方法
15 | * @return 接口信息
16 | */
17 | public InterfaceInfo getInterfaceInfo(String url, String method);
18 | }
19 |
--------------------------------------------------------------------------------
/byapi-common/src/main/java/com/example/common/service/DubboUserInterfaceService.java:
--------------------------------------------------------------------------------
1 | package com.example.common.service;
2 |
3 | /**
4 | * @author by
5 | */
6 | public interface DubboUserInterfaceService {
7 |
8 | /**
9 | * 统计接口调用次数
10 | *
11 | * @param interfaceId 接口ID
12 | * @param userId 用户ID
13 | */
14 | void invokeCount(long interfaceId, long userId);
15 | }
16 |
--------------------------------------------------------------------------------
/byapi-common/src/main/java/com/example/common/service/DubboUserService.java:
--------------------------------------------------------------------------------
1 | package com.example.common.service;
2 |
3 | import com.example.common.model.entity.User;
4 |
5 | /**
6 | * @author by
7 | */
8 | public interface DubboUserService {
9 |
10 | /**
11 | * 根据accessKey获取用户信息
12 | *
13 | * @param accessKey 签名
14 | * @return 用户信息
15 | */
16 | public User getInvokeUser(String accessKey);
17 | }
18 |
--------------------------------------------------------------------------------
/byapi-common/src/main/java/com/example/common/utils/PageBean.java:
--------------------------------------------------------------------------------
1 | package com.example.common.utils;
2 |
3 | import lombok.AllArgsConstructor;
4 | import lombok.Data;
5 | import lombok.NoArgsConstructor;
6 |
7 | import java.util.List;
8 |
9 | /**
10 | * 分页工具类
11 | *
12 | * @param
13 | * @author by
14 | */
15 | @Data
16 | @NoArgsConstructor
17 | @AllArgsConstructor(staticName = "of")
18 | public class PageBean {
19 | /**
20 | * 总条数
21 | */
22 | private Long total;
23 | /**
24 | * 总记录数
25 | */
26 | private List records;
27 | }
28 |
--------------------------------------------------------------------------------
/byapi-common/src/main/java/com/example/common/utils/PageRequest.java:
--------------------------------------------------------------------------------
1 | package com.example.common.utils;
2 |
3 | import lombok.Data;
4 |
5 | import java.io.Serializable;
6 |
7 | /**
8 | * 通用分页参数
9 | *
10 | * @author by
11 | */
12 | @Data
13 | public class PageRequest implements Serializable {
14 | /**
15 | * 当前页码
16 | */
17 | private Integer current = 1;
18 | /**
19 | * 每页记录数
20 | */
21 | private Integer pageSize = 10;
22 | }
23 |
--------------------------------------------------------------------------------
/byapi-common/src/main/java/com/example/common/utils/Result.java:
--------------------------------------------------------------------------------
1 | package com.example.common.utils;
2 |
3 | import lombok.Data;
4 |
5 | /**
6 | * 返回结果工具类
7 | *
8 | * @author by
9 | */
10 | @Data
11 | public class Result {
12 |
13 | /**
14 | * 响应码
15 | */
16 | private Integer code;
17 | /**
18 | * 响应数据
19 | */
20 | private T data;
21 | /**
22 | * 响应信息
23 | */
24 | private String message;
25 |
26 | /**
27 | * 响应成功,不带数据
28 | */
29 | public static Result success() {
30 | Result result = new Result<>();
31 | result.setCode(200);
32 | result.setData(null);
33 | result.setMessage("请求成功");
34 | return result;
35 | }
36 |
37 | /**
38 | * 响应成功,带返回数据
39 | */
40 | public static Result success(T data) {
41 | Result result = new Result<>();
42 | result.setCode(200);
43 | result.setData(data);
44 | result.setMessage("请求成功");
45 | return result;
46 | }
47 |
48 | /**
49 | * 响应失败
50 | */
51 | public static Result error(Integer code, String message) {
52 | Result result = new Result<>();
53 | result.setCode(code);
54 | result.setData(null);
55 | result.setMessage(message);
56 | return result;
57 | }
58 | }
59 |
--------------------------------------------------------------------------------
/byapi-common/src/main/java/com/example/common/utils/SignUtil.java:
--------------------------------------------------------------------------------
1 | package com.example.common.utils;
2 |
3 | import cn.hutool.crypto.digest.DigestAlgorithm;
4 | import cn.hutool.crypto.digest.Digester;
5 |
6 | /**
7 | * 签名工具类
8 | *
9 | * @author by
10 | */
11 | public class SignUtil {
12 |
13 | public static String generateSign(String body, String secretKey) {
14 | String newSecret = body + "-" + secretKey;
15 | Digester digester = new Digester(DigestAlgorithm.SHA256);
16 | return digester.digestHex(newSecret);
17 | }
18 | }
--------------------------------------------------------------------------------
/byapi-common/src/main/java/com/example/common/utils/TokenBucketLimiter.java:
--------------------------------------------------------------------------------
1 | package com.example.common.utils;
2 |
3 | import org.springframework.stereotype.Component;
4 |
5 | import java.util.concurrent.atomic.AtomicInteger;
6 |
7 | /**
8 | * 令牌桶算法实现类
9 | *
10 | * @author by
11 | */
12 | @Component
13 | public class TokenBucketLimiter {
14 | /**
15 | * 上一次获取令牌的时间
16 | */
17 | public long lastTime = System.currentTimeMillis();
18 | /**
19 | * 令牌桶的容量
20 | */
21 | public int capacity = 4;
22 | /**
23 | * 生成令牌的速率(每秒2个)
24 | */
25 | public int rate = 2;
26 | /**
27 | * 当前桶里的令牌数量
28 | */
29 | public AtomicInteger tokens = new AtomicInteger(0);
30 |
31 | /**
32 | * 令牌桶算法实现限流,默认每次请求消耗一个令牌
33 | *
34 | * @return 是否限流
35 | */
36 |
37 | public synchronized boolean isLimited() {
38 | //获取当前时间
39 | long currentTime = System.currentTimeMillis();
40 | //计算时间间隔(单位为ms)
41 | long gap = currentTime - lastTime;
42 | //计算在这段时间内生成的令牌
43 | int generateCount = (int) (gap / 1000 * rate);
44 | //计算可能的令牌总数
45 | int allTokensCount = tokens.get() + generateCount;
46 | //设置令牌桶里的令牌数量,这里要取总数量与桶容量之间的最小值
47 | tokens.set(Math.min(capacity, allTokensCount));
48 | //开始获取令牌
49 | if (tokens.get() < 1) {
50 | //当前桶里令牌数量小于1个,进行限流
51 | return true;
52 | }
53 | //令牌数量足够,领取令牌,重新计算上一次获取令牌的时间
54 | tokens.decrementAndGet();
55 | lastTime = currentTime;
56 | return false;
57 | }
58 | }
59 |
--------------------------------------------------------------------------------
/byapi-gateway/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 | 4.0.0
5 | com.example
6 | byapi-gateway
7 | 0.0.1-SNAPSHOT
8 | byapi-gateway
9 | byapi-gateway
10 |
11 | 1.8
12 | UTF-8
13 | UTF-8
14 | 2.6.13
15 | 2021.0.5
16 |
17 |
18 |
19 | org.springframework.boot
20 | spring-boot-starter-data-redis
21 |
22 |
23 | org.apache.dubbo
24 | dubbo
25 | 3.0.9
26 |
27 |
28 | com.alibaba.nacos
29 | nacos-client
30 | 2.1.0
31 |
32 |
33 | org.example
34 | byapi-common
35 | 1.0-SNAPSHOT
36 |
37 |
38 | org.springframework.cloud
39 | spring-cloud-starter-gateway
40 |
41 |
42 | org.springframework.boot
43 | spring-boot-starter-test
44 | test
45 |
46 |
47 |
48 |
49 |
50 | org.springframework.cloud
51 | spring-cloud-dependencies
52 | ${spring-cloud.version}
53 | pom
54 | import
55 |
56 |
57 | org.springframework.boot
58 | spring-boot-dependencies
59 | ${spring-boot.version}
60 | pom
61 | import
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 | org.apache.maven.plugins
70 | maven-compiler-plugin
71 | 3.8.1
72 |
73 | 1.8
74 | 1.8
75 | UTF-8
76 |
77 |
78 |
79 | org.springframework.boot
80 | spring-boot-maven-plugin
81 | ${spring-boot.version}
82 |
83 | com.example.gateway.ByapiGatewayApplication
84 | true
85 |
86 |
87 |
88 | repackage
89 |
90 | repackage
91 |
92 |
93 |
94 |
95 |
96 |
97 |
98 |
99 |
--------------------------------------------------------------------------------
/byapi-gateway/src/main/java/com/example/gateway/ByapiGatewayApplication.java:
--------------------------------------------------------------------------------
1 | package com.example.gateway;
2 |
3 | import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
4 | import org.springframework.boot.SpringApplication;
5 | import org.springframework.boot.autoconfigure.SpringBootApplication;
6 | import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
7 |
8 | /**
9 | * @author by
10 | */
11 | @SpringBootApplication(exclude = {DataSourceAutoConfiguration.class},scanBasePackages = {"com.example.gateway","com.example.common"})
12 | @EnableDubbo
13 | public class ByapiGatewayApplication {
14 |
15 | public static void main(String[] args) {
16 | SpringApplication.run(ByapiGatewayApplication.class, args);
17 | }
18 |
19 | }
20 |
--------------------------------------------------------------------------------
/byapi-gateway/src/main/java/com/example/gateway/config/RedisConfig.java:
--------------------------------------------------------------------------------
1 | package com.example.gateway.config;
2 |
3 | import com.fasterxml.jackson.annotation.JsonAutoDetect;
4 | import com.fasterxml.jackson.annotation.JsonTypeInfo;
5 | import com.fasterxml.jackson.annotation.PropertyAccessor;
6 | import com.fasterxml.jackson.databind.ObjectMapper;
7 | import org.springframework.context.annotation.Bean;
8 | import org.springframework.context.annotation.Configuration;
9 | import org.springframework.data.redis.connection.RedisConnectionFactory;
10 | import org.springframework.data.redis.core.RedisTemplate;
11 | import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
12 | import org.springframework.data.redis.serializer.StringRedisSerializer;
13 |
14 | /**
15 | * Redis配置类
16 | *
17 | * @author by
18 | */
19 | @Configuration
20 | public class RedisConfig {
21 |
22 | @Bean
23 | public RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory) {
24 | RedisTemplate template = new RedisTemplate<>();
25 | template.setConnectionFactory(redisConnectionFactory);
26 |
27 | //配置redis序列化方式
28 | Jackson2JsonRedisSerializer