centos7安装FastDFS详解

centos7安装FastDFS详解

配置FastDFS环境准备工作

参考文献

FastDFS:https://github.com/happyfish100/

官方安装文档:https://github.com/happyfish100/fastdfs/wiki

环境准备

  • Centos7.x 两台,分别安装tracker与storage
  • 下载安装包:
    • libfatscommon:FastDFS分离出的一些公用函数包
    • FastDFS:FastDFS本体
    • fastdfs-nginx-module:FastDFS和nginx的关联模块
    • nginx:发布访问服务

安装步骤 (tracker与storage两台服务器都要执行)

  • 安装基础环境

    yum install -y gcc gcc-c++
    yum -y install libevent
    
  • 安装libfatscommon函数库

    # 解压
    tar -zxvf libfastcommon-1.0.42.tar.gz
    
    • 进入libfastcommon文件夹,编译并且安装
    cd libfastcommon-1.0.42
    ./make.sh
    ./make.sh install
    
  • 安装fastdfs主程序文件

    # 解压
    tar -zxvf fastdfs-6.04.tar.gz
    

    进入到fastdfs目录,安装fastdfs

    cd fastdfs-6.04/
    ./make.sh
    ./make.sh install
    
  • 拷贝配置文件如下:

cp /home/software/FastDFS/fastdfs-6.04/conf/* /etc/fdfs/

配置tracker服务

修改tracker配置文件

vim /etc/fdfs/tracker.conf

修改tracker配置文件,此为tracker的工作目录,保存数据以及日志

base_path=/usr/local/fastdfs/tracker

创建tracker工作目录

mkdir /usr/local/fastdfs/tracker -p

启动tracker服务

/usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf

检查进程如下:

ps -ef|grep tracker

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-5FFaFENC-1597821912427)(D:\hexo\source_posts\image\image-20200619100511089.png)]

停止tracker

/usr/bin/stop.sh /etc/fdfs/tracker.conf

配置storage服务

修改storage配置文件

修改该storage.conf配置文件

vim /etc/fdfs/storage.conf
# 修改组名
group_name=groupTest
# 修改storage的工作空间
base_path=/usr/local/fastdfs/storage
# 修改storage的存储空间
store_path0=/usr/local/fastdfs/storage
# 修改tracker的地址和端口号,用于心跳
tracker_server=(trackerip):22122

创建storage工作目录

mkdir /usr/local/fastdfs/storage -p

启动storage

前提:必须首先启动tracker

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

检查进程如下:

ps -ef|grep storage

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-bUrfrZa6-1597821912433)(D:\hexo\source_posts\image\image-20200619101616170.png)]

测试上传(在storage服务器中操作)

修改的client配置文件

vim /etc/fdfs/client.conf
base_path=/usr/local/fastdfs/client
tracker_server=(trackerip):22122

创建client工作目录

mkdir /usr/local/fastdfs/client

测试:

/usr/bin/fdfs_test /etc/fdfs/client.conf upload ./testUpload.jpg

查看上传的文件

cd /usr/local/fastdfs/storage/data/00/00

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-lrNoNCm1-1597821912436)(D:\hexo\source_posts\image\image-20200619103434822.png)]

配置nginx服务器

fastdfs安装好以后是无法通过http访问的,这个时候就需要借助nginx了,所以需要安装fastdfs的第三方模块到nginx中,就能使用了。nginx需要和storage在同一个节点。

安装nginx插件

解压nginx的fastdfs压缩包

tar -zxvf fastdfs-nginx-module-1.22.tar.gz

复制配置文件如下:

cp mod_fastdfs.conf /etc/fdfs

修改/fastdfs-nginx-module/src/config文件,主要是修改路径,把local删除,因为fastdfs安装的时候我们没有修改路径,原路径是/usr/local/incloud

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-wyNcVDbe-1597821912438)(D:\hexo\source_posts\image\image-20200619154308352.png)]

安装nginx

  1. 官网http://nginx.org/下载对应的nginx包,推荐使用稳定版本

  2. 上传nginx到linux系统

  3. 安装依赖环境

    (1)安装gcc环境

      yum install gcc-c++
    

    (2)安装PCRE库,用于解析正则表达式

     yum install -y pcre pcre-devel
    

    (3)zlib压缩和解压缩依赖,

     yum install -y zlib zlib-devel
    

    (4)SSL 安全的加密的套接字协议层,用于HTTP安全传输,也就是https

     yum install -y openssl openssl-devel
    
  4. 解压,需要注意,解压后得到的是源码,源码需要编译后才能安装

    tar -zxvf nginx-1.16.1.tar.gz
    
  5. 编译之前,先创建nginx临时目录,如果不创建,在启动nginx的过程中会报错

    mkdir /var/temp/nginx -p
    
  6. 在nginx目录,输入如下命令进行配置,目的是为了创建makefile文件

    cd nginx-1.16.1
    
    ./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=/home/software/fdfs/fastdfs-nginx-module-1.22/src
    

    主要新增一个第三方模块

    修改 mod_fastdfs.conf 配置文件

    base_path=/usr/local/fastdfs/tmp
    tracker_server=(trackerip):22122
    group_name=groupTest
    url_have_group_name = true
    store0_path=/usr/local/fastdfs/storage
    
    mkdir /usr/local/fastdfs/tmp
    

    修改nginx.conf,添加如下虚拟主机:

    server {
        listen       8888;
        server_name  localhost;
    
        location /groupTest/M00 {
                ngx_fastdfs_module;
        }
    
    }
    
  7. make编译

    make
    
  8. 安装

    make install
    
  9. 进入sbin目录启动nginx

    ./nginx
    
    • 停止:./nginx -s stop
    • 重新加载:./nginx -s reload

浏览器测试

浏览器输入地址

例:http://10.10.10.115:8888/groupTest/M00/00/00/CgoKc17sIdSAIZ6pADfje97FrYA794.JPG
http://(storageip):8888/groupTest/M00/00/00/(imagename)

SpringBoot集成FastDFS

###引入依赖

<!-- https://mvnrepository.com/artifact/com.github.tobato/fastdfs-client -->
<dependency>
    <groupId>com.github.tobato</groupId>
    <artifactId>fastdfs-client</artifactId>
    <version>1.26.6</version>
</dependency>

###application.yml中加入配置

############################################################
#
# fdfs 配置
#
############################################################
fdfs:
  connect-timeout: 30   # 连接的超时时间
  so-timeout: 30        # 读取的超时时间
  tracker-list: (trackerip):22122   # tracker服务所在的ip地址和端口号

文件上传service

package com.gy.service.impl;

import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import com.gy.service.FdfsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;

@Service
public class FdfsServiceImpl implements FdfsService {

    @Autowired
    private FastFileStorageClient fastFileStorageClient;

    @Autowired
    private FileResource fileResource;

    @Override
    public String upload(MultipartFile file, String fileExtName) throws Exception {

        StorePath storePath = fastFileStorageClient.uploadFile(file.getInputStream(),
                                file.getSize(),
                                fileExtName,
                                null);

        String path = storePath.getFullPath();

        return path;
    }
    
}

###String转MultipartFile

工具类

package com.course.server.util;

import org.springframework.web.multipart.MultipartFile;
import sun.misc.BASE64Decoder;

import java.io.*;

public class Base64ToMultipartFile implements MultipartFile {

    private final byte[] imgContent;
    private final String header;

    public Base64ToMultipartFile(byte[] imgContent, String header) {
        this.imgContent = imgContent;
        this.header = header.split(";")[0];
    }

    @Override
    public String getName() {
        // TODO - implementation depends on your requirements
        return System.currentTimeMillis() + Math.random() + "." + header.split("/")[1];
    }

    @Override
    public String getOriginalFilename() {
        // TODO - implementation depends on your requirements
        return System.currentTimeMillis() + (int) Math.random() * 10000 + "." + header.split("/")[1];
    }

    @Override
    public String getContentType() {
        // TODO - implementation depends on your requirements
        return header.split(":")[1];
    }

    @Override
    public boolean isEmpty() {
        return imgContent == null || imgContent.length == 0;
    }

    @Override
    public long getSize() {
        return imgContent.length;
    }

    @Override
    public byte[] getBytes() throws IOException {
        return imgContent;
    }

    @Override
    public InputStream getInputStream() throws IOException {
        return new ByteArrayInputStream(imgContent);
    }

    @Override
    public void transferTo(File dest) throws IOException, IllegalStateException {
        new FileOutputStream(dest).write(imgContent);
    }

    public static MultipartFile base64ToMultipart(String base64) {
        try {
            String[] baseStrs = base64.split(",");

            BASE64Decoder decoder = new BASE64Decoder();
            byte[] b = new byte[0];
            b = decoder.decodeBuffer(baseStrs[1]);

            for(int i = 0; i < b.length; ++i) {
                if (b[i] < 0) {
                    b[i] += 256;
                }
            }

            return new Base64ToMultipartFile(b, baseStrs[0]);
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
}

controller

@RequestMapping("/upload")
public ResponseDto upload(String shardBase64) throws Exception {
    MultipartFile shard = Base64ToMultipartFile.base64ToMultipart(shardBase64);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值