mirror of
https://gitee.com/durcframework/SOP.git
synced 2025-08-11 21:57:56 +08:00
代码优化
This commit is contained in:
@@ -8,26 +8,11 @@ import lombok.Data;
|
|||||||
@Data
|
@Data
|
||||||
public class RouteConfigDto {
|
public class RouteConfigDto {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 路由id
|
||||||
|
*/
|
||||||
private String routeId;
|
private String routeId;
|
||||||
|
|
||||||
/** 限流策略,1:漏桶策略,2:令牌桶策略, 数据库字段:limit_type */
|
|
||||||
private Byte limitType;
|
|
||||||
|
|
||||||
/** 每秒可处理请求数, 数据库字段:exec_count_per_second */
|
|
||||||
private Integer execCountPerSecond;
|
|
||||||
|
|
||||||
/** 返回的错误码, 数据库字段:limit_code */
|
|
||||||
private String limitCode;
|
|
||||||
|
|
||||||
/** 返回的错误信息, 数据库字段:limit_msg */
|
|
||||||
private String limitMsg;
|
|
||||||
|
|
||||||
/** 令牌桶容量, 数据库字段:token_bucket_count */
|
|
||||||
private Integer tokenBucketCount;
|
|
||||||
|
|
||||||
/** 限流开启状态,1:开启,0关闭, 数据库字段:limit_status */
|
|
||||||
private Byte limitStatus;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 状态,0:待审核,1:启用,2:禁用
|
* 状态,0:待审核,1:启用,2:禁用
|
||||||
*/
|
*/
|
||||||
|
@@ -16,6 +16,9 @@ import java.util.concurrent.atomic.AtomicLong;
|
|||||||
@Data
|
@Data
|
||||||
public class ConfigLimitDto {
|
public class ConfigLimitDto {
|
||||||
|
|
||||||
|
public static final byte LIMIT_STATUS_OPEN = 1;
|
||||||
|
public static final byte LIMIT_STATUS_CLOSE = 0;
|
||||||
|
|
||||||
/** 数据库字段:id */
|
/** 数据库字段:id */
|
||||||
private Long id;
|
private Long id;
|
||||||
|
|
||||||
|
@@ -1,16 +1,6 @@
|
|||||||
package com.gitee.sop.gatewaycommon.bean;
|
package com.gitee.sop.gatewaycommon.bean;
|
||||||
|
|
||||||
import com.google.common.cache.CacheBuilder;
|
|
||||||
import com.google.common.cache.CacheLoader;
|
|
||||||
import com.google.common.cache.LoadingCache;
|
|
||||||
import com.google.common.util.concurrent.RateLimiter;
|
|
||||||
import lombok.AccessLevel;
|
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
import lombok.Getter;
|
|
||||||
import lombok.Setter;
|
|
||||||
|
|
||||||
import java.util.concurrent.TimeUnit;
|
|
||||||
import java.util.concurrent.atomic.AtomicLong;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author tanghc
|
* @author tanghc
|
||||||
@@ -19,73 +9,20 @@ import java.util.concurrent.atomic.AtomicLong;
|
|||||||
public class RouteConfig {
|
public class RouteConfig {
|
||||||
|
|
||||||
public static final byte STATUS_ENABLE = 1;
|
public static final byte STATUS_ENABLE = 1;
|
||||||
public static final byte LIMIT_STATUS_CLOSE = 0;
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 路由id
|
||||||
|
*/
|
||||||
private String routeId;
|
private String routeId;
|
||||||
|
|
||||||
/** 限流策略,1:漏桶策略,2:令牌桶策略, 数据库字段:limit_type */
|
|
||||||
private Byte limitType = 1;
|
|
||||||
|
|
||||||
/** 每秒可处理请求数, 数据库字段:exec_count_per_second */
|
|
||||||
private Integer execCountPerSecond = 10;
|
|
||||||
|
|
||||||
/** 返回的错误码, 数据库字段:limit_code */
|
|
||||||
private String limitCode = "isp.service-busy";
|
|
||||||
|
|
||||||
/** 返回的错误信息, 数据库字段:limit_msg */
|
|
||||||
private String limitMsg = "服务繁忙,请稍后再试";
|
|
||||||
|
|
||||||
/** 令牌桶容量, 数据库字段:token_bucket_count */
|
|
||||||
private Integer tokenBucketCount = 10;
|
|
||||||
|
|
||||||
/** 限流开启状态,1:开启,0关闭, 数据库字段:limit_status */
|
|
||||||
private Byte limitStatus = LIMIT_STATUS_CLOSE;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 状态,0:待审核,1:启用,2:禁用。默认启用
|
* 状态,0:待审核,1:启用,2:禁用。默认启用
|
||||||
*/
|
*/
|
||||||
private Byte status = STATUS_ENABLE;
|
private Byte status = STATUS_ENABLE;
|
||||||
|
|
||||||
/**
|
|
||||||
* 漏桶计数器
|
|
||||||
*/
|
|
||||||
private LoadingCache<Long, AtomicLong> counter = CacheBuilder.newBuilder()
|
|
||||||
.expireAfterWrite(2, TimeUnit.SECONDS)
|
|
||||||
.build(new CacheLoader<Long, AtomicLong>() {
|
|
||||||
@Override
|
|
||||||
public AtomicLong load(Long seconds) throws Exception {
|
|
||||||
return new AtomicLong(0);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 令牌桶
|
|
||||||
*/
|
|
||||||
@Getter(AccessLevel.PRIVATE)
|
|
||||||
@Setter(AccessLevel.PRIVATE)
|
|
||||||
private volatile RateLimiter rateLimiter;
|
|
||||||
|
|
||||||
public synchronized void initRateLimiter() {
|
|
||||||
rateLimiter = RateLimiter.create(tokenBucketCount);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 获取令牌桶
|
|
||||||
* @return
|
|
||||||
*/
|
|
||||||
public RateLimiter fetchRateLimiter() {
|
|
||||||
if (rateLimiter == null) {
|
|
||||||
synchronized (this.routeId) {
|
|
||||||
if (rateLimiter == null) {
|
|
||||||
rateLimiter = RateLimiter.create(tokenBucketCount);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return rateLimiter;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 是否启用
|
* 是否启用
|
||||||
|
*
|
||||||
* @return true:启用
|
* @return true:启用
|
||||||
*/
|
*/
|
||||||
public boolean enable() {
|
public boolean enable() {
|
||||||
|
@@ -1,37 +0,0 @@
|
|||||||
package com.gitee.sop.gatewaycommon.bean;
|
|
||||||
|
|
||||||
import lombok.Data;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @author tanghc
|
|
||||||
*/
|
|
||||||
@Data
|
|
||||||
public class RouteConfigDto {
|
|
||||||
|
|
||||||
private String routeId;
|
|
||||||
|
|
||||||
/** 限流策略,1:漏桶策略,2:令牌桶策略, 数据库字段:limit_type */
|
|
||||||
private Byte limitType;
|
|
||||||
|
|
||||||
/** 每秒可处理请求数, 数据库字段:exec_count_per_second */
|
|
||||||
private Integer execCountPerSecond;
|
|
||||||
|
|
||||||
/** 返回的错误码, 数据库字段:limit_code */
|
|
||||||
private String limitCode;
|
|
||||||
|
|
||||||
/** 返回的错误信息, 数据库字段:limit_msg */
|
|
||||||
private String limitMsg;
|
|
||||||
|
|
||||||
/** 令牌桶容量, 数据库字段:token_bucket_count */
|
|
||||||
private Integer tokenBucketCount;
|
|
||||||
|
|
||||||
/** 限流开启状态,1:开启,0关闭, 数据库字段:limit_status */
|
|
||||||
private Byte limitStatus;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 状态,0:待审核,1:启用,2:禁用
|
|
||||||
*/
|
|
||||||
private Byte status;
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
@@ -2,7 +2,6 @@ package com.gitee.sop.gatewaycommon.gateway.filter;
|
|||||||
|
|
||||||
import com.gitee.sop.gatewaycommon.bean.ApiConfig;
|
import com.gitee.sop.gatewaycommon.bean.ApiConfig;
|
||||||
import com.gitee.sop.gatewaycommon.bean.ConfigLimitDto;
|
import com.gitee.sop.gatewaycommon.bean.ConfigLimitDto;
|
||||||
import com.gitee.sop.gatewaycommon.bean.RouteConfig;
|
|
||||||
import com.gitee.sop.gatewaycommon.exception.ApiException;
|
import com.gitee.sop.gatewaycommon.exception.ApiException;
|
||||||
import com.gitee.sop.gatewaycommon.gateway.GatewayContext;
|
import com.gitee.sop.gatewaycommon.gateway.GatewayContext;
|
||||||
import com.gitee.sop.gatewaycommon.limit.LimitManager;
|
import com.gitee.sop.gatewaycommon.limit.LimitManager;
|
||||||
@@ -41,7 +40,7 @@ public class LimitFilter implements GlobalFilter, Ordered {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
// 单个限流功能未开启
|
// 单个限流功能未开启
|
||||||
if (configLimitDto.getLimitStatus() == RouteConfig.LIMIT_STATUS_CLOSE) {
|
if (configLimitDto.getLimitStatus() == ConfigLimitDto.LIMIT_STATUS_CLOSE) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
byte limitType = configLimitDto.getLimitType().byteValue();
|
byte limitType = configLimitDto.getLimitType().byteValue();
|
||||||
|
@@ -1,7 +1,6 @@
|
|||||||
package com.gitee.sop.gatewaycommon.limit;
|
package com.gitee.sop.gatewaycommon.limit;
|
||||||
|
|
||||||
import com.gitee.sop.gatewaycommon.bean.ConfigLimitDto;
|
import com.gitee.sop.gatewaycommon.bean.ConfigLimitDto;
|
||||||
import com.gitee.sop.gatewaycommon.bean.RouteConfig;
|
|
||||||
import com.google.common.cache.LoadingCache;
|
import com.google.common.cache.LoadingCache;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
|
||||||
@@ -16,7 +15,7 @@ public class DefaultLimitManager implements LimitManager {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public double acquireToken(ConfigLimitDto routeConfig) {
|
public double acquireToken(ConfigLimitDto routeConfig) {
|
||||||
if (routeConfig.getLimitStatus() == RouteConfig.LIMIT_STATUS_CLOSE) {
|
if (routeConfig.getLimitStatus() == ConfigLimitDto.LIMIT_STATUS_CLOSE) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
if (LimitType.LEAKY_BUCKET.getType() == routeConfig.getLimitType().byteValue()) {
|
if (LimitType.LEAKY_BUCKET.getType() == routeConfig.getLimitType().byteValue()) {
|
||||||
@@ -28,7 +27,7 @@ public class DefaultLimitManager implements LimitManager {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean acquire(ConfigLimitDto routeConfig) {
|
public boolean acquire(ConfigLimitDto routeConfig) {
|
||||||
if (routeConfig.getLimitStatus() == RouteConfig.LIMIT_STATUS_CLOSE) {
|
if (routeConfig.getLimitStatus() == ConfigLimitDto.LIMIT_STATUS_CLOSE) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
if (LimitType.TOKEN_BUCKET.getType() == routeConfig.getLimitType().byteValue()) {
|
if (LimitType.TOKEN_BUCKET.getType() == routeConfig.getLimitType().byteValue()) {
|
||||||
|
@@ -1,7 +1,6 @@
|
|||||||
package com.gitee.sop.gatewaycommon.manager;
|
package com.gitee.sop.gatewaycommon.manager;
|
||||||
|
|
||||||
import com.gitee.sop.gatewaycommon.bean.RouteConfig;
|
import com.gitee.sop.gatewaycommon.bean.RouteConfig;
|
||||||
import com.gitee.sop.gatewaycommon.bean.RouteConfigDto;
|
|
||||||
import com.gitee.sop.gatewaycommon.util.MyBeanUtil;
|
import com.gitee.sop.gatewaycommon.util.MyBeanUtil;
|
||||||
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
@@ -28,8 +27,8 @@ public class DefaultRouteConfigManager implements RouteConfigManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void update(RouteConfigDto routeConfigDto) {
|
public void update(RouteConfig routeConfig) {
|
||||||
this.doUpdate(routeConfigDto.getRouteId(), routeConfigDto);
|
this.doUpdate(routeConfig.getRouteId(), routeConfig);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void doUpdate(String routeId, Object res) {
|
protected void doUpdate(String routeId, Object res) {
|
||||||
@@ -40,7 +39,6 @@ public class DefaultRouteConfigManager implements RouteConfigManager {
|
|||||||
routeConfigMap.put(routeId, routeConfig);
|
routeConfigMap.put(routeId, routeConfig);
|
||||||
}
|
}
|
||||||
MyBeanUtil.copyPropertiesIgnoreNull(res, routeConfig);
|
MyBeanUtil.copyPropertiesIgnoreNull(res, routeConfig);
|
||||||
routeConfig.initRateLimiter();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected RouteConfig newRouteConfig() {
|
protected RouteConfig newRouteConfig() {
|
||||||
|
@@ -2,7 +2,6 @@ package com.gitee.sop.gatewaycommon.manager;
|
|||||||
|
|
||||||
import com.gitee.sop.gatewaycommon.bean.BeanInitializer;
|
import com.gitee.sop.gatewaycommon.bean.BeanInitializer;
|
||||||
import com.gitee.sop.gatewaycommon.bean.RouteConfig;
|
import com.gitee.sop.gatewaycommon.bean.RouteConfig;
|
||||||
import com.gitee.sop.gatewaycommon.bean.RouteConfigDto;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 路由配置管理
|
* 路由配置管理
|
||||||
@@ -11,9 +10,9 @@ import com.gitee.sop.gatewaycommon.bean.RouteConfigDto;
|
|||||||
public interface RouteConfigManager extends BeanInitializer {
|
public interface RouteConfigManager extends BeanInitializer {
|
||||||
/**
|
/**
|
||||||
* 更新路由配置
|
* 更新路由配置
|
||||||
* @param routeConfigDto 路由配置
|
* @param routeConfig 路由配置
|
||||||
*/
|
*/
|
||||||
void update(RouteConfigDto routeConfigDto);
|
void update(RouteConfig routeConfig);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取路由配置
|
* 获取路由配置
|
||||||
|
@@ -2,7 +2,6 @@ package com.gitee.sop.gatewaycommon.zuul.filter;
|
|||||||
|
|
||||||
import com.gitee.sop.gatewaycommon.bean.ApiConfig;
|
import com.gitee.sop.gatewaycommon.bean.ApiConfig;
|
||||||
import com.gitee.sop.gatewaycommon.bean.ConfigLimitDto;
|
import com.gitee.sop.gatewaycommon.bean.ConfigLimitDto;
|
||||||
import com.gitee.sop.gatewaycommon.bean.RouteConfig;
|
|
||||||
import com.gitee.sop.gatewaycommon.exception.ApiException;
|
import com.gitee.sop.gatewaycommon.exception.ApiException;
|
||||||
import com.gitee.sop.gatewaycommon.limit.LimitManager;
|
import com.gitee.sop.gatewaycommon.limit.LimitManager;
|
||||||
import com.gitee.sop.gatewaycommon.limit.LimitType;
|
import com.gitee.sop.gatewaycommon.limit.LimitType;
|
||||||
@@ -26,8 +25,6 @@ import java.util.List;
|
|||||||
*/
|
*/
|
||||||
public class PreLimitFilter extends BaseZuulFilter {
|
public class PreLimitFilter extends BaseZuulFilter {
|
||||||
|
|
||||||
public static final int LIMIT_STATUS_OPEN = 1;
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected FilterType getFilterType() {
|
protected FilterType getFilterType() {
|
||||||
return FilterType.PRE;
|
return FilterType.PRE;
|
||||||
@@ -52,7 +49,7 @@ public class PreLimitFilter extends BaseZuulFilter {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
// 单个限流功能未开启
|
// 单个限流功能未开启
|
||||||
if (configLimitDto.getLimitStatus() == RouteConfig.LIMIT_STATUS_CLOSE) {
|
if (configLimitDto.getLimitStatus() == ConfigLimitDto.LIMIT_STATUS_CLOSE) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
byte limitType = configLimitDto.getLimitType().byteValue();
|
byte limitType = configLimitDto.getLimitType().byteValue();
|
||||||
@@ -94,7 +91,7 @@ public class PreLimitFilter extends BaseZuulFilter {
|
|||||||
if (configLimitDto == null) {
|
if (configLimitDto == null) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (configLimitDto.getLimitStatus().intValue() == LIMIT_STATUS_OPEN) {
|
if (configLimitDto.getLimitStatus().intValue() == ConfigLimitDto.LIMIT_STATUS_OPEN) {
|
||||||
limitConfigList.add(configLimitDto);
|
limitConfigList.add(configLimitDto);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -7,7 +7,6 @@ import com.gitee.sop.gateway.mapper.ConfigRouteLimitMapper;
|
|||||||
import com.gitee.sop.gatewaycommon.bean.BaseRouteDefinition;
|
import com.gitee.sop.gatewaycommon.bean.BaseRouteDefinition;
|
||||||
import com.gitee.sop.gatewaycommon.bean.ChannelMsg;
|
import com.gitee.sop.gatewaycommon.bean.ChannelMsg;
|
||||||
import com.gitee.sop.gatewaycommon.bean.RouteConfig;
|
import com.gitee.sop.gatewaycommon.bean.RouteConfig;
|
||||||
import com.gitee.sop.gatewaycommon.bean.RouteConfigDto;
|
|
||||||
import com.gitee.sop.gatewaycommon.bean.TargetRoute;
|
import com.gitee.sop.gatewaycommon.bean.TargetRoute;
|
||||||
import com.gitee.sop.gatewaycommon.manager.DefaultRouteConfigManager;
|
import com.gitee.sop.gatewaycommon.manager.DefaultRouteConfigManager;
|
||||||
import com.gitee.sop.gatewaycommon.manager.RouteRepositoryContext;
|
import com.gitee.sop.gatewaycommon.manager.RouteRepositoryContext;
|
||||||
@@ -39,21 +38,12 @@ public class DbRouteConfigManager extends DefaultRouteConfigManager {
|
|||||||
loadAllRoute();
|
loadAllRoute();
|
||||||
|
|
||||||
Query query = new Query();
|
Query query = new Query();
|
||||||
|
|
||||||
configRouteBaseMapper.list(query)
|
configRouteBaseMapper.list(query)
|
||||||
.stream()
|
.stream()
|
||||||
.forEach(configRouteBase -> {
|
.forEach(configRouteBase -> {
|
||||||
String key = configRouteBase.getRouteId();
|
String key = configRouteBase.getRouteId();
|
||||||
putVal(key, configRouteBase);
|
putVal(key, configRouteBase);
|
||||||
});
|
});
|
||||||
|
|
||||||
configRouteLimitMapper.list(query)
|
|
||||||
.stream()
|
|
||||||
.forEach(configRouteLimit -> {
|
|
||||||
String key = configRouteLimit.getRouteId();
|
|
||||||
putVal(key, configRouteLimit);
|
|
||||||
});
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void loadAllRoute() {
|
protected void loadAllRoute() {
|
||||||
@@ -84,15 +74,15 @@ public class DbRouteConfigManager extends DefaultRouteConfigManager {
|
|||||||
ZookeeperContext.listenPath(path, nodeCache -> {
|
ZookeeperContext.listenPath(path, nodeCache -> {
|
||||||
String nodeData = new String(nodeCache.getCurrentData().getData());
|
String nodeData = new String(nodeCache.getCurrentData().getData());
|
||||||
ChannelMsg channelMsg = JSON.parseObject(nodeData, ChannelMsg.class);
|
ChannelMsg channelMsg = JSON.parseObject(nodeData, ChannelMsg.class);
|
||||||
final RouteConfigDto routeConfigDto = JSON.parseObject(channelMsg.getData(), RouteConfigDto.class);
|
final RouteConfig routeConfig = JSON.parseObject(channelMsg.getData(), RouteConfig.class);
|
||||||
switch (channelMsg.getOperation()) {
|
switch (channelMsg.getOperation()) {
|
||||||
case "reload":
|
case "reload":
|
||||||
log.info("重新加载路由配置信息,routeConfigDto:{}", routeConfigDto);
|
log.info("重新加载路由配置信息,routeConfigDto:{}", routeConfig);
|
||||||
load();
|
load();
|
||||||
break;
|
break;
|
||||||
case "update":
|
case "update":
|
||||||
log.info("更新路由配置信息,routeConfigDto:{}", routeConfigDto);
|
log.info("更新路由配置信息,routeConfigDto:{}", routeConfig);
|
||||||
update(routeConfigDto);
|
update(routeConfig);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
Reference in New Issue
Block a user