spring cloud nacos源码解析(3)Instance服务实体
1.当新的服务注册成功后创建instance实例对象。实例对象(pojo/instance)包含那些属性呢
private static final long serialVersionUID = -742906310567291979L; /** * 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>();
enable:决定是否实例可以接受请求;
ephemeral:是否为临时实例,决定了nacos健康心跳的方式
nacos两种健康检查模式 1.client上报模式
客户端通过心跳上报方式告知服务端(nacos注册中心)健康状态;
默认心跳间隔5秒;
nacos会在超过15秒未收到心跳后将实例设置为不健康状态;
超过30秒将实例删除;
2 服务端主动检测
nacos主动探知客户端健康状态,默认间隔为20秒;
健康检查失败后实例会被标记为不健康,不会被立即删除。
ephemeral为true对应的是服务健康检查模式中的 client 模式,为false对应的是 server 模式。
meta: 元数据,记录服务实例补充参数
instanceId: 实例唯一id
preserved.instance.id.generator 设置实例生成方法
/** * Generate instance id. * * @param currentInstanceIds current instance ids * @return new instance id */ public String generateInstanceId(Set<String> currentInstanceIds) { //实例生成的方法 String instanceIdGenerator = getInstanceIdGenerator(); if (Constants.SNOWFLAKE_INSTANCE_ID_GENERATOR.equalsIgnoreCase(instanceIdGenerator)) { return generateSnowflakeInstanceId(currentInstanceIds); } else { return generateInstanceId(); } }
默认方法:
public String generateInstanceId() { return getIp() + "#" + getPort() + "#" + getClusterName() + "#" + getServiceName(); }