全部產品
Search
文件中心

API Gateway:前端灰階外掛程式

更新時間:Oct 25, 2025

frontend-gray外掛程式實現了前端使用者灰階的功能,通過此外掛程式,不但可以用於業務A/B實驗,同時通過可灰階配合可監控可復原策略保證系統發布營運穩定性。

運行屬性

外掛程式執行階段:認證階段 外掛程式執行優先順序:1000

配置欄位

名稱

資料類型

填寫要求

預設值

描述

grayKey

string

非必填

-

使用者ID的唯一標識,可以來自Cookie或者Header中,比如 userid,如果沒有填寫則使用rules[].grayTagKeyrules[].grayTagValue過濾灰階規則

useManifestAsEntry

boolean

非必填

false

是否使用manifest作為入口。當設定為true時,系統將使用manifest檔案作為應用入口,適用於微前端架構。在這種模式下,系統會根據manifest檔案的內容來載入不同版本的前端資源。

localStorageGrayKey

string

非必填

-

使用JWT鑒權方式,使用者ID的唯一標識來自localStorage中,如果配置了當前參數,則grayKey失效

graySubKey

string

非必填

-

使用者身份資訊可能以JSON形式透出,比如:userInfo:{ userCode:"001" },當前例子graySubKey取值為userCode

storeMaxAge

int

非必填

60 * 60 * 24 * 365

網關設定Cookie最大儲存時間長度:單位為秒,預設為1年

cookieDomain

string

非必填

-

Cookie的Domain設定,可用於跨子域共用Cookie。例如:".example.com"

indexPaths

array of strings

非必填

-

強制處理的路徑,支援 Glob 模式比對。例如:在微前端情境下,XHR 介面如: /resource/**/manifest-main.json本質是一個資源請求,需要走外掛程式轉寄邏輯。

skippedPaths

array of strings

非必填

-

