28-FastDFS笔记

CentOS7 安装FastDFS分布式文件系统

最近要用到fastDFS,所以自己研究了一下,在搭建FastDFS的过程中遇到过很多的问题,为了能帮忙到以后搭建FastDFS的同学,少走弯路,与大家分享一下。FastDFS的作者淘宝资深架构余庆,这个优秀的轻量及的分布式文件系统的开源没多久,立马就火了。

FastDFS是为互联网应用量身定做的一套分布式文件存储系统,非常适合用来存储用户图片、视频、文档等文件

参考资料网站:

https://www.codepeople.cn/2019/01/14/CentOS7.4-FastDFS-install/

https://zhuanlan.zhihu.com/p/110089695

版本介绍

系统:centos7

fastdfs-6.06.tar.gz

libfastcommon-1.0.43.tar.gz 

fastdfs-nginx-module-1.22.tar.gz

nginx-1.18.0.tar.gz

一.FastDFS搭建工具下载

作者的GitHub地址:https://github.com/happyfish100

下载地址:

二. 将下载好的 4个压缩包拷贝至 /usr/local/src 目录下

三.安装编译工具及库文件

yum -y install make zlib zlib-devel gcc-c++ libtool  openssl openssl-devel

四.测试环境关闭防火墙

systemctl stop firewalld.service

五.开机禁用防火墙

systemctl disable firewalld.service

六.查看防火墙状态

firewall-cmd --state

七.解压下载好的包

tar -zxvf fastdfs-6.06.tar.gz
tar -zxvf fastdfs-nginx-module-1.22.tar.gz
tar -zxvf libfastcommon-1.0.43.tar.gz
tar -zxvf nginx-1.18.0.tar.gz

八.编译安装 libfastcommon

1、进入解压后的目录

# cd /usr/local/src/libfastcommon-1.0.43

2、编译安装

# ./make.sh && ./make.sh install

3、检查

ls /usr/lib64|grep libfastcommon
ls /usr/lib|grep libfastcommon

出现libfastcommon.so即成功

九.编译安装fastdfs

1、进入解压后的目录

# cd /usr/local/src/fastdfs-6.06

2、编译安装

# ./make.sh && ./make.sh install

3、检查

# ls /usr/bin|grep fdfs

4、启用配置文件

cd /etc/fdfs/
cp storage.conf.sample storage.conf
cp client.conf.sample client.conf
cp tracker.conf.sample tracker.conf
mkdir -p /fastdfs/tracker

5、修改 tracker.conf 文件

vim /etc/fdfs/tracker.conf
base_path = /fastdfs/tracker

6、启动 tracker 服务

/etc/init.d/fdfs_trackerd start

7、检查服务是否启动

ps -ef|grep fdfs

8、修改 storage.conf 文件

vim /etc/fdfs/storage.conf
base_path = /fastdfs/storage
store_path0 = /fastdfs/storage
tracker_server = 192.168.0.179:22122
http.server_port = 80

9、创建目录

mkdir -p /fastdfs/storage

10、启动 storage 服务

/etc/init.d/fdfs_storaged start

11、设置开机启动

vim /etc/rc.d/rc.local

加入配置

/etc/init.d/fdfs_trackerd start
/etc/init.d/fdfs_storaged start

12、编辑 client.conf 文件

vim /etc/fdfs/client.conf

修改

base_path = /fastdfs/tracker
tracker_server=192.168.0.179:22122

十.安装 nginx 和 fastdfs-nginx-module

1、安装pcre

yum -y install pcre pcre-devel

2、进入解压目录

cd /usr/local/src/nginx-1.18.0

3、添加 http_stub_status_module 模块

这里其实就是讲nginx安装到/usr/local/nginx目录下,启动时也是在这里

./configure --prefix=/usr/local/nginx --with-http_stub_status_module

4、进入 /usr/local/src/fastdfs-nginx-module-1.22/src/

cd /usr/local/src/fastdfs-nginx-module-1.22/src

5、编辑配置文件

vim config

6、将config文件中的/usr/local替换成/usr

:%s+/usr/local+/usr

7、进入 nginx 解压目录

cd /usr/local/src/nginx-1.18.0

8、添加fastdfs-nginx-module 注意路径,目录不一致要修改

./configure --add-module=/usr/local/src/fastdfs-nginx-module-1.22/src/

9、编译安装

make && make install

10、复制并修改fastdfs-ngin-module中的配置文件

cp /usr/local/src/fastdfs-nginx-module-1.22/src/mod_fastdfs.conf /etc/fdfs/
vim /etc/fdfs/mod_fastdfs.conf

修改配置

connect_timeout=10
tracker_server=192.168.0.179:22122
url_have_group_name = true
store_path0=/fastdfs/storage

11、进入fastdfd源码conf目录

