Skip to content

如何执行全链路智能编排高级蓝绿灰度发布

HaojunRen edited this page May 31, 2024 · 20 revisions

本文档只适用于Discovery 6.16.3及以上版本的集成方式

链路智能编排的方式,即路由链路在后台会智能化编排,用户不再需要关心服务实例的版本情况而进行手工编排,只需要配置跟业务参数有关的条件表达式即可,让蓝绿灰度发布变的更简单更易用

全链路智能编排版本逻辑

  • 线上所有的服务,每个服务至少有一个版本
  • 线上所有的服务,每个服务如果有两个版本,第一个划归为旧版本,第二个划归为新版本
  • 线上所有的服务,每个服务如果有三个及以上版本,第一个划归为旧版本,第二个到最后划归为新版本。通过分号分隔的方式放入,例如,2.0;3.0;4.0
  • 线上所有的服务,如果有未接入Nepxion Discovery框架,或者接入了却未设置版本,拉取实例列表的时候,版本默认视作为default值,并划归为旧版本

全链路智能编排实现原理

通过向控制台发送请求,控制台根据Json格式规则策略,根据新旧版本的判断,智能编排出两条新旧路由链路,并给它们赋予不同的条件表达式,最终创建出完整的Xml格式规则策略,保存到配置中心

全链路智能编排使用方式

动态版本场景下的全链路智能编排

动态版本指版本可排序,服务实例的版本号采用时间戳或者数字递增的方式,将排序后版本号列表的第一个值作为旧的稳定版本

  • 创建版本兜底规则策略

Yaml格式

service:
  - discovery-guide-service-a
  - discovery-guide-service-b

Json格式

{
  "service": ["discovery-guide-service-a", "discovery-guide-service-b"]
}

经过链路智能编排,等效于

<?xml version="1.0" encoding="UTF-8"?>
<rule>
    <strategy>
        <version>{"discovery-guide-service-a":"1.0","discovery-guide-service-b":"1.0"}</version>
    </strategy>
</rule>
  • 创建版本蓝绿规则策略

Yaml格式

service:
  - discovery-guide-service-a
  - discovery-guide-service-b
blueGreen:
  - expression: "#H['xyz'] == '1'"
    route: green
  - expression: "#H['xyz'] == '2'"
    route: blue

Json格式

{
  "service": ["discovery-guide-service-a", "discovery-guide-service-b"],
  "blueGreen": [
    {
      "expression": "#H['xyz'] == '1'",
      "route": "green"
    }, 
    {
      "expression": "#H['xyz'] == '2'",
      "route": "blue"
    }
  ]
}

经过链路智能编排,等效于

<?xml version="1.0" encoding="UTF-8"?>
<rule>
    <strategy>
        <version>{"discovery-guide-service-a":"1.0","discovery-guide-service-b":"1.0"}</version>
    </strategy>
    <strategy-release>
        <conditions type="blue-green">
            <condition id="condition-0" expression="#H['xyz'] == '1'" version-id="route-0"/>
            <condition id="condition-1" expression="#H['xyz'] == '2'" version-id="route-1"/>
        </conditions>
        <routes>
            <route id="route-0" type="version">{"discovery-guide-service-a":"1.0","discovery-guide-service-b":"1.0"}</route>
            <route id="route-1" type="version">{"discovery-guide-service-a":"1.1","discovery-guide-service-b":"1.1"}</route>
        </routes>
    </strategy-release>
</rule>
  • 创建版本灰度规则策略

Yaml格式

service:
  - discovery-guide-service-a
  - discovery-guide-service-b
gray:
  - expression: "#H['xyz'] == '3'"
    weight:
      - 90
      - 10
  - expression: "#H['xyz'] == '4'"
    weight:
      - 70
      - 30
  - weight:
      - 100
      - 0

Json格式

{
  "service": ["discovery-guide-service-a", "discovery-guide-service-b"],
  "gray": [
    {
      "expression": "#H['xyz'] == '3'",
      "weight": [90, 10]
    },
    {
      "expression": "#H['xyz'] == '4'",
      "weight": [70, 30]
    },
    {
      "weight": [100, 0]
    }
  ]
}

经过链路智能编排,等效于

<?xml version="1.0" encoding="UTF-8"?>
<rule>
    <strategy>
        <version>{"discovery-guide-service-a":"1.0","discovery-guide-service-b":"1.0"}</version>
    </strategy>
    <strategy-release>
        <conditions type="gray">
            <condition id="condition-0" expression="#H['xyz'] == '3'" version-id="route-0=90;route-1=10"/>
            <condition id="condition-1" expression="#H['xyz'] == '4'" version-id="route-0=70;route-1=30"/>
            <condition id="condition-2" version-id="route-0=100;route-1=0"/>
        </conditions>
        <routes>
            <route id="route-0" type="version">{"discovery-guide-service-a":"1.0","discovery-guide-service-b":"1.0"}</route>
            <route id="route-1" type="version">{"discovery-guide-service-a":"1.1","discovery-guide-service-b":"1.1"}</route>
        </routes>
    </strategy-release>
</rule>
  • 创建版本混合蓝绿灰度 + 内置Header规则策略

Yaml格式

service:
  - discovery-guide-service-a
  - discovery-guide-service-b
blueGreen:
  - expression: "#H['xyz'] == '1'"
    route: green
  - expression: "#H['xyz'] == '2'"
    route: blue
gray:
  - expression: "#H['xyz'] == '3'"
    weight:
      - 90
      - 10
  - expression: "#H['xyz'] == '4'"
    weight:
      - 70
      - 30
  - weight:
      - 100
      - 0
