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秒;

健康检查失败后实例会被标记为不健康,不会被立即删除。

 ephemeraltrue对应的是服务健康检查模式中的 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();
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值