linux搭建fastDFS全过程--亲测

2 篇文章 0 订阅
1 篇文章 0 订阅

前言

         由于自己是在腾讯云买的云服务器,亲测(搭建一台)自己作为文件服务器,玩玩,练练手。集群版 只是在单机版的上面多增加了点配置,基本大相径庭。

服务组件安装(环境)

yum install -y gcc

yum install -y gcc-c++

yum install -y pcre pcre-devel

yum install -y zlib zlib-devel

yum install -y openssl openssl-devel

安装包准备

安装包集成,无需再四处下载,https://download.csdn.net/download/qq_20409407/11983216

       fastDFS安装,只需要 1.libfastcommon 2.FastDFS 两个,就可以安装完成。

       至于 3.fastdfs-nginx-module4.nginx 主要是可以通过web界面进行访问。

1.libfastcommon( 本文下载稳定版中最新的,进行安装。传送门:https://github.com/happyfish100/libfastcommon/releases

   

2.FastDFS( 本文下载稳定版中最新的,进行安装。传送门:https://github.com/happyfish100/fastdfs/releases

3.fastdfs-nginx-module 本文下载稳定版中最新的,进行安装。

传送门:https://github.com/happyfish100/fastdfs-nginx-module/releases

4.nginx (传送门:wget http://nginx.org/download/nginx-1.9.5.tar.gz

安装部分

安装分为两个大部分:

第一部分,一个是安装fdfs,通过客户端连接直接可以使用的。

第二部分,通过fastdfs-nginx-module和nginx 直接可以在浏览器访问的。

将上一步刚刚下载的文件,全部传到需要安装的linux上(上传命令,不详细介绍)。

安装部分 --> 第一部分  安装fdfs

libfastcommon安装(如果报错,请检查上述环境是否安装)

#进入上传的目录,解压
tar -zxvf libfastcommon-1.0.41.tar.gz
## 进入解压目录
cd libfastcommon-1.0.41/
## 编译
./make.sh 
## 安装
./make.sh install

fast安装(如果报错,请检查上述环境是否安装)

#解压
tar -zxvf fastdfs-6.02.tar.gz
## 进入解压目录
cd fastdfs-6.02/
##编译
./make.sh 
## 安装
./make.sh install

安装完成,在 /etc/fdfs 会有如下4个配置文件

接下来,重点来了,配置跟踪服务器(Tracker Server)  和 配置存储服务器(Storage Server)

配置跟踪服务器(Tracker Server)

1、进入配置文件目录
cd /etc/fdfs/
2、准备配置文件
mv tracker.conf.sample tracker.conf
3、修改配置文件
vi tracker.conf
配置文件修改部分:
base_path=/data/fastdfs/tracker   //数据和日志存储路径,路径必须存在
store_lookup=0  //设置上传文件轮询存储组
4、启动tracker(启动之前,确保 /data/fastdfs/tracker 文件夹存在,)
mkdir -p /data/fastdfs/tracker
fdfs_trackerd /etc/fdfs/tracker.conf
5、查看启动结果
netstat -unltp|grep fdfs
6、设置开机自启动
vim /etc/rc.local
## 添加命令fdfs_trackerd /etc/fdfs/tracker.conf 到rc.local文件中

成功后可以看到服务

配置存储服务器(Storage Server)

1、进入配置文件目录
cd /etc/fdfs/
2、添加配置文件 单节点的话,只需要配置一个就可以
cp storage.conf.sample storage-group1.conf
## cp storage.conf.sample storage-group2.conf
3、修改配置文件(以group1为例,group2和group3类似)
vi storage-group1.conf
修改内容:
base_path=/data/fastdfs/storage/group1   //数据和日志存储路径,路径必须存在
store_path0=/data/fastdfs/storage/group1
port=23000 //存储服务端口(相同组不同服务器节点的端口必须相同)
//配置跟踪服务器
tracker_server=192.168.0.220:22122
## tracker_server=192.168.0.221:22122
## tracker_server=192.168.0.222:22122
group_name=group1  //指定存储组
4、启动存储组(启动前,保证目录 /data/fastdfs/storage/group1 存在)
mkdir -p /data/fastdfs/storage/group1
fdfs_storaged /etc/fdfs/storage-group1.conf
5、查看启动结果
netstat -unltp|grep fdfs
6、设置开机自启动
vim /etc/rc.local
添加命令fdfs_storaged /etc/fdfs/ storage-group1.conf 到rc.local文件中

启动成功后,会出现一个trackerd 和 一个 storaged

## 查看存储服务器挂载情况
fdfs_monitor /etc/fdfs/storage-group1.conf​​

挂载描信息:

配置客户端连接

#进入目录
cd /etc/fdfs/
# 重命名
mv client.conf.sample client.conf
#修改配置信息
vi /etc/fdfs/client.conf
##修改内容:
##配置跟踪服务器(单台的话,只需要配置一个)
tracker_server=192.168.0.220:22122
tracker_server=192.168.0.221:22122
tracker_server=192.168.0.222:22122
#配置日志文件目录
base_path=/data/fastdfs/client 

上传测试

#上传之前,保证 /data/fastdfs/client 目录存在
mkdir -p /data/fastdfs/client


[root@localhost fastdfs]# fdfs_upload_file /etc/fdfs/client.conf /opt/test.jpg

group1/M00/00/00/wKgA3FyUhIiAUjFwAAeckd7Z_4M274.jpg

[root@localhost fastdfs]# fdfs_upload_file /etc/fdfs/client.conf /opt/test.jpg

group2/M00/00/00/wKgA3FyUhIuACNz2AAeckd7Z_4M404.jpg

[root@localhost fastdfs]# fdfs_upload_file /etc/fdfs/client.conf /opt/test.jpg

group3/M00/00/00/wKgA3VyUhI2ABl71AAeckd7Z_4M513.jpg
  1.  

1、问题一:相同存储组文件无法共享;

原因:相同存储组,不同服务器的服务端口不同。

解决:相同存储组使用相同的端口。

搭建fastDFS完成,如不需要web界面访问的,就已经可以使用了,至于java客户端如何连接,放在本文的最后讲

 

上诉过程,是搭建fastDFS文件服务器的步骤,但是还可以通过nginx代理,浏览器访问到刚刚上传的图片和文件,搭建过程如下

安装部分 --> 第二部分  安装fastdfs-nginx-module和nginx

#确定环境安装 
yum -y  install pcre-devel opensslopenssl-devel


#第一步,将压缩包cp到/opt下,进行安装
cp fastdfs-nginx-module-1.21.tar.gz /opt/
cp nginx-1.9.5.tar.gz /opt/
#解压安装包
tar -zxvf nginx-1.9.5.tar.gz
tar -zxvf fastdfs-nginx-module-1.21.tar.gz
# 进入nginx目录
/opt/nginx-1.9.5
# 将 fastdfs-nginx-module 添加进nginx,此处注意 fastdfs-nginx-module 路径一定要正确
./configure --prefix=/usr/local/nginx --add-module=/opt/fastdfs-nginx-module-1.21/src
#上诉无报错的情况,直接make,有报错,检测环境,或者网上看看错误,有的是版本不兼容的问题
###编译  安装
make
make install

注意:make 和make install没有报错的情况,如报错,自行检测环境,或网上找下原因(本人被坑过)

配置mod_fastdfs.conf

#将mod_fastdfs.conf 复制到 /etc/fdfs/ 目录,如果目录跟我一样,直接复制
cp /opt/fastdfs-nginx-module-1.21/src/mod_fastdfs.conf /etc/fdfs/
#创建fastdfsNginxModule日志文件
/data/fastdfs/fastdfsNginxModule
#修改配置文件
vi /etc/fdfs/mod_fastdfs.conf

修改下面四个参数:
#输出日志文件
base_path=/data/fastdfs/fastdfsNginxModule
#配置自己的ip咯
tracker_server=192.168.153.130:22122
# 注:必须与 storage.conf 一样,别配错了, must same as storage.conf
store_path0=/data/fastdfs/storage/group1

# if the url / uri including the group name
# set to true when uri like ${group_name}/M00/00/00/xxx, such as group1/M00/xxx
# 按照官方 的意思,就是前面有组名的,需要加上这个,这里直接用true
url_have_group_name= true

找到fastDFS解压包,将以下几个配置文件,拷贝到 /etc/fdfs/ 目录下,无需更改配置

#找到解压包的目录,每个人习惯不同
/soft/fdfs/fastdfs-6.02/conf

#进入fastDFS目录,
cd /soft/fdfs/fastdfs-6.02/conf/
#复制以下2个配置文件,到/etc/fdfs 目录下
cp http.conf mime.types /etc/fdfs/

配置nginx配置文件

vim /usr/local/nginx/conf/nginx.conf
#如之前的配置,都是跟我的一样的,那么root直接复制过去就可以了,配置的结构,如下图
location /group1/M00{
    #root指向数据根目录,即storage的store_path
    root /data/fastdfs/storage/group1/data;
    ngx_fastdfs_module;
}

配置好了,只需要重启nginx了,改了配置,不需要重启fastDFS

#为了操作方便, 我直接配置nginx环境变量,因为nginx经常是需要用的

vim /etc/profile
#nginx环境变量,假造peofile最后一行
export NGINX_HOME=/usr/local/nginx
export PATH=$PATH:$NGINX_HOME/sbin

#刷新 /etc/profile 文件
source /etc/profile

接下来,启动nginx了

#没有配置环境变量,直接这样子启动,配置了直接  
/usr/local/nginx/sbin/nginx
或
nginx
#查看nginx是否启动
ps -ef|grep nginx

#测试启动成功,直接 
curl http://localhost

#查看错误日志
tail -f /usr/local/nginx/logs/error.log



# nginx相关命令
#nginx停止
nginx -s stop
#nginx重新加载
nginx -s reload

ip + 返回路径,就可以web界面访问了

group1/M00/00/00/wKgAjF3PsISAdlVhAABCQ1Y_4mc924.jpg

http://192.168.0.140/group1/M00/00/00/wKgAjF3PsISAdlVhAABCQ1Y_4mc924.jpg

恭喜搭建成功!!开森!!!

 

 

 

最后,忘记了,身为java开发工程师。怎么用java操作呢?

java连接相关

 

  依赖fastdfs client jar包,直接依赖


# mvn 引入 fastdfs client jar包,直接依赖
<dependency>
   <groupId>net.oschina.zcx7878</groupId>
   <artifactId>fastdfs-client-java</artifactId>
   <version>1.27.0.0</version>
</dependency>

添加FastDFS的配置

配置的主要内容为FastDFS的tracker server地址配置(fastdfs.properties),可以添加多个

fastdfs.tracker_servers=192.168.0.220:22122,192.168.0.221:22122,192.168.0.222:22122

公共类实现

public class FastDFSClient {

    private TrackerClient trackerClient;
    private TrackerServer trackerServer;
    private StorageServer storageServer;
    private StorageClient1 storageClient;
    
//TODO 记得构造方法里面,填入自己的配置文件信息
    public FastDFSClient() {
        try {
            Properties properties = PropertiesLoaderUtils.loadAllProperties("com/tomtop/etc/conf/fastdfs.properties");
            ClientGlobal.initByProperties(properties);
            trackerClient = new TrackerClient();
            trackerServer = trackerClient.getConnection();
            storageServer = null;
            storageClient = new StorageClient1(trackerServer, storageServer);
        }catch (Exception e){
            e.printStackTrace();
        }

    }

    /**
     * 上传文件方法
     * <p>Title: uploadFile</p>
     * <p>Description: </p>
     *
     * @param fileName 文件全路径
     * @param extName  文件扩展名,不包含(.)
     * @param metas    文件扩展信息
     * @return
     * @throws Exception
     */
    public String uploadFile(String fileName, String extName, NameValuePair[] metas) throws Exception {
        String result = storageClient.upload_file1(fileName, extName, metas);
        return result;
    }

    /**
     * 上传文件,传fileName
     *
     * @param fileName 文件的磁盘路径名称 如:D:/image/aaa.jpg
     * @return null为失败
     */
    public String uploadFile(String fileName) throws Exception {
        return uploadFile(fileName, null, null);
    }

    /**
     * @param fileName 文件的磁盘路径名称 如:D:/image/aaa.jpg
     * @param extName  文件的扩展名 如 txt jpg等
     * @return null为失败
     */
    public String uploadFile(String fileName, String extName) throws Exception {
        return uploadFile(fileName, extName, null);
    }

    /**
     * 上传文件方法
     * <p>Title: uploadFile</p>
     * <p>Description: </p>
     *
     * @param fileContent 文件的内容,字节数组
     * @param extName     文件扩展名
     * @param metas       文件扩展信息
     * @return
     * @throws Exception
     */
    public String uploadFile(byte[] fileContent, String extName, NameValuePair[] metas) throws Exception {
        String result = storageClient.upload_file1(fileContent, extName, metas);
        return result;
    }

    /**
     * 上传文件
     *
     * @param fileContent 文件的字节数组
     * @return null为失败
     * @throws Exception
     */
    public String uploadFile(byte[] fileContent) throws Exception {
        return uploadFile(fileContent, null, null);
    }

    /**
     * 上传文件
     *
     * @param fileContent 文件的字节数组
     * @param extName     文件的扩展名 如 txt  jpg png 等
     * @return null为失败
     */
    public String uploadFile(byte[] fileContent, String extName) throws Exception  {
        return uploadFile(fileContent, extName, null);
    }

    /**
     * 文件下载到磁盘
     *
     * @param path   图片路径
     * @param output 输出流 中包含要输出到磁盘的路径
     * @return -1失败,0成功
     */
    public int downloadFile(String path, BufferedOutputStream output) throws Exception {
        int result = -1;
        byte[] b = storageClient.download_file1(path);
        try {
            if (b != null) {
                output.write(b);
                result = 0;
            }
        } catch (Exception e) {
            throw e;
        } //用户可能取消了下载
        finally {
            if (output != null)
                try {
                    output.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
        }
        return result;
    }

    /**
     * 获取文件数组
     *
     * @param path 文件的路径 如group1/M00/00/00/wKgRsVjtwpSAXGwkAAAweEAzRjw471.jpg
     * @return
     */
    public byte[] downloadDytes(String path) {
        byte[] b = null;
        try {
            b = storageClient.download_file1(path);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (MyException e) {
            e.printStackTrace();
        }
        return b;
    }

    /**
     * 删除文件
     *
     * @param group       组名 如:group1
     * @param storagePath 不带组名的路径名称 如:M00/00/00/wKgRsVjtwpSAXGwkAAAweEAzRjw471.jpg
     * @return -1失败,0成功
     */
    public Integer deleteFile(String group, String storagePath) {
        int result = -1;
        try {
            result = storageClient.delete_file(group, storagePath);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (MyException e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * @param storagePath 文件的全部路径 如:group1/M00/00/00/wKgRsVjtwpSAXGwkAAAweEAzRjw471.jpg
     * @return -1失败,0成功
     * @throws IOException
     * @throws Exception
     */
    public Integer deleteFile(String storagePath) {
        int result = -1;
        try {
            result = storageClient.delete_file1(storagePath);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (MyException e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 获取远程服务器文件资源信息
     * @param groupName   文件组名 如:group1
     * @param remoteFileName M00/00/00/wKgRsVjtwpSAXGwkAAAweEAzRjw471.jpg
     * @return
     */
    public FileInfo getFile(String groupName,String remoteFileName){
        try {
            return storageClient.get_file_info(groupName, remoteFileName);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值