go-fastdfs部署
一、配置环境
1.1、系统安装
-
安装CentOS7 配置CPU 2核、内存 4G、硬盘 200G,根据需求配置硬盘容量。
-
配置YUM源
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.ba
wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
yum makecache
- 无需GUI环境,选择最小系统即可,但需要自行安装以下工具:
# 必须安装
yum install wget
# 选择安装
yum install vim
yum install net-tools.x86_64 # 该包包含ifconfig命令,可用ip命令替代
二、单机部署
2.1、下载go-fastdfs
mkdir /home/go-fastdfs
cd /home/go-fastdfs
wget --no-check-certificate https://github.com/sjqzhang/go-fastdfs/releases/download/v1.3.1/fileserver -O fileserver && chmod +x fileserver
2.2、配置防火墙
firewall-cmd --zone=public --add-port=8080/tcp --permanent (--permanent永久生效,没有此参数重启后失效)
重新载入
firewall-cmd --reload
2.3、运行
./fileserver
# 访问地址:http://go-fastdfs的IP:8080/group1
三、集群部署
3.1、安装配置go-fastdfs
# 安装配置一个节点三个副本为例
# 副本IP:10.1.1.20、10.1.1.21、10.1.1.22
# NginxIP:192.168.1.120、10.1.1.23
# 配置固定IP请参考之前编写蓝鲸的文档
# 安装副本请参考单机部署
# 节点配置:修改conf/cfg.json配置文件
vim cfg.json
# 修改内容"peers": ["http://10.1.1.20:8080","http://10.1.1.21:8080","http://10.1.1.22:8080"],
#
3.2、安装配置nginx
# 下载nginx安装包
wget http://nginx.org/download/nginx-1.19.1.tar.gz
# 安装需要编译的插件
yum install gcc c++ yum install -y pcre pcre-devel yum install -y zlib zlib-devel yum install -y openssl openssl-devel
tar -zxvf nginx-1.19.1.tar.gz
cd nginx-1.19.1.tar.gz
./configure
make && make install
# 修改nginx配置文件
vim /usr/local/nginx/conf/nginx.conf
# 清空文件
# 将如下内容写入配置文件中
worker_processes auto;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/html;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
error_log /var/log/nginx/error.log error;
sendfile on;
keepalive_timeout 65;
rewrite_log on;
client_max_body_size 0;
proxy_redirect ~/(\w+)/big/upload/(.*) /$1/big/upload/$2; #继点续传一定要设置(注意)
#以下是一下横向扩展的配置,当前统一大集群容量不够时,只要增加一个小集群,也就是增加一个
#upstream ,一个小群集内按业务需求设定副本数,也就是机器数。
# 节点1
upstream gofastdfs-group1 {
server 10.1.1.20:8080;
server 10.1.1.21:8080;
server 10.1.1.22:8080;
#server 10.1.14.37:8082;
ip_hash; #notice:very important(注意)
}
server {
listen 80;
server_name localhost;
if ( $request_uri ~ /godfs/group ) {
# 注意group会随组的前缀改变而改变
rewrite ^/godfs/(.*)$ /$1 last;
}
location ~ /group(\d) {
#统一在url前增加godfs,以便统一出入口。
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_pass http://gofastdfs-group$1;
}
location ~ /godfs/upload {
#这是一个横向扩展配置,前期可能只有一个集群group1,当group1满后,只需将上传指向group2,
#也就是将rewrite , proxy_pass 中的group1改为group2即可。
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
rewrite ^/godfs/upload /group1/upload break;
proxy_pass http://gofastdfs-group1;
}
location ~ /godfs/big/upload {
#以上上类似。
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
rewrite ^/godfs/upload /group1/big/upload break;
proxy_pass http://gofastdfs-group1;
}
}
}
# 创建日志目录
mkdir /var/log/nginx/
# 运行nginx
/usr/local/nginx/sbin/nginx
3.3、配置防火墙
副本开放8080端口、nginx服务器开放80端口
firewall-cmd --zone=public --add-port=8080/tcp --permanent
重新载入
firewall-cmd --reload
3.4、运行
# 运行3副本
./fileserver
# 访问地址:http://192.168.1.120/group1
四、go-fastdfs API
例Python:
import requests
url = 'http://192.168.1.120/group1/upload'
files = {'file': open('report.xls', 'rb')}
options={'output':'json','path':'','scene':''} #参阅浏览器上传的选项
r = requests.post(url,data=options, files=files)
print(r.text)
例Go:
package main
import (
"fmt"
"github.com/astaxie/beego/httplib"
)
func main() {
var obj interface{}
req:=httplib.Post("http://192.168.1.120/group1/upload")
req.PostFile("file","/root/go/src/go-gastdfs/456.txt")
req.Param("output","json")
req.Param("scene","")
req.Param("path","")
req.ToJSON(&obj)
fmt.Println(obj)
}
建议使用Linux开发
4.1、API通用说明
一、统一使用POST请求
二、返回格式统一为json
格式如下
{
"status":"ok",
"message":"",
"data":{}
}
二、url中的group只有在support_group_manage设置为true才有。
例如:
http://10.1.5.9:8080/group/reload
默认:
http://10.1.5.9:8080/reload
说明:url中的group为cfg.json中的group参数值。
4.2、配置管理API
http://10.1.5.9:8080/group/reload
参数:
action: set(修改参数),get获取参数,reload重新加载参数
cfg:json参数 与 action=set配合完成参数设置
4.3、文件统计信息API
http://10.1.50.90:8080/group/stat
4.4、文件上传API
http://10.1.50.90:8080/group/upload
参数:
file:上传的文件
scene:场景
output:输出
path:自定义路径
具体请参阅示例代码(用浏览器访问http://127.0.0.1:8080)
4.5、文件秒传
http://10.1.50.90:8080/group/upload
参数:
md5:文件的摘要
摘要算法要与cfg.json中配置的一样
例子:http://127.0.0.1:8080/upload?md5=430a71f5c5e093a105452819cc48cc9c&output=json
4.6、文件删除
http://10.1.50.90:8080/group/delete
参数:
md5:文件的摘要(md5|sha1) 视配置定
path:文件路径
md5与path二选一
说明:md5或path都是上传文件时返回的信息,要以json方式返回才能看到(参阅浏览器上传)
http://127.0.0.1:8080/delete?md5=430a71f5c5e093a105452819cc48cc9c
4.7、文件信息
http://10.1.50.90:8080/group/get_file_info
参数:
md5:文件的摘要(md5|sha1) 视配置定
path:文件路径
md5与path二选一
说明:md5或path都是上传文件时返回的信息,要以json方式返回才能看到(参阅浏览器上传)
例子:http://127.0.0.1:8080/get_file_info?md5=430a71f5c5e093a105452819cc48cc9c
4.8、文件列表
http://10.1.50.90:8080/group/list_dir
参数:
dir:要查看文件列表的目录名
例子:http://127.0.0.1:8080/list_dir?dir=default
4.9、修复统计信息
http://10.1.50.90:8080/group/repair_stat
参数:
date:要修复的日期,格式如:20190725
例子:http://127.0.0.1:8080/repair_stat?date=20190725
4.10、同步失败修复
http://10.1.50.90:8080/group/repair
参数:
force:是否强行修复(0|1)
例子:http://127.0.0.1:8080/repair?force=1
4.11、从文件目录中修复元数据(性能较差)
http://10.1.50.90:8080/group/repair_fileinfo
需要开启搬迁功能,修改cfg.json配置文件中的 enable_migrate 设为true
:20190725
例子:http://127.0.0.1:8080/repair_stat?date=20190725
4.10、同步失败修复
http://10.1.50.90:8080/group/repair
参数:
force:是否强行修复(0|1)
例子:http://127.0.0.1:8080/repair?force=1
4.11、从文件目录中修复元数据(性能较差)
http://10.1.50.90:8080/group/repair_fileinfo
需要开启搬迁功能,修改cfg.json配置文件中的 enable_migrate 设为true