简单实现 web/app端 经API GateWay 将请求转发至微服务,并将数据返回给client客户端

博观而约取,厚积而薄发。这篇文章主要讲述简单实现 web/app端 经API GateWay 将请求转发至微服务,并将数据返回给client客户端相关的知识,希望能为你提供帮助。
简单实现 web/app端 经API GateWay 将请求转发至微服务,并将数据返回给client客户端。
环境:Windows10,Linux虚拟机,postman,python3.7
【简单实现 web/app端 经API GateWay 将请求转发至微服务,并将数据返回给client客户端】postman是在本地Windows系统上请求,API GateWay代码是运行在本地window上的,一个简单的微服务在Linux上运行。
详情见下:
1、API GateWay 部分代码:

# server.py # 从wsgiref模块导入: from wsgiref.simple_server import make_server # 导入我们自己编写的application函数: from hello import application# 创建一个服务器,IP地址为空,端口是8000,处理函数是application: httpd = make_server(\'\', 8000, application) print(\'Serving HTTP on port 8000...\') # 开始监听HTTP请求: httpd.serve_forever()

# hello.py import json import requestsdef application(environ, start_response): start_response(\'200 OK\', [(\'Content-Type\', \'application/json\')]) # start_response(\'200 OK\', [(\'Content-Type\', \'text/html\')]) # return [b\'< h1> Hello, web!< /h1> \'] print(environ[\'PATH_INFO\']) url = environ[\'PATH_INFO\']# web客户端请求 API网关的 url params = { "name": "john", "age": 16, "address": "地址", "salary": "200" } resp = req(url, params=params) print(\'真实服务器返回给api网关的内容:\', resp, type(resp)) return [json.dumps(resp)] # return [b"{\'a\': 1, \'b\': 2}"]# 模拟请求真实的服务 def req(url, params=None): response = requests.post(\'http://192.168.175.134:6666\' + url, json=params) return response.json()

2、微服务代码:
简单实现 web/app端 经API GateWay 将请求转发至微服务,并将数据返回给client客户端

文章图片

3、web/APP端请求,通过postman模拟:
简单实现 web/app端 经API GateWay 将请求转发至微服务,并将数据返回给client客户端

文章图片

测试过程中遇到的错误:
C:\\Users\\XH\\Anaconda3\\python.exe D:/dev/dev_py/library/api_gateway_server/server.py Serving HTTP on port 8000... /api/ssss 真实服务器返回给api网关的内容: {\'a\': 1, \'b\': 2} < class \'dict\'> 127.0.0.1 - - [24/Apr/2020 19:18:41] "POST /api/ssss HTTP/1.1" 200 0 Traceback (most recent call last): File "C:\\Users\\XH\\Anaconda3\\lib\\wsgiref\\handlers.py", line 138, in run self.finish_response() File "C:\\Users\\XH\\Anaconda3\\lib\\wsgiref\\handlers.py", line 181, in finish_response self.write(data) File "C:\\Users\\XH\\Anaconda3\\lib\\wsgiref\\handlers.py", line 267, in write "write() argument must be a bytes instance" AssertionError: write() argument must be a bytes instance 127.0.0.1 - - [24/Apr/2020 19:18:41] "POST /api/ssss HTTP/1.1" 500 59 ---------------------------------------- Exception happened during processing of request from (\'127.0.0.1\', 55306) Traceback (most recent call last): File "C:\\Users\\XH\\Anaconda3\\lib\\wsgiref\\handlers.py", line 138, in run self.finish_response() File "C:\\Users\\XH\\Anaconda3\\lib\\wsgiref\\handlers.py", line 181, in finish_response self.write(data) File "C:\\Users\\XH\\Anaconda3\\lib\\wsgiref\\handlers.py", line 267, in write "write() argument must be a bytes instance" AssertionError: write() argument must be a bytes instanceDuring handling of the above exception, another exception occurred:Traceback (most recent call last): File "C:\\Users\\XH\\Anaconda3\\lib\\wsgiref\\handlers.py", line 141, in run self.handle_error() File "C:\\Users\\XH\\Anaconda3\\lib\\wsgiref\\handlers.py", line 369, in handle_error self.finish_response() File "C:\\Users\\XH\\Anaconda3\\lib\\wsgiref\\handlers.py", line 181, in finish_response self.write(data) File "C:\\Users\\XH\\Anaconda3\\lib\\wsgiref\\handlers.py", line 275, in write self.send_headers() File "C:\\Users\\XH\\Anaconda3\\lib\\wsgiref\\handlers.py", line 332, in send_headers if not self.origin_server or self.client_is_modern(): File "C:\\Users\\XH\\Anaconda3\\lib\\wsgiref\\handlers.py", line 345, in client_is_modern return self.environ[\'SERVER_PROTOCOL\'].upper() != \'HTTP/0.9\' TypeError: \'NoneType\' object is not subscriptableDuring handling of the above exception, another exception occurred:Traceback (most recent call last): File "C:\\Users\\XH\\Anaconda3\\lib\\socketserver.py", line 313, in _handle_request_noblock self.process_request(request, client_address) File "C:\\Users\\XH\\Anaconda3\\lib\\socketserver.py", line 344, in process_request self.finish_request(request, client_address) File "C:\\Users\\XH\\Anaconda3\\lib\\socketserver.py", line 357, in finish_request self.RequestHandlerClass(request, client_address, self) File "C:\\Users\\XH\\Anaconda3\\lib\\socketserver.py", line 717, in __init__ self.handle() File "C:\\Users\\XH\\Anaconda3\\lib\\wsgiref\\simple_server.py", line 133, in handle handler.run(self.server.get_app()) File "C:\\Users\\XH\\Anaconda3\\lib\\wsgiref\\handlers.py", line 144, in run self.close() File "C:\\Users\\XH\\Anaconda3\\lib\\wsgiref\\simple_server.py", line 35, in close self.status.split(\' \',1)[0], self.bytes_sent AttributeError: \'NoneType\' object has no attribute \'split\' ----------------------------------------

解决:
简单实现 web/app端 经API GateWay 将请求转发至微服务,并将数据返回给client客户端

文章图片

经测试,路子可以走通。
简单实现 web/app端 经API GateWay 将请求转发至微服务,并将数据返回给client客户端

文章图片

以上。
参考:廖雪峰的WSGI接口,博客园二流子的python3.4中自定义wsgi函数,make_server函数报错问题。

    推荐阅读