|
|
@ -1,14 +1,14 @@ |
|
|
|
package com.gitee.sop.gateway.manager; |
|
|
|
package com.gitee.sop.gateway.manager; |
|
|
|
|
|
|
|
|
|
|
|
import com.alibaba.fastjson.JSON; |
|
|
|
import com.alibaba.fastjson.JSON; |
|
|
|
|
|
|
|
import com.gitee.fastmybatis.core.query.Query; |
|
|
|
import com.gitee.sop.gateway.entity.ConfigGrayUserkey; |
|
|
|
import com.gitee.sop.gateway.entity.ConfigGrayUserkey; |
|
|
|
import com.gitee.sop.gateway.loadbalancer.ServiceGrayConfig; |
|
|
|
|
|
|
|
import com.gitee.sop.gateway.mapper.ConfigGrayUserkeyMapper; |
|
|
|
import com.gitee.sop.gateway.mapper.ConfigGrayUserkeyMapper; |
|
|
|
import com.gitee.sop.gatewaycommon.bean.ChannelMsg; |
|
|
|
import com.gitee.sop.gatewaycommon.bean.ChannelMsg; |
|
|
|
import com.gitee.sop.gatewaycommon.bean.UserKeyDefinition; |
|
|
|
import com.gitee.sop.gatewaycommon.bean.UserKeyDefinition; |
|
|
|
|
|
|
|
import com.gitee.sop.gatewaycommon.manager.DefaultUserKeyManager; |
|
|
|
import com.gitee.sop.gatewaycommon.manager.ZookeeperContext; |
|
|
|
import com.gitee.sop.gatewaycommon.manager.ZookeeperContext; |
|
|
|
import com.google.common.collect.Maps; |
|
|
|
import com.gitee.sop.gatewaycommon.zuul.loadbalancer.ServiceGrayConfig; |
|
|
|
import com.google.common.collect.Sets; |
|
|
|
|
|
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
|
import org.apache.commons.lang3.StringUtils; |
|
|
|
import org.apache.commons.lang3.StringUtils; |
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
@ -27,14 +27,11 @@ import java.util.stream.Stream; |
|
|
|
* |
|
|
|
* |
|
|
|
* @author tanghc |
|
|
|
* @author tanghc |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
@Service |
|
|
|
|
|
|
|
@Slf4j |
|
|
|
@Slf4j |
|
|
|
public class UserKeyManager { |
|
|
|
@Service |
|
|
|
|
|
|
|
public class DbUserKeyManager extends DefaultUserKeyManager { |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
private static final int STATUS_ENABLE = 1; |
|
|
|
* KEY:instanceId |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
private Map<String, ServiceGrayConfig> serviceUserKeyMap = Maps.newConcurrentMap(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Autowired |
|
|
|
@Autowired |
|
|
|
private Environment environment; |
|
|
|
private Environment environment; |
|
|
@ -42,18 +39,14 @@ public class UserKeyManager { |
|
|
|
@Autowired |
|
|
|
@Autowired |
|
|
|
private ConfigGrayUserkeyMapper configGrayUserkeyMapper; |
|
|
|
private ConfigGrayUserkeyMapper configGrayUserkeyMapper; |
|
|
|
|
|
|
|
|
|
|
|
public boolean containsKey(String serviceId, Object userKey) { |
|
|
|
@Override |
|
|
|
if (serviceId == null || userKey == null) { |
|
|
|
public void load() { |
|
|
|
return false; |
|
|
|
Query query = new Query(); |
|
|
|
|
|
|
|
query.eq("status", STATUS_ENABLE); |
|
|
|
|
|
|
|
List<ConfigGrayUserkey> list = configGrayUserkeyMapper.list(query); |
|
|
|
|
|
|
|
for (ConfigGrayUserkey configGrayUserkey : list) { |
|
|
|
|
|
|
|
this.setServiceGrayConfig(configGrayUserkey); |
|
|
|
} |
|
|
|
} |
|
|
|
return this.getServiceGrayConfig(serviceId).containsKey(userKey); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public String getVersion(String serviceId, String nameVersion) { |
|
|
|
|
|
|
|
if (serviceId == null || nameVersion == null) { |
|
|
|
|
|
|
|
return null; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return this.getServiceGrayConfig(serviceId).getVersion(nameVersion); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
@ -61,11 +54,12 @@ public class UserKeyManager { |
|
|
|
* |
|
|
|
* |
|
|
|
* @param configGrayUserkey 灰度配置 |
|
|
|
* @param configGrayUserkey 灰度配置 |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public void setServiceGrayConfig(String serviceId, ConfigGrayUserkey configGrayUserkey) { |
|
|
|
public void setServiceGrayConfig(ConfigGrayUserkey configGrayUserkey) { |
|
|
|
if (configGrayUserkey == null) { |
|
|
|
if (configGrayUserkey == null) { |
|
|
|
return; |
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
|
this.clear(serviceId); |
|
|
|
String instanceId = configGrayUserkey.getInstanceId(); |
|
|
|
|
|
|
|
this.clear(instanceId); |
|
|
|
String userKeyData = configGrayUserkey.getUserKeyContent(); |
|
|
|
String userKeyData = configGrayUserkey.getUserKeyContent(); |
|
|
|
String nameVersionContent = configGrayUserkey.getNameVersionContent(); |
|
|
|
String nameVersionContent = configGrayUserkey.getNameVersionContent(); |
|
|
|
String[] userKeys = StringUtils.split(userKeyData, ','); |
|
|
|
String[] userKeys = StringUtils.split(userKeyData, ','); |
|
|
@ -73,7 +67,7 @@ public class UserKeyManager { |
|
|
|
log.info("添加userKey,userKeys.length:{}, nameVersionList:{}", userKeys.length, Arrays.toString(nameVersionList)); |
|
|
|
log.info("添加userKey,userKeys.length:{}, nameVersionList:{}", userKeys.length, Arrays.toString(nameVersionList)); |
|
|
|
|
|
|
|
|
|
|
|
List<String> list = Stream.of(userKeys).collect(Collectors.toList()); |
|
|
|
List<String> list = Stream.of(userKeys).collect(Collectors.toList()); |
|
|
|
ServiceGrayConfig serviceGrayConfig = getServiceGrayConfig(serviceId); |
|
|
|
ServiceGrayConfig serviceGrayConfig = getServiceGrayConfig(instanceId); |
|
|
|
serviceGrayConfig.getUserKeys().addAll(list); |
|
|
|
serviceGrayConfig.getUserKeys().addAll(list); |
|
|
|
|
|
|
|
|
|
|
|
Map<String, String> grayNameVersion = serviceGrayConfig.getGrayNameVersion(); |
|
|
|
Map<String, String> grayNameVersion = serviceGrayConfig.getGrayNameVersion(); |
|
|
@ -89,20 +83,10 @@ public class UserKeyManager { |
|
|
|
/** |
|
|
|
/** |
|
|
|
* 清空用户key |
|
|
|
* 清空用户key |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public void clear(String serviceId) { |
|
|
|
public void clear(String instanceId) { |
|
|
|
getServiceGrayConfig(serviceId).clear(); |
|
|
|
getServiceGrayConfig(instanceId).clear(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public ServiceGrayConfig getServiceGrayConfig(String serviceId) { |
|
|
|
|
|
|
|
ServiceGrayConfig serviceGrayConfig = serviceUserKeyMap.get(serviceId); |
|
|
|
|
|
|
|
if (serviceGrayConfig == null) { |
|
|
|
|
|
|
|
serviceGrayConfig = new ServiceGrayConfig(); |
|
|
|
|
|
|
|
serviceGrayConfig.setUserKeys(Sets.newConcurrentHashSet()); |
|
|
|
|
|
|
|
serviceGrayConfig.setGrayNameVersion(Maps.newConcurrentMap()); |
|
|
|
|
|
|
|
serviceUserKeyMap.put(serviceId, serviceGrayConfig); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return serviceGrayConfig; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@PostConstruct |
|
|
|
@PostConstruct |
|
|
|
protected void after() throws Exception { |
|
|
|
protected void after() throws Exception { |
|
|
@ -113,14 +97,14 @@ public class UserKeyManager { |
|
|
|
ChannelMsg channelMsg = JSON.parseObject(nodeData, ChannelMsg.class); |
|
|
|
ChannelMsg channelMsg = JSON.parseObject(nodeData, ChannelMsg.class); |
|
|
|
String data = channelMsg.getData(); |
|
|
|
String data = channelMsg.getData(); |
|
|
|
UserKeyDefinition userKeyDefinition = JSON.parseObject(data, UserKeyDefinition.class); |
|
|
|
UserKeyDefinition userKeyDefinition = JSON.parseObject(data, UserKeyDefinition.class); |
|
|
|
String serviceId = userKeyDefinition.getServiceId(); |
|
|
|
String instanceId = userKeyDefinition.getInstanceId(); |
|
|
|
switch (channelMsg.getOperation()) { |
|
|
|
switch (channelMsg.getOperation()) { |
|
|
|
case "set": |
|
|
|
case "set": |
|
|
|
ConfigGrayUserkey configGrayUserkey = configGrayUserkeyMapper.getByColumn("service_id", serviceId); |
|
|
|
ConfigGrayUserkey configGrayUserkey = configGrayUserkeyMapper.getByColumn("instance_id", instanceId); |
|
|
|
this.setServiceGrayConfig(serviceId, configGrayUserkey); |
|
|
|
this.setServiceGrayConfig(configGrayUserkey); |
|
|
|
break; |
|
|
|
break; |
|
|
|
case "clear": |
|
|
|
case "clear": |
|
|
|
clear(serviceId); |
|
|
|
clear(instanceId); |
|
|
|
break; |
|
|
|
break; |
|
|
|
default: |
|
|
|
default: |
|
|
|
log.error("userKey消息,错误的消息指令,nodeData:{}", nodeData); |
|
|
|
log.error("userKey消息,错误的消息指令,nodeData:{}", nodeData); |