5.nacos启动之-注册表中核心内存数据结构

注册中心的注册功能核心数据结构都在com.alibaba.nacos.naming.core包下,主要由servicemanager,service,cluster,instance几个概念组成。并且由serviceManager来管理,首先来看ServiceManager类,该类持有一个ConcurrentHashMap<String, Map<String, Service>>类型的serviceMap:

private Map<String, Map<String, Service>> serviceMap = new ConcurrentHashMap<>();

这个map就是注册中心核心数据结构了。该map的key是个namespace,value是个Map<String, Service>,该map的key是group::serviceName,value是个service类。

对于service类,核心的属性有下面几个:

private String token;
private List<String> owners = new ArrayList<>();
private Boolean resetWeight = false;
private Boolean enabled = true;
private Selector selector = new NoneSelector();
private String namespaceId;
private Map<String, Cluster> clusterMap = new HashMap<String, Cluster>();

注册相关的核心数据结构就是clusterMap了,key是个string,保存的是clustername,value是个Cluster类型。来看cluster类,核心数据结构是两个Set<Instance>。

@JSONField(serialize = false)
private Set<Instance> persistentInstances = new HashSet<>();

@JSONField(serialize = false)
private Set<Instance> 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<String, String> metadata = new HashMap<String, String>();

总结起来就是:

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<String, String> metadata = new HashMap<String, String>();

private volatile long lastBeat = System.currentTimeMillis();

cluster:

ephemeralInstances :HashSet<Instance>

service:

clusterMap:Map<String(clusterName), Cluster>

serviceManager:

serviceMap:Map<namespace, Map<group::serviceName, Service>>

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值