使用RPC和内部路由构建一个动态代理

正在进行中 (要求uWSGI 1.9.14,我们使用PyPy作为引擎)

第一步:构建你的映射函数

使用hostname作为映射 (你可以使用任何你需要的)

  1. import uwsgi
  2.  
  3. def my_mapper(hostname):
  4. return "127.0.0.1:3031"
  5.  
  6. uwsgi.register_rpc('the_mapper', my_mapper)

将其保持为myfuncs.py

第二步:构建一个路由表

  1. [uwsgi]
  2. ; enable the pypy engine
  3. pypy-home = /opt/pypy
  4. ; execute the myfuncs.py file (the 'the_mapper' rpc function will be registered)
  5. pypy-exec = myfuncs.py
  6.  
  7. ; bind to a port
  8. http-socket = :9090
  9.  
  10. ; let's define our routing table
  11.  
  12. ; at every request (route-run execute the action without making check, use it instead of --route .*) run the_mapper passing HTTP_HOST as argument
  13. ; and place the result in the MYNODE variable
  14. route-run = rpcvar:MYNODE the_mapper ${HTTP_HOST}
  15. ; print the MYNODE variable (just for fun)
  16. route-run = log:${MYNODE}
  17. ; proxy the request to the chosen backend node
  18. route-run = http:${MYNODE}
  19.  
  20. ; enable offloading for automagic non-blocking behaviour
  21. ; a good value for offloading is the number of cpu cores
  22. offload-threads = 2