FastDfs單機版搭建


詳細的最新版fastdfs單機版搭建

前言

  目前項目是tomcat單機部署的,圖片、視頻也是上傳到tomcat目錄下,關鍵是此項目的主要內容還就是針對圖片、視頻的,這讓我非常擔憂;文件服務器的應用是必然的,而且時間還不會太久。之前一直有聽說fastdfs,但一直沒去認真琢磨他,最近才開始去研究它,今天只是去搭建一個簡單的單機版,集群版后續再出;至於架構、原理什么我就不寫了,網上資料非常多。

環境准備

  系統:Centos6.7

  fastdfs:到https://github.com/happyfish100下載,都下載最新的:fastdfs-master、libfastcommon-master、fastdfs-nginx-module-master,避免版本問題

  Linux終端工具:xshell、xftp

       Linux ip:192.168.1.207

fastdfs安裝

       上傳相關包到/opt下,如圖

  安裝zip、unzip

              [root@fastdfs2 opt]# yum install -y unzip zip

       解壓fastdfs-master.zip

              [root@fastdfs2 opt]# unzip -o fastdfs-master.zip -d /usr/local

       編譯安裝fast

              [root@fastdfs2 opt]# cd /usr/local/fastdfs-master

              [root@fastdfs2 fastdfs-master]# ./make.sh

              報錯:./make.sh: line 146: perl: command not found

       安裝perl

              [root@fastdfs2 fastdfs-master]# yum -y install perl

              再運行./make.sh,報錯:make: cc:命令未找到

       安裝gcc

              [root@fastdfs2 fastdfs-master]# yum install gcc-c++

              再運行./make.sh,仍出現如下錯誤:

    缺少libfastcommon中的相關基礎庫

       安裝libfastcommon

              解壓libfastcommon-master.zip

                     [root@fastdfs2 fastdfs-master]# cd /opt

                     [root@fastdfs2 opt]# unzip -o libfastcommon-master.zip -d /usr/local

              安裝libfastcommon

                     [root@fastdfs2 opt]# cd /usr/local/libfastcommon-master/

                     [root@fastdfs2 libfastcommon-master]# ./make.sh

                     [root@fastdfs2 libfastcommon-master]# ./make.sh install

       再裝fastdfs

              [root@fastdfs2 local]# cd /usr/local/fastdfs-master/

              [root@fastdfs2 fastdfs-master]# ./make.sh

              [root@fastdfs2 fastdfs-master]# ./make.sh install

       拷貝配置文件

              將fastdfs安裝目錄下的conf下的文件拷貝到/etc/fdfs/下

              [root@fastdfs2 fastdfs-master]# cp -r conf/* /etc/fdfs/

       自此fastdfs安裝完成了,接下來配置trackerd和storaged,並啟動它們。

  fdfs可執行命令

         [root@fastdfs2 fdfs]# ll /usr/bin/fdfs*

-rwxr-xr-x. 1 root root 210899 6月   8 21:00 /usr/bin/fdfs_appender_test
-rwxr-xr-x. 1 root root 210868 6月   8 21:00 /usr/bin/fdfs_appender_test1 -rwxr-xr-x. 1 root root 198740 6月 8 21:00 /usr/bin/fdfs_append_file -rwxr-xr-x. 1 root root 198044 6月 8 21:00 /usr/bin/fdfs_crc32 -rwxr-xr-x. 1 root root 198735 6月 8 21:00 /usr/bin/fdfs_delete_file -rwxr-xr-x. 1 root root 199112 6月 8 21:00 /usr/bin/fdfs_download_file -rwxr-xr-x. 1 root root 198960 6月 8 21:00 /usr/bin/fdfs_file_info -rwxr-xr-x. 1 root root 211836 6月 8 21:00 /usr/bin/fdfs_monitor -rwxr-xr-x. 1 root root 762687 6月 8 21:00 /usr/bin/fdfs_storaged -rwxr-xr-x. 1 root root 212997 6月 8 21:00 /usr/bin/fdfs_test -rwxr-xr-x. 1 root root 212618 6月 8 21:00 /usr/bin/fdfs_test1 -rwxr-xr-x. 1 root root 329821 6月 8 21:00 /usr/bin/fdfs_trackerd -rwxr-xr-x. 1 root root 199076 6月 8 21:00 /usr/bin/fdfs_upload_appender -rwxr-xr-x. 1 root root 203766 6月 8 21:00 /usr/bin/fdfs_upload_file

