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-admin/sop-admin-server/src/main/resources/application-dev.properties b/sop-admin/sop-admin-server/src/main/resources/application-dev.properties index e33fd59c..89a60bf1 100644 --- a/sop-admin/sop-admin-server/src/main/resources/application-dev.properties +++ b/sop-admin/sop-admin-server/src/main/resources/application-dev.properties @@ -9,10 +9,6 @@ mysql.password=root # nacos注册中心地址 nacos.url=127.0.0.1:8848 - -# 网关地址,多个用逗号隔开 -# 在不使用nacos时有用,使用nacos时注释掉 -gateway.host=127.0.0.1:8081 # ------- 需要改的配置end ------- # eureka注册中心 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); } });