![JAR search and dependency download from the Maven repository](/logo.png)
com.alibaba.arthas.tunnel.server.cluster.RedisTunnelClusterStore Maven / Gradle / Ivy
package com.alibaba.arthas.tunnel.server.cluster;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import com.alibaba.arthas.tunnel.server.AgentClusterInfo;
import com.fasterxml.jackson.databind.ObjectMapper;
/**
*
* @author hengyunabc 2020-10-27
*
*/
public class RedisTunnelClusterStore implements TunnelClusterStore {
private final static Logger logger = LoggerFactory.getLogger(RedisTunnelClusterStore.class);
// 定义jackson对象
private static final ObjectMapper MAPPER = new ObjectMapper();
private String prefix = "arthas-tunnel-agent-";
private StringRedisTemplate redisTemplate;
@Override
public AgentClusterInfo findAgent(String agentId) {
try {
ValueOperations opsForValue = redisTemplate.opsForValue();
String infoStr = opsForValue.get(prefix + agentId);
if (infoStr == null) {
throw new IllegalArgumentException("can not find info for agentId: " + agentId);
}
AgentClusterInfo info = MAPPER.readValue(infoStr, AgentClusterInfo.class);
return info;
} catch (Throwable e) {
logger.error("try to read agentInfo error. agentId:{}", agentId, e);
throw new RuntimeException(e);
}
}
@Override
public void removeAgent(String agentId) {
ValueOperations opsForValue = redisTemplate.opsForValue();
opsForValue.getOperations().delete(prefix + agentId);
}
@Override
public void addAgent(String agentId, AgentClusterInfo info, long timeout, TimeUnit timeUnit) {
try {
ValueOperations opsForValue = redisTemplate.opsForValue();
String infoStr = MAPPER.writeValueAsString(info);
opsForValue.set(prefix + agentId, infoStr, timeout, timeUnit);
} catch (Throwable e) {
logger.error("try to add agentInfo error. agentId:{}", agentId, e);
throw new RuntimeException(e);
}
}
public StringRedisTemplate getRedisTemplate() {
return redisTemplate;
}
public void setRedisTemplate(StringRedisTemplate redisTemplate) {
this.redisTemplate = redisTemplate;
}
@Override
public Collection allAgentIds() {
ValueOperations opsForValue = redisTemplate.opsForValue();
int length = prefix.length();
final Set redisValues = opsForValue.getOperations().keys(prefix + "*");
if (redisValues != null) {
final ArrayList result = new ArrayList<>(redisValues.size());
for (String value : redisValues) {
result.add(value.substring(length));
}
return result;
} else {
logger.error("try to get allAgentIds error. redis returned null.");
return Collections.emptyList();
}
}
@Override
public Map agentInfo(String appName) {
try {
ValueOperations opsForValue = redisTemplate.opsForValue();
String prefixWithAppName = prefix + appName + "_";
ArrayList keys = new ArrayList<>(opsForValue.getOperations().keys(prefixWithAppName + "*"));
List values = opsForValue.getOperations().opsForValue().multiGet(keys);
Map result = new HashMap<>();
Iterator iterator = values.iterator();
for (String key : keys) {
String infoStr = iterator.next();
AgentClusterInfo info = MAPPER.readValue(infoStr, AgentClusterInfo.class);
String agentId = key.substring(prefix.length());
result.put(agentId, info);
}
return result;
} catch (Throwable e) {
logger.error("try to query agentInfo error. appName:{}", appName, e);
throw new RuntimeException(e);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy