注册中心的注册功能核心数据结构都在com.alibaba.nacos.naming.core包下,主要由servicemanager,service,cluster,instance几个概念组成。并且由serviceManager来管理,首先来看ServiceManager类,该类持有一个ConcurrentHashMap
private Map> serviceMap = new ConcurrentHashMap<>();
这个map就是注册中心核心数据结构了。该map的key是个namespace,value是个Map
对于service类,核心的属性有下面几个:
private String token;
private List owners = new ArrayList<>();
private Boolean resetWeight = false;
private Boolean enabled = true;
private Selector selector = new NoneSelector();
private String namespaceId;
private Map clusterMap = new HashMap();
注册相关的核心数据结构就是clusterMap了,key是个string,保存的是clustername,value是个Cluster类型。来看cluster类,核心数据结构是两个Set
@JSONField(serialize = false)
private Set persistentInstances = new HashSet<>();
@JSONField(serialize = false)
private Set ephemeralInstances = new HashSet<>();
第一个是持久化类型,该类型会被持久化处理,第二个是暂时的类型,默认的服务都是这种类型,放在内存中。最后来看instance,instance本身定义的属性不多:
private static final double MAX_WEIGHT_VALUE = 10000.0D;
private static final double MIN_POSTIVE_WEIGHT_VALUE = 0.01D;
private static final double MIN_WEIGHT_VALUE = 0.00D;
private volatile long lastBeat = System.currentTimeMillis();
@JSONField(serialize = false)
private volatile boolean mockValid = false;
private volatile boolean marked = false;
private String tenant;
private String app;
public static final Pattern IP_PATTERN
= Pattern.compile("(\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}):?(\\d{1,5})?");
public static final String SPLITER = "_";
多数都是无关紧要的属性,核心属性定义在父类中:
/**
* unique id of this instance.
*/
private String instanceId;
/**
* instance ip
*/
private String ip;
/**
* instance port
*/
private int port;
/**
* instance weight
*/
private double weight = 1.0D;
/**
* instance health status
*/
private boolean healthy = true;
/**
* If instance is enabled to accept request
*/
private boolean enabled = true;
/**
* If instance is ephemeral
*
* @since 1.0.0
*/
private boolean ephemeral = true;
/**
* cluster information of instance
*/
private String clusterName;
/**
* Service information of instance
*/
private String serviceName;
/**
* user extended attributes
*/
private Map metadata = new HashMap();
总结起来就是:
instance属性;
private String instanceId;
private String ip;
private int port;
private double weight = 1.0D;//权重
private boolean healthy = true;//健康状态
private boolean enabled = true;
private boolean ephemeral = true;
private String clusterName;
private String serviceName;
private Map
private volatile long lastBeat = System.currentTimeMillis();
cluster:
ephemeralInstances :HashSet
service:
clusterMap:Map
serviceManager:
serviceMap:Map