In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article will explain in detail how to use the Apache APISIX plug-in in Python. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.
Foreword:
As an interpretive high-level programming language, Python has simple syntax, easy to use, good readability of code, and good performance in cross-platform, portability and development efficiency. At the same time, as a high-level programming language, its high degree of encapsulation and abstraction shields many underlying details (for example: GC) so that we can pay more attention to the development of application logic in the development process.
At the same time, as a 30-year-old development language, its ecology and various modules have been very perfect, and most of our development and application scenarios can find very mature modules or solutions from the community.
Python's other advantages are no longer discussed one by one, of course, its disadvantages are more obvious: as an interpretive language, Python has a large performance gap compared with compiled languages such as C++ and Go.
First, understand: project architecture
Apache-apisix-python-runner project can be understood as a bridge between Apache APISIX and Python. Python can be directly applied to Apache APISIX plug-in development through Python Runner. The most important thing is to let more Python developers who are interested in Apache APISIX and API gateway know more about and use Apache APISIX through this project. The following is the architecture diagram supported by Apache APISIX in multiple languages.
The above figure shows the workflow of Apache APISIX on the left, Plugin Runner on the right is the plug-in runner for each language, and the apisix-python-plugin-runner introduced in this article is the Plugin Runner that supports the Python language.
When configuring a Plugin Runner in Apache APISIX, Apache APISIX will start a child process to run Plugin Runner, which belongs to the same user as the Apache APISIX process, and when we restart or reload Apache APISIX, Plugin Runner will also be restarted.
If you configure the ext-plugin-* plug-in for a given route, the request to hit that route will trigger Apache APISIX to initiate a RPC call to Plugin Runner through Unix Socket. The call is divided into two phases:
Ext-plugin-pre-req: before executing the Apache APISIX built-in plug-in (Lua language plug-in)
Ext-plugin-post-req: after executing the Apache APISIX built-in plug-in (Lua language plug-in)
You can choose and configure the timing of Plugin Runner execution according to your needs. Plugin Runner handles the RPC call, creates a mock request inside it, runs the plug-in written in multiple languages, and returns the result to Apache APISIX.
The execution order of multilingual plug-ins is defined in the ext-plugin-* plug-in configuration item and, like other plug-ins, they can be enabled and redefined at run time.
II. Installation: deployment testing
Basic operating environment: Apache APISIX 2.7, Python 3.6 +
For the installation and deployment of Apache APISIX, please refer to the official Apache APISIX documentation: how to build Apache APISIX (https://github.com/apache/api...) To deploy.
1. Download and install Python Runner$ git clone https://github.com/apache/apisix-python-plugin-runner.git$ cd apisix-python-plugin-runner$ make install2. Configure Python Runner
Development mode configuration
Run Python Runner:
$cd / path/to/apisix-python-plugin-runner$ APISIX_LISTEN_ADDRESS=unix:/tmp/runner.sock python3 apisix/main.py start modify the Apache APISIX configuration file $vim / path/to/apisix/conf/config.yamlapisix: admin_key:-name: "admin" key: edd1c9f034335f136f87ad84b625c8f1 role: adminext-plugin: path_for_test: / tmp/runner.sock
Production mode configuration
Modify Apache APISIX configuration file
$vim / path/to/apisix/conf/config.yamlapisix: admin_key:-name: "admin" key: edd1c9f034335f136f87ad84b625c8f1 role: adminext-plugin: cmd: ["python3", "/ path/to/apisix-python-plugin-runner/apisix/main.py", "start"]
Python Runner configuration (optional)
If you need to adjust the Log Level or Unix Domain Socket environment variables, you can modify the Runner configuration file.
$vim / path/to/apisix-python-plugin-runner/apisix/config.yamlsocket: file: $env.APISIX_LISTEN_ADDRESS # Environment variable or absolute pathlogging: level: debug # error warn info debug3. Start Python Runner$ cd / path/to/apisix# Start or Restart$. / bin/apisix [start | restart]
Just start or restart Apache APISIX when Apache APISIX and Python Runner have been configured and started.
4. Test Python Runner
Configure Apache APISIX routing and plug-in information:
# use the default demo plug-in to test $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 upstream 1980 ": 1}'
Plugins.ext-plugin-pre-req.conf is configured for Runner plug-ins, and conf for array format can set multiple plug-ins at the same time.
The name in the plug-in configuration object is the plug-in name, which needs to be consistent with the plug-in code file and the object name.
The value in the plug-in configuration object is the plug-in configuration and can be a JSON string.
Access authentication:
$curl http://127.0.0.1:9080/get-iHTTP/1.1 200 OKDate: Fri, 13 Aug 2021 13:39:18 GMTContent-Type: text/plain; charset=utf-8Transfer-Encoding: chunkedConnection: keep-alivehost: 127.0.0.1:9080accept: * / * user-agent: curl/7.64.1X-Resp-A6-Runner: PythonServer: APISIX/2.7Hello, Python Runner of APISIX III, practice: plug-in development 1. Plug-in directory / path/to/apisix-python-plugin-runner/apisix/plugins
The .py files in this directory will be loaded automatically.
two。 Plug-in example / path/to/apisix-python-plugin-runner/apisix/plugins/stop.py/path/to/apisix-python-plugin-runner/apisix/plugins/rewrite.py3. Plug-in format from apisix.runner.plugin.base import Basefrom apisix.runner.http.request import Requestfrom apisix.runner.http.response import Responseclass Stop (Base): def _ _ init__ (self): "Example of `body`, 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:" # you can obtain configuration information through `self.co nfig` in the plug-in If the plug-in is configured as JSON, it will automatically convert to dictionary structure # print (self.config) # set response header information headers = request.headers headers ["X-Resp-A6-Runner"] = "Python" response.headers = headers # set response body information response.body = "Hello Python Runner of APISIX "# set response status code response.status_code = 201# interrupt the request process by calling `self.stop () ` At this point, the client will immediately respond to the request # if the call `self.rewrite () `is not displayed or the call `self.rewrite ()` is displayed, the request # will be defaulted to `self.rewrite () `self.stop () 4. Plug-in specifications and precautions
The implementation plug-in object must inherit the Base class
Plug-ins must implement the filter function
Filter function parameters can only contain Request and Response class objects as arguments
The Request object parameter can get the request information
The Response object parameter can set the response information
Self.config can get plug-in configuration information
When self.stop () is called in the filter function, the request is immediately interrupted to respond to the data.
When self.rewrite () is called in the filter function, the request will continue.
On "how to use the Apache APISIX plug-in in Python" this article is shared here, I hope the above content can be of some help to you, so that you can learn more knowledge, if you think the article is good, please share it out for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.