欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

在Python的框架中为MySQL实现restful接口的教程

程序员文章站 2022-06-19 19:22:38
最近在做游戏服务分层的时候,一直想把mysql的访问独立成一个单独的服务dbgate,原因如下:     请求收拢到dbgate,可...

最近在做游戏服务分层的时候,一直想把mysql的访问独立成一个单独的服务dbgate,原因如下:

  1.     请求收拢到dbgate,可以使dbgate变为无状态的,方便横向扩展
  2.     当请求量或者存储量变大时,mysql需要做分库分表,dbgate可以内部直接处理,外界无感知
  3.     通过restful限制对数据请求的形式,仅支持简单的get/post/patch/put 进行增删改查,并不支持复杂查询。这个也是和游戏业务的特性有关,如果网站等需要复杂查询的业务,对此并不适合
  4.     dbgate使用多进程模式,方便控制与mysql之间的链接数,进行mysql访问量阀值保护
  5.     方便在dbgate上进行访问量统计,慢查询统计、权限控制等等一系列逻辑
  6.     目前是使用python,以后要使用其他语言进行mysql操作时,只要进行标准的http请求即可,不会出现不兼容的情况

当然坏处也是有的:

  1.     首当其冲就是单次请求的响应时间变长,毕竟中间加了一层服务,并且还是http格式
  2.     部署上比原来复杂了一些,很多对mysql直接操作的思维需要进行转变,一开始可能会有些不适

不过总的来说,还是利大于弊,所以最终还是决定搭建dbgate

当然,我们不可能去手工挨个写每个库表对应的restful服务,值得庆幸的是django和flask都提供了对应的解决方案,我们一个个介绍.
flask

参考链接:

flask-restless使用方法比较简单,我直接贴一下代码即可:

# -*- coding: utf-8 -*-

import datetime
from flask import flask
from flask_sqlalchemy import sqlalchemy
from flask_restless import apimanager


app = flask(__name__)
db = sqlalchemy(app)
restless = apimanager(app, flask_sqlalchemy_db=db)


class user(db.model):
    """
    user
    """

    id = db.column(db.integer, primary_key=true)
    username = db.column(db.string(255), unique=true, nullable=false)
    password = db.column(db.string(255), nullable=false)
    create_time = db.column(db.datetime, nullable=false, default=datetime.datetime.utcnow)
    login_time = db.column(db.datetime)


restless.create_api(user, methods=['get', 'post', 'delete', 'patch', 'put'], results_per_page=100)

db.create_all()

if __name__ == '__main__':
    app.run(port=25000)
 
# -*- coding: utf-8 -*-
 
import datetime
from flask import flask
from flask_sqlalchemy import sqlalchemy
from flask_restless import apimanager
 
 
app = flask(__name__)
db = sqlalchemy(app)
restless = apimanager(app, flask_sqlalchemy_db=db)
 
 
class user(db.model):
    """
    user
    """
 
    id = db.column(db.integer, primary_key=true)
    username = db.column(db.string(255), unique=true, nullable=false)
    password = db.column(db.string(255), nullable=false)
    create_time = db.column(db.datetime, nullable=false, default=datetime.datetime.utcnow)
    login_time = db.column(db.datetime)
 
 
restless.create_api(user, methods=['get', 'post', 'delete', 'patch', 'put'], results_per_page=100)
 
db.create_all()
 
if __name__ == '__main__':
    app.run(port=25000)

其对应的restful操作如下:

获取用户列表:   get /user
添加用户:       post /user
获取单个用户:   get /user/1
覆盖单个用户:   put /user/1
修改单个用户:   patch /user/1

获取用户列表:   get /user
添加用户:       post /user
获取单个用户:   get /user/1
覆盖单个用户:   put /user/1
修改单个用户:   patch /user/1

注意:

  •     在http请求中,记得加入header: content-type: application/json
  •     flask-restless中,put和patch一样,都是传入什么字段,只修改什么字段,不会完全覆盖

django

参考链接: django rest framework

django用起来要更复杂一些,也因为django版自带了一个可视化的操作页面,如下:

在Python的框架中为MySQL实现restful接口的教程

1. 在settings中添加:

rest_framework = {
  # use hyperlinked styles by default.
  # only used if the `serializer_class` attribute is not set on a view.
  'default_model_serializer_class':
    'rest_framework.serializers.hyperlinkedmodelserializer',

  # use django's standard `django.contrib.auth` permissions,
  # or allow read-only access for unauthenticated users.
  'default_permission_classes': [
    #'rest_framework.permissions.djangomodelpermissionsoranonreadonly',
    'rest_framework.permissions.isadminuser',
  ]
}
 