配置並啟動trackerd

       修改trackerd.conf

              不改也可以,但是要保證/home/yuqing/fastdfs路徑存在

              [root@fastdfs2 fdfs]# cd /etc/fdfs/

              [root@fastdfs2 fdfs]# vi tracker.conf

              將base_path=/home/yuqing/fastdfs改成base_path=/data/fastdfs

       創建trackerd數據、日志目錄

              [root@fastdfs2 fdfs]# mkdir -p /data/fastdfs

       啟動trackerd

              [root@fastdfs2 fdfs]# /usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf restart

              查看trackerd進程,如下圖:

    說明trackered已經啟動起來;其實也可以查看日志:/data/fastdfs/logs/trackerd.log,來判斷trackerd是否正常啟動起來。

配置並啟動storaged

       修改storage.conf

              [root@fastdfs2 fdfs]# cd /etc/fdfs/

              [root@fastdfs2 fdfs]# vi storage.conf

              base_path=/home/yuqing/fastdfs改為:base_path=/data/fastdfs

              store_path0=/home/yuqing/fastdfs改為:store_path0=/data/fastdfs/storage

              tracker_server=192.168.209.121:22122改為:tracker_server=192.168.1.207:22122,這個ip改成自己的

       創建storaged數據、日志目錄

              [root@fastdfs2 fdfs]# mkdir -p /data/fastdfs/storage

       啟動storaged

              [root@fastdfs2 fdfs]# /usr/bin/fdfs_storaged /etc/fdfs/storage.conf restart

              查看storaged進程,如下圖:

    說明storaged已經啟動起來;其實也可以查看日志:/data/fastdfs/logs/storaged.log來判斷storaged是否正常啟動起來。

上傳圖片測試

       本地(win環境)安裝fastdfs連接驅動

              從https://github.com/happyfish100/fastdfs-client-java下載源碼,我下載的是zip包,解壓后目錄如下圖:

    maven本地安裝:mvn clean install

    

    當然也可以用ant構建:ant clean package

  書寫測試代碼

    代碼結構如圖

    fdfs_client_mine.conf:

connect_timeout = 2
network_timeout = 30 charset = UTF-8 http.tracker_http_port = 8080 http.anti_steal_token = no http.secret_key = FastDFS1234567890 tracker_server = 192.168.1.207:22122

              FastdfsClientTest.java:

package com.lee.fastdfs.test; import org.csource.common.NameValuePair; import org.csource.fastdfs.*; import org.junit.Test; import java.io.FileNotFoundException; import java.io.IOException; public class FastdfsClientTest { //客戶端配置文件 public String conf_filename = "fdfs_client_mine.conf"; //本地文件,要上傳的文件 public String local_filename = "G:\\IdeaProjects\\fastdfs\\src\\main\\resources\\mygirl.jpg"; //上傳文件  @Test public void testUpload() { for(int i=0;i<10;i++){ try { ClientGlobal.init(conf_filename); TrackerClient tracker = new TrackerClient(); TrackerServer trackerServer = tracker.getConnection(); StorageServer storageServer = null; StorageClient storageClient = new StorageClient(trackerServer, storageServer); NameValuePair nvp [] = new NameValuePair[]{ new NameValuePair("item_id", "100010"), new NameValuePair("width", "80"), new NameValuePair("height", "90") }; String fileIds[] = storageClient.upload_file(local_filename, null, nvp); System.out.println(fileIds.length); System.out.println("組名:" + fileIds[0]); System.out.println("路徑: " + fileIds[1]); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } } } }

       上傳圖片:mygirl.jpg

              執行測試代碼,當輸出如下信息時,表示上傳成功:

2
組名:group1
路徑: M00/00/00/wKgBz1salX-AKZMdAABHO7x65CM183.jpg 2 組名:group1 路徑: M00/00/00/wKgBz1salX-ATR4PAABHO7x65CM553.jpg 2 組名:group1 路徑: M00/00/00/wKgBz1salX-AKFyFAABHO7x65CM038.jpg 2 組名:group1 路徑: M00/00/00/wKgBz1salX-ASse4AABHO7x65CM705.jpg 2 組名:group1 路徑: M00/00/00/wKgBz1salX-AKUn7AABHO7x65CM247.jpg 2 組名:group1 路徑: M00/00/00/wKgBz1salX-AdT4cAABHO7x65CM342.jpg 2 組名:group1 路徑: M00/00/00/wKgBz1salX-AR0V0AABHO7x65CM992.jpg 2 組名:group1 路徑: M00/00/00/wKgBz1salX-AMpoLAABHO7x65CM691.jpg 2 組名:group1 路徑: M00/00/00/wKgBz1salX-AcjanAABHO7x65CM587.jpg 2 組名:group1 路徑: M00/00/00/wKgBz1salX-Ab8qnAABHO7x65CM687.jpg

    我們到服務器上看看,圖片確實已經上傳了,如下圖:

    由於現在還沒有和nginx整合無法使用http下載。

FastDFS 和nginx整合

       fastdfs-nginx-module安裝

              解壓

                     [root@fastdfs2 00]# cd /opt

                     [root@fastdfs2 opt]# unzip -o fastdfs-nginx-module-master.zip -d /usr/local

              拷貝配置文件

                     [root@fastdfs2 opt]# cd /usr/local/fastdfs-nginx-module-master/src

                     [root@fastdfs2 src]# cp mod_fastdfs.conf /etc/fdfs/

              編輯配置文件

                     [root@fastdfs2 src]# cd /etc/fdfs/

                     [root@fastdfs2 fdfs]# vi mod_fastdfs.conf

                     base_path=/tmp改成:base_path=/data/fastdfs

                     tracker_server=tracker:22122改成:tracker_server=192.168.1.207:22122

                     url_have_group_name = false改成:url_have_group_name = true;#url中包含group名稱

                     store_path0=/home/yuqing/fastdfs改成:store_path0=/data/fastdfs/storage

       nginx安裝

              nginx依賴包安裝

                     [root@fastdfs2 fdfs]# cd /opt

                     [root@fastdfs2 opt]# yum -y install zlib zlib-devel openssl openssl--devel pcre pcre-devel

              解壓nginx

                     [root@fastdfs2 opt]# tar -zxvf nginx-1.13.12.tar.gz

              安裝nginx並添加fastdfs模塊

                     [root@fastdfs2 opt]# cd nginx-1.13.12

                     [root@fastdfs2 nginx-1.13.12]# ./configure --prefix=/usr/local/nginx --add-module=/usr/local/fastdfs-nginx-module-master/src

                     [root@fastdfs2 nginx-1.13.12]# make

                     [root@fastdfs2 nginx-1.13.12]# make install

              檢查nginx模塊

                     [root@fastdfs2 nginx-1.13.12]# cd /usr/local/nginx/sbin/

                     [root@fastdfs2 sbin]# ./nginx -V

nginx version: nginx/1.13.12
uilt by gcc 4.4.7 20120313 (Red Hat 4.4.7-18) (GCC) configure arguments: --prefix=/usr/local/nginx --add-module=/usr/local/fastdfs-nginx-module-master/src

                     已經把fastdfs模塊添加進去了。

              配置nginx配置文件

                     [root@fastdfs2 sbin]# cd /usr/local/nginx/conf

                     [root@fastdfs2 conf]# vi nginx-fdfs.conf

                     內容如下,ip注意改成自己的:

events {
    use epoll;

    worker_connections  1024; } http { server { listen 80; server_name 192.168.1.207; location /group1/M00/{ #root /home/FastDFS/fdfs_storage/data; ngx_fastdfs_module; } } server { listen 8888; server_name 192.168.1.207; location / { root html; index index.html index.htm; } } }

  啟動nginx

                     [root@fastdfs2 conf]# cd /usr/local/nginx/sbin/

                     [root@fastdfs2 sbin]# ./nginx -c /usr/local/nginx/conf/nginx-fdfs.conf

       訪問圖片

              文件路徑在上面的上傳圖片的測試代碼中有輸入,我們進行拼裝下:

              http://192.168.1.207/group1/M00/00/00/wKgBz1salX-ATR4PAABHO7x65CM553.jpg

 總結

  1、fastdfs相關包推薦直接到官網下載,能避免因為版本而造成的問題

  2、上傳成功而訪問卻出現nginx:400問題,極有可能url_have_group_name = false沒有改成:url_have_group_name = true;


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM