通过restful api申请mysql、redis资源
- Centos 64位
- Python 3.6
- PIP 3
- Docker 19.03.4
- Mysql 5.7
- Mysql 8.0
- Redis 5.0
- Flask 2.0.1
- Flask-RESTful 0.3.9
- PyMySQL 0.9.3
- 安装依赖库
# 安装python依赖(推荐使用pyenv虚拟环境)
pip3 install -r requirements.txt
# 安装docker 镜像(mysql、redis)
docker pull mysql:8.0
docker pull redis:5.0
- 克隆仓库
git clone https://github.com/ansionfor/python_docker.git
3、修改mysql配置(config/mysq.ini)
[mysql]
host=127.0.0.1
user=root
pwd=root
db=docker_demo
4、导入sql文件
source init.sql
5、启动
python3 main.py
├── api # 接口目录
│ ├── Machine.py
│ ├── MysqlMachine.py
│ └── RedisMachine.py
├── common # 公共库
│ ├── Auth.py # 鉴权类
│ ├── MysqlHandle.py # mysql操作类
├── config # 配置目录
│ └── mysql.ini
|── service # 逻辑层
| └── DockerSvr.py
├── init.sql # 初始化sql
├── main.py # 入口文件
- 自动生成端口、端口冲突检测
- 自动生成账号密码
- 并发安全创建实例
- 代码分层,架构清晰
每个请求需要在header带上Authorization信息,现有三个测试用户user1,user2,user3
eg.
curl -X GET -H "Authorization:user1" 127.0.0.1:5000/list/1
- Method: POST
- URL:
/mysql
- Headers: Content-Type:application/json
- Body:
{
"charset":"utf8",
"collection":"utf8_general_ci"
}
- Body
{
"code":200,
"msg":"success",
"data":{
"host":"127.0.0.1",
"user":"root",
"pwd":56903401,
"port":2001,
"dockerId": "6aecdcdf980b"
}
}
eg.
curl -X POST -H "Authorization:user1" -H "Content-Type:application/json" 127.0.0.1:5000/mysql -d '{"charset":"utf8"}'
- Method: POST
- URL:
/redis
- Headers: Content-Type:application/json
- Body:
{
"memory":500 # 容器最大内存(M)
}
- Body
{
"code":200,
"msg":"success",
"data":{
"host":"127.0.0.1",
"pwd":56903401,
"port":2002,
"dockerId": "6aecdcdf980b"
}
}
eg.
curl -X POST -H "Authorization:user1" -H "Content-Type:application/json" 127.0.0.1:5000/redis -d '{"memory":"400"}'
- Method: GET
- URL:
/list/<int:dockerType>
# dockerType:mysql 1,redis 2
- Body
{
"code":200,
"msg":"success",
"data":[
{
"pwd":71880651,
"host":"127.0.0.1",
"port":2006,
"user":"root",
"dockerId":"6aecdcdf980b"
}
]
}
eg.
curl -X GET -H "Authorization:user1" 127.0.0.1:5000/list/1
- Method: GET
- URL:
/detail/<string:dockerId>
- Body
{
"code":200,
"msg":"success",
"data":{
"pwd":71880651,
"host":"127.0.0.1",
"port":2006,
"user":"root",
"dockerId":"6aecdcdf980b"
}
}
eg.
curl -X GET -H "Authorization:user1" 127.0.0.1:5000/detail/6aecdcdf980b
- 容器内的数据跟配置映射到宿主机,容器内只存放运行环境
- 使用gunicorn替换flask自带的wsgi,支持高并发
- 增加supervisor管理常驻进程