rest_framework = {
  # use hyperlinked styles by default.
  # only used if the `serializer_class` attribute is not set on a view.
  'default_model_serializer_class':
    'rest_framework.serializers.hyperlinkedmodelserializer',
 
  # use django's standard `django.contrib.auth` permissions,
  # or allow read-only access for unauthenticated users.
  'default_permission_classes': [
    #'rest_framework.permissions.djangomodelpermissionsoranonreadonly',
    'rest_framework.permissions.isadminuser',
  ]
}

2. 通过startapp建立一个app: demo
3. 修改demo的models:

class user(models.model):
  # key是保留字
  password = models.integerfield()
  nick = models.charfield(max_length=255)
  create_time = models.datetimefield(default=datetime.datetime.now)
 
class user(models.model):
  # key是保留字
  password = models.integerfield()
  nick = models.charfield(max_length=255)
  create_time = models.datetimefield(default=datetime.datetime.now)

4. 在demo下新建serializers.py

from rest_framework import serializers
from models import user

class userserializer(serializers.modelserializer): class meta: model = user from rest_framework import serializers from models import user class userserializer(serializers.modelserializer): class meta: model = user


5. 在demo下修改views.py

from django.shortcuts import render
from rest_framework import viewsets

from serializers import userserializer
from models import user


class userviewset(viewsets.modelviewset):
  queryset = user.objects.all()
  serializer_class = userserializer
 
from django.shortcuts import render
from rest_framework import viewsets
 
from serializers import userserializer
from models import user
 
 
class userviewset(viewsets.modelviewset):
  queryset = user.objects.all()
  serializer_class = userserializer

6. 在demo下新建urls.py

import os.path
from django.conf.urls import patterns, include, url
from django.conf.urls.static import static
from django.conf import settings
import views

from rest_framework import routers

appname = os.path.basename(os.path.dirname(os.path.abspath(__file__)))

router = routers.defaultrouter()
router.register('users', views.userviewset, appname)

urlpatterns = patterns('',
            url(r'^', include(router.urls)),
)
 
import os.path
from django.conf.urls import patterns, include, url
from django.conf.urls.static import static
from django.conf import settings
import views
 
from rest_framework import routers
 
appname = os.path.basename(os.path.dirname(os.path.abspath(__file__)))
 
router = routers.defaultrouter()
router.register('users', views.userviewset, appname)
 
urlpatterns = patterns('',
            url(r'^', include(router.urls)),
)

7. 在mysite.urls下include demo.urls和rest_framework.urls

urlpatterns = patterns('',
  url(r'^demo/', include('demo.urls')),
  url(r'^admin/', include(admin.site.urls)),
  url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework'))
)
 
urlpatterns = patterns('',
  url(r'^demo/', include('demo.urls')),
  url(r'^admin/', include(admin.site.urls)),
  url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework'))
)

8. 执行初始化数据操作:

python manage.py syncdb
 
python manage.py syncdb

之后访问: http://127.0.0.1:8000/demo 即可看到如下界面了:

在Python的框架中为MySQL实现restful接口的教程

对应的测试代码如下:

import json
import requests
from urlparse import urljoin

base_url = 'http://127.0.0.1:16500/'
auth = ('admin', 'admin')


def test_get_user_list():
  rsp = requests.get(urljoin(base_url, '/demo/users/'), auth=auth, headers={
    'accept': 'application/json'
  })
  assert rsp.ok


def test_post_user_list():
  json_data = dict(
    password=0,
    nick='oo',
    create_time='2014-03-3t03:3:3'
  )
  rsp = requests.post(urljoin(base_url, '/demo/users/'), auth=auth, headers={
    'accept': 'application/json',
    'content-type': 'application/json',
  }, data=json.dumps(json_data))
  assert rsp.ok


def test_get_user():
  rsp = requests.get(urljoin(base_url, '/demo/users/1'), auth=auth, headers={
    'accept': 'application/json',
    'content-type': 'application/json',
  })
  assert rsp.ok


def test_put_user():
  json_data = dict(
    password=100,
    nick='xx',
    create_time='2014-03-3t03:3:3'
  )
  # 注意最后的 /
  rsp = requests.put(urljoin(base_url, '/demo/users/1/'), auth=auth, headers={
    'accept': 'application/json',
    'content-type': 'application/json',
    }, data=json.dumps(json_data),
  )
  assert rsp.ok, rsp.status_code

django rest framework 是严格区分put和patch的,这一点和flask-restless 不一样,需要注意。

ok,就这样。