mirror of
https://gitee.com/durcframework/SOP.git
synced 2025-08-11 21:57:56 +08:00
Merge branch 'master' into registry-nacos
This commit is contained in:
@@ -1,31 +1,31 @@
|
||||
* [首页](/?t=1562728495209)
|
||||
* [首页](/?t=1562744547267)
|
||||
* 开发文档
|
||||
* [快速体验](files/10010_快速体验.md?t=1562728495211)
|
||||
* [项目接入到SOP](files/10011_项目接入到SOP.md?t=1562728495228)
|
||||
* [新增接口](files/10020_新增接口.md?t=1562728495229)
|
||||
* [业务参数校验](files/10030_业务参数校验.md?t=1562728495229)
|
||||
* [错误处理](files/10040_错误处理.md?t=1562728495229)
|
||||
* [编写文档](files/10041_编写文档.md?t=1562728495229)
|
||||
* [接口交互详解](files/10050_接口交互详解.md?t=1562728495229)
|
||||
* [easyopen支持](files/10070_easyopen支持.md?t=1562728495229)
|
||||
* [使用签名校验工具](files/10080_使用签名校验工具.md?t=1562728495229)
|
||||
* [ISV管理](files/10085_ISV管理.md?t=1562728495229)
|
||||
* [自定义路由](files/10086_自定义路由.md?t=1562728495229)
|
||||
* [路由授权](files/10090_路由授权.md?t=1562728495229)
|
||||
* [接口限流](files/10092_接口限流.md?t=1562728495229)
|
||||
* [监控日志](files/10093_监控日志.md?t=1562728495230)
|
||||
* [SDK开发](files/10095_SDK开发.md?t=1562728495230)
|
||||
* [使用SpringCloudGateway](files/10096_使用SpringCloudGateway.md?t=1562728495230)
|
||||
* [应用授权](files/10097_应用授权.md?t=1562728495230)
|
||||
* [更改数据节点名称](files/10099_更改数据节点名称.md?t=1562728495230)
|
||||
* [对接前端](files/10100_对接前端.md?t=1562728495230)
|
||||
* [自定义过滤器](files/10102_自定义过滤器.md?t=1562728495230)
|
||||
* [文件上传](files/10104_文件上传.md?t=1562728495230)
|
||||
* [nacos注册中心](files/10106_nacos注册中心.md?t=1562728495230)
|
||||
* [扩展其它注册中心](files/10107_扩展其它注册中心.md?t=1562728495230)
|
||||
* [快速体验](files/10010_快速体验.md?t=1562744547269)
|
||||
* [项目接入到SOP](files/10011_项目接入到SOP.md?t=1562744547286)
|
||||
* [新增接口](files/10020_新增接口.md?t=1562744547286)
|
||||
* [业务参数校验](files/10030_业务参数校验.md?t=1562744547287)
|
||||
* [错误处理](files/10040_错误处理.md?t=1562744547287)
|
||||
* [编写文档](files/10041_编写文档.md?t=1562744547287)
|
||||
* [接口交互详解](files/10050_接口交互详解.md?t=1562744547287)
|
||||
* [easyopen支持](files/10070_easyopen支持.md?t=1562744547287)
|
||||
* [使用签名校验工具](files/10080_使用签名校验工具.md?t=1562744547287)
|
||||
* [ISV管理](files/10085_ISV管理.md?t=1562744547287)
|
||||
* [自定义路由](files/10086_自定义路由.md?t=1562744547287)
|
||||
* [路由授权](files/10090_路由授权.md?t=1562744547287)
|
||||
* [接口限流](files/10092_接口限流.md?t=1562744547288)
|
||||
* [监控日志](files/10093_监控日志.md?t=1562744547288)
|
||||
* [SDK开发](files/10095_SDK开发.md?t=1562744547288)
|
||||
* [使用SpringCloudGateway](files/10096_使用SpringCloudGateway.md?t=1562744547288)
|
||||
* [应用授权](files/10097_应用授权.md?t=1562744547288)
|
||||
* [更改数据节点名称](files/10099_更改数据节点名称.md?t=1562744547288)
|
||||
* [对接前端](files/10100_对接前端.md?t=1562744547288)
|
||||
* [自定义过滤器](files/10102_自定义过滤器.md?t=1562744547288)
|
||||
* [文件上传](files/10104_文件上传.md?t=1562744547289)
|
||||
* [nacos注册中心](files/10106_nacos注册中心.md?t=1562744547289)
|
||||
* [扩展其它注册中心](files/10107_扩展其它注册中心.md?t=1562744547289)
|
||||
* 原理分析
|
||||
* [原理分析之@ApiMapping](files/90010_原理分析之@ApiMapping.md?t=1562728495230)
|
||||
* [原理分析之路由存储](files/90011_原理分析之路由存储.md?t=1562728495231)
|
||||
* [原理分析之如何路由](files/90012_原理分析之如何路由.md?t=1562728495231)
|
||||
* [原理分析之文档归纳](files/90013_原理分析之文档归纳.md?t=1562728495231)
|
||||
* [常见问题](files/90100_常见问题.md?t=1562728495231)
|
||||
* [原理分析之@ApiMapping](files/90010_原理分析之@ApiMapping.md?t=1562744547289)
|
||||
* [原理分析之路由存储](files/90011_原理分析之路由存储.md?t=1562744547289)
|
||||
* [原理分析之如何路由](files/90012_原理分析之如何路由.md?t=1562744547289)
|
||||
* [原理分析之文档归纳](files/90013_原理分析之文档归纳.md?t=1562744547289)
|
||||
* [常见问题](files/90100_常见问题.md?t=1562744547289)
|
||||
|
@@ -47,3 +47,24 @@ eureka:
|
||||
```
|
||||
|
||||
参考:https://www.jianshu.com/p/5ad8317961b7
|
||||
|
||||
## 直接访问服务的swagger-ui.html ,提示access forbidden
|
||||
|
||||
找到微服务的`OpenServiceConfig.java`,重写内部类Swagger2中的swaggerAccessProtected()方法,返回false。线上请设置成true
|
||||
|
||||
```java
|
||||
// 开启文档
|
||||
@Configuration
|
||||
@EnableSwagger2
|
||||
public static class Swagger2 extends SwaggerSupport {
|
||||
@Override
|
||||
protected String getDocTitle() {
|
||||
return "故事API";
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean swaggerAccessProtected() {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
```
|
@@ -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;
|
||||
}
|
||||
});
|
||||
|
Reference in New Issue
Block a user