五月综合激情婷婷六月,日韩欧美国产一区不卡,他扒开我内裤强吻我下面视频 ,无套内射无矿码免费看黄,天天躁,日日躁,狠狠躁

新聞動態(tài)

Python 帶你快速上手 Apache APISIX 插件開發(fā)

發(fā)布日期:2022-01-03 04:49 | 文章來源:CSDN

熟悉 Apache APISIX 的小伙伴都知道,之前在社區(qū)中我們已經(jīng)支持了 Java 和 Go 語言的 Runner,今天 Apache APISIX Python Runner 也來了,社區(qū)中的小伙伴們在開發(fā) Apache APISIX 插件時又多了一種新選擇。

Python 語言作為一個解釋型的高級編程語言,它語法簡潔易上手、代碼可讀性好 ,在跨平臺 、可移植性 、開發(fā)效率上都有很好的表現(xiàn),同時作為一個高級編程語言它的封裝抽象程度比較高屏蔽了很多底層細節(jié)(例如:GC )讓我們在開發(fā)的過程中可以更專注應(yīng)用邏輯的開發(fā)。

同時作為一個有 30 年歷史的老牌開發(fā)語言,它的生態(tài)以及各種模塊已經(jīng)非常完善,我們大部分的開發(fā)和應(yīng)用場景都可以從社區(qū)中找到很成熟的模塊或解決方案。

Python 其他的優(yōu)點就不再一一贅述,當然它的缺點也比較明顯:Python 作為一門解釋性語言,相較于 C++ 和 Go 這樣的編譯型語言,在性能上的差距還是比較大的。

一、了解:項目架構(gòu)

apache-apisix-python-runner 這個項目可以理解為 Apache APISIX Python 之間的一道橋梁,通過 Python Runner 可以把 Python 直接應(yīng)用到 Apache APISIX 的插件開發(fā)中,最重要的還是希望讓更多對 Apache APISIX 和 API 網(wǎng)關(guān)感興趣的 Python 開發(fā)者通過這個項目,更多地了解和使用 Apache APISIX,以下為 Apache APISIX 多語言支持的架構(gòu)圖。

上圖左邊是 Apache APISIX 的工作流程,右邊的 Plugin Runner 是各語言的插件運行器,本文介紹的 apisix-python-plugin-runner 就是支持 Python 語言的 Plugin Runner。

Apache APISIX 中配置一個 Plugin Runner 時,Apache APISIX 會啟動一個子進程運行 Plugin Runner,該子進程與 Apache APISIX 進程屬于同一個用戶,當我們重啟或重新加載 Apache APISIX 時,Plugin Runner 也將被重啟。

如果你為一個給定的路由配置了 ext-plugin-* 插件,請求命中該路由時將觸發(fā) Apache APISIX 通過 Unix Socket Plugin Runner 發(fā)起 RPC 調(diào)用。調(diào)用分為兩個階段:

  • ext-plugin-pre-req :在執(zhí)行 Apache APISIX 內(nèi)置插件(Lua 語言插件)之前
  • ext-plugin-post-req :在執(zhí)行 Apache APISIX 內(nèi)置插件(Lua 語言插件)之后

大家可以根據(jù)需要選擇并配置 Plugin Runner 的執(zhí)行時機。Plugin Runner 會處理 RPC 調(diào)用,在其內(nèi)部創(chuàng)建一個模擬請求,然后運行多語言編寫的插件,并將結(jié)果返回給 Apache APISIX。

多語言插件的執(zhí)行順序是在 ext-plugin-* 插件配置項中定義的,像其他插件一樣,它們可以被啟用并在運行中重新定義。

二、安裝:部署測試

基礎(chǔ)運行環(huán)境:Apache APISIX 2.7、Python 3.6+

