使用python的第三方jwt作鑑權服務, 生產token代碼:python
def create_token(self, userId, product, level): payload = { "product": product, "level": level, "exp": int(time.time()) + 86400 * 7, "username": userId, } token = jwt.encode(payload, SECRETKEY, algorithm='HS256') return token
mongodb版本是3.6,數據庫操做使用了pymongo;使用了自定義的對象倉儲,對比直接操做數據格式自己,這一點確定是拖性能後退的
鑑權句柄的實現:web
class AuthHandle(RequestHandler): def post(self): try: body = json.loads(self.request.body) user = UserRepository().get(body['username']) if not user: user = UserRepository().create_new(body['username']) token = Authenticationner().create_token(user.userId, user.product, user.level) self.write({'token': token}) except Exception: Logger.error(traceback.format_exc())
tornado作web服務和路由轉發:mongodb
class Application(Application): def __init__(self): handlers = [ (r"/users/login", AuthHandle), ] super(Application, self).__init__(handlers, **settings) if __name__ == "__main__": application = Application() application.listen(address.get("port"), address.get("ip")) tornado.ioloop.IOLoop.instance().start()
使用cenos環境,雙核,8G內存,沒有反向代理和緩存的前提下,性能表現以下數據庫
使用jmeter作200併發壓力,結果以下:json
最大時延4s,TPS達到39.6/s,感受仍是很理想的緩存
from tornado.options import options, define application = Application() define("port", default=address.get("port"), help="", type=int) http_server = tornado.httpserver.HTTPServer(application) http_server.bind(options.port, address.get("ip")) http_server.start(0) # 進程數量等於機器核數量 tornado.ioloop.IOLoop.instance().start()
性能有明顯提高:性能優化
最大時延484ms,TPS達到了126併發