zoukankan      html  css  js  c++  java
  • 简单实现 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:UsersXHAnaconda3python.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:UsersXHAnaconda3libwsgirefhandlers.py", line 138, in run
        self.finish_response()
      File "C:UsersXHAnaconda3libwsgirefhandlers.py", line 181, in finish_response
        self.write(data)
      File "C:UsersXHAnaconda3libwsgirefhandlers.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:UsersXHAnaconda3libwsgirefhandlers.py", line 138, in run
        self.finish_response()
      File "C:UsersXHAnaconda3libwsgirefhandlers.py", line 181, in finish_response
        self.write(data)
      File "C:UsersXHAnaconda3libwsgirefhandlers.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:UsersXHAnaconda3libwsgirefhandlers.py", line 141, in run
        self.handle_error()
      File "C:UsersXHAnaconda3libwsgirefhandlers.py", line 369, in handle_error
        self.finish_response()
      File "C:UsersXHAnaconda3libwsgirefhandlers.py", line 181, in finish_response
        self.write(data)
      File "C:UsersXHAnaconda3libwsgirefhandlers.py", line 275, in write
        self.send_headers()
      File "C:UsersXHAnaconda3libwsgirefhandlers.py", line 332, in send_headers
        if not self.origin_server or self.client_is_modern():
      File "C:UsersXHAnaconda3libwsgirefhandlers.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:UsersXHAnaconda3libsocketserver.py", line 313, in _handle_request_noblock
        self.process_request(request, client_address)
      File "C:UsersXHAnaconda3libsocketserver.py", line 344, in process_request
        self.finish_request(request, client_address)
      File "C:UsersXHAnaconda3libsocketserver.py", line 357, in finish_request
        self.RequestHandlerClass(request, client_address, self)
      File "C:UsersXHAnaconda3libsocketserver.py", line 717, in __init__
        self.handle()
      File "C:UsersXHAnaconda3libwsgirefsimple_server.py", line 133, in handle
        handler.run(self.server.get_app())
      File "C:UsersXHAnaconda3libwsgirefhandlers.py", line 144, in run
        self.close()
      File "C:UsersXHAnaconda3libwsgirefsimple_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函数报错问题

  • 相关阅读:
    8-12-COMPETITION
    The Rotation Game(IDA*算法)
    Largest Rectangle in a Histogram(DP)
    Top ShooterHDU2863&&继续xxx定律HDU3784
    NYOJ 536 开心的mdd(DP)
    POJ1328 Radar Installation 图形转化 贪心 排序
    cout:格式算子与保留精度
    POJ 2109 Power of Cryptography二分+大数相乘和pow为什么可以直接过的原因
    字符串交换函数
    纪念一次失败的暴力 统计回文字串
  • 原文地址:https://www.cnblogs.com/lovebkj/p/12769524.html
Copyright © 2011-2022 走看看