1.简介
- C语言
- 开源
- 冗余备份、负载均衡、线性扩容
- github fastdfs hanppyfish 开源的
2.架构
image.png- Tracker server(调度服务器):负载均衡和调度
- Storage server(存储服务器):负载文件存储
2. 背景
为什么文件不存放到本地:---(为什么不自己创建图片存储服务器)
- 文件同名不同内容(处理:根据内容随机自行创建文件名)
- 文件同内容,不同名(处理:MD5进行文件内容校验--图片为二进制)
- 存储大文件(压缩、解压缩)
- 虚拟机挂掉,文件找不回来,文件备份问题(各种备份服务器,物理备份,虚拟备份)
- 文件下载速度 (与文件大小有关,与文件能否切割,类似迅雷的各个片段下载,还可以利用CDN分布存储网络---各个地区建立虚拟服务器)
- 用户图片格式,转码等处理问题(转码算法)
- 其他问题
fastDFS 能解决上述所有问题
3.文件上传流程
image.png4.安装FastDFS
- 从docker hub中拉取fastdfs镜像
docker pull youkou1/fastdfs
- 查看镜像是否拉取成功
docker images
- 安装tracker
docker run -dti --network=host --name tracker -v /var/fdfs/tracker:/var/fdfs youkou1/fastdfs tracker
- 安装storage
# 10.0.1.15 这个需要修改 22122 默认端口不可修改,需要映射
# ip a 查询(是这个enp0s3, 并不是127那个)
docker run -dti --network=host --name storage -e TRACKER_SERVER=10.0.2.15:22122 -v /var/fdfs/storage:/var/fdfs youkou1/fastdfs storage
- 查看容器是否启动成功,如果为Up状态则启动成功
docker container ls -a
- 创建成功后 会自动监听 8888端口(获取url的端口)
# 需要端口转发8888
sudo netstat -ltunp | grep 8888
- 设置自启动
docker update --restart=always <CONTAINER ID>
5.测试是否安装成功
- django项目根目录下创建utils/fastdfs/logs日志文件夹,用于存放日志信息
- 创建utils/fastdfs/client.cnf配置文件
# connect timeout in seconds
# default value is 30s
connect_timeout=30
# network timeout in seconds
# default value is 30s
network_timeout=60
#------------------------Need to modify log save addr-----------------------#
# the base path to store log files
base_path=utils/fastdfs/logs
#------------------------Need to modify ip-----------------------#
# tracker_server can ocur more than once, and tracker_server format is
# "host:port", host can be hostname or ip address
tracker_server=10.0.2.15:22122
#standard log level as syslog, case insensitive, value list:
### emerg for emergency
### alert
### crit for critical
### error
### warn for warning
### notice
### info
### debug
log_level=info
# if use connection pool
# default value is false
use_connection_pool = false
# connections whose the idle time exceeds this time will be closed
# unit: second
# default value is 3600
connection_pool_max_idle_time = 3600
# if load FastDFS parameters from tracker server
# default value is false
load_fdfs_parameters_from_tracker=false
# if use storage ID instead of IP address
# same as tracker.conf
# valid only when load_fdfs_parameters_from_tracker is false
# default value is false
use_storage_id = false
# specify storage ids filename, can use relative or absolute path
# same as tracker.conf
# valid only when load_fdfs_parameters_from_tracker is false
storage_ids_filename = storage_ids.conf
#HTTP settings
http.tracker_server_port=80
(Dream) pyvip@vip:~/DreamBlog$ python manage.py shell
Python 3.6.5 (default, Apr 1 2018, 05:46:30)
[GCC 7.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from fdfs_client.client import Fdfs_client
>>> FDFS_Client = Fdfs_client('utils/fastdfs/client.conf')
>>> ret = FDFS_Client.upload_by_filename('media/2018.png')
getting connection
<fdfs_client.connection.Connection object at 0x7f6580a4af98>
<fdfs_client.fdfs_protol.Tracker_header object at 0x7f6580a4af60>
>>> print(ret)
{'Group name': 'group1', 'Remote file_id': 'group1/M00/00/00/CgACD1yBvbOAQYwpAAfh_rrm7jw155.png', 'Status': 'Upload successed.', 'Local file name': 'media/2018.png', 'Uploaded size': '504.00KB', 'Storage IP': '10.0.2.15'}
# 可以通过 访问 http://127.0.0.1:8888/'Remote file_id' 获取图片
http://127.0.0.1:8888/group1/M00/00/00/group1/M00/00/00/CgACD1yBvbOAQYwpAAfh_rrm7jw155.png
- 安装相关包
# fdfs_client.zip文件从百度云中下载
# rz fdfs_client.zip(rz命令迁移虚拟环境中)
pip install fdfs_client.zip
pip install mutagen
pip install requests
- 前端js
// ================== 上传图片文件至服务器 ================
let $upload_to_server = $("#upload-news-thumbnail");
$upload_to_server.change(function () {
let file = this.files[0]; // 获取文件
let oFormData = new FormData(); // 创建一个 FormData
oFormData.append("image_file", file); // 把文件添加进去
// 发送请求
$.ajax({
url: "/admin/news/images/",
method: "POST",
data: oFormData,
processData: false, // 定义文件的传输
contentType: false,
})
.done(function (res) {
if (res.errno === "0") {
// 更新标签成功
message.showSuccess("图片上传成功");
let sImageUrl = res["data"]["image_url"];
// console.log(thumbnailUrl);
$thumbnailUrl.val('');
$thumbnailUrl.val(sImageUrl);
} else {
message.showError(res.errmsg)
}
})
.fail(function () {
message.showError('服务器超时,请重试!');
});
});
网友评论