pull/3/head
tanghc 4 years ago
parent 8e8f24f5e8
commit f223a56fd5
  1. 4
      doc/docs/files/10092_接口限流.md
  2. 4
      sop-admin/sop-admin-server/src/main/java/com/gitee/sop/adminserver/api/service/param/LimitNewAddParam.java
  3. 4
      sop-admin/sop-admin-server/src/main/java/com/gitee/sop/adminserver/api/service/param/LimitNewUpdateParam.java
  4. 4
      sop-admin/sop-admin-server/src/main/java/com/gitee/sop/adminserver/api/service/param/LimitParam.java
  5. 4
      sop-admin/sop-admin-server/src/main/java/com/gitee/sop/adminserver/api/service/result/LimitNewVO.java
  6. 4
      sop-admin/sop-admin-server/src/main/java/com/gitee/sop/adminserver/api/service/result/LimitVO.java
  7. 2
      sop-admin/sop-admin-server/src/main/java/com/gitee/sop/adminserver/bean/ConfigLimitDto.java
  8. 2
      sop-admin/sop-admin-server/src/main/java/com/gitee/sop/adminserver/common/LimitEnum.java
  9. 2
      sop-admin/sop-admin-server/src/main/java/com/gitee/sop/adminserver/entity/ConfigLimit.java
  10. 2
      sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/limit/DefaultLimitManager.java
  11. 2
      sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/limit/LimitType.java
  12. 2
      sop-gateway/src/main/java/com/gitee/sop/gateway/entity/ConfigLimit.java
  13. 2
      sop-gateway/src/main/java/com/gitee/sop/gateway/entity/ConfigRouteLimit.java
  14. 15
      sop-mysql5.6以下版本.sql
  15. 27
      sop.sql

@ -2,10 +2,10 @@
SOP提供了简单的接口限流策略:
- 漏桶策略:每秒处理固定数量的请求,超出请求返回错误信息。
- 窗口策略:每秒处理固定数量的请求,超出请求返回错误信息。
- 令牌桶策略:每秒放置固定数量的令牌数,每个请求进来后先去拿令牌,拿到了令牌才能继续,拿不到则等候令牌重新生成了再拿。
如果一个接口设置了漏桶策略,假设接口每秒可处理5个请求,一秒内同时有6个请求进来,前5个接口是能够访问的,第六个请求将返回错误信息。
如果一个接口设置了窗口策略,假设接口每秒可处理5个请求,一秒内同时有6个请求进来,前5个接口是能够访问的,第六个请求将返回错误信息。
如果设置了令牌桶策略,桶的容量是5,那么每秒中生成5个令牌,同一时间有6个请求进来,那么前5个能成功拿到令牌继续,第六个则等待,令牌重新生成了再拿。