用於排除特定路徑,避免當前外掛程式處理這些請求,支援 Glob 模式比對。例如,在 rewrite 情境下,XHR 介面請求 /api/** 如果經過外掛程式轉寄邏輯,可能會導致非預期的結果。

skippedByHeaders

map of string to string

非必填

-

用於通過要求標頭過濾,指定哪些請求不被當前外掛程式處理。skippedPaths 的優先順序高於當前配置,且頁面HTML請求不受本配置的影響。

rules

array of object

必填

-

使用者定義不同的灰階規則,適配不同的灰階情境

rewrite

object

非必填

-

重寫配置,一般用於OSS/CDN前端部署的重寫配置

baseDeployment

object

非必填

-

配置Base基準規則的配置

grayDeployments

array of object

非必填

-

配置Gray灰階的生效規則,以及生效版本

backendGrayTag

string

非必填

x-mse-tag

後端灰階版本Tag,如果配置了,cookie中將攜帶值為${backendGrayTag}:${grayDeployments[].backendVersion}

uniqueGrayTag

string

非必填

x-higress-uid

開啟按照比例灰階時候,網關會產生一個唯一標識存在cookie中,一方面用於session黏貼,另一方面後端也可以使用這個值用於全鏈路的灰階串聯

injection

object

非必填

-

往首頁HTML中注入全域資訊,比如<script>window.global = {...}</script>

rules欄位配置說明:

名稱

資料類型

填寫要求

預設值

描述

name

string

必填

-

規則名稱唯一標識,和deploy.gray[].name進行關聯生效

grayKeyValue

array of string

非必填

-

使用者ID 白名單列表

grayTagKey

string

非必填

-

使用者分類打標的標籤key值,來自Cookie

grayTagValue

array of string

非必填

-

使用者分類打標的標籤value值,來自Cookie

rewrite欄位配置說明:

indexRouting首頁重寫和fileRouting檔案重寫,本質都是首碼匹配,比如/app1/mfe/app1/{version}/index.html代表/app1為首碼的請求,路由到/mfe/app1/{version}/index.html頁面上,其中{version}代表版本號碼,在運行過程中會被baseDeployment.version或者grayDeployments[].version動態替換。

{version} 作為保留欄位,在執行過程中會被baseDeployment.version或者grayDeployments[].version動態替換前端版本。

名稱

資料類型

填寫要求

預設值

描述

host

string

非必填

-

host地址,如果是OSS則設定為 VPC 內網訪問地址。

indexRouting

map of string to string

非必填

-

用於定義首頁重寫路由規則。每個鍵 (Key) 表示首頁的路由路徑,值 (Value) 則指向重新導向的目標檔案。例如,鍵為 /app1 對應的值為 /mfe/app1/{version}/index.html。生效version為0.0.1, 訪問路徑為 /app1,則重新導向到 /mfe/app1/0.0.1/index.html

fileRouting

map of string to string

非必填

-

用於定義資源檔重寫路由規則。每個鍵 (Key) 表示資源訪問路徑,值 (Value) 則指向重新導向的目標檔案。例如,鍵為 /app1/ 對應的值為 /mfe/app1/{version}。生效version為0.0.1,訪問路徑為 /app1/js/a.js,則重新導向到 /mfe/app1/0.0.1/js/a.js

baseDeployment欄位配置說明:

名稱

資料類型

填寫要求

預設值

描述

version

string

必填

-

Base版本的版本號碼,作為兜底的版本

backendVersion

string

非必填

-

後端灰階版本,配合key${backendGrayTag},寫入cookie中

versionPredicates

string

非必填

-

version含義相同,但是滿足多版本的需求:根據不同路由映射不同的Version版本。一般用於微前端的情境:一個主應用需要管理多個微應用

grayDeployments欄位配置說明:

名稱

資料類型

填寫要求

預設值

描述

version

string

必填

-

Gray版本的版本號碼,如果命中灰階規則,則使用此版本。如果是非CDN部署,在header添加x-higress-tag

versionPredicates

string

非必填

-

version含義相同,但是滿足多版本的需求:根據不同路由映射不同的Version版本。一般用於微前端的情境:一個主應用需要管理多個微應用

backendVersion

string

非必填

-

後端灰階版本,會在XHR/Fetch請求的header頭添加 x-mse-tag到後端

name

string

必填

-

規則名稱和rules[].name關聯,

enabled

boolean

非必填

-

是否啟動當前灰階規則

weight

int

非必填

-

按照比例灰階,比如50。

配置樣本

基礎配置(按使用者灰階)

grayKey: userid
rules:
- name: inner-user
  grayKeyValue:
  - '00000001'
  - '00000005'
- name: beta-user
  grayKeyValue:
  - '00000002'
  - '00000003'
  grayTagKey: level
  grayTagValue:
  - level3
  - level5
baseDeployment:
  version: base
grayDeployments:
  - name: beta-user
    version: gray
    enabled: true

cookie中的使用者唯一標識為 userid,當前灰階規則配置了beta-user的規則。

當滿足下面調試的時候,會使用version: gray版本,否則使用version: base版本。

  • cookie中userid等於00000002或者00000003

  • cookie中level等於level3或者level5的使用者

按比例灰階

grayKey: userid
rules:
- name: inner-user
  grayKeyValue:
  - '00000001'
  - '00000005'
baseDeployment:
  version: base
grayDeployments:
  - name: inner-user
    version: gray
    enabled: true
    weight: 80

總的灰階規則為100%,其中灰階版本的權重為80%,基準版本為20%。

使用者資訊存在JSON中

grayKey: appInfo
graySubKey: userId
rules:
- name: inner-user
  grayKeyValue:
  - '00000001'
  - '00000005'
- name: beta-user
  grayKeyValue:
  - '00000002'
  - '00000003'
  grayTagKey: level
  grayTagValue:
  - level3
  - level5
baseDeployment:
  version: base
grayDeployments:
  - name: beta-user
    version: gray
    enabled: true

cookie存在appInfo的JSON資料,其中包含userId欄位作為當前的唯一標識。當前灰階規則配置了beta-user的規則。 當滿足下面的調試條件時,會使用version: gray版本,否則使用version: base版本。

  • cookie中userid等於00000002或者00000003

  • cookie中level等於level3或者level5的使用者

使用者資訊儲存在LocalStorage

由於網關外掛程式需要識別使用者為唯一身份資訊,HTTP協議進行資訊傳輸,只能在Header中傳遞。如果使用者資訊儲存在LocalStorage,在首頁注入一段指令碼將LocalStorage中的使用者資訊設定到cookie中。

(function() {
	var grayKey = '@@X_GRAY_KEY';
	var cookies = document.cookie.split('; ').filter(function(row) {
		return row.indexOf(grayKey + '=') === 0;
	});

	try {
		if (typeof localStorage !== 'undefined' && localStorage !== null) {
			var storageValue = localStorage.getItem(grayKey);
			var cookieValue = cookies.length > 0 ? decodeURIComponent(cookies[0].split('=')[1]) : null;
			if (storageValue && storageValue.indexOf('=') < 0 && cookieValue !== storageValue) {
				document.cookie = grayKey + '=' + encodeURIComponent(storageValue) + '; path=/;';
				window.location.reload();
			}
		}
	} catch (error) {
		// xx
	}
})();

rewrite重寫配置

一般用於CDN部署情境,{version}會在運行過程中動態替換為真正的版本。

grayKey: userid
rules:
- name: inner-user
  grayKeyValue:
  - '00000001'
  - '00000005'
- name: beta-user
  grayKeyValue:
  - '00000002'
  - '00000003'
  grayTagKey: level
  grayTagValue:
  - level3
  - level5
rewrite:
  host: frontend-gray.oss-cn-shanghai-internal.aliyuncs.com
  notFoundUri: /mfe/app1/dev/404.html
  indexRouting:
    /app1: '/mfe/app1/{version}/index.html'
    /: '/mfe/app1/{version}/index.html',
  fileRouting:
    /: '/mfe/app1/{version}'
    /app1/: '/mfe/app1/{version}'
baseDeployment:
  version: base
grayDeployments:
  - name: beta-user
    version: gray
    enabled: true

indexRouting:首頁路由配置

訪問 /app1/app123,/app1/index.html/app1/xxx/xxxx 都會路由到/mfe/app1/{version}/index.html

fileRouting:檔案路由配置

下面檔案對應均生效

  • /js/a.js => /mfe/app1/v1.0.0/js/a.js

  • /js/template/a.js => /mfe/app1/v1.0.0/js/template/a.js

  • /app1/js/a.js => /mfe/app1/v1.0.0/js/a.js

  • /app1/js/template/a.js => /mfe/app1/v1.0.0/js/template/a.js

在HTML首頁注入代碼

grayKey: userid
rules:
- name: inner-user
  grayKeyValue:
  - '00000001'
  - '00000005'
baseDeployment:
  version: base
grayDeployments:
  - name: beta-user
    version: gray
    enabled: true
injection:
  head: 
    - <script>console.log('Header')</script>
  body:
    first:
      - <script>console.log('hello world before')</script>
      - <script>console.log('hello world before1')</script>
    last:
      - <script>console.log('hello world after')</script>
      - <script>console.log('hello world after2')</script>

通過 injection往HTML首頁注入代碼,可以在head標籤注入代碼,也可以在body標籤的firstlast位置注入代碼。