cd /usr/local/src/fastdfs-6.06/conf

12、将http.conf,mime.types两个文件拷贝到/etc/fdfs/目录下

cp http.conf mime.types /etc/fdfs/

13、创建一个软连接,在/fastdfs/storage文件存储目录下创建软连接,将其链接到实际存放数据 的目录

ln -s /fastdfs/storage/data/ /fastdfs/storage/data/M00

14、编辑 nginx 配置

vim /usr/local/nginx/conf/nginx.conf

修改配置

server {
    listen       80;
    server_name  192.168.0.179;
    location ~/group([0-9])/M00 {
            root  /fastdfs/storage/data;
            ngx_fastdfs_module;
    }
}

15、启动 nginx

cd /usr/local/nginx/sbin
./nginx

测试

1、下载测试图片到本地,在root目录下

wget -c "https://t.ki4.cn/2019/10/zau26v.jpg" -O springboot.jpg

2、上传测试图片

/usr/bin/fdfs_test /etc/fdfs/client.conf upload /root/springboot.jpg
或使用命令
/usr/bin/fdfs_upload_file /etc/fdfs/client.conf /root/springboot.jpg

得到返回文件名

group1/M00/00/00/wKgAs17MHPaAa5inAACumtGPxf4201.jpg

与ip拼接:http://192.168.0.179/group1/M00/00/00/wKgAs17MHPaAa5inAACumtGPxf4201.jpg

二、Spring Boot 整合 FastDFS

1.引入依赖
<dependency>
    <groupId>org.csource</groupId>
    <artifactId>fastdfs-client-java</artifactId>
    <version>1.27-SNAPSHOT</version>
</dependency>
2.application.yml中加入配置
fdfs:
  connect-timeout: 1000 # 连接超时时间
  so-timeout: 1500
  thumbImage: #上传时生成的缩略图大小
    width: 100
    height: 100
  pool:
    max-total: 500
    max-wait-millis: 2000
  tracker-list: 192.168.18.129:22122 #fdfs服务器 跟踪器ip:端口
3.封装工具类
/**
 * FdfsUtil 工具类
 **/
@Component
public class FdfsUtil {

    private final Logger logger = LoggerFactory.getLogger(FdfsUtil.class);

    //引入fdfs
    @Autowired
    private FastFileStorageClient storageClient;


    /**
     *	MultipartFile类型的文件上传ַ
     * @param file
     * @return
     * @throws IOException
     */
    public String uploadFile(MultipartFile file) throws IOException {
        StorePath storePath = storageClient.uploadFile(file.getInputStream(), file.getSize(),
                                                       FilenameUtils.getExtension(file.getOriginalFilename()), null);
        return getResAccessUrl(storePath);
    }

    /**
     * 普通的文件上传
     *
     * @param file
     * @return
     * @throws IOException
     */
    public String uploadFile(File file) throws IOException {
        FileInputStream inputStream = new FileInputStream(file);
        StorePath path = storageClient.uploadFile(inputStream, file.length(),
                                                  FilenameUtils.getExtension(file.getName()), null);
        return getResAccessUrl(path);
    }

    /**
     * 带输入流形式的文件上传
     *
     * @param is
     * @param size
     * @param fileName
     * @return
     */
    public String uploadFileStream(InputStream is, long size, String fileName) {
        StorePath path = storageClient.uploadFile(is, size, fileName, null);
        return getResAccessUrl(path);
    }

    /**
     * 将一段文本文件写到fastdfs的服务器上
     *
     * @param content
     * @param fileExtension
     * @return
     */
    public String uploadFile(String content, String fileExtension) {
        byte[] buff = content.getBytes(StandardCharsets.UTF_8);
        ByteArrayInputStream stream = new ByteArrayInputStream(buff);
        StorePath path = storageClient.uploadFile(stream, buff.length, fileExtension, null);
        return getResAccessUrl(path);
    }

    /**
     * 返回文件上传成功后的地址名称ַ
     * @param storePath
     * @return
     */
    private String getResAccessUrl(StorePath storePath) {
        return  storePath.getFullPath();
    }

    /**
     * 删除文件
     * @param fileUrl
     */
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void deleteFile(String fileUrl) throws FdfsException {
        if (StringUtils.isEmpty(fileUrl)) {
            return;
        }
        try {
            StorePath storePath = StorePath.praseFromUrl(fileUrl);
            storageClient.deleteFile(storePath.getGroup(), storePath.getPath());
        } catch (FdfsUnsupportStorePathException e) {
            logger.warn("删除图片错误:"+e.getMessage());
        }
    }

    public String upfileImage(InputStream is, long size, String fileExtName, Set<MateData> metaData) {
        StorePath path = storageClient.uploadFile(is, size, fileExtName, metaData);
        return getResAccessUrl(path);
    }