@ -30,8 +30,8 @@ public class LimitNewAddParam {
@NotBlank(message = "serviceId can not null")
private String serviceId;
/** 限流策略,1:漏桶策略,2:令牌桶策略, 数据库字段:limit_type */
@ApiDocField(description = "限流策略,1:漏桶策略,2:令牌桶策略")
/** 限流策略,1:窗口策略,2:令牌桶策略, 数据库字段:limit_type */
@ApiDocField(description = "限流策略,1:窗口策略,2:令牌桶策略")
@NotNull(message = "limitType不能为空")
private Byte limitType;

@ -35,8 +35,8 @@ public class LimitNewUpdateParam {
@NotBlank(message = "serviceId can not null")
private String serviceId;
/** 限流策略,1:漏桶策略,2:令牌桶策略, 数据库字段:limit_type */
@ApiDocField(description = "限流策略,1:漏桶策略,2:令牌桶策略")
/** 限流策略,1:窗口策略,2:令牌桶策略, 数据库字段:limit_type */
@ApiDocField(description = "限流策略,1:窗口策略,2:令牌桶策略")
@NotNull(message = "limitType不能为空")
private Byte limitType;

@ -20,8 +20,8 @@ public class LimitParam {
@NotBlank(message = "serviceId can not null")
private String serviceId;
/** 限流策略,1:漏桶策略,2:令牌桶策略, 数据库字段:limit_type */
@ApiDocField(description = "限流策略,1:漏桶策略,2:令牌桶策略")
/** 限流策略,1:窗口策略,2:令牌桶策略, 数据库字段:limit_type */
@ApiDocField(description = "限流策略,1:窗口策略,2:令牌桶策略")
@NotNull
private Byte limitType;

@ -41,9 +41,9 @@ public class LimitNewVO {
private String serviceId;
/**
* 限流策略1漏桶策略2令牌桶策略, 数据库字段limit_type
* 限流策略1窗口策略2令牌桶策略, 数据库字段limit_type
*/
@ApiDocField(description = "限流策略,1:漏桶策略,2:令牌桶策略")
@ApiDocField(description = "限流策略,1:窗口策略,2:令牌桶策略")
private Byte limitType;
/**

@ -32,9 +32,9 @@ public class LimitVO {
private String serviceId;
/**
* 限流策略1漏桶策略2令牌桶策略, 数据库字段limit_type
* 限流策略1窗口策略2令牌桶策略, 数据库字段limit_type
*/
@ApiDocField(description = "限流策略,1:漏桶策略,2:令牌桶策略")
@ApiDocField(description = "限流策略,1:窗口策略,2:令牌桶策略")
private Byte limitType;
/**

@ -21,7 +21,7 @@ public class ConfigLimitDto {
/** 服务id, 数据库字段:service_id */
private String serviceId;
/** 限流策略,1:漏桶策略,2:令牌桶策略, 数据库字段:limit_type */
/** 限流策略,1:窗口策略,2:令牌桶策略, 数据库字段:limit_type */
private Byte limitType;
/** 每秒可处理请求数, 数据库字段:exec_count_per_second */

@ -6,7 +6,7 @@ package com.gitee.sop.adminserver.common;
*/
public enum LimitEnum {
/**
* 限流策略1漏桶策略
* 限流策略1窗口策略
*/
TYPE_LEAKY_BUCKET((byte) 1),
/**

@ -37,7 +37,7 @@ public class ConfigLimit {
/** 服务id, 数据库字段:service_id */
private String serviceId;
/** 限流策略,1:漏桶策略,2:令牌桶策略, 数据库字段:limit_type */
/** 限流策略,1:窗口策略,2:令牌桶策略, 数据库字段:limit_type */
private Byte limitType;
/** 每秒可处理请求数, 数据库字段:exec_count_per_second */

@ -39,7 +39,7 @@ public class DefaultLimitManager implements LimitManager {
// 被限流了
return counter.get(routeConfig.getId()).incrementAndGet() <= execCountPerSecond;
} catch (ExecutionException e) {
log.error("漏桶限流出错,routeConfig:{}", routeConfig, e);
log.error("窗口限流出错,routeConfig:{}", routeConfig, e);
return false;
}
}

@ -7,7 +7,7 @@ package com.gitee.sop.gatewaycommon.limit;
*/
public enum LimitType {
/**
* 漏桶策略每秒处理固定数量的请求超出请求返回错误信息
* 窗口策略每秒处理固定数量的请求超出请求返回错误信息
*/
LEAKY_BUCKET(1),
/**

@ -38,7 +38,7 @@ public class ConfigLimit {
/** 服务id, 数据库字段:service_id */
private String serviceId;
/** 限流策略,1:漏桶策略,2:令牌桶策略, 数据库字段:limit_type */
/** 限流策略,1:窗口策略,2:令牌桶策略, 数据库字段:limit_type */
private Byte limitType;
/** 每秒可处理请求数, 数据库字段:exec_count_per_second */

@ -28,7 +28,7 @@ public class ConfigRouteLimit {
/** 路由id, 数据库字段:route_id */
private String routeId;
/** 限流策略,1:漏桶策略,2:令牌桶策略, 数据库字段:limit_type */
/** 限流策略,1:窗口策略,2:令牌桶策略, 数据库字段:limit_type */
private Byte limitType;
/** 每秒可处理请求数, 数据库字段:exec_count_per_second */

@ -87,7 +87,7 @@ CREATE TABLE `config_limit` (
`app_key` VARCHAR(128) DEFAULT NULL,
`limit_ip` VARCHAR(300) DEFAULT NULL COMMENT '限流ip,多个用英文逗号隔开',
`service_id` VARCHAR(64) NOT NULL DEFAULT '' COMMENT '服务id',
`limit_type` TINYINT(4) NOT NULL DEFAULT '1' COMMENT '限流策略,1:漏桶策略,2:令牌桶策略',
`limit_type` TINYINT(4) NOT NULL DEFAULT '1' COMMENT '限流策略,1:窗口策略,2:令牌桶策略',
`exec_count_per_second` INT(11) DEFAULT NULL COMMENT '每秒可处理请求数',
`duration_seconds` INT(11) NOT NULL DEFAULT '1' COMMENT '限流持续时间,默认1秒,即每durationSeconds秒允许多少请求(当limit_type=1时有效)',
`limit_code` VARCHAR(64) DEFAULT NULL COMMENT '返回的错误码',
@ -117,7 +117,7 @@ CREATE TABLE `config_route_limit` (
`id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
`route_id` VARCHAR(64) NOT NULL COMMENT '路由id',
`service_id` VARCHAR(64) NOT NULL DEFAULT '',
`limit_type` TINYINT(4) NOT NULL DEFAULT '1' COMMENT '限流策略,1:漏桶策略,2:令牌桶策略',
`limit_type` TINYINT(4) NOT NULL DEFAULT '1' COMMENT '限流策略,1:窗口策略,2:令牌桶策略',
`exec_count_per_second` INT(11) DEFAULT NULL COMMENT '每秒可处理请求数',
`limit_code` VARCHAR(64) DEFAULT NULL COMMENT '返回的错误码',
`limit_msg` VARCHAR(100) DEFAULT NULL COMMENT '返回的错误信息',
@ -231,17 +231,6 @@ INSERT INTO `admin_user_info` (`id`, `username`, `password`, `status`, `gmt_crea
ALTER TABLE `admin_user_info` ENABLE KEYS;
INSERT INTO `config_limit` (`id`, `route_id`, `app_key`, `limit_ip`, `service_id`, `limit_type`, `exec_count_per_second`, `limit_code`, `limit_msg`, `token_bucket_count`, `limit_status`, `order_index`, `remark`, `gmt_create`, `gmt_modified`) VALUES
(1,'alipay.story.get1.0','','192.168.1.1,172.2.2.3','story-service',2,5,'','',6,1,3,NULL,'2019-05-17 19:21:35','2019-05-21 09:12:15'),
(2,'alipay.story.get1.0','2019032617262200001','','story-service',2,5,'service-budy','服务器忙',10,1,0,'','2019-05-17 19:39:30','2019-05-21 15:36:52'),
(3,'alipay.story.find1.0','20190331562013861008375808','','story-service',1,3,'service-busy','服务器忙',5,1,1,NULL,'2019-05-17 20:20:32','2019-05-20 17:40:17'),
(4,'alipay.story.get1.2','','','story-service',1,5,'service-busy','服务器忙',3,1,1,'','2019-05-20 16:27:21','2019-05-21 15:53:10'),
(5,'','20190401562373796095328256','','story-service',1,5,'service-busy','服务器忙',5,1,0,'这个appKey调用很频繁,重点照顾','2019-05-21 15:48:08','2019-05-21 18:45:32'),
(6,'','','10.1.30.54','story-service',1,5,'service-busy','服务器忙',5,1,0,'这个ip在攻击我们','2019-05-21 15:55:33','2019-05-21 18:17:29'),
(7,'story.get1.1','','10.1.30.54','story-service',1,5,'service-busy','服务器忙',5,1,0,NULL,'2019-05-21 16:30:48','2019-05-21 16:30:48'),
(8,'','20190513577548661718777857','10.1.30.54','story-service',1,5,'service-busy','服务器忙',5,1,0,NULL,'2019-05-21 17:10:45','2019-05-21 17:10:52');
INSERT INTO `config_route_base` (`id`, `route_id`, `status`, `gmt_create`, `gmt_modified`) VALUES
(1,'story.get1.1',1,'2019-04-09 19:15:58','2019-04-09 19:16:54'),
(2,'alipay.story.get1.0',1,'2019-04-09 19:19:57','2019-04-19 14:45:33'),

@ -89,7 +89,7 @@ CREATE TABLE `config_limit` (
`app_key` varchar(128) DEFAULT NULL,
`limit_ip` varchar(300) DEFAULT NULL COMMENT '限流ip,多个用英文逗号隔开',
`service_id` varchar(64) NOT NULL DEFAULT '' COMMENT '服务id',
`limit_type` tinyint(4) NOT NULL DEFAULT '1' COMMENT '限流策略,1:漏桶策略,2:令牌桶策略',
`limit_type` tinyint(4) NOT NULL DEFAULT '1' COMMENT '限流策略,1:窗口策略,2:令牌桶策略',
`exec_count_per_second` int(11) DEFAULT NULL COMMENT '每秒可处理请求数',
`duration_seconds` int(11) NOT NULL DEFAULT '1' COMMENT '限流持续时间,默认1秒,即每durationSeconds秒允许多少请求(当limit_type=1时有效)',
`limit_code` varchar(64) DEFAULT NULL COMMENT '返回的错误码',
@ -119,7 +119,7 @@ CREATE TABLE `config_route_limit` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`route_id` varchar(64) NOT NULL COMMENT '路由id',
`service_id` varchar(64) NOT NULL DEFAULT '',
`limit_type` tinyint(4) NOT NULL DEFAULT '1' COMMENT '限流策略,1:漏桶策略,2:令牌桶策略',
`limit_type` tinyint(4) NOT NULL DEFAULT '1' COMMENT '限流策略,1:窗口策略,2:令牌桶策略',
`exec_count_per_second` int(11) DEFAULT NULL COMMENT '每秒可处理请求数',
`limit_code` varchar(64) DEFAULT NULL COMMENT '返回的错误码',
`limit_msg` varchar(100) DEFAULT NULL COMMENT '返回的错误信息',
@ -229,31 +229,10 @@ CREATE TABLE `user_info` (
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8 COMMENT='用户信息表';
INSERT INTO `admin_user_info` (`id`, `username`, `password`, `status`, `gmt_create`, `gmt_modified`) VALUES
(1,'admin','a62cd510fb9a8a557a27ef279569091f',1,'2019-04-02 19:55:26','2019-04-02 19:55:26');
INSERT INTO `config_limit` (`id`, `route_id`, `app_key`, `limit_ip`, `service_id`, `limit_type`, `exec_count_per_second`, `duration_seconds`, `limit_code`, `limit_msg`, `token_bucket_count`, `limit_status`, `order_index`, `remark`, `gmt_create`, `gmt_modified`) VALUES
(1,'alipay.story.get1.0','','192.168.1.1,172.2.2.3','story-service',2,5,1,'','',6,1,3,NULL,'2019-05-17 19:21:35','2019-05-21 09:12:15'),
(2,'alipay.story.get1.0','2019032617262200001','','story-service',2,5,1,'service-budy','服务器忙',10,1,0,'','2019-05-17 19:39:30','2019-05-21 15:36:52'),
(3,'alipay.story.find1.0','20190331562013861008375808','','story-service',1,3,1,'service-busy','服务器忙',5,1,1,NULL,'2019-05-17 20:20:32','2019-05-20 17:40:17'),
(4,'alipay.story.get1.2','','','story-service',1,5,1,'service-busy','服务器忙',3,1,1,'','2019-05-20 16:27:21','2019-05-21 15:53:10'),
(5,'','20190401562373796095328256','','story-service',1,5,1,'service-busy','服务器忙',5,1,0,'这个appKey调用很频繁,重点照顾','2019-05-21 15:48:08','2019-05-21 18:45:32'),
(6,'','','10.1.30.54','story-service',1,5,1,'service-busy','服务器忙',5,1,0,'这个ip在攻击我们','2019-05-21 15:55:33','2019-05-21 18:17:29'),
(7,'story.get1.1','','10.1.30.54','story-service',1,5,1,'service-busy','服务器忙',5,1,0,NULL,'2019-05-21 16:30:48','2019-05-21 16:30:48'),
(8,'','20190513577548661718777857','10.1.30.54','story-service',1,5,1,'service-busy','服务器忙',5,1,0,NULL,'2019-05-21 17:10:45','2019-05-21 17:10:52');
INSERT INTO `config_route_base` (`id`, `route_id`, `status`, `gmt_create`, `gmt_modified`) VALUES
(1,'story.get1.1',1,'2019-04-09 19:15:58','2019-04-09 19:16:54'),
(2,'alipay.story.get1.0',1,'2019-04-09 19:19:57','2019-04-19 14:45:33'),
@ -272,8 +251,6 @@ INSERT INTO `config_route_limit` (`id`, `route_id`, `service_id`, `limit_type`,
(4,'alipay.category.get1.0','story-service',1,5,'error-code','服务器忙',5,1,'2019-05-07 12:22:04','2019-05-07 12:25:06');
INSERT INTO `isv_info` (`id`, `app_key`, `status`, `sign_type`, `remark`, `gmt_create`, `gmt_modified`) VALUES
(1,'2019032617262200001',1,1,NULL,'2019-03-27 10:10:34','2019-05-09 11:10:38'),
(3,'asdfasdf',2,1,NULL,'2019-03-27 11:01:11','2019-05-11 10:45:01'),

Loading…
Cancel
Save