mirror of
https://gitee.com/durcframework/SOP.git
synced 2025-08-11 21:57:56 +08:00
1.3.0
This commit is contained in:
@@ -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;
|
||||
}
|
@@ -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;
|
||||
}
|
@@ -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 -> {
|
||||
|
@@ -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;
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
@@ -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();
|
||||
}
|
||||
}
|
||||
|
@@ -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> {
|
||||
}
|
@@ -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> {
|
||||
}
|
Reference in New Issue
Block a user