使用django项目进行开发,可以在setting文件中直接写死指定的数据库,但是一旦项目需要上线就需要切换相应的配置,如果每次本地调试提交代码手动去修改配置的话就特别麻烦,可能有时候不注意还会搞忘了(亲身经历),这里可以读取yaml文件,根据不同的命令指定切换不同的配置,这里以切换mysql数据库为例切换不同的数据库,当然这套配置不仅限与python开发,其他语言也是一样的
这是项目目录
这里需要首先安装pyyaml(可以写在requirement.txt)
pip install pyyaml -i https://pypi.douban.com/simple --trusted-host pypi.douban.com
首先可以在__init__文件中写配置,根据debug或者product切换不同文件下的配置
import logging
import os
import sys
import yaml
def get_env():
env = os.environ.get("ENV")
if env is None:
env = "debug"
return env
def load_config():
env = get_env()
path = f"config_{env}.yaml"
with open(path, encoding='utf-8') as f:
cfg = yaml.load(f.read(), Loader=yaml.FullLoader)
print(f"当前运行环境:{env}")
return cfg
class Config(object):
def __init__(self, cfg: str):
self.cfg_type = cfg
self.config = load_config()
def get_config(self, key: str):
return self.config[self.cfg_type][key]
mysql_cfg = Config("MYSQL")
django的配置文件setting
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'HOST': mysql_cfg.get_config("HOST"),
'PORT': mysql_cfg.get_config("PORT"),
'USER': mysql_cfg.get_config("USER"),
'PASSWORD': mysql_cfg.get_config("PASSWORD"),
'NAME': mysql_cfg.get_config("NAME")
},
}
config_debug.yaml
MYSQL:
HOST: '127.0.0.1'
PORT: 3306
USER: '用户名'
PASSWORD: '密码'
NAME: 'ordermanagement_test'
config_product.yaml
MYSQL:
HOST: '127.0.0.1'
PORT: 3306
USER: '用户名'
PASSWORD: '密码'
NAME: 'ordermanagement_test1'
Dockerfile
FROM python:3.6
WORKDIR /app
COPY requirements.txt requirements.txt
RUN pip install -U pip -i https://pypi.tuna.tsinghua.edu.cn/simple/
RUN pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple/
EXPOSE 8000
docker-compose.debug.yml
version: '3.8'
services:
ordermanagement:
image: ordermanagement:1.0
restart: always
environment:
- ENV=debug
container_name: "compose-ordermanagement-debug"
volumes:
- "./:/app:rw"
- "/home/trainer/workshop/ordermanagement/statics:/app/statics"
command:
- /bin/sh
- -c
- |
cd /app && python manage.py runserver 0.0.0.0:8000
ports:
- "8999:8000"
networks:
- app_net
networks:
app_net:
docker-compose.product.yml
version: '3.8'
services:
ordermanagement:
image: ordermanagement:1.0
restart: always
environment:
- ENV=product
container_name: "compose-ordermanagement-product"
volumes:
- "./:/app:rw"
- "/home/trainer/workshop/ordermanagement/statics:/app/statics"
command:
- /bin/sh
- -c
- |
cd /app && python manage.py runserver 0.0.0.0:8000
ports:
- "8999:8000"
networks:
- app_net
tty: true
networks:
app_net:
另外我这里使用了Makefile去简化命令,在Makefile文件中指定执行不同的yml文件去切换配置
build:
sudo docker build -t ordermanagement:1.0 .
d.exec:
sudo docker exec -it compose-ordermanagement-debug bash
d.log:
sudo docker logs -f -n 100 compose-ordermanagement-debug
d.up:
sudo docker compose -f docker-compose.debug.yml up -d
d.ps:
sudo docker compose -f docker-compose.debug.yml ps
d.down:
sudo docker compose -f docker-compose.debug.yml down
p.exec:
sudo docker exec -it compose-ordermanagement-product bash
p.log:
sudo docker logs -f -n 100 compose-ordermanagement-product
p.up:
sudo docker compose -f docker-compose.product.yml up -d
p.down:
sudo docker compose -f docker-compose.product.yml down
p.ps:
sudo docker compose -f docker-compose.product.yml ps
比如我现在要在开发服务器使用docker-compose部署
make build
make d.up
make d.down
生产服务器
make build
make p.up
make p.down
顺便提一下,python 开发者使用pycharm启动项目的话,可以设置一下环境变量用以切换
以上便是我日常开发所使用的切换配置的方法,如果有其他好的方法可以留言上链接以便学习一下,谢谢!
|