    /**
     * 下载文件
     *
     * @param fileUrl 文件URL
     * @return 文件字节
     * @throws IOException
     */
    public byte[] downloadFile(String fileUrl) throws IOException {
        String group = fileUrl.substring(0, fileUrl.indexOf("/"));
        String path = fileUrl.substring(fileUrl.indexOf("/") + 1);
        DownloadByteArray downloadByteArray = new DownloadByteArray();
        byte[] bytes = storageClient.downloadFile(group, path, downloadByteArray);
        return bytes;
    }
}
4.启动类上添加@Import(FdfsClientConfig.class)注解
/**
 * 启动类
 **/
@SpringBootApplication
@EnableTransactionManagement  //开启事务管理
@MapperScan("com.coderman.*.mapper") //扫描mapper
@Import(FdfsClientConfig.class) //启动类添加fdfs 注解
public class XinguanApplication {
    public static void main(String[] args) {
        SpringApplication.run(XinguanApplication.class,args);
    }
}

三、Docker 安装 fastdfs

关于Docker的安装不同的操作系统有不同的安装方法,这里就不再赘述,默认你的电脑或服务器上已经安装好了Docker,并已启动。

这里使用的是阿里云服务器,相关防火墙端口和安全组已打开

  1. 拉取fdfs镜像
docker pull delron/fastdfs:latest
  1. 查看镜像
[root@iZwz96m5ykb29ky4ysqx57Z storage]# docker images
delron/fastdfs     latest    8487e86fc6ee   2 years ago    464MB
  1. 创建tracker容器
docker run -id --name tracker --restart=always --net host -v /home/fdfs/tracker:/var/fdfs delron/fastdfs tracker

默认tracker是监听在22122端口的。

  1. 创建storage容器
docker run -id --name storage --restart=always --net host -v /home/fdfs/storage:/var/fdfs -e TRACKER_SERVER="192.168.18.130:22122" delron/fastdfs storage
  1. 查看容器是否启动

docker ps看到如下,说明启动成功。

[root@iZwz96m5ykb29ky4ysqx57Z storage]# docker ps
CONTAINER ID   IMAGE            COMMAND                  CREATED             STATUS             PORTS     NAMES
1cd02534a7ad   delron/fastdfs   "/usr/bin/start1.sh …"   About an hour ago   Up About an hour             storage
b9d86bde13ae   delron/fastdfs   "/usr/bin/start1.sh …"   About an hour ago   Up About an hour             tracker
  1. 安装vim 命令
# yum install vim

如果没有yum,安装便好

# sudo yum install epel-release
# sudo yum update
  1. docker exec -it 容器id bash 进入storage的容器
# docker exec -it 1cd02534a7ad /bin/bash
  1. 了解storage.conf 内容,/etc/fdfs/storage.conf
[root@iZwz96m5ykb29ky4ysqx57Z fdfs]# vim storage.conf

# the storage server port
port=23000 //storage默认服务端口

# the base path to store data and log files
base_path=/var/fdfs  //文件存储的位置(storage容器的)

# store_path#, based 0, if store_path0 not exists, it's value is base_path
# the paths must be exist
store_path0=/var/fdfs  //上传的文件存储位置(storage容器的)

# tracker_server can ocur more than once, and tracker_server format is
#  "host:port", host can be hostname or ip address
tracker_server=192.168.18.130:22122  //tracker服务器的地址

# the port of the web server on this storage server
http.server_port=80  //http访问的端口,默认是8888,可以不修改
  1. 配置nginx

进入/usr/local/ngxin/conf 默认配置,不修改也是可以的。

# vim /usr/local/nginx/conf/nginx.conf

修改配置:

   server {
        listen       80;
        server_name  localhost;
        location ~/group[0-9]/ {
            ngx_fastdfs_module;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root html;
        }
    }
  1. 进入到/etc/fdfs/client.conf 看
# tracker_server can ocur more than once, and tracker_server format is
#  "host:port", host can be hostname or ip address
tracker_server=192.168.18.130:22122
  1. 测试文件上传

在我们宿主机的 /home/fdfs/storage 下放一张图片 01.jpg

[root@iZwz96m5ykb29ky4ysqx57Z storage]# pwd
/home/fdfs/storage
[root@iZwz96m5ykb29ky4ysqx57Z storage]# ls
01.jpg  data  logs

进入storage容器,/var/fdfs目录下, 执行命令

# fdfs_upload_file  /etc/fdfs/client.conf 01.jpg

看到返回了group1/xxx/xx 的字符串

启动nginx 访问图片

# /usr/local/nginx/sbin/nginx

url访问:192.168.18.130/group1/M00/00/00/rB7OZ2BsDkeARMz3AAL5Iu0tK60754.jpg

看到如下图说明成功。
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值