簡單實現 web/app端 經API GateWay 將請求轉發至微服務,並將數據返回給client客戶端


簡單實現 web/app端 經API GateWay 將請求轉發至微服務,並將數據返回給client客戶端。

環境:Windows10,Linux虛擬機,postman,Python3.7

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 requests


def 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、微服務代碼:

3、web/APP端請求,通過postman模擬:

測試過程中遇到的錯誤:

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 instance

During 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 subscriptable

During 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'
----------------------------------------

解決:

經測試,路子可以走通。

以上。

參考:廖雪峰的WSGI接口,博客園二流子的python3.4中自定義wsgi函數,make_server函數報錯問題


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM