如何安裝FastDFS

前言

本文基於CentOS6.5安裝java

FastDFS方案

主機IP 名稱
192.168.1.1 trackerd
192.168.1.2 storaged

環境準備

下載軟件:

安裝gcc

命令:yum install make cmake gcc gcc-c++apache

解壓libfastcommon

命令:unzip libfastcommon-master.zip -d /usr/local/fast/vim

關閉防火牆

在學習時能夠不用考慮防火牆的問題tomcat

正文

安裝libfastcommon

./make.sh 
./make.sh install
mkdir -p /usr/lib64
install -m 755 libfastcommon.so /usr/lib64
mkdir -p /usr/include/fastcommon

打印:app

install -m 644 common_define.h hash.h chain.h logger.h base64.h shared_func.h pthread_func.h ini_file_reader.h _os_bits.h sockopt.h sched_thread.h http_func.h md5.h local_ip_func.h avl_tree.h ioevent.h ioevent_loop.h fast_task_queue.h fast_timer.h process_ctrl.h fast_mblock.h connection_pool.h /usr/include/fastcommon

注意安裝的路徑:libfastcommon默認安裝到了/usr/lib64/這個位置。ide

進行軟連接建立

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

安裝FastDFS(同時進行)

解壓

tar -zxvf FastDFS_v5.05.tar.gz -C /usr/local/fast/

安裝

cd /usr/local/fast/FastDFS
./make.sh
./make.sh install

打印:

mkdir -p /usr/bin
mkdir -p /etc/fdfs
cp -f fdfs_trackerd /usr/bin
if [ ! -f /etc/fdfs/tracker.conf.sample ]; then cp -f ../conf/tracker.conf /etc/fdfs/tracker.conf.sample; fi
mkdir -p /usr/bin
mkdir -p /etc/fdfs
cp -f fdfs_storaged  /usr/bin
if [ ! -f /etc/fdfs/storage.conf.sample ]; then cp -f ../conf/storage.conf /etc/fdfs/storage.conf.sample; fi
mkdir -p /usr/bin
mkdir -p /etc/fdfs
mkdir -p /usr/lib64
cp -f fdfs_monitor fdfs_test fdfs_test1 fdfs_crc32 fdfs_upload_file fdfs_download_file fdfs_delete_file fdfs_file_info fdfs_appender_test fdfs_appender_test1 fdfs_append_file fdfs_upload_appender /usr/bin
if [ 0 -eq 1 ]; then cp -f libfdfsclient.a /usr/lib64; fi
if [ 1 -eq 1 ]; then cp -f libfdfsclient.so /usr/lib64; fi
mkdir -p /usr/include/fastdfs
cp -f ../common/fdfs_define.h ../common/fdfs_global.h ../common/mime_file_parser.h ../common/fdfs_http_shared.h ../tracker/tracker_types.h ../tracker/tracker_proto.h ../tracker/fdfs_shared_func.h ../storage/trunk_mgr/trunk_shared.h tracker_client.h storage_client.h storage_client1.h client_func.h client_global.h fdfs_client.h /usr/include/fastdfs
if [ ! -f /etc/fdfs/client.conf.sample ]; then cp -f ../conf/client.conf /etc/fdfs/client.conf.sample; fi

服務腳本在:

ls /etc/init.d/ | grep fdfs
fdfs_storaged
fdfs_trackerd

配置文件在:

ls /etc/fdfs/
client.conf.sample  storage.conf.sample  tracker.conf.sample

命令行工具在/usr/bin/目錄下

ls /usr/bin/ | grep fdfs
fdfs_appender_test
fdfs_appender_test1
fdfs_append_file
fdfs_crc32
fdfs_delete_file
fdfs_download_file
fdfs_file_info
fdfs_monitor
fdfs_storaged
fdfs_test
fdfs_test1
fdfs_trackerd
fdfs_upload_appender
fdfs_upload_file

由於FastDFS服務腳本設置的bin目錄爲/usr/local/bin/下,可是實際咱們安裝在了/usr/bin/下面。因此咱們須要修改FastDFS配置文件中的路徑,也就是須要修改倆 個配置文件:

  • 命令:vim /etc/init.d/fdfs_storaged

  • 進行全局替換命令:%s+/usr/local/bin+/usr/bin

  • 命令:vim /etc/init.d/fdfs_trackerd

  • 進行全局替換命令:%s+/usr/local/bin+/usr/bin

配置跟蹤器(trackerd)

修改配置文件

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

修改

# the base path to store data and log files
base_path=/fastdfs/tracker

mkdir -p /fastdfs/tracker

  • 目錄命令:cd /fastdfs/tracker/ && ll

  • 啓動tracker命令:/etc/init.d/fdfs_trackerd start

  • 查看進程命令:ps -el | grep fdfs

  • 中止tracker命令:/etc/init.d/fdfs_trackerd stop

查看是否啓動成功

啓動前:
cd /fastdfs/tracker/ && ll
total 0
啓動

/etc/init.d/fdfs_trackerd start

啓動成功
cd /fastdfs/tracker/ && ll
total 8
drwxr-xr-x. 2 root root 4096 Dec 14 20:48 data
drwxr-xr-x. 2 root root 4096 Dec 14 20:48 logs

能夠設置開機啓動跟蹤器:(通常生產環境須要開機啓動一些服務,如keepalived、Nginx、tomcat等等)

  • 命令:vim /etc/rc.d/rc.local

  • 加入配置:/etc/init.d/fdfs_trackerd start

配置FastDFS存儲(storaged)

修改配置文件storage.conf

  • 進入文件目錄:cd /etc/fdfs/,進行copy storage文件一份

  • 命令:cd /etc/fdfs/

  • 命令:cp storage.conf.sample storage.conf

  • 命令:vim /etc/fdfs/storage.conf

修改內容:

base_path=/fastdfs/storage
store_path0=/fastdfs/storage
tracker_server=192.168.1.1:22122
http.server_port=80

建立存儲目錄:

mkdir -p /fastdfs/storage

啓動存儲(storage)

命令:/etc/init.d/fdfs_storaged start

查看是否啓動成功

ps -ef | grep fdfs

能夠設置開機啓動跟蹤器:(通常生產環境須要開機啓動一些服務,如keepaliv ed、linux、tomcat等等)

  • 命令:vim /etc/rc.d/rc.local

  • 加入配置:/etc/init.d/fdfs_storaged start

測試

首先咱們在跟蹤器(tracker)裏copy一份client.conf文件。

命令:cd /etc/fdfs/

命令:cp client.conf.sample client.conf

編輯client.conf文件

vim /etc/fdfs/client.conf

修改內容:

base_path=/fastdfs/tracker
tracker_server=**192.168.1.1**:22122

測試上傳

/usr/bin/fdfs_upload_file /etc/fdfs/client.conf /root/FastDFS/libfastcommon-master.zip

打印:

group1/M00/00/00/2-YyVlhRQlaAZLlgAAGP6hUWM6I411.zip

存儲器查看文件

cd /fastdfs/storage/data/00/00/ && ll
total 1
-rw-r--r--. 1 root root 102378 Dec 14 21:00 2-YyVlhRQlaAZLlgAAGP6hUWM6I411.zip

FastDFS與Nginx整合(Storage)

解壓Nginx

tar -zxvf nginx-1.6.2.tar.gz

安裝fastdfs-nginx-module_v1.16.tar.gz

tar -zxvf FastDFS/fastdfs-nginx-module_v1.16.tar.gz -C /usr/local/fast/
cd /usr/local/fast/fastdfs-nginx-module/src/

編輯配置文件config

vim /usr/local/fast/fastdfs-nginx-module/src/config

修改前:
CORE_INCS="$CORE_INCS /usr/local/include/fastdfs /usr/local/include/fastcommon/"
修改後:
CORE_INCS="$CORE_INCS /usr/include/fastdfs /usr/include/fastcommon/"

FastDFS與Nginx進行集成

cd nginx-1.6.2/

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

make && make install

如報錯 自行安裝pcre

複製、修改配置文件

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

vim /etc/fdfs/mod_fastdfs.conf

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

複製FastDFS裏的2個文件

cd /usr/local/fast/FastDFS/conf/

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

創建軟鏈接

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

修改Nginx配置

vim nginx.conf

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

啓動Nginx

/usr/local/nginx/sbin/nginx

報錯: error while loading shared libraries: libpcre.so.1: cannot open shared object file: No such file or directory

  • 32位系統 ln -s /usr/local/lib/libpcre.so.1 /lib

  • 64位系統ln -s /usr/local/lib/libpcre.so.1 /lib64

測試

訪問 http://192.168.1.2/group1/M00/00/00/2-YyVlhRQlaAZLlgAAGP6hUWM6I411.zip

  • 啓動中止命令

  • 啓動命令: 啓動tracker命令:/etc/init.d/fdfs_trackerd start

  • 查看進程命令:ps -el | grep fdfs

  • 啓動storage命令:/etc/init.d/fdfs_storaged start

  • 查看進程命令:ps -el | grep fdfs

  • 啓動nginx命令:/usr/local/nginx/sbin/nginx

  • 中止命令: 中止tracker命令:/etc/init.d/fdfs_trackerd stop

  • 關閉storage命令:/etc/init.d/fdfs_storaged stop

  • 關閉nginx命令:/usr/local/nginx/sbin/nginx -s stop

使用Java客戶端操做

classpath下創建文件fastdfs_client.conf

輸入如下內容

tracker_server = 192.168.125.129:22122

package vip.xubin.utils;import org.apache.commons.lang3.StringUtils;import org.apache.log4j.Logger;import org.csource.common.NameValuePair;import org.csource.fastdfs.*;import java.io.*;/**
 * FastDFS 工具類
 */public class FastDFSClientUtils {

    private static final String CONF_FILENAME = Thread.currentThread().getContextClassLoader().getResource("fastdfs_client.conf").getPath();

    private static Logger logger = Logger.getLogger(FastDFSClientUtils.class);


    private static TrackerClient trackerClient;


    //加載文件
    static {
        try {
            ClientGlobal.init(CONF_FILENAME);
            TrackerGroup trackerGroup = ClientGlobal.g_tracker_group;
            trackerClient = new TrackerClient(trackerGroup);
        } catch (Exception e) {
            logger.error(e);
        }
    }

    /**
     * 上傳
     * @param file 文件
     * @param path 路徑
     * @return
     *          上傳成功返回id,失敗返回null
     */
    public static String upload(File file, String path) {
        TrackerServer trackerServer = null;
        StorageServer storageServer = null;
        StorageClient1 storageClient1 = null;
        FileInputStream fis = null;
        try {
            NameValuePair[] meta_list = null; // new NameValuePair[0];
            fis = new FileInputStream(file);
            byte[] file_buff = null;
            if (fis != null) {
                int len = fis.available();
                file_buff = new byte[len];
                fis.read(file_buff);
            }

            trackerServer = trackerClient.getConnection();
            if (trackerServer == null) {
                logger.error("getConnection return null");
            }
            storageServer = trackerClient.getStoreStorage(trackerServer);
            storageClient1 = new StorageClient1(trackerServer, storageServer);
            String fileid = storageClient1.upload_file1(file_buff, getFileExt(path), meta_list);

            return fileid;
        } catch (Exception ex) {
            logger.error(ex);
            return null;
        }finally{
            if (fis != null){
                try {
                    fis.close();
                } catch (IOException e) {
                    logger.error(e);
                }
            }
            if (storageServer != null){
                try {
                    storageServer.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (trackerServer != null){
                try {
                    trackerServer.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            storageClient1 = null;
        }
    }

    /**
     * 上傳
     * @param data 數據
     * @param extName 路徑
     * @return
     *          上傳成功返回id,失敗返回null
     */
    public static String upload(byte[] data, String extName) {
        TrackerServer trackerServer = null;
        StorageServer storageServer = null;
        StorageClient1 storageClient1 = null;
        try {
            NameValuePair[] meta_list = null; // new NameValuePair[0];

            trackerServer = trackerClient.getConnection();
            if (trackerServer == null) {
                logger.error("getConnection return null");
            }
            storageServer = trackerClient.getStoreStorage(trackerServer);
            storageClient1 = new StorageClient1(trackerServer, storageServer);
            String fileid = storageClient1.upload_file1(data, extName, meta_list);
            return fileid;
        } catch (Exception ex) {
            logger.error(ex);
            return null;
        }finally{
            if (storageServer != null){
                try {
                    storageServer.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (trackerServer != null){
                try {
                    trackerServer.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            storageClient1 = null;
        }
    }

    /**
     * 下載
     * @param fileId 文件id
     * @return
     *          返回InputStream
     */
    public static InputStream download(String groupName, String fileId) {
        TrackerServer trackerServer = null;
        StorageServer storageServer = null;
        StorageClient1 storageClient1 = null;
        try {
            trackerServer = trackerClient.getConnection();
            if (trackerServer == null) {
                logger.error("getConnection return null");
            }
            storageServer = trackerClient.getStoreStorage(trackerServer, groupName);
            storageClient1 = new StorageClient1(trackerServer, storageServer);
            byte[] bytes = storageClient1.download_file1(fileId);
            InputStream inputStream = new ByteArrayInputStream(bytes);
            return inputStream;
        } catch (Exception ex) {
            logger.error(ex);
            return null;
        } finally {
            if (storageServer != null){
                try {
                    storageServer.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (trackerServer != null){
                try {
                    trackerServer.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            storageClient1 = null;
        }
    }

    /**
     * 刪除
     * @param fileId 文件id
     * @return
     *          刪除成功返回0,非0則操做失敗,返回錯誤代碼
     */
    public static int delete(String groupName, String fileId) {
        TrackerServer trackerServer = null;
        StorageServer storageServer = null;
        StorageClient1 storageClient1 = null;
        try {
            trackerServer = trackerClient.getConnection();
            if (trackerServer == null) {
                logger.error("getConnection return null");
            }
            storageServer = trackerClient.getStoreStorage(trackerServer, groupName);
            storageClient1 = new StorageClient1(trackerServer, storageServer);
            int result = storageClient1.delete_file1(fileId);
            return result;
        } catch (Exception ex) {
            logger.error(ex);
            return 0;
        } finally {
            if (storageServer != null){
                try {
                    storageServer.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (trackerServer != null){
                try {
                    trackerServer.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            storageClient1 = null;
        }
    }

    /**
     * 更新文件
     * @param oldFileId 舊文件id
     * @param file 新文件
     * @param path 新文件路徑
     * @return
     *          上傳成功返回id,失敗返回null
     */
    public static String modify(String oldGroupName, String oldFileId, File file, String path) {
        String fileid = null;
        try {
            // 先上傳
            fileid = upload(file, path);
            if (fileid == null) {
                return null;
            }
            // 再刪除
            int delResult = delete(oldGroupName, oldFileId);
            if (delResult != 0) {
                return null;
            }
        } catch (Exception ex) {
            logger.error(ex);
            return null;
        }
        return fileid;
    }

    /**
     * 獲取文件後綴名
     * @param fileName
     * @return
     *          如:"jpg"、"txt"、"zip" 等
     */
    private static String getFileExt(String fileName) {
        if (StringUtils.isBlank(fileName) || !fileName.contains(".")) {
            return "";
        } else {
            return fileName.substring(fileName.lastIndexOf(".") + 1);
        }
    }}

總結

FastDFS的安裝算是比較麻煩的 不過按照本篇 一步一步來 是絕對能夠安裝成功的 集羣的安裝就更麻煩了 我的暫時用不到 就不安裝了

參考

相關文章
相關標籤/搜索