CentOS7下搭建分布式文件系统FastDFS与上传下载api

文章目录

CentOS7上安装FastDFS

一、下载FastDFS

下载地址:http://sourceforge.net/projects/FastDFS/ 或 https://github.com/happyfish100/FastDFS

此次安装使用的是 FastDFS_v5.05.tar.gz 下载完成后上传到linux

二、安装FastDFS

1.安装FastDFS需要先将官网下载的源码进行编译,需要安装gcc环境

如果没有gcc环境,需要安装gcc

yum -y install gcc-c++

2.FastDFS依赖libevent库,需要安装

yum -y install libevent

3.安装libfastcommona

libfastcommona是FastDFS提供的,libfastcommona包含了FastDFS运行所需要的一些基础库

3.1下载地址:
https://sourceforge.net/projects/libfastcommon/files/latest/download
或
https://github.com/happyfish100/libfastcommon

此次安装使用的是 libfastcommonV1.0.7.tar.gz 下载完成后上传到linux

3.2将libfastcommonV1.0.7.tar.gz 解压到/usr/local/下
tar -zxvf libfastcommonV1.0.7.tar.gz -C /usr/local/
3.3切换到 cd /usr/local/libfastcommon-1.0.7/ 目录,进行编译安装
./make.sh           #编译
./make.sh install   #安装

在这里插入图片描述

3.4 libfastcommon安装好后会自动生成 libfastcommon.so库文件到/usr/lib64下

由于FastDFS程序引用/usr/lib目录,所以需要将/usr/lib64下的库文件 libfastcommon.so拷贝到/usr/lib下

cp libfastcommon.so /usr/lib

4.安装tracker

4.1将tracker解压到/usr/local/下
tar -zxvf FastDFS_v5.05.tar.gz -C /usr/local
4.2 切换到/usr/local/FastDFS下,编译安装
./make.sh            #编译
./make.sh install 	 #安装

在这里插入图片描述

