聊聊eureka instance的overriddenstatus

本文主要研究一下eureka instance的overriddenstatusjava

overriddenstatus

eureka-client-1.8.8-sources.jar!/com/netflix/appinfo/InstanceInfo.javagit

/**
         * Sets the status overridden by some other external process.This is
         * mostly used in putting an instance out of service to block traffic to
         * it.
         *
         * @param status the overridden {@link InstanceStatus} of the instance.
         * @return @return the {@link InstanceInfo} builder.
         */
        public Builder setOverriddenStatus(InstanceStatus status) {
            result.overriddenstatus = status;
            return this;
        }
經過註釋能夠看到,這個overriddenstatus的意思就是用於外部的一些操做,在netflix裏頭就是用於red/black部署的時候,先把指定服務設置爲OUT_OF_SERVICE來故意關閉請求流量。

圖片描述

InstanceStatus枚舉定義以下:
public enum InstanceStatus {
        UP, // Ready to receive traffic
        DOWN, // Do not send traffic- healthcheck callback failed
        STARTING, // Just about starting- initializations to be done - do not
        // send traffic
        OUT_OF_SERVICE, // Intentionally shutdown for traffic
        UNKNOWN;

        public static InstanceStatus toEnum(String s) {
            if (s != null) {
                try {
                    return InstanceStatus.valueOf(s.toUpperCase());
                } catch (IllegalArgumentException e) {
                    // ignore and fall through to unknown
                    logger.debug("illegal argument supplied to InstanceStatus.valueOf: {}, defaulting to {}", s, UNKNOWN);
                }
            }
            return UNKNOWN;
        }
    }

操做

設置OUT_OF_SERVICE

curl -i -X PUT http://localhost:8761/eureka/apps/client1/127.0.0.1:client1:8081/status?value=OUT_OF_SERVICE
HTTP/1.1 200
Content-Type: application/xml
Content-Length: 0
Date: Wed, 16 May 2018 06:52:29 GMT

刪除OUT_OF_SERVICE

curl -i -X DELETE http://localhost:8761/eureka/apps/client1/127.0.0.1:client1:8081/status
HTTP/1.1 200
Content-Type: application/xml
Content-Length: 0
Date: Wed, 16 May 2018 06:54:30 GM

小結

eureka instance的overriddenstatus對於部署來講很是好用,好比red/black升級,將部分原服務先設置爲OUT_OF_SERVICE,中止接收請求,即變爲black,以後新部署的服務啓動起來,即爲red。若是新服務正常,就能夠關閉舊服務了,假設新服務出現問題,則立馬刪除掉新服務,將原有服務的overriddenstatus刪除掉,恢復UP,恢復接收流量。github

doc

相關文章
相關標籤/搜索