mirror of
https://gitee.com/durcframework/SOP.git
synced 2025-08-11 21:57:56 +08:00
2.0.0
This commit is contained in:
@@ -43,7 +43,6 @@ public class ZuulConfig extends AlipayZuulConfiguration {
|
||||
//@Configuration
|
||||
//public class ZuulConfig extends EasyopenZuulConfiguration {
|
||||
// static {
|
||||
// new ManagerInitializer();
|
||||
// Map<String, String> appSecretMap = new HashMap<>();
|
||||
// appSecretMap.put("easyopen_test", "G9w0BAQEFAAOCAQ8AMIIBCgKCA");
|
||||
// ApiConfig.getInstance().addAppSecret(appSecretMap);
|
||||
|
@@ -17,7 +17,7 @@ import java.io.IOException;
|
||||
@Controller
|
||||
public class RedirectController {
|
||||
|
||||
@Value("${zuul.servlet-path}")
|
||||
@Value("${zuul.servlet-path:/zuul}")
|
||||
private String path;
|
||||
|
||||
@RequestMapping("/{method}/{version}/")
|
||||
@@ -32,4 +32,12 @@ public class RedirectController {
|
||||
request.getRequestDispatcher(path).forward(request, response);
|
||||
}
|
||||
|
||||
@RequestMapping("/")
|
||||
public void index(
|
||||
HttpServletRequest request
|
||||
, HttpServletResponse response
|
||||
) throws ServletException, IOException {
|
||||
request.getRequestDispatcher(path).forward(request, response);
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -1,11 +1,9 @@
|
||||
package com.gitee.sop.gateway.loadbalancer;
|
||||
|
||||
import com.gitee.sop.gateway.manager.DbEnvGrayManager;
|
||||
import com.gitee.sop.gatewaycommon.bean.SpringContext;
|
||||
import com.gitee.sop.gatewaycommon.param.Param;
|
||||
import com.gitee.sop.gatewaycommon.zuul.loadbalancer.BaseServerChooser;
|
||||
import com.netflix.loadbalancer.Server;
|
||||
import com.netflix.niws.loadbalancer.DiscoveryEnabledServer;
|
||||
import org.springframework.cloud.alibaba.nacos.ribbon.NacosServer;
|
||||
import org.springframework.core.env.Environment;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
@@ -41,7 +39,7 @@ public class EnvironmentServerChooser extends BaseServerChooser {
|
||||
|
||||
private String getEnvValue(Server server) {
|
||||
// eureka存储的metadata
|
||||
Map<String, String> metadata = ((DiscoveryEnabledServer) server).getInstanceInfo().getMetadata();
|
||||
Map<String, String> metadata = ((NacosServer) server).getMetadata();
|
||||
return metadata.get(MEDATA_KEY_ENV);
|
||||
}
|
||||
|
||||
@@ -59,19 +57,4 @@ public class EnvironmentServerChooser extends BaseServerChooser {
|
||||
return domain.equals(serverName);
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否是灰度用户,可修改此方法实现自己想要的
|
||||
*
|
||||
* @param param 接口参数
|
||||
* @param userKeyManager userKey管理
|
||||
* @param server 服务器实例
|
||||
* @param request request
|
||||
* @return true:是
|
||||
*/
|
||||
protected boolean isGrayUser(Param param, DbEnvGrayManager userKeyManager, Server server, HttpServletRequest request) {
|
||||
String instanceId = server.getMetaInfo().getInstanceId();
|
||||
// 这里的灰度用户为appKey,包含此appKey则为灰度用户,允许访问
|
||||
String appKey = param.fetchAppKey();
|
||||
return userKeyManager.containsKey(instanceId, appKey);
|
||||
}
|
||||
}
|
||||
|
@@ -1,21 +1,23 @@
|
||||
package com.gitee.sop.gateway.manager;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.nacos.api.annotation.NacosInjected;
|
||||
import com.alibaba.nacos.api.config.ConfigService;
|
||||
import com.alibaba.nacos.api.config.listener.AbstractListener;
|
||||
import com.gitee.fastmybatis.core.query.Query;
|
||||
import com.gitee.sop.gateway.entity.ConfigGray;
|
||||
import com.gitee.sop.gateway.entity.ConfigGrayInstance;
|
||||
import com.gitee.sop.gateway.mapper.ConfigGrayInstanceMapper;
|
||||
import com.gitee.sop.gateway.mapper.ConfigGrayMapper;
|
||||
import com.gitee.sop.gatewaycommon.bean.ChannelMsg;
|
||||
import com.gitee.sop.gatewaycommon.bean.NacosConfigs;
|
||||
import com.gitee.sop.gatewaycommon.bean.ServiceGrayDefinition;
|
||||
import com.gitee.sop.gatewaycommon.manager.DefaultEnvGrayManager;
|
||||
import com.gitee.sop.gatewaycommon.manager.ZookeeperContext;
|
||||
import com.gitee.sop.gatewaycommon.zuul.loadbalancer.ServiceGrayConfig;
|
||||
import com.google.common.collect.Sets;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
@@ -41,15 +43,15 @@ public class DbEnvGrayManager extends DefaultEnvGrayManager {
|
||||
private static final Function<String[], String> FUNCTION_KEY = arr -> arr[0];
|
||||
private static final Function<String[], String> FUNCTION_VALUE = arr -> arr[1];
|
||||
|
||||
@Autowired
|
||||
private Environment environment;
|
||||
|
||||
@Autowired
|
||||
private ConfigGrayMapper configGrayMapper;
|
||||
|
||||
@Autowired
|
||||
private ConfigGrayInstanceMapper configGrayInstanceMapper;
|
||||
|
||||
@NacosInjected
|
||||
private ConfigService configService;
|
||||
|
||||
@Override
|
||||
public void load() {
|
||||
|
||||
@@ -98,27 +100,26 @@ public class DbEnvGrayManager extends DefaultEnvGrayManager {
|
||||
|
||||
@PostConstruct
|
||||
protected void after() throws Exception {
|
||||
ZookeeperContext.setEnvironment(environment);
|
||||
String isvChannelPath = ZookeeperContext.getServiceGrayChannelPath();
|
||||
ZookeeperContext.listenPath(isvChannelPath, nodeCache -> {
|
||||
String nodeData = new String(nodeCache.getCurrentData().getData());
|
||||
ChannelMsg channelMsg = JSON.parseObject(nodeData, ChannelMsg.class);
|
||||
String data = channelMsg.getData();
|
||||
ServiceGrayDefinition userKeyDefinition = JSON.parseObject(data, ServiceGrayDefinition.class);
|
||||
String serviceId = userKeyDefinition.getServiceId();
|
||||
switch (channelMsg.getOperation()) {
|
||||
case "set":
|
||||
ConfigGray configGray = configGrayMapper.getByColumn("service_id", serviceId);
|
||||
this.setServiceGrayConfig(configGray);
|
||||
break;
|
||||
case "open":
|
||||
openGray(userKeyDefinition.getInstanceId(), serviceId);
|
||||
break;
|
||||
case "close":
|
||||
closeGray(userKeyDefinition.getInstanceId());
|
||||
break;
|
||||
default:
|
||||
|
||||
configService.addListener(NacosConfigs.DATA_ID_GRAY, NacosConfigs.GROUP_CHANNEL, new AbstractListener() {
|
||||
@Override
|
||||
public void receiveConfigInfo(String configInfo) {
|
||||
ChannelMsg channelMsg = JSON.parseObject(configInfo, ChannelMsg.class);
|
||||
String data = channelMsg.getData();
|
||||
ServiceGrayDefinition userKeyDefinition = JSON.parseObject(data, ServiceGrayDefinition.class);
|
||||
String serviceId = userKeyDefinition.getServiceId();
|
||||
switch (channelMsg.getOperation()) {
|
||||
case "set":
|
||||
ConfigGray configGray = configGrayMapper.getByColumn("service_id", serviceId);
|
||||
setServiceGrayConfig(configGray);
|
||||
break;
|
||||
case "open":
|
||||
openGray(userKeyDefinition.getInstanceId(), serviceId);
|
||||
break;
|
||||
case "close":
|
||||
closeGray(userKeyDefinition.getInstanceId());
|
||||
break;
|
||||
default:
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
@@ -1,14 +1,16 @@
|
||||
package com.gitee.sop.gateway.manager;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.nacos.api.annotation.NacosInjected;
|
||||
import com.alibaba.nacos.api.config.ConfigService;
|
||||
import com.alibaba.nacos.api.config.listener.AbstractListener;
|
||||
import com.gitee.sop.gateway.mapper.IPBlacklistMapper;
|
||||
import com.gitee.sop.gatewaycommon.bean.ChannelMsg;
|
||||
import com.gitee.sop.gatewaycommon.bean.NacosConfigs;
|
||||
import com.gitee.sop.gatewaycommon.manager.DefaultIPBlacklistManager;
|
||||
import com.gitee.sop.gatewaycommon.manager.ZookeeperContext;
|
||||
import lombok.Data;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
@@ -24,10 +26,10 @@ import java.util.List;
|
||||
public class DbIPBlacklistManager extends DefaultIPBlacklistManager {
|
||||
|
||||
@Autowired
|
||||
IPBlacklistMapper ipBlacklistMapper;
|
||||
private IPBlacklistMapper ipBlacklistMapper;
|
||||
|
||||
@Autowired
|
||||
Environment environment;
|
||||
@NacosInjected
|
||||
private ConfigService configService;
|
||||
|
||||
@Override
|
||||
public void load() {
|
||||
@@ -39,23 +41,23 @@ public class DbIPBlacklistManager extends DefaultIPBlacklistManager {
|
||||
|
||||
@PostConstruct
|
||||
protected void after() throws Exception {
|
||||
ZookeeperContext.setEnvironment(environment);
|
||||
String path = ZookeeperContext.getIpBlacklistChannelPath();
|
||||
ZookeeperContext.listenPath(path, nodeCache -> {
|
||||
String nodeData = new String(nodeCache.getCurrentData().getData());
|
||||
ChannelMsg channelMsg = JSON.parseObject(nodeData, ChannelMsg.class);
|
||||
final IPDto ipDto = JSON.parseObject(channelMsg.getData(), IPDto.class);
|
||||
String ip = ipDto.getIp();
|
||||
switch (channelMsg.getOperation()) {
|
||||
case "add":
|
||||
log.info("添加IP黑名单,ip:{}", ip);
|
||||
add(ip);
|
||||
break;
|
||||
case "delete":
|
||||
log.info("移除IP黑名单,ip:{}", ip);
|
||||
remove(ip);
|
||||
break;
|
||||
default:
|
||||
configService.addListener(NacosConfigs.DATA_ID_IP_BLACKLIST, NacosConfigs.GROUP_CHANNEL, new AbstractListener() {
|
||||
@Override
|
||||
public void receiveConfigInfo(String configInfo) {
|
||||
ChannelMsg channelMsg = JSON.parseObject(configInfo, ChannelMsg.class);
|
||||
final IPDto ipDto = JSON.parseObject(channelMsg.getData(), IPDto.class);
|
||||
String ip = ipDto.getIp();
|
||||
switch (channelMsg.getOperation()) {
|
||||
case "add":
|
||||
log.info("添加IP黑名单,ip:{}", ip);
|
||||
add(ip);
|
||||
break;
|
||||
case "delete":
|
||||
log.info("移除IP黑名单,ip:{}", ip);
|
||||
remove(ip);
|
||||
break;
|
||||
default:
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
@@ -1,17 +1,19 @@
|
||||
package com.gitee.sop.gateway.manager;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.nacos.api.annotation.NacosInjected;
|
||||
import com.alibaba.nacos.api.config.ConfigService;
|
||||
import com.alibaba.nacos.api.config.listener.AbstractListener;
|
||||
import com.gitee.sop.gateway.entity.IsvDetailDTO;
|
||||
import com.gitee.sop.gateway.mapper.IsvInfoMapper;
|
||||
import com.gitee.sop.gatewaycommon.bean.ApiConfig;
|
||||
import com.gitee.sop.gatewaycommon.bean.ChannelMsg;
|
||||
import com.gitee.sop.gatewaycommon.bean.IsvDefinition;
|
||||
import com.gitee.sop.gatewaycommon.manager.ZookeeperContext;
|
||||
import com.gitee.sop.gatewaycommon.bean.NacosConfigs;
|
||||
import com.gitee.sop.gatewaycommon.secret.CacheIsvManager;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
@@ -25,11 +27,10 @@ import java.util.List;
|
||||
public class DbIsvManager extends CacheIsvManager {
|
||||
|
||||
@Autowired
|
||||
IsvInfoMapper isvInfoMapper;
|
||||
|
||||
@Autowired
|
||||
Environment environment;
|
||||
private IsvInfoMapper isvInfoMapper;
|
||||
|
||||
@NacosInjected
|
||||
private ConfigService configService;
|
||||
|
||||
@Override
|
||||
public void load() {
|
||||
@@ -45,23 +46,24 @@ public class DbIsvManager extends CacheIsvManager {
|
||||
@PostConstruct
|
||||
protected void after() throws Exception {
|
||||
ApiConfig.getInstance().setIsvManager(this);
|
||||
ZookeeperContext.setEnvironment(environment);
|
||||
String isvChannelPath = ZookeeperContext.getIsvInfoChannelPath();
|
||||
ZookeeperContext.listenPath(isvChannelPath, nodeCache -> {
|
||||
String nodeData = new String(nodeCache.getCurrentData().getData());
|
||||
ChannelMsg channelMsg = JSON.parseObject(nodeData, ChannelMsg.class);
|
||||
final IsvDefinition isvDefinition = JSON.parseObject(channelMsg.getData(), IsvDefinition.class);
|
||||
switch (channelMsg.getOperation()) {
|
||||
case "update":
|
||||
log.info("更新ISV信息,isvDefinition:{}", isvDefinition);
|
||||
update(isvDefinition);
|
||||
break;
|
||||
case "remove":
|
||||
log.info("删除ISV,isvDefinition:{}", isvDefinition);
|
||||
remove(isvDefinition.getAppKey());
|
||||
break;
|
||||
default:
|
||||
|
||||
configService.addListener(NacosConfigs.DATA_ID_ISV, NacosConfigs.GROUP_CHANNEL, new AbstractListener() {
|
||||
@Override
|
||||
public void receiveConfigInfo(String configInfo) {
|
||||
ChannelMsg channelMsg = JSON.parseObject(configInfo, ChannelMsg.class);
|
||||
final IsvDefinition isvDefinition = JSON.parseObject(channelMsg.getData(), IsvDefinition.class);
|
||||
switch (channelMsg.getOperation()) {
|
||||
case "update":
|
||||
log.info("更新ISV信息,isvDefinition:{}", isvDefinition);
|
||||
update(isvDefinition);
|
||||
break;
|
||||
case "remove":
|
||||
log.info("删除ISV,isvDefinition:{}", isvDefinition);
|
||||
remove(isvDefinition.getAppKey());
|
||||
break;
|
||||
default:
|
||||
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
@@ -1,6 +1,9 @@
|
||||
package com.gitee.sop.gateway.manager;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.nacos.api.annotation.NacosInjected;
|
||||
import com.alibaba.nacos.api.config.ConfigService;
|
||||
import com.alibaba.nacos.api.config.listener.AbstractListener;
|
||||
import com.gitee.fastmybatis.core.query.Query;
|
||||
import com.gitee.sop.gateway.entity.IsvInfo;
|
||||
import com.gitee.sop.gateway.entity.PermIsvRole;
|
||||
@@ -10,8 +13,8 @@ import com.gitee.sop.gateway.mapper.PermIsvRoleMapper;
|
||||
import com.gitee.sop.gateway.mapper.PermRolePermissionMapper;
|
||||
import com.gitee.sop.gatewaycommon.bean.ChannelMsg;
|
||||
import com.gitee.sop.gatewaycommon.bean.IsvRoutePermission;
|
||||
import com.gitee.sop.gatewaycommon.bean.NacosConfigs;
|
||||
import com.gitee.sop.gatewaycommon.manager.DefaultIsvRoutePermissionManager;
|
||||
import com.gitee.sop.gatewaycommon.manager.ZookeeperContext;
|
||||
import lombok.Data;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.codec.digest.DigestUtils;
|
||||
@@ -51,6 +54,9 @@ public class DbIsvRoutePermissionManager extends DefaultIsvRoutePermissionManage
|
||||
@Autowired
|
||||
IsvInfoMapper isvInfoMapper;
|
||||
|
||||
@NacosInjected
|
||||
private ConfigService configService;
|
||||
|
||||
@Override
|
||||
public void load() {
|
||||
// key: appKey, value: roleCodeList
|
||||
@@ -72,7 +78,10 @@ public class DbIsvRoutePermissionManager extends DefaultIsvRoutePermissionManage
|
||||
this.update(isvRoutePermission);
|
||||
}
|
||||
|
||||
// 获取ISV对应的角色
|
||||
/**
|
||||
* 获取ISV对应的角色
|
||||
* @return 返回ISV角色信息,key:appId,value:角色code列表
|
||||
*/
|
||||
public Map<String, List<String>> getIsvRoleCode() {
|
||||
Query query = new Query();
|
||||
List<PermIsvRole> permIsvRoles = permIsvRoleMapper.list(query);
|
||||
@@ -121,39 +130,32 @@ public class DbIsvRoutePermissionManager extends DefaultIsvRoutePermissionManage
|
||||
|
||||
@PostConstruct
|
||||
protected void after() throws Exception {
|
||||
ZookeeperContext.setEnvironment(environment);
|
||||
String isvChannelPath = ZookeeperContext.getIsvRoutePermissionChannelPath();
|
||||
ZookeeperContext.listenPath(isvChannelPath, nodeCache -> {
|
||||
String nodeData = new String(nodeCache.getCurrentData().getData());
|
||||
ChannelMsg channelMsg = JSON.parseObject(nodeData, ChannelMsg.class);
|
||||
final IsvRoutePermission isvRoutePermission = JSON.parseObject(channelMsg.getData(), IsvRoutePermission.class);
|
||||
switch (channelMsg.getOperation()) {
|
||||
case "reload":
|
||||
log.info("重新加载路由权限信息,isvRoutePermission:{}", isvRoutePermission);
|
||||
String listenPath = isvRoutePermission.getListenPath();
|
||||
String code = "";
|
||||
try {
|
||||
load();
|
||||
} catch (Exception e) {
|
||||
log.error("重新加载路由权限失败, channelMsg:{}", channelMsg, e);
|
||||
code = e.getMessage();
|
||||
}
|
||||
try {
|
||||
ZookeeperContext.updatePath(listenPath, code);
|
||||
} catch (Exception e1) {
|
||||
log.error("重新加载路由权限信息, zookeeper操作失败, path: {}", listenPath, e1);
|
||||
}
|
||||
break;
|
||||
case "update":
|
||||
log.info("更新ISV路由权限信息,isvRoutePermission:{}", isvRoutePermission);
|
||||
update(isvRoutePermission);
|
||||
break;
|
||||
case "remove":
|
||||
log.info("删除ISV路由权限信息,isvRoutePermission:{}", isvRoutePermission);
|
||||
remove(isvRoutePermission.getAppKey());
|
||||
break;
|
||||
default:
|
||||
configService.addListener(NacosConfigs.DATA_ID_ROUTE_PERMISSION, NacosConfigs.GROUP_CHANNEL, new AbstractListener() {
|
||||
@Override
|
||||
public void receiveConfigInfo(String configInfo) {
|
||||
ChannelMsg channelMsg = JSON.parseObject(configInfo, ChannelMsg.class);
|
||||
final IsvRoutePermission isvRoutePermission = JSON.parseObject(channelMsg.getData(), IsvRoutePermission.class);
|
||||
switch (channelMsg.getOperation()) {
|
||||
case "reload":
|
||||
log.info("重新加载路由权限信息,isvRoutePermission:{}", isvRoutePermission);
|
||||
String listenPath = isvRoutePermission.getListenPath();
|
||||
try {
|
||||
load();
|
||||
} catch (Exception e) {
|
||||
log.error("重新加载路由权限失败, channelMsg:{}", channelMsg, e);
|
||||
}
|
||||
break;
|
||||
case "update":
|
||||
log.info("更新ISV路由权限信息,isvRoutePermission:{}", isvRoutePermission);
|
||||
update(isvRoutePermission);
|
||||
break;
|
||||
case "remove":
|
||||
log.info("删除ISV路由权限信息,isvRoutePermission:{}", isvRoutePermission);
|
||||
remove(isvRoutePermission.getAppKey());
|
||||
break;
|
||||
default:
|
||||
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
@@ -1,12 +1,15 @@
|
||||
package com.gitee.sop.gateway.manager;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.nacos.api.annotation.NacosInjected;
|
||||
import com.alibaba.nacos.api.config.ConfigService;
|
||||
import com.alibaba.nacos.api.config.listener.AbstractListener;
|
||||
import com.gitee.fastmybatis.core.query.Query;
|
||||
import com.gitee.sop.gateway.mapper.ConfigLimitMapper;
|
||||
import com.gitee.sop.gatewaycommon.bean.ChannelMsg;
|
||||
import com.gitee.sop.gatewaycommon.bean.ConfigLimitDto;
|
||||
import com.gitee.sop.gatewaycommon.bean.NacosConfigs;
|
||||
import com.gitee.sop.gatewaycommon.manager.DefaultLimitConfigManager;
|
||||
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;
|
||||
@@ -29,6 +32,9 @@ public class DbLimitConfigManager extends DefaultLimitConfigManager {
|
||||
@Autowired
|
||||
Environment environment;
|
||||
|
||||
@NacosInjected
|
||||
private ConfigService configService;
|
||||
|
||||
@Override
|
||||
public void load() {
|
||||
Query query = new Query();
|
||||
@@ -47,22 +53,22 @@ public class DbLimitConfigManager extends DefaultLimitConfigManager {
|
||||
|
||||
@PostConstruct
|
||||
protected void after() throws Exception {
|
||||
ZookeeperContext.setEnvironment(environment);
|
||||
String path = ZookeeperContext.getLimitConfigChannelPath();
|
||||
ZookeeperContext.listenPath(path, nodeCache -> {
|
||||
String nodeData = new String(nodeCache.getCurrentData().getData());
|
||||
ChannelMsg channelMsg = JSON.parseObject(nodeData, ChannelMsg.class);
|
||||
final ConfigLimitDto configLimitDto = JSON.parseObject(channelMsg.getData(), ConfigLimitDto.class);
|
||||
switch (channelMsg.getOperation()) {
|
||||
case "reload":
|
||||
log.info("重新加载限流配置信息,configLimitDto:{}", configLimitDto);
|
||||
load();
|
||||
break;
|
||||
case "update":
|
||||
log.info("更新限流配置信息,configLimitDto:{}", configLimitDto);
|
||||
update(configLimitDto);
|
||||
break;
|
||||
default:
|
||||
configService.addListener(NacosConfigs.DATA_ID_LIMIT_CONFIG, NacosConfigs.GROUP_CHANNEL, new AbstractListener() {
|
||||
@Override
|
||||
public void receiveConfigInfo(String configInfo) {
|
||||
ChannelMsg channelMsg = JSON.parseObject(configInfo, ChannelMsg.class);
|
||||
final ConfigLimitDto configLimitDto = JSON.parseObject(channelMsg.getData(), ConfigLimitDto.class);
|
||||
switch (channelMsg.getOperation()) {
|
||||
case "reload":
|
||||
log.info("重新加载限流配置信息,configLimitDto:{}", configLimitDto);
|
||||
load();
|
||||
break;
|
||||
case "update":
|
||||
log.info("更新限流配置信息,configLimitDto:{}", configLimitDto);
|
||||
update(configLimitDto);
|
||||
break;
|
||||
default:
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
@@ -1,16 +1,19 @@
|
||||
package com.gitee.sop.gateway.manager;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.nacos.api.annotation.NacosInjected;
|
||||
import com.alibaba.nacos.api.config.ConfigService;
|
||||
import com.alibaba.nacos.api.config.listener.AbstractListener;
|
||||
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.BaseRouteDefinition;
|
||||
import com.gitee.sop.gatewaycommon.bean.ChannelMsg;
|
||||
import com.gitee.sop.gatewaycommon.bean.NacosConfigs;
|
||||
import com.gitee.sop.gatewaycommon.bean.RouteConfig;
|
||||
import com.gitee.sop.gatewaycommon.bean.RouteDefinition;
|
||||
import com.gitee.sop.gatewaycommon.bean.TargetRoute;
|
||||
import com.gitee.sop.gatewaycommon.manager.DefaultRouteConfigManager;
|
||||
import com.gitee.sop.gatewaycommon.manager.RouteRepositoryContext;
|
||||
import com.gitee.sop.gatewaycommon.manager.ZookeeperContext;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.core.env.Environment;
|
||||
@@ -35,13 +38,15 @@ public class DbRouteConfigManager extends DefaultRouteConfigManager {
|
||||
@Autowired
|
||||
Environment environment;
|
||||
|
||||
@NacosInjected
|
||||
private ConfigService configService;
|
||||
|
||||
@Override
|
||||
public void load() {
|
||||
loadAllRoute();
|
||||
|
||||
Query query = new Query();
|
||||
configRouteBaseMapper.list(query)
|
||||
.stream()
|
||||
.forEach(configRouteBase -> {
|
||||
String key = configRouteBase.getRouteId();
|
||||
putVal(key, configRouteBase);
|
||||
@@ -50,14 +55,13 @@ public class DbRouteConfigManager extends DefaultRouteConfigManager {
|
||||
|
||||
protected void loadAllRoute() {
|
||||
Collection<? extends TargetRoute> targetRoutes = RouteRepositoryContext.getRouteRepository().getAll();
|
||||
targetRoutes.stream()
|
||||
.forEach(targetRoute -> {
|
||||
BaseRouteDefinition routeDefinition = targetRoute.getRouteDefinition();
|
||||
targetRoutes.forEach(targetRoute -> {
|
||||
RouteDefinition routeDefinition = targetRoute.getRouteDefinition();
|
||||
initRouteConfig(routeDefinition);
|
||||
});
|
||||
}
|
||||
|
||||
protected void initRouteConfig(BaseRouteDefinition routeDefinition) {
|
||||
protected void initRouteConfig(RouteDefinition routeDefinition) {
|
||||
String routeId = routeDefinition.getId();
|
||||
RouteConfig routeConfig = newRouteConfig();
|
||||
routeConfig.setRouteId(routeId);
|
||||
@@ -71,22 +75,22 @@ public class DbRouteConfigManager extends DefaultRouteConfigManager {
|
||||
|
||||
@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 RouteConfig routeConfig = JSON.parseObject(channelMsg.getData(), RouteConfig.class);
|
||||
switch (channelMsg.getOperation()) {
|
||||
case "reload":
|
||||
log.info("重新加载路由配置信息,routeConfigDto:{}", routeConfig);
|
||||
load();
|
||||
break;
|
||||
case "update":
|
||||
log.info("更新路由配置信息,routeConfigDto:{}", routeConfig);
|
||||
update(routeConfig);
|
||||
break;
|
||||
default:
|
||||
configService.addListener(NacosConfigs.DATA_ID_ROUTE_CONFIG, NacosConfigs.GROUP_CHANNEL, new AbstractListener() {
|
||||
@Override
|
||||
public void receiveConfigInfo(String configInfo) {
|
||||
ChannelMsg channelMsg = JSON.parseObject(configInfo, ChannelMsg.class);
|
||||
final RouteConfig routeConfig = JSON.parseObject(channelMsg.getData(), RouteConfig.class);
|
||||
switch (channelMsg.getOperation()) {
|
||||
case "reload":
|
||||
log.info("重新加载路由配置信息,routeConfigDto:{}", routeConfig);
|
||||
load();
|
||||
break;
|
||||
case "update":
|
||||
log.info("更新路由配置信息,routeConfigDto:{}", routeConfig);
|
||||
update(routeConfig);
|
||||
break;
|
||||
default:
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
@@ -1,19 +0,0 @@
|
||||
package com.gitee.sop.gateway.manager;
|
||||
|
||||
import com.gitee.sop.gatewaycommon.bean.ApiConfig;
|
||||
|
||||
/**
|
||||
* @author tanghc
|
||||
*/
|
||||
public class ManagerInitializer {
|
||||
|
||||
public ManagerInitializer() {
|
||||
ApiConfig apiConfig = ApiConfig.getInstance();
|
||||
apiConfig.setIsvManager(new DbIsvManager());
|
||||
apiConfig.setIsvRoutePermissionManager(new DbIsvRoutePermissionManager());
|
||||
apiConfig.setRouteConfigManager(new DbRouteConfigManager());
|
||||
apiConfig.setLimitConfigManager(new DbLimitConfigManager());
|
||||
apiConfig.setIpBlacklistManager(new DbIPBlacklistManager());
|
||||
apiConfig.setUserKeyManager(new DbEnvGrayManager());
|
||||
}
|
||||
}
|
@@ -3,13 +3,13 @@ spring.application.name=api-gateway
|
||||
|
||||
# ------- 需要改的配置 -------
|
||||
# mysql数据库账号
|
||||
mysql.host=localhost:3306
|
||||
mysql.username=root
|
||||
mysql.password=root
|
||||
|
||||
# eureka注册中心地址
|
||||
eureka.url=http://localhost:1111/eureka/
|
||||
# zookeeper地址
|
||||
zookeeper.url=localhost:2181
|
||||
# nacos地址
|
||||
nacos.url=127.0.0.1:8848
|
||||
|
||||
# zipkin服务监控地址,没有开启不用改
|
||||
zipkin.url=http://127.0.0.1:9411/
|
||||
|
||||
@@ -35,18 +35,19 @@ ribbon.ReadTimeout=2000
|
||||
# 请谨慎设置,因为post请求大多都是写入请求,如果要支持重试,确保服务的幂等性
|
||||
ribbon.OkToRetryOnAllOperations=false
|
||||
|
||||
# eureka注册中心
|
||||
eureka.client.serviceUrl.defaultZone=${eureka.url}
|
||||
# nacos cloud配置
|
||||
spring.cloud.nacos.discovery.server-addr=${nacos.url}
|
||||
nacos.config.server-addr=${nacos.url}
|
||||
|
||||
# zookeeper配置
|
||||
spring.cloud.zookeeper.connect-string=${zookeeper.url}
|
||||
|
||||
# 数据库连接配置
|
||||
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
|
||||
spring.datasource.url=jdbc:mysql://localhost:3306/sop?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull
|
||||
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
|
||||
spring.datasource.url=jdbc:mysql://${mysql.host}/sop?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&serverTimezone=Asia/Shanghai
|
||||
spring.datasource.username=${mysql.username}
|
||||
spring.datasource.password=${mysql.password}
|
||||
|
||||
# 不用改
|
||||
mybatis.fill.com.gitee.fastmybatis.core.support.DateFillInsert=gmt_create
|
||||
mybatis.fill.com.gitee.fastmybatis.core.support.DateFillUpdate=gmt_modified
|
||||
|
||||
# 文件上传配置
|
||||
spring.servlet.multipart.enabled=true
|
||||
spring.servlet.multipart.max-file-size=20MB
|
||||
@@ -56,3 +57,5 @@ spring.servlet.multipart.max-request-size=20MB
|
||||
spring.zipkin.base-url=${zipkin.url}
|
||||
# 设置sleuth收集信息的比率,默认0.1,最大是1,数字越大越耗性能
|
||||
spring.sleuth.sampler.probability=1
|
||||
|
||||
logging.level.com.gitee=debug
|
@@ -1,52 +0,0 @@
|
||||
package com.gitee.sop.gateway;
|
||||
|
||||
import com.gitee.sop.gatewaycommon.bean.SopConstants;
|
||||
import junit.framework.TestCase;
|
||||
import org.apache.curator.framework.CuratorFramework;
|
||||
import org.apache.curator.framework.CuratorFrameworkFactory;
|
||||
import org.apache.curator.retry.ExponentialBackoffRetry;
|
||||
|
||||
/**
|
||||
* @author tanghc
|
||||
*/
|
||||
public class CuratorTest extends TestCase {
|
||||
|
||||
private String zookeeperServerAddr = "localhost:2181";
|
||||
|
||||
/**
|
||||
* 递归删除节点,只能在测试环境用。
|
||||
*
|
||||
* @throws Exception
|
||||
*/
|
||||
public void testDel() throws Exception {
|
||||
CuratorFramework client = CuratorFrameworkFactory.builder()
|
||||
.connectString(zookeeperServerAddr)
|
||||
.retryPolicy(new ExponentialBackoffRetry(1000, 3))
|
||||
.build();
|
||||
|
||||
client.start();
|
||||
|
||||
try {
|
||||
client.delete().deletingChildrenIfNeeded().forPath(SopConstants.SOP_SERVICE_ROUTE_PATH);
|
||||
} catch (Exception e) {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除临时节点
|
||||
*/
|
||||
public void testDelTemp() {
|
||||
String tempRoot = "/com.gitee.sop.service.tmp";
|
||||
CuratorFramework client = CuratorFrameworkFactory.builder()
|
||||
.connectString(zookeeperServerAddr)
|
||||
.retryPolicy(new ExponentialBackoffRetry(1000, 3))
|
||||
.build();
|
||||
|
||||
client.start();
|
||||
|
||||
try {
|
||||
client.delete().deletingChildrenIfNeeded().forPath(tempRoot);
|
||||
} catch (Exception e) {
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user