fastDFS安装使用
fastDFS是用c语言编写的一款开源的分布式文件系统
fastDFS为互联网量身定制,考虑了
1.冗余备份
2.负载均衡
3.线性扩容
等机制
使用fastDFS很容易可以搭建一套高性能的文件服务器集群
来提供文件上传,下载等服务
优点:
FastDFS架构包括Tracker server和Storage server
客户端请求Tracker server进行文件上传,下载
通过Tracker server调度
最终由storage server完成文件的上传和下载
tracker server的作用是负载均衡和调度
通过tracker server在文件上传的时候
可以根据一些方法找到storage serer提供文件上传服务
可以将tracker称为追踪服务器或调度服务器
storage server作用是文件存储
客户端上传的文件最终存储在storage服务器上
storage server没有实现自己的文件系统
而是利用操作系统的文件系统来管理文件
可以将storage称为存储服务器
服务端两个角色:
Tracker:管理集群,tracker 也可以实现集群。每个 tracker 节点地位平等。收集 Storage 集群的状态。
Storage:实际保存文件
Storage 分为多个组,每个组之间保存的文件是不同的。每 个组内部可以有多个成员,组成员内部保存的内容是一样的,组成员的地位是一致的,没有 主从的概念。
##### 3.2.5FastDFS安装
###### 3.2.5.1安装FastDFS依赖包
1. 解压缩libfastcommon-master.zip
2. 进入到libfastcommon-master的目录中
3. 执行**./make.sh**
4. 执行**sudo ./make.sh install**
###### 3.2.5.2安装FastDFS
1. 解压缩fastdfs-master.zip
2. 进入到 fastdfs-master目录中
3. 执行 **./make.sh**
4. 执行 **sudo ./make.sh install**
###### 3.2.5.3配置跟踪服务器tracker
1. ```shell
sudo cp /etc/fdfs/tracker.conf.sample /etc/fdfs/tracker.conf
```
2. 在/home/itcast/目录中创建目录 fastdfs/tracker
```shell
mkdir -p /home/itcast/fastdfs/tracker
```
3. 编辑/etc/fdfs/tracker.conf配置文件 sudo vim /etc/fdfs/tracker.conf
修改 base_path=/home/itcast/fastdfs/tracker
###### 3.2.5.4配置存储服务器storage
1. ```
sudo cp /etc/fdfs/storage.conf.sample /etc/fdfs/storage.conf
```
2. 在/home/itcast/fastdfs/ 目录中创建目录 storage
```shell
mkdir –p /home/itcast/fastdfs/storage
```
3. 编辑/etc/fdfs/storage.conf配置文件 sudo vim /etc/fdfs/storage.conf
修改内容:
```shell
base_path=/home/itcast/fastdfs/storage
store_path0=/home/itcast/fastdfs/storage
tracker_server=自己ubuntu虚拟机的ip地址:22122
```
###### 3.2.5.5启动tracker和storage
进入到/etc/fdfs/下面执行以下两条指令
```shell
sudo fdfs_trackerd /etc/fdfs/tracker.conf
sudo fdfs_storaged /etc/fdfs/storage.conf
```
###### 3.2.5.6测试是否安装成功
1. **sudo cp /etc/fdfs/client.conf.sample /etc/fdfs/client.conf **
2. 编辑/etc/fdfs/client.conf配置文件 **sudo vim /etc/fdfs/client.conf**
修改内容:
```shell
base_path=/home/itcast/fastdfs/tracker
tracker_server=自己ubuntu虚拟机的ip地址:22122
```
3. 上传文件测试(fastDHT)
sudo fdfs_upload_file /etc/fdfs/client.conf 要上传的图片文件
如果返回类似**group1/M00/00/00/rBIK6VcaP0aARXXvAAHrUgHEviQ394.jpg **的文件id则说明文件上传成功
###### 3.2.5.7安装fastdfs-nginx-module
1. 解压缩 nginx-1.8.1.tar.gz
2. 解压缩 fastdfs-nginx-module-master.zip
3. 进入nginx-1.8.1目录中
4. 执行
```shell
sudo ./configure --prefix=/usr/local/nginx/ --add-module=fastdfs-nginx-module-master解压后的目录的绝对路径/src
```
注意:**这时候会报一个错,说没有PCRE库**

下载缺少的库
```shell
sudo apt-get install libpcre3 libpcre3-dev
```
+ 首先你需要去更换源,因为ubuntu自带的源没有这个库
+ 更换下载源为阿里的源
+ 先把原来的源文件备份
```shell
sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak
```
+ 编辑源文件
```shell
sudo vim /etc/apt/sources.list
```
把原来的内容全部删掉,粘贴一下内容:
```shell
deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
```
更换完源之后执行
```shell
sudo apt-get update
sudo apt-get install libpcre3 libpcre3-dev
```
然后进入nginx-1.8.1目录中,再次执行:
```shell
sudo ./configure --prefix=/usr/local/nginx/ --add-module=fastdfs-nginx-module-master解压后的目录的绝对路径/src
```
这时候还会报一个错(错误还真多),错误原因是因为nginx编译的时候把警告当错误处理,事实上这个警告并不影响(程序员忽略警告):

解决方法:
找到objs目录下的Makefile
vim Makefile
删掉里面的-Werror(**如果没有修改权限,修改一下这个文件的权限,`chmod 777 Makefile`**)

然后回到nginx-1.8.1目录中
执行完成后执行**sudo make**
执行**sudo make install**
5. sudo cp fastdfs-nginx-module-master解压后的目录中src下mod_fastdfs.conf /etc/fdfs/mod_fastdfs.conf
6. sudo vim /etc/fdfs/mod_fastdfs.conf
修改内容:
```shell
connect_timeout=10
tracker_server=自己ubuntu虚拟机的ip地址:22122
url_have_group_name=true
store_path0=/home/python/fastdfs/storage
```
7. sudo cp 解压缩的fastdfs-master目录中的conf中的http.conf /etc/fdfs/http.conf
8. sudo cp 解压缩的fastdfs-master目录中conf的mime.types /etc/fdfs/mime.types
9. sudo vim /usr/local/nginx/conf/nginx.conf
在http部分中添加配置信息如下:
```shell
server {
listen 8888;
server_name localhost;
location ~/group[0-9]/ {
ngx_fastdfs_module;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
```
10. 启动nginx
sudo /usr/local/nginx/sbin/nginx
先把上传图片的代码抽离出来
func UploadImage(this*beego.Controller,filePath string)string{
//1.那数据
//那标题
f,h,err:=this.GetFile(filePath)
defer f.Close()
//上传文件处理
//1.判断文件格式
ext := path.Ext(h.Filename)
if ext != ".jpg" && ext != ".png"&&ext != ".jpeg"{
beego.Info("上传文件格式不正确")
return ""
}
//2.文件大小
if h.Size>5000000{
beego.Info("文件太大,不允许上传")
return ""
}
//3.不能重名
fileName := time.Now().Format("2006-01-02 15:04:05")
err2:=this.SaveToFile(filePath,"./static/img/"+fileName+ext)
if err != nil{
beego.Info("上传文件失败")
return ""
}
if err2 != nil{
beego.Info("上传文件失败",err2)
return ""
}
return "/static/img/"+fileName+ext
}