org.zodiac.sentinel.base.cluster.parser.ClusterStateAssignParser Maven / Gradle / Ivy
package org.zodiac.sentinel.base.cluster.parser;
import com.alibaba.csp.sentinel.cluster.ClusterStateManager;
import com.alibaba.csp.sentinel.datasource.Converter;
import com.alibaba.csp.sentinel.log.RecordLog;
import com.alibaba.csp.sentinel.util.AppNameUtil;
import java.util.Set;
import org.zodiac.commons.json.JsonTypeReference;
import org.zodiac.commons.util.Asserts;
import org.zodiac.commons.util.serialize.JsonUtil;
import org.zodiac.sentinel.base.constants.SentinelBaseConstants;
public class ClusterStateAssignParser implements Converter {
private final String namespacePrefix;
public ClusterStateAssignParser() {
this(SentinelBaseConstants.NS_PREFIX);
}
public ClusterStateAssignParser(String namespacePrefix) {
this.namespacePrefix = Asserts.notBlank(namespacePrefix, "namespace prefix % is blank", namespacePrefix);
}
@Override
public Integer convert(String source) {
if (source == null) {
return null;
}
RecordLog.info("[ClusterAssignStateParser] Get data: {}", source);
Set nameSpaceList = JsonUtil.json2ObjectWithType(source, new JsonTypeReference>() {});
if (nameSpaceList == null || nameSpaceList.isEmpty()) {
return ClusterStateManager.CLUSTER_NOT_STARTED;
}
return extractMode(nameSpaceList);
}
protected final String getNamespacePrefix() {
return namespacePrefix;
}
private int extractMode(Set nameSpaceList) {
if (nameSpaceList == null || nameSpaceList.isEmpty()) {
return ClusterStateManager.CLUSTER_NOT_STARTED;
}
String projectName = AppNameUtil.getAppName();
if (nameSpaceList.contains(getNamespacePrefix() + projectName)) {
return ClusterStateManager.CLUSTER_CLIENT;
}
return ClusterStateManager.CLUSTER_NOT_STARTED;
}
}