Linux安装FastDFS的全过程以及文件服务器开发过程

下载安装文件

(1)FastDFS安装包
在这里插入图片描述
(2)获取libfastcommon安装包
在这里插入图片描述
(3)获取fastdfs-nginx-module安装包
(4)获取nginx安装包
获取完成最新安装包之后,上传到/opt/software目录下。

安装FastDFS

安装依赖

yum -y install gcc-c++ perl

安装libfastcommon类库

tar -zxvf libfastcommon-1.0.39.tar.gz
cd libfastcommon-1.0.39
./make.sh
./make.sh install

在这里插入图片描述

创建软连接

因为 FastDFS 主程序设置的 lib 目录是/usr/local/lib

ln -s /usr/lib64/libfastcommon.so /usr/local/lib/libfastcommon.so

安装FastDFS

tar -zxvf fastdfs-5.11.tar.gz
cd fastdfs-5.11
./make.sh
./make.sh install

在这里插入图片描述

相关文件和目录

我们是采用默认安装的方式安装的,安装完成对应的文件和目录如下:
服务脚本:

/etc/init.d/fdfs_storaged
/etc/init.d/fdfs_tracker

配置文件:

/etc/fdfs/client.conf.sample
/etc/fdfs/storage.conf.sample
/etc/fdfs/storage_ids.conf.sample
/etc/fdfs/tracker.conf.sample
配置FastDFS跟踪器

复制tracker.conf.sample并重命名为tracker.conf

cd /etc/fdfs/
cp tracker.conf.sample tracker.conf
vi /etc/fdfs/tracker.conf

改动信息

disabled=false                              #默认为false 此处不修改 不启用=false就是启用。
port=22122                                  #跟踪器端口号,默认为22122
base_path=/home/fastdfs    #目录地址 依实际挂载的数据盘自行设置,根目录必须存在,子目录会自动创建

创建基础数据目录(base_path对应的路径)

mkdir -p /home/fastdfs    

启动tracker

/etc/init.d/fdfs_trackerd start
ps -ef | grep fdfs
/etc/init.d/fdfs_trackerd stop

在这里插入图片描述
设置开机启动

vi /etc/rc.d/rc.local
## FastDFS Tracker
/etc/init.d/fdfs_trackerd start
chmod +x /etc/rc.d/rc.local
配置FastDFS存储器

复制storage.conf.sample并重命名为storage.conf

cd /etc/fdfs/
cp storage.conf.sample storage.conf
vi /etc/fdfs/storage.conf

改动点

disabled=false  #默认为false 此处不修改 不启用=false就是启用
port=23000 #存储器端口号,默认为23000
base_path=/home/fastdfs/storage  #目录地址 依实际挂载的数据盘自行设置,根目录必须存在,子目录会自动创建
store_path0=/home/fastdfs/storage
#存放文件时storage server支持多个路径,如果不配置base_path0,那边它就和base_path对应的路径一样
tracker_server=172.30.0.4:22122 # tracker_server 的列表
http.server_port=8888 # HTTP服务端口 默认为8888 

mkdir -p /fastdfs/storage

启动storaged

/etc/init.d/fdfs_storaged start
/etc/init.d/fdfs_storaged stop
vi /etc/rc.d/rc.local
## FastDFS Storaged
/etc/init.d/fdfs_storaged start

测试文件上传

cp /etc/fdfs/client.conf.sample /etc/fdfs/client.conf
vi /etc/fdfs/client.conf

改动

base_path=/home/fastdfs/tracker
tracker_server=172.30.0.4:22122

测试

/usr/bin/fdfs_upload_file /etc/fdfs/client.conf /opt/software/jdk-8u221-linux-x64.tar.gz 

结果

group1/M00/00/00/rB4ABF1vceOAAnmpC6Do1RH_edA.tar.gz

在这里插入图片描述

Nginx模块

(1)已经安装过Nginx,动态添加模块
查看已经安装的模块
在这里插入图片描述
首先解压后要修改fastdfs-nginx-module-1.20/src/config

cd /opt/software/
tar -zxvf fastdfs-nginx-module-1.20.tar.gz
ngx_module_incs="/usr/include/fastdfs /usr/include/fastcommon/"
CORE_INCS="$CORE_INCS /usr/include/fastdfs /usr/include/fastcommon/"

再编译

cd nginx-1.16.1
./configure --prefix=/usr/local/nginx  --with-http_stub_status_module --with-http_ssl_module --add-module=/opt/software/fastdfs-nginx-module-1.20/src
make

注意不要make install了

cd /usr/local/nginx/sbin/
mv nginx nginx.bak
cp /opt/software/nginx-1.16.1/objs/nginx /usr/local/nginx/sbin/

如果不修改配置文件,就会报错
在这里插入图片描述
(2)编译安装Nginx

cd /opt/software/
tar -zxvf fastdfs-nginx-module-1.20.tar.gz
cd nginx-1.16.1
./configure --prefix=/usr/local/nginx  --with-http_stub_status_module --with-http_ssl_module --add-module=/opt/software/fastdfs-nginx-module-1.20/src
make
make install

复制 fastdfs-nginx-module 源码中的配置文件到/etc/fdfs 目录

connect_timeout=10
base_path=/tmp
tracker_server=172.30.0.4:22122 #tracker跟踪器地址
storage_server_port=23000 #storage存储器的端口
group_name=group1 #组名
url_have_group_name = true #url hava group name 顾名思义 默认为false 改为true
store_path0=/home/fastdfs/storage #存储器存储地址

其他操作

cd /opt/software/fastdfs-5.11/conf
cp http.conf mime.types /etc/fdfs/
ln -s /home/fastdfs/storage/data/ /home/fastdfs/storage/data/M00

添加nginx配置文件 listen 80

 location ~/group([0-9])/M00 {
        ngx_fastdfs_module;
    }

验证

访问:http://xxx/group1/M00/00/00/rB4ABF1vceOAAnmpC6Do1RH_edA.tar.gz

总结

  1. 地址不能用127.0.0.1 或者localhost
  2. 安装nginx模块一定要修改模块的配置文件
  3. 文件地址配置文件不要配错了,要一致,其他的就按照默认的就可以
  4. 参考文档1
  5. 参考文档2

Java接口开放

 <fastdfs.version>1.26.7</fastdfs.version>
 
    <dependency>
        <groupId>com.github.tobato</groupId>
        <artifactId>fastdfs-client</artifactId>
        <version>${fastdfs.version}</version>
    </dependency>

配置文件

fdfs:
  soTimeout: 1500
  connectTimeout: 600
  trackerList: 172.30.0.4:22122

FastDFSClientWrapper

@Autowired
    private FastFileStorageClient storageClient;

    /**
     * 文件上传
     *
     * @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 storePath.getFullPath();
    }

Controller

  @Autowired
    private FastDFSClientWrapper dfsClient;

    // 上传文件
    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    public String upload(MultipartFile file, HttpServletRequest request, HttpServletResponse response) throws Exception {
        return dfsClient.uploadFile(file);
    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值