Django服务健康检查模块配置
# 进入项目目录,激活python 虚拟环境
source py3/bin/activate
# 安装模块
pip install django-health-check
# 修改django配置,INSTALLED_APPS模块添加
vim webserver/webserver/settings.py
'health_check',
'health_check.db',
'health_check.storage',
# 添加路径,更新配置
vim webserver/webserver/urls.py
url(r'^task/', include(('task.urls', 'task'), namespace='task')),
url(r'^ht/', include('health_check.urls')),
# 安装支持包
python -m pip install --upgrade djangorestframework
python -m pip install --upgrade django-cors-headers
# 更新 requirements.txt
pip freeze > requirements.txt
# 更新配置文件
vim task_webserver/settings.py
CORS_ORIGIN_WHITELIST = (
'http://127.0.0.1:8080',
'http://localhost:8080', #凡是出现在白名单中的域名,都可以访问后端接口
)
# 更新数据库
python manage.py makemigrations
python manage.py migrate
# 更新nginx 配置
location ^~ /ht/ {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://webserver/ht/;
}
# 启动服务,测试
Django 1.11 升级为 Django 3.2.4
# 升级后安装数据库支持
pip3 install mysqlclient
# 报错
/usr/bin/ld: 找不到 -lmariadb
# 软件升级
yum install MariaDB-shared
# 修改配置
vim gamestore/settings.py
DEFAULT_AUTO_FIELD='django.db.models.AutoField'
# 更新数据表结构
python manage.py migrate
# centos7.9 mysql5.6版本执行
yum install mysql-devel gcc gcc-devel python-devel
Django使用loguru 记录日志
# setting.py 关闭默认日志记录器
LOGGING_CONFIG=None
# 新日志模块
from loguru import logger
logger.add("/var/log/gamestore/order.log", retention='10 days', level="DEBUG")
# 需要记录日志的文件,import 直接使用
from loguru import logger
网站性能分析
通过听云 python 探针统计数据
# 安装听云
source py3/bin/activate
pip install tingyun
# 更新 requirements.txt
pip freeze > requirements.txt
# 配置
tingyun-admin generate-config f4993d3e7d5622e553131b262f650e4d support/tingyun.conf
# 启动
TING_YUN_CONFIG_FILE=../support/tingyun.conf tingyun-admin run-program python manage.py runserver 127.0.0.1:8013
通过newrelic 分析网站性能
pip install newrelic
# 下载配置文件
newrelic.ini
# 手动启动
NEW_RELIC_CONFIG_FILE=../support/newrelic.ini newrelic-admin run-program python manage.py runserver 127.0.0.1:8013
根据已有数据库生成模型类
# price_info 为表名,不加默认生成所有
python manage.py inspectdb price_info
评论 (0)