This commit is contained in:
tanghc
2019-04-10 17:48:51 +08:00
parent 45ab847197
commit a3b87a5f5f
51 changed files with 1440 additions and 159 deletions

View File

@@ -0,0 +1,39 @@
package com.gitee.sop.gateway.entity;
import lombok.Data;
import javax.persistence.Column;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import java.util.Date;
/**
* 表名config_route_base
* 备注:路由配置表
*
* @author tanghc
*/
@Table(name = "config_route_base")
@Data
public class ConfigRouteBase {
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
/** 数据库字段id */
private Long id;
/** 路由id, 数据库字段route_id */
private String routeId;
/** 状态1启用2禁用, 数据库字段status */
private Byte status;
/** 数据库字段gmt_create */
private Date gmtCreate;
/** 数据库字段gmt_modified */
private Date gmtModified;
}

View File

@@ -0,0 +1,54 @@
package com.gitee.sop.gateway.entity;
import lombok.Data;
import javax.persistence.Column;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import java.util.Date;
/**
* 表名config_route_limit
* 备注:路由限流配置
*
* @author tanghc
*/
@Table(name = "config_route_limit")
@Data
public class ConfigRouteLimit {
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
/** 数据库字段id */
private Integer id;
/** 路由id, 数据库字段route_id */
private String routeId;
/** 限流策略1漏桶策略2令牌桶策略, 数据库字段type */
private Byte type;
/** 每秒可处理请求数, 数据库字段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;
/** 数据库字段gmt_create */
private Date gmtCreate;
/** 数据库字段gmt_modified */
private Date gmtModified;
}

View File

@@ -33,7 +33,6 @@ public class DbIsvManager extends CacheIsvManager {
@Override
public void load(Function<Object, String> secretGetter) {
log.info("从数据库读取ISV信息保存到本地");
List<IsvInfo> isvInfoList = isvInfoMapper.list(new Query());
isvInfoList.stream()
.forEach(isvInfo -> {

View File

@@ -0,0 +1,83 @@
package com.gitee.sop.gateway.manager;
import com.alibaba.fastjson.JSON;
import com.gitee.fastmybatis.core.query.Query;
import com.gitee.sop.gateway.mapper.ConfigRouteBaseMapper;
import com.gitee.sop.gateway.mapper.ConfigRouteLimitMapper;
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.manager.DefaultRouteConfigManager;
import com.gitee.sop.gatewaycommon.manager.ZookeeperContext;
import com.gitee.sop.gatewaycommon.util.MyBeanUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
/**
* @author tanghc
*/
@Component
@Slf4j
public class DbRouteConfigManager extends DefaultRouteConfigManager {
@Autowired
ConfigRouteBaseMapper configRouteBaseMapper;
@Autowired
ConfigRouteLimitMapper configRouteLimitMapper;
@Autowired
Environment environment;
@Override
public void load() {
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 putVal(String key, Object object) {
RouteConfig routeConfig = routeConfigMap.getOrDefault(key, newRouteConfig());
MyBeanUtil.copyPropertiesIgnoreNull(object, routeConfig);
routeConfigMap.put(key, routeConfig);
}
@PostConstruct
protected void after() throws Exception {
ZookeeperContext.setEnvironment(environment);
String path = ZookeeperContext.getRouteConfigChannelPath();
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);
switch (channelMsg.getOperation()) {
case "reload":
log.info("重新加载路由配置信息routeConfigDto:{}", routeConfigDto);
load();
break;
case "update":
log.info("更新路由配置信息routeConfigDto:{}", routeConfigDto);
update(routeConfigDto);
break;
}
});
}
}

View File

@@ -13,15 +13,19 @@ import org.springframework.stereotype.Component;
@Slf4j
public class ManagerInitializer {
@Autowired
private DbIsvManager dbIsvManager;
DbIsvManager dbIsvManager;
@Autowired
private DbIsvRoutePermissionManager dbIsvRoutePermissionManager;
DbIsvRoutePermissionManager dbIsvRoutePermissionManager;
@Autowired
DbRouteConfigManager dbRouteConfigManager;
public void init() {
ApiConfig apiConfig = ApiConfig.getInstance();
apiConfig.setIsvManager(dbIsvManager);
apiConfig.setIsvRoutePermissionManager(dbIsvRoutePermissionManager);
apiConfig.setRouteConfigManager(dbRouteConfigManager);
// 从数据库加载isv信息
log.debug("从数据库加载isv信息");
@@ -32,5 +36,8 @@ public class ManagerInitializer {
log.debug("从数据库加载路由权限信息");
dbIsvRoutePermissionManager.load();
log.debug("从数据库加载路由配置信息");
dbRouteConfigManager.load();
}
}

View File

@@ -0,0 +1,11 @@
package com.gitee.sop.gateway.mapper;
import com.gitee.fastmybatis.core.mapper.CrudMapper;
import com.gitee.sop.gateway.entity.ConfigRouteBase;
/**
* @author tanghc
*/
public interface ConfigRouteBaseMapper extends CrudMapper<ConfigRouteBase, Long> {
}

View File

@@ -0,0 +1,11 @@
package com.gitee.sop.gateway.mapper;
import com.gitee.fastmybatis.core.mapper.CrudMapper;
import com.gitee.sop.gateway.entity.ConfigRouteLimit;
/**
* @author tanghc
*/
public interface ConfigRouteLimitMapper extends CrudMapper<ConfigRouteLimit, Integer> {
}