header:
  xyz: 1

Json格式

使用时候,请删除中文注释,否则会报错

{
  "service": ["discovery-guide-service-a", "discovery-guide-service-b"],
  "blueGreen": [
    {
      "expression": "#H['xyz'] == '1'",
      // 绿(旧版本)路由链路
      "route": "green"
    }, 
    {
      "expression": "#H['xyz'] == '2'",
      // 蓝(新版本)路由链路
      "route": "blue"
    }
  ],
  "gray": [
    {
      "expression": "#H['xyz'] == '3'",
      // 稳定(旧版本)路由链路权重,灰度(新版本)路由链路权重
      "weight": [90, 10]
    },
    {
      "expression": "#H['xyz'] == '4'",
      "weight": [70, 30]
    },
    {
      "weight": [100, 0]
    }
  ],
  "header": {"xyz": "1"}
}

经过链路智能编排,等效于

<?xml version="1.0" encoding="UTF-8"?>
<rule>
    <strategy>
        <version>{"discovery-guide-service-a":"1.0","discovery-guide-service-b":"1.0"}</version>
    </strategy>
    <strategy-release>
        <conditions type="blue-green">
            <condition id="condition-0" expression="#H['xyz'] == '1'" version-id="route-0"/>
            <condition id="condition-1" expression="#H['xyz'] == '2'" version-id="route-1"/>
        </conditions>
        <conditions type="gray">
            <condition id="condition-0" expression="#H['xyz'] == '3'" version-id="route-0=10;route-1=90"/>
            <condition id="condition-1" expression="#H['xyz'] == '4'" version-id="route-0=40;route-1=60"/>
            <condition id="condition-2" version-id="route-0=0;route-1=100"/>
        </conditions>
        <routes>
            <route id="route-0" type="version">{"discovery-guide-service-a":"1.0","discovery-guide-service-b":"1.0"}</route>
            <route id="route-1" type="version">{"discovery-guide-service-a":"1.1","discovery-guide-service-b":"1.1"}</route>
        </routes>
        <header>{"xyz":"1"}</header>
    </strategy-release>
</rule>

静态版本场景下的全链路智能编排

静态版本指版本不可排序,服务实例的版本号采用非时间戳或者非数字递增的方式(例如,旧版本的版本号为base,新版本的版本号为gray),将根据服务实例全局唯一ID的时间戳前缀进行排序,把上线时间最早的服务实例的版本号作为旧的稳定版本

在规则策略上加入版本号排序类型sort: time(Yaml)或者"sort": "time"(Json)即可

Yaml格式

service:
  - discovery-guide-service-a
  - discovery-guide-service-b
blueGreen:
  - expression: "#H['xyz'] == '1'"
    route: green
  - expression: "#H['xyz'] == '2'"
    route: blue
gray:
  - expression: "#H['xyz'] == '3'"
    weight:
      - 90
      - 10
  - expression: "#H['xyz'] == '4'"
    weight:
      - 70
      - 30
  - weight:
      - 100
      - 0
header:
  xyz: 1
sort: time

Json格式

使用时候,请删除中文注释,否则会报错

{
  "service": ["discovery-guide-service-a", "discovery-guide-service-b"],
  "blueGreen": [
    {
      "expression": "#H['xyz'] == '1'",
      // 绿(旧版本)路由链路
      "route": "green"
    }, 
    {
      "expression": "#H['xyz'] == '2'",
      // 蓝(新版本)路由链路
      "route": "blue"
    }
  ],
  "gray": [
    {
      "expression": "#H['xyz'] == '3'",
      // 稳定(旧版本)路由链路权重,灰度(新版本)路由链路权重
      "weight": [90, 10]
    },
    {
      "expression": "#H['xyz'] == '4'",
      "weight": [70, 30]
    },
    {
      "weight": [100, 0]
    }
  ],
  "header": {"xyz": "1"},
  "sort": "time"
}

版本号排序类型(sort),可选值为versiontime,缺省为version(不需要配置sort: version

  • 当排序类型为version时,适用于版本号采用时间戳或者数字递增的方式。处理逻辑为将排序后版本号列表的第一个值作为旧的稳定版本
  • 当排序类型为time时,不限于版本号的格式。处理逻辑为将根据服务实例全局唯一ID的时间戳前缀进行排序,把上线时间最早的服务实例的版本号作为旧的稳定版本

另外,如果采用服务实例上线的时间戳作为版本排序依据,那么服务端版本故障转移或者版本偏好的功能使用时,需要把版本号排序类型设置为time。该方案同时适用于业务服务,Spring Cloud Gateway和Zuul网关

# 版本号排序类型。缺失则默认为version
# 版本故障转移或者版本偏好启动时,需要寻址旧的稳定版本
# 1. 当排序类型为version时,适用于版本号采用时间戳或者数字递增的方式。处理逻辑为将排序后版本号列表的第一个值作为旧的稳定版本
# 2. 当排序类型为time时,不限于版本号的格式。处理逻辑为将根据服务实例全局唯一ID的时间戳前缀进行排序,把上线时间最早的服务实例的版本号作为旧的稳定版本
# spring.application.strategy.version.sort.type=version
spring.application.strategy.version.sort.type=time

环境搭建,请参考

具体用法,请参考




2017-2050 ©Nepxion Studio Apache License

           

Total visits

讲义篇

集成篇

概念篇

实践篇

功能篇

配置篇

扩展篇

测试篇

升级篇

贡献篇

Clone this wiki locally