代码优化

1.x
tanghc 5 years ago
parent 844470091e
commit 70a8aacd9b
  1. 21
      sop-admin/sop-admin-server/src/main/java/com/gitee/sop/adminserver/bean/RouteConfigDto.java
  2. 3
      sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/bean/ConfigLimitDto.java
  3. 71
      sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/bean/RouteConfig.java
  4. 37
      sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/bean/RouteConfigDto.java
  5. 3
      sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/filter/LimitFilter.java
  6. 5
      sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/limit/DefaultLimitManager.java
  7. 6
      sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/manager/DefaultRouteConfigManager.java
  8. 5
      sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/manager/RouteConfigManager.java
  9. 7
      sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/zuul/filter/PreLimitFilter.java
  10. 18
      sop-gateway/src/main/java/com/gitee/sop/gateway/manager/DbRouteConfigManager.java

@ -8,26 +8,11 @@ import lombok.Data;
@Data
public class RouteConfigDto {
/**
* 路由id
*/
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禁用
*/

@ -16,6 +16,9 @@ import java.util.concurrent.atomic.AtomicLong;
@Data
public class ConfigLimitDto {
public static final byte LIMIT_STATUS_OPEN = 1;
public static final byte LIMIT_STATUS_CLOSE = 0;
/** 数据库字段:id */
private Long id;

@ -1,16 +1,6 @@
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.Getter;
import lombok.Setter;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;
/**
* @author tanghc
@ -19,73 +9,20 @@ import java.util.concurrent.atomic.AtomicLong;
public class RouteConfig {
public static final byte STATUS_ENABLE = 1;
public static final byte LIMIT_STATUS_CLOSE = 0;
/**
* 路由id
*/
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禁用默认启用
*/
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启用
*/
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.ConfigLimitDto;
import com.gitee.sop.gatewaycommon.bean.RouteConfig;
import com.gitee.sop.gatewaycommon.exception.ApiException;
import com.gitee.sop.gatewaycommon.gateway.GatewayContext;
import com.gitee.sop.gatewaycommon.limit.LimitManager;
@ -41,7 +40,7 @@ public class LimitFilter implements GlobalFilter, Ordered {
return null;
}
// 单个限流功能未开启
if (configLimitDto.getLimitStatus() == RouteConfig.LIMIT_STATUS_CLOSE) {
if (configLimitDto.getLimitStatus() == ConfigLimitDto.LIMIT_STATUS_CLOSE) {
return null;
}
byte limitType = configLimitDto.getLimitType().byteValue();

@ -1,7 +1,6 @@
package com.gitee.sop.gatewaycommon.limit;
import com.gitee.sop.gatewaycommon.bean.ConfigLimitDto;
import com.gitee.sop.gatewaycommon.bean.RouteConfig;
import com.google.common.cache.LoadingCache;
import lombok.extern.slf4j.Slf4j;
@ -16,7 +15,7 @@ public class DefaultLimitManager implements LimitManager {
@Override
public double acquireToken(ConfigLimitDto routeConfig) {
if (routeConfig.getLimitStatus() == RouteConfig.LIMIT_STATUS_CLOSE) {
if (routeConfig.getLimitStatus() == ConfigLimitDto.LIMIT_STATUS_CLOSE) {
return 0;
}
if (LimitType.LEAKY_BUCKET.getType() == routeConfig.getLimitType().byteValue()) {
@ -28,7 +27,7 @@ public class DefaultLimitManager implements LimitManager {
@Override
public boolean acquire(ConfigLimitDto routeConfig) {
if (routeConfig.getLimitStatus() == RouteConfig.LIMIT_STATUS_CLOSE) {
if (routeConfig.getLimitStatus() == ConfigLimitDto.LIMIT_STATUS_CLOSE) {
return true;
}
if (LimitType.TOKEN_BUCKET.getType() == routeConfig.getLimitType().byteValue()) {

@ -1,7 +1,6 @@
package com.gitee.sop.gatewaycommon.manager;
import com.gitee.sop.gatewaycommon.bean.RouteConfig;
import com.gitee.sop.gatewaycommon.bean.RouteConfigDto;
import com.gitee.sop.gatewaycommon.util.MyBeanUtil;
import java.util.Map;
@ -28,8 +27,8 @@ public class DefaultRouteConfigManager implements RouteConfigManager {
}
@Override
public void update(RouteConfigDto routeConfigDto) {
this.doUpdate(routeConfigDto.getRouteId(), routeConfigDto);
public void update(RouteConfig routeConfig) {
this.doUpdate(routeConfig.getRouteId(), routeConfig);
}
protected void doUpdate(String routeId, Object res) {
@ -40,7 +39,6 @@ public class DefaultRouteConfigManager implements RouteConfigManager {
routeConfigMap.put(routeId, routeConfig);
}
MyBeanUtil.copyPropertiesIgnoreNull(res, routeConfig);
routeConfig.initRateLimiter();
}
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.RouteConfig;
import com.gitee.sop.gatewaycommon.bean.RouteConfigDto;
/**
* 路由配置管理
@ -11,9 +10,9 @@ import com.gitee.sop.gatewaycommon.bean.RouteConfigDto;
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.ConfigLimitDto;
import com.gitee.sop.gatewaycommon.bean.RouteConfig;
import com.gitee.sop.gatewaycommon.exception.ApiException;
import com.gitee.sop.gatewaycommon.limit.LimitManager;
import com.gitee.sop.gatewaycommon.limit.LimitType;
@ -26,8 +25,6 @@ import java.util.List;
*/
public class PreLimitFilter extends BaseZuulFilter {
public static final int LIMIT_STATUS_OPEN = 1;
@Override
protected FilterType getFilterType() {
return FilterType.PRE;
@ -52,7 +49,7 @@ public class PreLimitFilter extends BaseZuulFilter {
return null;
}
// 单个限流功能未开启
if (configLimitDto.getLimitStatus() == RouteConfig.LIMIT_STATUS_CLOSE) {
if (configLimitDto.getLimitStatus() == ConfigLimitDto.LIMIT_STATUS_CLOSE) {
return null;
}
byte limitType = configLimitDto.getLimitType().byteValue();
@ -94,7 +91,7 @@ public class PreLimitFilter extends BaseZuulFilter {
if (configLimitDto == null) {
continue;
}
if (configLimitDto.getLimitStatus().intValue() == LIMIT_STATUS_OPEN) {
if (configLimitDto.getLimitStatus().intValue() == ConfigLimitDto.LIMIT_STATUS_OPEN) {
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.ChannelMsg;
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.manager.DefaultRouteConfigManager;
import com.gitee.sop.gatewaycommon.manager.RouteRepositoryContext;
@ -39,21 +38,12 @@ public class DbRouteConfigManager extends DefaultRouteConfigManager {
loadAllRoute();
Query query = new Query();
configRouteBaseMapper.list(query)
.stream()
.forEach(configRouteBase -> {
String key = configRouteBase.getRouteId();
putVal(key, configRouteBase);
});
configRouteLimitMapper.list(query)
.stream()
.forEach(configRouteLimit -> {
String key = configRouteLimit.getRouteId();
putVal(key, configRouteLimit);
});
}
protected void loadAllRoute() {
@ -84,15 +74,15 @@ public class DbRouteConfigManager extends DefaultRouteConfigManager {
ZookeeperContext.listenPath(path, nodeCache -> {
String nodeData = new String(nodeCache.getCurrentData().getData());
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()) {
case "reload":
log.info("重新加载路由配置信息,routeConfigDto:{}", routeConfigDto);
log.info("重新加载路由配置信息,routeConfigDto:{}", routeConfig);
load();
break;
case "update":
log.info("更新路由配置信息,routeConfigDto:{}", routeConfigDto);
update(routeConfigDto);
log.info("更新路由配置信息,routeConfigDto:{}", routeConfig);
update(routeConfig);
break;
}
});

Loading…
Cancel
Save