mirror of
https://gitee.com/durcframework/SOP.git
synced 2025-08-11 21:57:56 +08:00
3.2.0
This commit is contained in:
@@ -1,9 +1,12 @@
|
|||||||
package com.gitee.sop.adminserver.service;
|
package com.gitee.sop.adminserver.service;
|
||||||
|
|
||||||
|
import com.gitee.fastmybatis.core.query.Query;
|
||||||
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.ServiceInstanceVO;
|
import com.gitee.sop.adminserver.api.service.result.ServiceInstanceVO;
|
||||||
import com.gitee.sop.adminserver.bean.ServiceInfo;
|
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.entity.ConfigGrayInstance;
|
||||||
|
import com.gitee.sop.adminserver.mapper.ConfigGrayInstanceMapper;
|
||||||
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.BeanUtils;
|
||||||
@@ -12,8 +15,12 @@ import org.springframework.stereotype.Service;
|
|||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
import java.util.concurrent.atomic.AtomicInteger;
|
import java.util.concurrent.atomic.AtomicInteger;
|
||||||
|
import java.util.function.Function;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author tanghc
|
* @author tanghc
|
||||||
@@ -22,9 +29,13 @@ import java.util.concurrent.atomic.AtomicInteger;
|
|||||||
@Service
|
@Service
|
||||||
public class ServerService {
|
public class ServerService {
|
||||||
|
|
||||||
|
public static final int GRAY_OPEN = 1;
|
||||||
@Autowired
|
@Autowired
|
||||||
private RegistryService registryService;
|
private RegistryService registryService;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private ConfigGrayInstanceMapper grayInstanceMapper;
|
||||||
|
|
||||||
public List<ServiceInstanceVO> listService(ServiceSearchParam param) {
|
public List<ServiceInstanceVO> listService(ServiceSearchParam param) {
|
||||||
List<ServiceInfo> serviceInfos;
|
List<ServiceInfo> serviceInfos;
|
||||||
try {
|
try {
|
||||||
@@ -33,6 +44,10 @@ public class ServerService {
|
|||||||
log.error("获取服务实例失败", e);
|
log.error("获取服务实例失败", e);
|
||||||
return Collections.emptyList();
|
return Collections.emptyList();
|
||||||
}
|
}
|
||||||
|
List<ConfigGrayInstance> configGrayInstances = grayInstanceMapper.list(new Query());
|
||||||
|
// key: instanceId
|
||||||
|
Map<String, ConfigGrayInstance> grayInstanceMap = configGrayInstances.stream()
|
||||||
|
.collect(Collectors.toMap(ConfigGrayInstance::getInstanceId, Function.identity()));
|
||||||
List<ServiceInstanceVO> serviceInfoVoList = new ArrayList<>();
|
List<ServiceInstanceVO> serviceInfoVoList = new ArrayList<>();
|
||||||
AtomicInteger idGen = new AtomicInteger(1);
|
AtomicInteger idGen = new AtomicInteger(1);
|
||||||
serviceInfos.stream()
|
serviceInfos.stream()
|
||||||
@@ -58,12 +73,22 @@ public class ServerService {
|
|||||||
instanceVO.setId(id);
|
instanceVO.setId(id);
|
||||||
instanceVO.setParentId(pid);
|
instanceVO.setParentId(pid);
|
||||||
if (instanceVO.getMetadata() == null) {
|
if (instanceVO.getMetadata() == null) {
|
||||||
instanceVO.setMetadata(Collections.emptyMap());
|
instanceVO.setMetadata(new HashMap<>(8));
|
||||||
}
|
}
|
||||||
|
bindGrayEnv(instanceVO, grayInstanceMap);
|
||||||
serviceInfoVoList.add(instanceVO);
|
serviceInfoVoList.add(instanceVO);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
return serviceInfoVoList;
|
return serviceInfoVoList;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void bindGrayEnv(ServiceInstanceVO instanceVO, Map<String, ConfigGrayInstance> grayInstanceMap) {
|
||||||
|
String instanceId = instanceVO.getInstanceId();
|
||||||
|
ConfigGrayInstance configGrayInstance = grayInstanceMap.get(instanceId);
|
||||||
|
if (configGrayInstance != null && configGrayInstance.getStatus() == GRAY_OPEN) {
|
||||||
|
Map<String, String> metadata = instanceVO.getMetadata();
|
||||||
|
metadata.put("env", "gray");
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@@ -33,6 +33,9 @@ public class NacosRegistryListener extends BaseRegistryListener {
|
|||||||
@Autowired
|
@Autowired
|
||||||
private NacosDiscoveryProperties nacosDiscoveryProperties;
|
private NacosDiscoveryProperties nacosDiscoveryProperties;
|
||||||
|
|
||||||
|
@Autowired(required = false)
|
||||||
|
private List<RegistryEvent> registryEventList;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public synchronized void onEvent(ApplicationEvent applicationEvent) {
|
public synchronized void onEvent(ApplicationEvent applicationEvent) {
|
||||||
NamingService namingService = nacosDiscoveryProperties.namingServiceInstance();
|
NamingService namingService = nacosDiscoveryProperties.namingServiceInstance();
|
||||||
@@ -86,13 +89,21 @@ public class NacosRegistryListener extends BaseRegistryListener {
|
|||||||
instanceDefinition.setPort(instance.getPort());
|
instanceDefinition.setPort(instance.getPort());
|
||||||
instanceDefinition.setMetadata(instance.getMetadata());
|
instanceDefinition.setMetadata(instance.getMetadata());
|
||||||
pullRoutes(instanceDefinition);
|
pullRoutes(instanceDefinition);
|
||||||
|
if (registryEventList != null) {
|
||||||
|
registryEventList.forEach(registryEvent -> registryEvent.onRegistry(instanceDefinition));
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// 如果有服务删除
|
// 如果有服务删除
|
||||||
Set<String> removedServiceIdList = getRemovedServiceId(serviceNames);
|
Set<String> removedServiceIdList = getRemovedServiceId(serviceNames);
|
||||||
if (removedServiceIdList.size() > 0) {
|
if (removedServiceIdList.size() > 0) {
|
||||||
removedServiceIdList.forEach(this::removeRoutes);
|
removedServiceIdList.forEach(serviceId->{
|
||||||
|
this.removeRoutes(serviceId);
|
||||||
|
if (registryEventList != null) {
|
||||||
|
registryEventList.forEach(registryEvent -> registryEvent.onRemove(serviceId));
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
cacheServices = new HashSet<>(serviceNames);
|
cacheServices = new HashSet<>(serviceNames);
|
||||||
@@ -111,7 +122,6 @@ public class NacosRegistryListener extends BaseRegistryListener {
|
|||||||
return cache;
|
return cache;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Data
|
@Data
|
||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
private static class InstanceInfo {
|
private static class InstanceInfo {
|
||||||
|
@@ -0,0 +1,23 @@
|
|||||||
|
package com.gitee.sop.gatewaycommon.route;
|
||||||
|
|
||||||
|
import com.gitee.sop.gatewaycommon.bean.InstanceDefinition;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 新的实例注册事件
|
||||||
|
*
|
||||||
|
* @author tanghc
|
||||||
|
*/
|
||||||
|
public interface RegistryEvent {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 新实例注册进来时触发
|
||||||
|
* @param instanceDefinition 实例信息
|
||||||
|
*/
|
||||||
|
void onRegistry(InstanceDefinition instanceDefinition);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 服务下线时触发
|
||||||
|
* @param serviceId 服务id
|
||||||
|
*/
|
||||||
|
void onRemove(String serviceId);
|
||||||
|
}
|
@@ -6,8 +6,10 @@ import com.gitee.sop.gateway.entity.ConfigGrayInstance;
|
|||||||
import com.gitee.sop.gateway.mapper.ConfigGrayInstanceMapper;
|
import com.gitee.sop.gateway.mapper.ConfigGrayInstanceMapper;
|
||||||
import com.gitee.sop.gateway.mapper.ConfigGrayMapper;
|
import com.gitee.sop.gateway.mapper.ConfigGrayMapper;
|
||||||
import com.gitee.sop.gatewaycommon.bean.ChannelMsg;
|
import com.gitee.sop.gatewaycommon.bean.ChannelMsg;
|
||||||
|
import com.gitee.sop.gatewaycommon.bean.InstanceDefinition;
|
||||||
import com.gitee.sop.gatewaycommon.bean.ServiceGrayDefinition;
|
import com.gitee.sop.gatewaycommon.bean.ServiceGrayDefinition;
|
||||||
import com.gitee.sop.gatewaycommon.manager.DefaultEnvGrayManager;
|
import com.gitee.sop.gatewaycommon.manager.DefaultEnvGrayManager;
|
||||||
|
import com.gitee.sop.gatewaycommon.route.RegistryEvent;
|
||||||
import com.gitee.sop.gatewaycommon.zuul.loadbalancer.ServiceGrayConfig;
|
import com.gitee.sop.gatewaycommon.zuul.loadbalancer.ServiceGrayConfig;
|
||||||
import com.google.common.collect.Sets;
|
import com.google.common.collect.Sets;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
@@ -30,7 +32,7 @@ import java.util.stream.Stream;
|
|||||||
*/
|
*/
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@Service
|
@Service
|
||||||
public class DbEnvGrayManager extends DefaultEnvGrayManager {
|
public class DbEnvGrayManager extends DefaultEnvGrayManager implements RegistryEvent {
|
||||||
|
|
||||||
private static final int STATUS_ENABLE = 1;
|
private static final int STATUS_ENABLE = 1;
|
||||||
|
|
||||||
@@ -44,19 +46,26 @@ public class DbEnvGrayManager extends DefaultEnvGrayManager {
|
|||||||
private ConfigGrayInstanceMapper configGrayInstanceMapper;
|
private ConfigGrayInstanceMapper configGrayInstanceMapper;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void load() {
|
public void onRegistry(InstanceDefinition instanceDefinition) {
|
||||||
|
String instanceId = instanceDefinition.getInstanceId();
|
||||||
|
ConfigGrayInstance grayInstance = configGrayInstanceMapper.getByColumn("instance_id", instanceId);
|
||||||
|
if (grayInstance != null && grayInstance.getStatus() == STATUS_ENABLE) {
|
||||||
|
log.info("实例[{}]开启灰度发布", grayInstance.getInstanceId());
|
||||||
|
this.openGray(grayInstance.getInstanceId(), grayInstance.getServiceId());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onRemove(String serviceId) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void load() {
|
||||||
List<ConfigGray> list = configGrayMapper.list(new Query());
|
List<ConfigGray> list = configGrayMapper.list(new Query());
|
||||||
for (ConfigGray configGray : list) {
|
for (ConfigGray configGray : list) {
|
||||||
this.setServiceGrayConfig(configGray);
|
this.setServiceGrayConfig(configGray);
|
||||||
}
|
}
|
||||||
|
|
||||||
Query query = new Query();
|
|
||||||
query.eq("status", STATUS_ENABLE);
|
|
||||||
List<ConfigGrayInstance> grayInstanceList = configGrayInstanceMapper.list(query);
|
|
||||||
for (ConfigGrayInstance configGrayInstance : grayInstanceList) {
|
|
||||||
this.openGray(configGrayInstance.getInstanceId(), configGrayInstance.getServiceId());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@@ -1,7 +1,6 @@
|
|||||||
package com.gitee.sop.test;
|
package com.gitee.sop.test;
|
||||||
|
|
||||||
import com.alibaba.fastjson.JSON;
|
import com.alibaba.fastjson.JSON;
|
||||||
import com.gitee.sop.test.alipay.AlipayApiException;
|
|
||||||
import com.gitee.sop.test.alipay.AlipaySignature;
|
import com.gitee.sop.test.alipay.AlipaySignature;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
@@ -37,6 +36,8 @@ public class AlipayClientPostTest extends TestBase {
|
|||||||
@Test
|
@Test
|
||||||
public void testGet() throws Exception {
|
public void testGet() throws Exception {
|
||||||
|
|
||||||
|
// 接口实现见:com.gitee.sop.storyweb.controller.AlipayController.getStory
|
||||||
|
|
||||||
// 公共请求参数
|
// 公共请求参数
|
||||||
Map<String, String> params = new HashMap<String, String>();
|
Map<String, String> params = new HashMap<String, String>();
|
||||||
params.put("app_id", appId);
|
params.put("app_id", appId);
|
||||||
@@ -71,123 +72,4 @@ public class AlipayClientPostTest extends TestBase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// 忽略验证,不校验签名,只需传接口名、版本号、业务参数
|
|
||||||
@Test
|
|
||||||
public void testIgnore() {
|
|
||||||
// 公共请求参数
|
|
||||||
Map<String, String> params = new HashMap<String, String>();
|
|
||||||
params.put("method", "story.get");
|
|
||||||
params.put("version", "2.1");
|
|
||||||
// 业务参数
|
|
||||||
Map<String, String> bizContent = new HashMap<>();
|
|
||||||
bizContent.put("id", "222");
|
|
||||||
bizContent.put("name", "忽略验证name");
|
|
||||||
|
|
||||||
params.put("biz_content", JSON.toJSONString(bizContent));
|
|
||||||
|
|
||||||
System.out.println("----------- 返回结果 -----------");
|
|
||||||
String responseData = post(url, params);// 发送请求
|
|
||||||
System.out.println(responseData);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testStoryget() throws AlipayApiException {
|
|
||||||
// 公共请求参数
|
|
||||||
Map<String, String> params = new HashMap<String, String>();
|
|
||||||
params.put("app_id", appId);
|
|
||||||
params.put("method", "story.get");
|
|
||||||
params.put("version", "2.2");
|
|
||||||
params.put("format", "json");
|
|
||||||
params.put("charset", "utf-8");
|
|
||||||
params.put("sign_type", "RSA2");
|
|
||||||
params.put("timestamp", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
|
|
||||||
// 业务参数
|
|
||||||
Map<String, String> bizContent = new HashMap<>();
|
|
||||||
bizContent.put("id", "222");
|
|
||||||
bizContent.put("name", "忽略验证22");
|
|
||||||
|
|
||||||
params.put("biz_content", JSON.toJSONString(bizContent));
|
|
||||||
|
|
||||||
System.out.println("----------- 请求信息 -----------");
|
|
||||||
System.out.println("请求参数:" + buildParamQuery(params));
|
|
||||||
System.out.println("商户秘钥:" + privateKey);
|
|
||||||
String content = AlipaySignature.getSignContent(params);
|
|
||||||
System.out.println("待签名内容:" + content);
|
|
||||||
String sign = AlipaySignature.rsa256Sign(content, privateKey, "utf-8");
|
|
||||||
System.out.println("签名(sign):" + sign);
|
|
||||||
|
|
||||||
params.put("sign", sign);
|
|
||||||
|
|
||||||
System.out.println("----------- 返回结果 -----------");
|
|
||||||
String responseData = post(url, params);// 发送请求
|
|
||||||
System.out.println(responseData);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testGetStory2() throws AlipayApiException {
|
|
||||||
// 公共请求参数
|
|
||||||
Map<String, String> params = new HashMap<String, String>();
|
|
||||||
params.put("app_id", appId);
|
|
||||||
params.put("method", "getStory2");
|
|
||||||
params.put("version", "1.0");
|
|
||||||
params.put("format", "json");
|
|
||||||
params.put("charset", "utf-8");
|
|
||||||
params.put("sign_type", "RSA2");
|
|
||||||
params.put("timestamp", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
|
|
||||||
// 业务参数
|
|
||||||
Map<String, String> bizContent = new HashMap<>();
|
|
||||||
bizContent.put("name", "Jim");
|
|
||||||
bizContent.put("age", "2");
|
|
||||||
|
|
||||||
params.put("biz_content", JSON.toJSONString(bizContent));
|
|
||||||
|
|
||||||
System.out.println("----------- 请求信息 -----------");
|
|
||||||
System.out.println("请求参数:" + buildParamQuery(params));
|
|
||||||
System.out.println("商户秘钥:" + privateKey);
|
|
||||||
String content = AlipaySignature.getSignContent(params);
|
|
||||||
System.out.println("待签名内容:" + content);
|
|
||||||
String sign = AlipaySignature.rsa256Sign(content, privateKey, "utf-8");
|
|
||||||
System.out.println("签名(sign):" + sign);
|
|
||||||
|
|
||||||
params.put("sign", sign);
|
|
||||||
|
|
||||||
System.out.println("----------- 返回结果 -----------");
|
|
||||||
String responseData = get(url, params);// 发送请求
|
|
||||||
System.out.println(responseData);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testGetJson() throws AlipayApiException {
|
|
||||||
// 公共请求参数
|
|
||||||
Map<String, String> params = new HashMap<String, String>();
|
|
||||||
params.put("app_id", appId);
|
|
||||||
params.put("method", "getJson");
|
|
||||||
params.put("version", "1.0");
|
|
||||||
params.put("format", "json");
|
|
||||||
params.put("charset", "utf-8");
|
|
||||||
params.put("sign_type", "RSA2");
|
|
||||||
params.put("timestamp", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
|
|
||||||
// 业务参数
|
|
||||||
Map<String, String> bizContent = new HashMap<>();
|
|
||||||
bizContent.put("name", "Jim");
|
|
||||||
bizContent.put("age", "2");
|
|
||||||
|
|
||||||
params.put("biz_content", JSON.toJSONString(bizContent));
|
|
||||||
|
|
||||||
System.out.println("----------- 请求信息 -----------");
|
|
||||||
System.out.println("请求参数:" + buildParamQuery(params));
|
|
||||||
System.out.println("商户秘钥:" + privateKey);
|
|
||||||
String content = AlipaySignature.getSignContent(params);
|
|
||||||
System.out.println("待签名内容:" + content);
|
|
||||||
String sign = AlipaySignature.rsa256Sign(content, privateKey, "utf-8");
|
|
||||||
System.out.println("签名(sign):" + sign);
|
|
||||||
|
|
||||||
params.put("sign", sign);
|
|
||||||
|
|
||||||
System.out.println("----------- 返回结果 -----------");
|
|
||||||
String responseData = get(url, params);// 发送请求
|
|
||||||
System.out.println(responseData);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user