我在负载均衡器后面有两台服务器。如果在一台服务器上运行PUT或POST事务,则另一台服务器将无法查询最新数据,即
请求1:将车辆名称(ID为2)从Car 001更改为Car 101。
请求2:获取id 2的车辆名称,它将显示Car 001直到几分钟,然后它将变为Car 101。
我的文件夹结构如下:
以下是模型中init.py的代码:
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_marshmallow import Marshmallow
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = "url"
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.config['SQLALCHEMY_POOL_RECYCLE'] = 5
app.config['SQLALCHEMY_POOL_SIZE'] = 10
db = SQLAlchemy(app)
ma = Marshmallow(app)
这是车型:
import arrow, json, uuid
import jwt
from datetime import datetime
import datetime as dt
from functools import wraps
from api.models import db, ma
from api.titan.v1.models.vehicle_state import VehicleState
class Vehicle(db.Model):
id = db.Column(db.Integer(), primary_key=True)
name = db.Column(db.String(300), index=True)
createdAt = db.Column(db.DateTime)
plateNumber = db.Column(db.String(300))
这是车辆请求处理:
import arrow, json, uuid, pytz
import requests
from datetime import datetime
from flask_restful import reqparse, abort, Api, Resource
from werkzeug.exceptions import BadRequest
from flask import request, jsonify
from api.models import db
class Vehicles(Resource):
def get(self, vehicle_id=None):
response_object = {
'object': 'vehicle',
'verb': 'GET',
'timestamp': arrow.utcnow().timestamp
}
if vehicle_id is None:
vehicles = db.session.query(Vehicle).filter_by(companyId=user['companyId']).all()
response_object['data'] = [schema.dump(i).data for i in vehicles]
else:
vehicle = db.session.query(Vehicle).filter_by(id=vehicle_id, companyId=user['companyId']).first()
response_object['data'] = schema.dump(vehicle).data
return response_object, 200
def put(self, vehicle_id):
data = request.get_json()
response_object = {
'object': 'vehicle',
'verb': 'PUT',
'timestamp': arrow.utcnow().timestamp,
}
vehicle = db.session.query(Vehicle).filter_by(id=vehicle_id).first()
vehicle.name = data['name']
db.session.commit()
response_object['data'] = vehicle
return response_object, 200