适配eureka

pull/1/head
tanghc 5 years ago
parent 77017cdd01
commit ebae593ab6
  1. 51
      sop-admin/sop-admin-server/src/main/java/com/gitee/sop/adminserver/api/service/ServiceApi.java
  2. 48
      sop-admin/sop-admin-server/src/main/java/com/gitee/sop/adminserver/service/ConfigPushService.java
  3. 69
      sop-admin/sop-admin-server/src/main/java/com/gitee/sop/adminserver/service/ServerService.java
  4. 4
      sop-admin/sop-admin-server/src/main/resources/application-dev.properties
  5. 6
      sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/manager/AbstractConfiguration.java
  6. 4
      sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/manager/NacosEventProcessor.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.ServiceIdParam;
import com.gitee.sop.adminserver.api.service.param.ServiceInstanceParam; 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.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.ServiceInfoVo;
import com.gitee.sop.adminserver.api.service.result.ServiceInstanceVO; import com.gitee.sop.adminserver.api.service.result.ServiceInstanceVO;
import com.gitee.sop.adminserver.bean.ChannelMsg; import com.gitee.sop.adminserver.bean.ChannelMsg;
import com.gitee.sop.adminserver.bean.MetadataEnum; import com.gitee.sop.adminserver.bean.MetadataEnum;
import com.gitee.sop.adminserver.bean.NacosConfigs; import com.gitee.sop.adminserver.bean.NacosConfigs;
import com.gitee.sop.adminserver.bean.ServiceGrayDefinition; 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.bean.ServiceInstance;
import com.gitee.sop.adminserver.common.BizException; import com.gitee.sop.adminserver.common.BizException;
import com.gitee.sop.adminserver.common.ChannelOperation; 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.mapper.ConfigServiceRouteMapper;
import com.gitee.sop.adminserver.service.ConfigPushService; import com.gitee.sop.adminserver.service.ConfigPushService;
import com.gitee.sop.adminserver.service.RegistryService; import com.gitee.sop.adminserver.service.RegistryService;
import com.gitee.sop.adminserver.service.ServerService;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors; import java.util.stream.Collectors;
/** /**
@ -53,6 +47,9 @@ public class ServiceApi {
@Autowired @Autowired
private RegistryService registryService; private RegistryService registryService;
@Autowired
private ServerService serverService;
@Autowired @Autowired
private ConfigGrayMapper configGrayMapper; private ConfigGrayMapper configGrayMapper;
@ -96,45 +93,7 @@ public class ServiceApi {
@Api(name = "service.instance.list") @Api(name = "service.instance.list")
@ApiDocMethod(description = "获取注册中心的服务列表", elementClass = ServiceInfoVo.class) @ApiDocMethod(description = "获取注册中心的服务列表", elementClass = ServiceInfoVo.class)
List<ServiceInstanceVO> listService(ServiceSearchParam param) { List<ServiceInstanceVO> listService(ServiceSearchParam param) {
List<ServiceInfo> serviceInfos; return serverService.listService(param);
try {
serviceInfos = registryService.listAllService(1, Integer.MAX_VALUE);
} catch (Exception e) {
log.error("获取服务实例失败", e);
return Collections.emptyList();
}
List<ServiceInstanceVO> 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<ServiceInstance> 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;
} }
@Api(name = "service.instance.offline") @Api(name = "service.instance.offline")

@ -1,9 +1,8 @@
package com.gitee.sop.adminserver.service; package com.gitee.sop.adminserver.service;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSON;
import com.alibaba.nacos.api.annotation.NacosInjected; import com.gitee.sop.adminserver.api.service.param.ServiceSearchParam;
import com.alibaba.nacos.api.config.ConfigService; import com.gitee.sop.adminserver.api.service.result.ServiceInstanceVO;
import com.alibaba.nacos.api.exception.NacosException;
import com.gitee.sop.adminserver.bean.ChannelMsg; import com.gitee.sop.adminserver.bean.ChannelMsg;
import com.gitee.sop.adminserver.bean.GatewayPushDTO; import com.gitee.sop.adminserver.bean.GatewayPushDTO;
import com.gitee.sop.adminserver.bean.HttpTool; import com.gitee.sop.adminserver.bean.HttpTool;
@ -11,13 +10,17 @@ import com.gitee.sop.adminserver.common.BizException;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils; import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.io.IOException; import java.io.IOException;
import java.util.Collections; import java.util.Collection;
import java.util.HashMap; import java.util.HashMap;
import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/** /**
* @author tanghc * @author tanghc
@ -27,11 +30,12 @@ import java.util.Map;
public class ConfigPushService { public class ConfigPushService {
private static final String GATEWAY_PUSH_URL = "http://%s/configChannelMsg"; 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(); private static HttpTool httpTool = new HttpTool();
@NacosInjected @Autowired
private ConfigService configService; private ServerService serverService;
@Value("${gateway.host:}") @Value("${gateway.host:}")
private String gatewayHost; private String gatewayHost;
@ -40,10 +44,19 @@ public class ConfigPushService {
private String secret; private String secret;
public void publishConfig(String dataId, String groupId, ChannelMsg channelMsg) { 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); GatewayPushDTO gatewayPushDTO = new GatewayPushDTO(dataId, groupId, channelMsg);
ServiceSearchParam serviceSearchParam = new ServiceSearchParam();
serviceSearchParam.setServiceId(API_GATEWAY_SERVICE_ID);
List<ServiceInstanceVO> serviceInstanceList = serverService.listService(serviceSearchParam);
Collection<String> hostList = serviceInstanceList
.stream()
.map(ServiceInstanceVO::getIpPort)
.collect(Collectors.toList());
this.pushByHost(hostList, gatewayPushDTO);
}
private void pushByHost(Collection<String> hosts, GatewayPushDTO gatewayPushDTO) {
for (String host : hosts) {
String url = String.format(GATEWAY_PUSH_URL, host); String url = String.format(GATEWAY_PUSH_URL, host);
try { try {
String requestBody = JSON.toJSONString(gatewayPushDTO); String requestBody = JSON.toJSONString(gatewayPushDTO);
@ -54,20 +67,15 @@ public class ConfigPushService {
throw new IOException(resp); throw new IOException(resp);
} }
} catch (IOException e) { } catch (IOException e) {
log.error("nacos配置失败, dataId={}, groupId={}, operation={}, url={}", dataId, groupId, channelMsg.getOperation(), url, e); log.error("nacos配置失败, dataId={}, groupId={}, operation={}, url={}",
gatewayPushDTO.getDataId()
, gatewayPushDTO.getGroupId()
, gatewayPushDTO.getChannelMsg().getOperation()
, url
, e);
throw new BizException("推送配置失败"); throw new BizException("推送配置失败");
} }
} }
} else {
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配置失败");
}
}
} }
public static String buildRequestBodySign(String requestBody, String secret) { public static String buildRequestBodySign(String requestBody, String secret) {

@ -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<ServiceInstanceVO> listService(ServiceSearchParam param) {
List<ServiceInfo> serviceInfos;
try {
serviceInfos = registryService.listAllService(1, Integer.MAX_VALUE);
} catch (Exception e) {
log.error("获取服务实例失败", e);
return Collections.emptyList();
}
List<ServiceInstanceVO> 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<ServiceInstance> 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;
}
}

@ -9,10 +9,6 @@ mysql.password=root
# nacos注册中心地址 # nacos注册中心地址
nacos.url=127.0.0.1:8848 nacos.url=127.0.0.1:8848
# 网关地址,多个用逗号隔开
# 在不使用nacos时有用,使用nacos时注释掉
gateway.host=127.0.0.1:8081
# ------- 需要改的配置end ------- # ------- 需要改的配置end -------
# eureka注册中心 # eureka注册中心

@ -66,12 +66,6 @@ public class AbstractConfiguration implements ApplicationContextAware {
return new SopPropertiesFactory(); return new SopPropertiesFactory();
} }
@Bean
@ConditionalOnClass(name = "com.alibaba.nacos.api.config.ConfigService")
public NacosEventProcessor nacosEventProcessor() {
return new NacosEventProcessor();
}
/** /**
* 微服务路由加载 * 微服务路由加载
*/ */

@ -5,7 +5,6 @@ import com.alibaba.nacos.api.annotation.NacosInjected;
import com.alibaba.nacos.api.config.ConfigService; import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.config.listener.AbstractListener; import com.alibaba.nacos.api.config.listener.AbstractListener;
import com.alibaba.nacos.api.exception.NacosException; 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.ChannelMsg;
import com.gitee.sop.gatewaycommon.bean.NacosConfigs; import com.gitee.sop.gatewaycommon.bean.NacosConfigs;
import com.gitee.sop.gatewaycommon.secret.IsvManager; import com.gitee.sop.gatewaycommon.secret.IsvManager;
@ -15,9 +14,12 @@ import org.springframework.beans.factory.annotation.Autowired;
import javax.annotation.PostConstruct; import javax.annotation.PostConstruct;
/** /**
* 用不到了
*
* @author tanghc * @author tanghc
*/ */
@Slf4j @Slf4j
@Deprecated
public class NacosEventProcessor { public class NacosEventProcessor {
@NacosInjected @NacosInjected

Loading…
Cancel
Save