Apache APISIX 的安裝部署可參考 Apache APISIX 官方文檔:如何構(gòu)建 Apache APISIX (https://github.com/apache/api...)進行部署。

1. 下載安裝 Python Runner

$ git clone https://github.com/apache/apisix-python-plugin-runner.git
$ cd apisix-python-plugin-runner
$ make install

2. 配置 Python Runner

  • 開發(fā)模式配置

運行 Python Runner:

    $ cd /path/to/apisix-python-plugin-runner
    $ APISIX_LISTEN_ADDRESS=unix:/tmp/runner.sock python3 apisix/main.py start
    修改 Apache APISIX 配置文件
    $ vim /path/to/apisix/conf/config.yaml
    apisix:
      admin_key:
     - name: "admin"
    key: edd1c9f034335f136f87ad84b625c8f1
    role: admin
    ext-plugin:
      path_for_test: /tmp/runner.sock
    
    
  • 生產(chǎn)模式配置

修改 Apache APISIX 配置文件

    $ vim /path/to/apisix/conf/config.yaml
    apisix:
      admin_key:
     - name: "admin"
    key: edd1c9f034335f136f87ad84b625c8f1
    role: admin
    ext-plugin:
      cmd: [ "python3", "/path/to/apisix-python-plugin-runner/apisix/main.py", "start" ]
    
    
  • Python Runner 配置(可選)

如果需要對 Log Level Unix Domain Socket 環(huán)境變量調(diào)整可以修改 Runner 的配置文件

$ vim /path/to/apisix-python-plugin-runner/apisix/config.yaml
socket:
  file: $env.APISIX_LISTEN_ADDRESS # Environment variable or absolute path
logging:
  level: debug # error warn info debug

3. 啟動 Python Runner

$ cd /path/to/apisix
# Start or Restart
$ ./bin/apisix [ start | restart ]

啟動或重啟 Apache APISIX 即可,此時 Apache APISIX Python Runner 已經(jīng)完成配置并啟動。

4. 測試 Python Runner

配置 Apache APISIX 路由及插件信息:

# 使用默認demo插件進行測試
$ curl http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
{
  "uri": "/get",
  "plugins": {
 "ext-plugin-pre-req": {
"conf": [
  { "name": "stop", "value":"{\"body\":\"hello\"}"}
]
 }
  },
  "upstream": {
  "type": "roundrobin",
  "nodes": {
"127.0.0.1:1980": 1
  }
 }
}'

  • plugins.ext-plugin-pre-req.confRunner 插件配置,conf 為數(shù)組格式可以同時設(shè)置多個插件。
  • 插件配置對象中 name 為插件名稱,該名稱需要與插件代碼文件和對象名稱一致。
  • 插件配置對象中 value 為插件配置,可以為 JSON 字符串。

訪問驗證:

$ curl http://127.0.0.1:9080/get -i
HTTP/1.1 200 OK
Date: Fri, 13 Aug 2021 13:39:18 GMT
Content-Type: text/plain; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
host: 127.0.0.1:9080
accept: */*
user-agent: curl/7.64.1
X-Resp-A6-Runner: Python
Server: APISIX/2.7
Hello, Python Runner of APISIX

三、實踐:插件開發(fā)

1. 插件目錄

/path/to/apisix-python-plugin-runner/apisix/plugins

此目錄中的 .py 文件將會被自動加載。

2. 插件示例

/path/to/apisix-python-plugin-runner/apisix/plugins/stop.py
/path/to/apisix-python-plugin-runner/apisix/plugins/rewrite.py

3. 插件格式

from apisix.runner.plugin.base import Base
from apisix.runner.http.request import Request
from apisix.runner.http.response import Response

class Stop(Base):
 def __init__(self):
  """
  Example of `stop` type plugin, features:
This type of plugin can customize response `body`, `header`, `http_code`
This type of plugin will interrupt the request
  """
  super(Stop, self).__init__(self.__class__.__name__)
 def filter(self, request: Request, response: Response):
  """
  The plugin executes the main function
  :param request:
request parameters and information
  :param response:
response parameters and information
  :return:
  """
  # 在插件中可以通過 `self.config` 獲取配置信息,如果插件配置為JSON將自動轉(zhuǎn)換為字典結(jié)構(gòu)
  # print(self.config)
  # 設(shè)置響應(yīng)頭信息
  headers = request.headers
  headers["X-Resp-A6-Runner"] = "Python"
  response.headers = headers
  # 設(shè)置響應(yīng)體信息
  response.body = "Hello, Python Runner of APISIX"
  # 設(shè)置響應(yīng)狀態(tài)碼
  response.status_code = 201
  # 通過調(diào)用 `self.stop()` 中斷請求流程,此時將立即響應(yīng)請求給客戶端
  # 如果未顯示調(diào)用 `self.stop()` 或 顯示調(diào)用 `self.rewrite()`將繼續(xù)將請求
  # 默認為 `self.rewrite()`
  self.stop()

4. 插件規(guī)范及注意事項

  • 實現(xiàn)插件對象必須繼承 Base
  • 插件必須實現(xiàn) filter 函數(shù)
  • filter 函數(shù)參數(shù)只能包含 Request Response 類對象作為參數(shù)
  • Request 對象參數(shù)可以獲取請求信息
  • Response 對象參數(shù)可以設(shè)置響應(yīng)信息
  • self.config 可以獲取插件配置信息
  • filter 函數(shù)中調(diào)用 self.stop() 時將馬上中斷請求,響應(yīng)數(shù)據(jù)。
  • filter 函數(shù)中調(diào)用 self.rewrite() 時,將會繼續(xù)請求。

到此這篇關(guān)于Python 帶你快速上手 Apache APISIX 插件開發(fā)的文章就介紹到這了,更多相關(guān)Python Apache APISIX 插件開發(fā)內(nèi)容請搜索本站以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持本站!

版權(quán)聲明:本站文章來源標注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請保持原文完整并注明來源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非maisonbaluchon.cn所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學(xué)習(xí)參考,不代表本站立場,如有內(nèi)容涉嫌侵權(quán),請聯(lián)系alex-e#qq.com處理。

相關(guān)文章

實時開通

自選配置、實時開通

免備案

全球線路精選!

全天候客戶服務(wù)

7x24全年不間斷在線

專屬顧問服務(wù)

1對1客戶咨詢顧問

在線
客服

在線客服:7*24小時在線

客服
熱線

400-630-3752
7*24小時客服服務(wù)熱線

關(guān)注
微信

關(guān)注官方微信
頂部