4.3安装成功将安装目录下的conf下的文件拷贝到/etc/fdfs/下
cp /usr/local/FastDFS/conf/* /etc/fdfs

进入/etc/fdfs/目录拷贝一份新的tracker配置文件:

cp tracker.conf.sample tracker.conf

修改tracker.conf :vi tracker.conf

base_path=/home/yuqing/FastDFS 改为 base_path=/home/fastdfs
http.server_port=8080 改为 http.server_port=80 #配置http端口 在文件最后

# the method of selecting group to upload files 上传文件选择storage策略
# 0: round robin 轮询策略
# 1: specify group  指定一组,不用
# 2: load balance, select the max free space group to upload file 负载均衡策略,将比较闲的返回
store_lookup=2

当选择第二种策略要修改这个配置为选中的storage组名,如不是第二种不用管
# which group to upload file
# when store_lookup set to 1, must set store_group to the group name
store_group=group2

在/home创建/fastdfs目录

mkdir /home/fastdfs
4.3启动tracker
/usr/bin/fdfs_trackerd  /etc/fdfs/tracker.conf restart

在/home/fastdfs目录下会生成俩个目录,一个是数据,一个是日志
在这里插入图片描述

5.配置和启动storage

如果要安装多台storage在别的服务器,那么每台服务器都需要按上面的步骤二(1,2,3)安装环境

如果是安装在一台服务器,只需要从5.2开始

同tracker安装

5.2 配置storage

由于在同一台机器配置,上面已经安装过FastDFS,只需要配置storage就可以

切换目录到 /etc/fdfs/ 拷贝一份新的storage配置文件

cp storage.conf.sample storage.conf

修改storage.conf: vim storage.conf

group_name=group1 #配置组名
base_path=/home/yuqing/fastdfs   改为:base_path=/home/fastdfs
#store存放文件的位置(store_path)
store_path0=/home/yuqing/fastdfs  改为: store_path0=/home/fdfs_storage 
#如果有多个挂载磁盘则定义多个store_path,如下
#store_path1=.....
#store_path2=......

#配置tracker服务器:IP
tracker_server=192.168.1.20:22122
#如果有多个则配置多个tracker
#tracker_server=192.168.1.4:22122

#配置http端口
http.server_port=88

创建目录

mkdir /home/fdfs_storage

启动storage

/usr/bin/fdfs_storaged /etc/fdfs/storage.conf restart

三、使用FastDFS自带工具测试

1.切换到/etc/fdfs目录下

拷贝一份新的client文件

cp client.conf.sample client.conf

2.修改client.conf

base_path=/home/fastdfs
tracker_server=192.168.101.3:22122 #如果有多台tracker_server可以下面加
tracker_server=192.168.101.4:22122

3.拷贝一张图片到CentOS服务器上

fdfs_delete_file              # 删除文件       
fdfs_upload_file              #上传文件
fdfs_test                      # 测试文件
fdfs_download_file                #下载文件

测试图片上传

/usr/bin/fdfs_test /etc/fdfs/client.conf upload /service/tools/aa.png

上传成功:
在这里插入图片描述
http://192.168.23.10/group1/M00/00/00/wKgXCl3OQMCAW1dhAAB-VQMLUfs327_big.jpg对应storage服务器上面的/home/fdfs_storage/data/00/00/wKisFFpBG9eAHaQvAAAWKd1hQR4158_big.jpg
在这里插入图片描述
由于还没有和nginx整合无法使用http下载

三、FastDFS 和 nginx 整合

单独安装nginx代理服务,它的作用是代理访问storage上的文件,实现负载均衡

1.下载上传fastdfs-nginx-module_v1.16.tar.gz 到Centos服务器上

作用: FastDFS通过Tracker服务器,将文件放在存储服务器存储中,但同组存储服务器之间需要进入文件复制,有同步延迟的问题。假设Tracker服务器将文件上传到了192.168.4.125,上传成功后文件ID已经返回给客户端。此时FastDFS存储合并机制合并到这个文件同步到同组存储192.168.4.126,在文件还没有复制完成的情况下,客户端如果用这个文件ID在192.168.4.126上取文件,就会出现而fastdfs-nginx-module可以重定向文件连接到源服务器取文件,避免客户端通过复制导致其文件无法访问错误。

2.解压fastdfs-nginx-module_v1.16.tar.gz 到/usr/local目录下

tar -zxvf fastdfs-nginx-module_v1.16.tar.gz -C /usr/local

3.进入到cd /usr/local/fastdfs-nginx-module/src/ 目录,修改 config文件

将/usr/local修改为/usr,修改前:
在这里插入图片描述
修改后:
在这里插入图片描述

4.将/usr/local/fastdfs-nginx-module/src/下的mod_fastdfs.conf 拷贝到/etc/fdfs/下

cp mod_fastdfs.conf /etc/fdfs/

5.修改/etc/fdfs/mod_fastdfs.conf

base_path=/home/fastdfs   #base_path=/tmp 修改为 base_path=/home/fastdfs
tracker_server=192.168.172.20:22122 
#tracker_server=192.168.172.20:22122 #(多个tracker配置多行)
url_have_group_name=true        #url中包含group名称
store_path0=/home/fdfs_storage  #指定文件存储路径(上面配置的store路径)

6.将 libfdfsclient.so 拷贝至/usr/lib 下

cp /usr/lib64/libfdfsclient.so /usr/lib/

7.创建nginx/client 目录

mkdir -p /var/temp/nginx/client

8.nginx安装

8.1安装nginx依赖环境
yum install gcc-c++    
yum install -y pcre pcre-devel
yum install -y zlib zlib-devel
yum install -y openssl openssl-devel
8.2下载nginx
wget https://mirrors.huaweicloud.com/nginx/nginx-1.17.5.tar.gz
8.2解压nginx到/usr/local
tar -zxvf nginx-1.17.5.tar.gz -C /usr/local
8.3 进入nginx-1.17.5目录 使用 configure 命令

cd /usr/local/nginx-1.17.5/

./configure \
--prefix=/usr/local/nginx \
--pid-path=/var/run/nginx/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--with-http_gzip_static_module \
--http-client-body-temp-path=/var/temp/nginx/client \
--http-proxy-temp-path=/var/temp/nginx/proxy \
--http-fastcgi-temp-path=/var/temp/nginx/fastcgi \
--http-uwsgi-temp-path=/var/temp/nginx/uwsgi \
--http-scgi-temp-path=/var/temp/nginx/scgi \
--add-module=/usr/local/fastdfs-nginx-module/src

上边将临时文件指定为 --http-client-body-temp-path=/var/temp/nginx/client需要创建/local/nginx目录

mkdir /var/temp/nginx/client -p
8.4 编译并安装
make 
make install

安装完成后生成目录/usr/local/nginx:
在这里插入图片描述

8.5 拷贝配置文件到/etc/fdfs下
cd /usr/local/FastDFS/conf
cp http.conf mime.types /etc/fdfs/
8.6 修改nginx配置
进入nginx配置文件夹
cd /usr/local/nginx/conf
修改nginx.conf文件
vi nginx.conf

修改内容:

server {
  listen 80;                 
  server_name 192.168.23.10; #修改为本机ip
  location /group1/M00/{
  		root /home/FastDFS/fdfs_storage/data;#文件所在目录
  		ngx_FastDFS_module;
  } 
}

说明:

server_name 指定本机 ip

location /group1/M00/:group1 为 nginx 服务 FastDFS 的分组名称,M00 是 FastDFS 自动生成编号,对应store_path0=/home/FastDFS/fdfs_storage,如果 FastDFS 定义 store_path1,这里就是 M01

9.在浏览器访问刚刚上传成功的图片

如果访问不到检查防火墙是否开启

firewall-cmd --state #查看默认防火墙状态
systemctl stop firewalld.service #停止防火墙
systemctl disable firewalld.service #禁止防火墙开机启动

在这里插入图片描述

四、FastDFS使用用法

1.引入依赖

<dependency>
    <groupId>net.oschina.zcx7878</groupId>
    <artifactId>fastdfs-client-java</artifactId>
    <version>1.27.0.0</version>
</dependency>

2.需要配置文件 这个是.properties格式配置文件 也支持yml格式

fastdfs.connect_timeout_in_seconds = 5
fastdfs.network_timeout_in_seconds = 30
fastdfs.charset = UTF-8
fastdfs.tracker_servers = 192.168.23.10:22122 

3.上传和下载api

@SpringBootTest
@RunWith(SpringRunner.class)
public class TestFastDFS {
    //上传文件
    @Test
    public void testUpload() {
        //加载配置文件
        try {
            ClientGlobal.initByProperties("config/fastdfs-client.properties");
            //定义TrackerClient,用于请求TrackerServer
            TrackerClient trackerClient = new TrackerClient();
            //连接tracker
            TrackerServer trackerServer = trackerClient.getConnection();
            //获取Storage
            StorageServer storageServer = trackerClient.getStoreStorage(trackerServer);
            //创建StorageClient1
            StorageClient1 storageClient1 = new StorageClient1(trackerServer, storageServer);
            //像storage服务器上传文件 返回值是在storage中的地址
            String png = storageClient1.upload_appender_file1("C:\\Users\\z\\Pictures\\Feedback\\{7DCF74B9-B3D6-4BF6-8698-74052E16C765}/Capture001.png", "png", null);
            System.out.println(png);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (MyException e) {
            e.printStackTrace();
        }

    }
    //下载文件
    @Test
    public void testDownload(){
        try {
            ClientGlobal.initByProperties("config/fastdfs-client.properties");
            //定义TrackerClient,用于请求TrackerServer
            TrackerClient trackerClient = new TrackerClient();
            //连接tracker
            TrackerServer trackerServer = trackerClient.getConnection();
            //获取Storage
            StorageServer storageServer = trackerClient.getStoreStorage(trackerServer);
            //创建StorageClient1
            StorageClient1 storageClient1 = new StorageClient1(trackerServer, storageServer);
            //下载文件
            byte[] bytes = storageClient1.download_file1("group1/M00/00/00/wKgXCl3OckyEE2T7AAAAAFnm4n4635.png");
            FileOutputStream fos = new FileOutputStream(new File("d:/a.png"));
            fos.write(bytes);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (MyException e) {
            e.printStackTrace();
        }

    }
}

更多api可以查看:https://github.com/happyfish100/fastdfs-client-java

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值