diff --git a/changelog.md b/changelog.md index 8e03d15c..d7bfeb84 100644 --- a/changelog.md +++ b/changelog.md @@ -1,5 +1,9 @@ # changelog +## 2.2.0 + +- 支持eureka注册中心,需要执行`sop-2.2.0.sql`升级文件 + ## 2.1.3 - 优化文件上传校验 diff --git a/sop-2.2.0.sql b/sop-2.2.0.sql new file mode 100644 index 00000000..8a01be97 --- /dev/null +++ b/sop-2.2.0.sql @@ -0,0 +1,23 @@ +use sop; + +DROP TABLE IF EXISTS `config_service_route`; + +CREATE TABLE `config_service_route` ( + `id` varchar(128) NOT NULL DEFAULT '' COMMENT '路由id', + `service_id` varchar(128) NOT NULL DEFAULT '', + `name` varchar(128) NOT NULL DEFAULT '' COMMENT '接口名', + `version` varchar(64) NOT NULL DEFAULT '' COMMENT '版本号', + `predicates` varchar(256) DEFAULT NULL COMMENT '路由断言(SpringCloudGateway专用)', + `filters` varchar(256) DEFAULT NULL COMMENT '路由过滤器(SpringCloudGateway专用)', + `uri` varchar(128) NOT NULL DEFAULT '' COMMENT '路由规则转发的目标uri', + `path` varchar(128) NOT NULL DEFAULT '' COMMENT 'uri后面跟的path', + `order` int(11) NOT NULL DEFAULT '0' COMMENT '路由执行的顺序', + `ignore_validate` tinyint(4) NOT NULL DEFAULT '0' COMMENT '是否忽略验证,业务参数验证除外', + `status` tinyint(4) NOT NULL DEFAULT '1' COMMENT '状态,0:待审核,1:启用,2:禁用', + `merge_result` tinyint(4) NOT NULL DEFAULT '0' COMMENT '是否合并结果', + `permission` tinyint(4) NOT NULL DEFAULT '0' COMMENT '是否需要授权才能访问', + `gmt_create` datetime DEFAULT CURRENT_TIMESTAMP, + `gmt_modified` datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, + PRIMARY KEY (`id`), + KEY `idx_serviceid` (`service_id`) USING BTREE +) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='路由配置'; diff --git a/sop-admin/sop-admin-server/src/main/java/com/gitee/sop/adminserver/api/service/ServiceApi.java b/sop-admin/sop-admin-server/src/main/java/com/gitee/sop/adminserver/api/service/ServiceApi.java index 3ea9be79..fd99efe9 100644 --- a/sop-admin/sop-admin-server/src/main/java/com/gitee/sop/adminserver/api/service/ServiceApi.java +++ b/sop-admin/sop-admin-server/src/main/java/com/gitee/sop/adminserver/api/service/ServiceApi.java @@ -9,14 +9,12 @@ import com.gitee.sop.adminserver.api.service.param.ServiceGrayConfigParam; import com.gitee.sop.adminserver.api.service.param.ServiceIdParam; import com.gitee.sop.adminserver.api.service.param.ServiceInstanceParam; import com.gitee.sop.adminserver.api.service.param.ServiceSearchParam; -import com.gitee.sop.adminserver.api.service.result.RouteServiceInfo; import com.gitee.sop.adminserver.api.service.result.ServiceInfoVo; import com.gitee.sop.adminserver.api.service.result.ServiceInstanceVO; import com.gitee.sop.adminserver.bean.ChannelMsg; import com.gitee.sop.adminserver.bean.MetadataEnum; import com.gitee.sop.adminserver.bean.NacosConfigs; import com.gitee.sop.adminserver.bean.ServiceGrayDefinition; -import com.gitee.sop.adminserver.bean.ServiceInfo; import com.gitee.sop.adminserver.bean.ServiceInstance; import com.gitee.sop.adminserver.common.BizException; import com.gitee.sop.adminserver.common.ChannelOperation; @@ -28,17 +26,13 @@ import com.gitee.sop.adminserver.mapper.ConfigGrayMapper; import com.gitee.sop.adminserver.mapper.ConfigServiceRouteMapper; import com.gitee.sop.adminserver.service.ConfigPushService; import com.gitee.sop.adminserver.service.RegistryService; +import com.gitee.sop.adminserver.service.ServerService; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang.StringUtils; -import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.util.CollectionUtils; import java.io.IOException; -import java.util.ArrayList; -import java.util.Collections; import java.util.List; -import java.util.concurrent.atomic.AtomicInteger; import java.util.stream.Collectors; /** @@ -53,6 +47,9 @@ public class ServiceApi { @Autowired private RegistryService registryService; + @Autowired + private ServerService serverService; + @Autowired private ConfigGrayMapper configGrayMapper; @@ -96,45 +93,7 @@ public class ServiceApi { @Api(name = "service.instance.list") @ApiDocMethod(description = "获取注册中心的服务列表", elementClass = ServiceInfoVo.class) List listService(ServiceSearchParam param) { - List serviceInfos; - try { - serviceInfos = registryService.listAllService(1, Integer.MAX_VALUE); - } catch (Exception e) { - log.error("获取服务实例失败", e); - return Collections.emptyList(); - } - List serviceInfoVoList = new ArrayList<>(); - AtomicInteger idGen = new AtomicInteger(1); - serviceInfos.stream() - .filter(serviceInfo -> { - if (StringUtils.isBlank(param.getServiceId())) { - return true; - } - return StringUtils.containsIgnoreCase(serviceInfo.getServiceId(), param.getServiceId()); - }) - .forEach(serviceInfo -> { - int pid = idGen.getAndIncrement(); - String serviceId = serviceInfo.getServiceId(); - ServiceInstanceVO parent = new ServiceInstanceVO(); - parent.setId(pid); - parent.setServiceId(serviceId); - parent.setParentId(0); - serviceInfoVoList.add(parent); - List instanceList = serviceInfo.getInstances(); - for (ServiceInstance instance : instanceList) { - ServiceInstanceVO instanceVO = new ServiceInstanceVO(); - BeanUtils.copyProperties(instance, instanceVO); - int id = idGen.getAndIncrement(); - instanceVO.setId(id); - instanceVO.setParentId(pid); - if (instanceVO.getMetadata() == null) { - instanceVO.setMetadata(Collections.emptyMap()); - } - serviceInfoVoList.add(instanceVO); - } - }); - - return serviceInfoVoList; + return serverService.listService(param); } @Api(name = "service.instance.offline") diff --git a/sop-admin/sop-admin-server/src/main/java/com/gitee/sop/adminserver/service/ConfigPushService.java b/sop-admin/sop-admin-server/src/main/java/com/gitee/sop/adminserver/service/ConfigPushService.java index 1dc06106..c6d76fac 100644 --- a/sop-admin/sop-admin-server/src/main/java/com/gitee/sop/adminserver/service/ConfigPushService.java +++ b/sop-admin/sop-admin-server/src/main/java/com/gitee/sop/adminserver/service/ConfigPushService.java @@ -1,9 +1,8 @@ package com.gitee.sop.adminserver.service; import com.alibaba.fastjson.JSON; -import com.alibaba.nacos.api.annotation.NacosInjected; -import com.alibaba.nacos.api.config.ConfigService; -import com.alibaba.nacos.api.exception.NacosException; +import com.gitee.sop.adminserver.api.service.param.ServiceSearchParam; +import com.gitee.sop.adminserver.api.service.result.ServiceInstanceVO; import com.gitee.sop.adminserver.bean.ChannelMsg; import com.gitee.sop.adminserver.bean.GatewayPushDTO; import com.gitee.sop.adminserver.bean.HttpTool; @@ -11,13 +10,17 @@ import com.gitee.sop.adminserver.common.BizException; import lombok.extern.slf4j.Slf4j; import org.apache.commons.codec.digest.DigestUtils; import org.apache.commons.lang.StringUtils; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import java.io.IOException; -import java.util.Collections; +import java.util.Collection; import java.util.HashMap; +import java.util.List; import java.util.Map; +import java.util.stream.Collectors; +import java.util.stream.Stream; /** * @author tanghc @@ -27,11 +30,12 @@ import java.util.Map; public class ConfigPushService { private static final String GATEWAY_PUSH_URL = "http://%s/configChannelMsg"; + private static final String API_GATEWAY_SERVICE_ID = "api-gateway"; private static HttpTool httpTool = new HttpTool(); - @NacosInjected - private ConfigService configService; + @Autowired + private ServerService serverService; @Value("${gateway.host:}") private String gatewayHost; @@ -40,34 +44,38 @@ public class ConfigPushService { private String secret; public void publishConfig(String dataId, String groupId, ChannelMsg channelMsg) { - if (StringUtils.isNotBlank(gatewayHost)) { - String[] hosts = gatewayHost.split(","); - for (String host : hosts) { - GatewayPushDTO gatewayPushDTO = new GatewayPushDTO(dataId, groupId, channelMsg); - String url = String.format(GATEWAY_PUSH_URL, host); - try { - String requestBody = JSON.toJSONString(gatewayPushDTO); - Map header = new HashMap<>(8); - header.put("sign", buildRequestBodySign(requestBody, secret)); - String resp = httpTool.requestJson(url, requestBody, header); - if (!"ok".equals(resp)) { - throw new IOException(resp); - } - } catch (IOException e) { - log.error("nacos配置失败, dataId={}, groupId={}, operation={}, url={}", dataId, groupId, channelMsg.getOperation(), url, e); - throw new BizException("推送配置失败"); - } - } - } else { + GatewayPushDTO gatewayPushDTO = new GatewayPushDTO(dataId, groupId, channelMsg); + ServiceSearchParam serviceSearchParam = new ServiceSearchParam(); + serviceSearchParam.setServiceId(API_GATEWAY_SERVICE_ID); + List serviceInstanceList = serverService.listService(serviceSearchParam); + Collection hostList = serviceInstanceList + .stream() + .map(ServiceInstanceVO::getIpPort) + .collect(Collectors.toList()); + this.pushByHost(hostList, gatewayPushDTO); + } + + private void pushByHost(Collection hosts, GatewayPushDTO gatewayPushDTO) { + for (String host : hosts) { + String url = String.format(GATEWAY_PUSH_URL, host); try { - log.info("nacos配置, dataId={}, groupId={}, operation={}", dataId, groupId, channelMsg.getOperation()); - configService.publishConfig(dataId, groupId, JSON.toJSONString(channelMsg)); - } catch (NacosException e) { - log.error("nacos配置失败, dataId={}, groupId={}, operation={}", dataId, groupId, channelMsg.getOperation(), e); - throw new BizException("nacos配置失败"); + String requestBody = JSON.toJSONString(gatewayPushDTO); + Map header = new HashMap<>(8); + header.put("sign", buildRequestBodySign(requestBody, secret)); + String resp = httpTool.requestJson(url, requestBody, header); + if (!"ok".equals(resp)) { + throw new IOException(resp); + } + } catch (IOException e) { + log.error("nacos配置失败, dataId={}, groupId={}, operation={}, url={}", + gatewayPushDTO.getDataId() + , gatewayPushDTO.getGroupId() + , gatewayPushDTO.getChannelMsg().getOperation() + , url + , e); + throw new BizException("推送配置失败"); } } - } public static String buildRequestBodySign(String requestBody, String secret) { diff --git a/sop-admin/sop-admin-server/src/main/java/com/gitee/sop/adminserver/service/ServerService.java b/sop-admin/sop-admin-server/src/main/java/com/gitee/sop/adminserver/service/ServerService.java new file mode 100644 index 00000000..4949567e --- /dev/null +++ b/sop-admin/sop-admin-server/src/main/java/com/gitee/sop/adminserver/service/ServerService.java @@ -0,0 +1,69 @@ +package com.gitee.sop.adminserver.service; + +import com.gitee.sop.adminserver.api.service.param.ServiceSearchParam; +import com.gitee.sop.adminserver.api.service.result.ServiceInstanceVO; +import com.gitee.sop.adminserver.bean.ServiceInfo; +import com.gitee.sop.adminserver.bean.ServiceInstance; +import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang.StringUtils; +import org.springframework.beans.BeanUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.concurrent.atomic.AtomicInteger; + +/** + * @author tanghc + */ +@Slf4j +@Service +public class ServerService { + + @Autowired + private RegistryService registryService; + + public List listService(ServiceSearchParam param) { + List serviceInfos; + try { + serviceInfos = registryService.listAllService(1, Integer.MAX_VALUE); + } catch (Exception e) { + log.error("获取服务实例失败", e); + return Collections.emptyList(); + } + List serviceInfoVoList = new ArrayList<>(); + AtomicInteger idGen = new AtomicInteger(1); + serviceInfos.stream() + .filter(serviceInfo -> { + if (StringUtils.isBlank(param.getServiceId())) { + return true; + } + return StringUtils.containsIgnoreCase(serviceInfo.getServiceId(), param.getServiceId()); + }) + .forEach(serviceInfo -> { + int pid = idGen.getAndIncrement(); + String serviceId = serviceInfo.getServiceId(); + ServiceInstanceVO parent = new ServiceInstanceVO(); + parent.setId(pid); + parent.setServiceId(serviceId); + parent.setParentId(0); + serviceInfoVoList.add(parent); + List instanceList = serviceInfo.getInstances(); + for (ServiceInstance instance : instanceList) { + ServiceInstanceVO instanceVO = new ServiceInstanceVO(); + BeanUtils.copyProperties(instance, instanceVO); + int id = idGen.getAndIncrement(); + instanceVO.setId(id); + instanceVO.setParentId(pid); + if (instanceVO.getMetadata() == null) { + instanceVO.setMetadata(Collections.emptyMap()); + } + serviceInfoVoList.add(instanceVO); + } + }); + + return serviceInfoVoList; + } +} diff --git a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/manager/AbstractConfiguration.java b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/manager/AbstractConfiguration.java index e80ee1c2..af52ea31 100644 --- a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/manager/AbstractConfiguration.java +++ b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/manager/AbstractConfiguration.java @@ -66,12 +66,6 @@ public class AbstractConfiguration implements ApplicationContextAware { return new SopPropertiesFactory(); } - @Bean - @ConditionalOnClass(name = "com.alibaba.nacos.api.config.ConfigService") - public NacosEventProcessor nacosEventProcessor() { - return new NacosEventProcessor(); - } - /** * 微服务路由加载 */ diff --git a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/manager/NacosEventProcessor.java b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/manager/NacosEventProcessor.java index e8e392a1..949b3250 100644 --- a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/manager/NacosEventProcessor.java +++ b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/manager/NacosEventProcessor.java @@ -5,7 +5,6 @@ import com.alibaba.nacos.api.annotation.NacosInjected; import com.alibaba.nacos.api.config.ConfigService; import com.alibaba.nacos.api.config.listener.AbstractListener; import com.alibaba.nacos.api.exception.NacosException; -import com.gitee.sop.gatewaycommon.bean.ApiConfig; import com.gitee.sop.gatewaycommon.bean.ChannelMsg; import com.gitee.sop.gatewaycommon.bean.NacosConfigs; import com.gitee.sop.gatewaycommon.secret.IsvManager; @@ -15,9 +14,12 @@ import org.springframework.beans.factory.annotation.Autowired; import javax.annotation.PostConstruct; /** + * 用不到了 + * * @author tanghc */ @Slf4j +@Deprecated public class NacosEventProcessor { @NacosInjected @@ -56,8 +58,8 @@ public class NacosEventProcessor { configService.addListener(NacosConfigs.DATA_ID_GRAY, NacosConfigs.GROUP_CHANNEL, new AbstractListener() { @Override public void receiveConfigInfo(String configInfo) { - ChannelMsg channelMsg = JSON.parseObject(configInfo, ChannelMsg.class); - envGrayManager.process(channelMsg); + ChannelMsg channelMsg = JSON.parseObject(configInfo, ChannelMsg.class); + envGrayManager.process(channelMsg); } }); } @@ -66,8 +68,8 @@ public class NacosEventProcessor { 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); - ipBlacklistManager.process(channelMsg); + ChannelMsg channelMsg = JSON.parseObject(configInfo, ChannelMsg.class); + ipBlacklistManager.process(channelMsg); } }); } @@ -76,8 +78,8 @@ public class NacosEventProcessor { configService.addListener(NacosConfigs.DATA_ID_ISV, NacosConfigs.GROUP_CHANNEL, new AbstractListener() { @Override public void receiveConfigInfo(String configInfo) { - ChannelMsg channelMsg = JSON.parseObject(configInfo, ChannelMsg.class); - isvManager.process(channelMsg); + ChannelMsg channelMsg = JSON.parseObject(configInfo, ChannelMsg.class); + isvManager.process(channelMsg); } }); } @@ -86,8 +88,8 @@ public class NacosEventProcessor { 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); - isvRoutePermissionManager.process(channelMsg); + ChannelMsg channelMsg = JSON.parseObject(configInfo, ChannelMsg.class); + isvRoutePermissionManager.process(channelMsg); } }); } @@ -96,7 +98,7 @@ public class NacosEventProcessor { 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); + ChannelMsg channelMsg = JSON.parseObject(configInfo, ChannelMsg.class); limitConfigManager.process(channelMsg); } }); diff --git a/sop.sql b/sop.sql index 27ec44f8..b405893c 100644 --- a/sop.sql +++ b/sop.sql @@ -23,6 +23,7 @@ DROP TABLE IF EXISTS `config_gray_instance`; DROP TABLE IF EXISTS `config_gray`; DROP TABLE IF EXISTS `config_common`; DROP TABLE IF EXISTS `admin_user_info`; +DROP TABLE IF EXISTS `config_service_route`; CREATE TABLE `admin_user_info` ( @@ -211,7 +212,25 @@ CREATE TABLE `user_info` ( KEY `idx_unamepwd` (`username`,`password`) USING BTREE ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8 COMMENT='用户信息表'; - +CREATE TABLE `config_service_route` ( + `id` varchar(128) NOT NULL DEFAULT '' COMMENT '路由id', + `service_id` varchar(128) NOT NULL DEFAULT '', + `name` varchar(128) NOT NULL DEFAULT '' COMMENT '接口名', + `version` varchar(64) NOT NULL DEFAULT '' COMMENT '版本号', + `predicates` varchar(256) DEFAULT NULL COMMENT '路由断言(SpringCloudGateway专用)', + `filters` varchar(256) DEFAULT NULL COMMENT '路由过滤器(SpringCloudGateway专用)', + `uri` varchar(128) NOT NULL DEFAULT '' COMMENT '路由规则转发的目标uri', + `path` varchar(128) NOT NULL DEFAULT '' COMMENT 'uri后面跟的path', + `order` int(11) NOT NULL DEFAULT '0' COMMENT '路由执行的顺序', + `ignore_validate` tinyint(4) NOT NULL DEFAULT '0' COMMENT '是否忽略验证,业务参数验证除外', + `status` tinyint(4) NOT NULL DEFAULT '1' COMMENT '状态,0:待审核,1:启用,2:禁用', + `merge_result` tinyint(4) NOT NULL DEFAULT '0' COMMENT '是否合并结果', + `permission` tinyint(4) NOT NULL DEFAULT '0' COMMENT '是否需要授权才能访问', + `gmt_create` datetime DEFAULT CURRENT_TIMESTAMP, + `gmt_modified` datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, + PRIMARY KEY (`id`), + KEY `idx_serviceid` (`service_id`) USING BTREE +) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='路由配置'; SET FOREIGN_KEY_CHECKS = @PREVIOUS_FOREIGN_KEY_CHECKS;