fastDFS搭建

1.1.1  FastDFS安裝環境

   FastDFS是C語言開發,建議在linux上運行,本教程使用Centos6.5做爲安裝環境。java

   安裝FastDFS須要先將官網下載的源碼進行編譯,編譯依賴gcc環境,若是沒有gcc環境,須要安裝gcc:yum install gcc-c++linux

1.1.2  安裝libevent

FastDFS依賴libevent庫,須要安裝:nginx

yum -y install libeventc++

1.1.3  安裝libfastcommon

         libfastcommon是FastDFS官方提供的,libfastcommon包含了FastDFS運行所須要的一些基礎庫。tomcat

將libfastcommonV1.0.7.tar.gz拷貝至/usr/local/下服務器

cd /usr/localapp

tar -zxvf libfastcommonV1.0.7.tar.gz測試

cd libfastcommon-1.0.7url

./make.shspa

./make.sh install

 

注意:libfastcommon安裝好後會自動將庫文件拷貝至/usr/lib64下,因爲FastDFS程序引用usr/lib目錄因此須要將/usr/lib64下的庫文件拷貝至/usr/lib下。

要拷貝的文件以下:

1.1.4  tracker編譯安裝

將FastDFS_v5.05.tar.gz拷貝至/usr/local/下

tar -zxvf FastDFS_v5.05.tar.gz

 

cd FastDFS

 

./make.sh

./make.sh install

 

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

2.1.1  配置

安裝成功後進入/etc/fdfs目錄:

 

拷貝一份新的tracker配置文件:

cp tracker.conf.sample tracker.conf

 

修改tracker.conf

vi tracker.conf

base_path=/home/yuqing/FastDFS  

改成:

base_path=/home/FastDFS

配置http端口:

http.server_port=80

 

2.1.2  啓動

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

啓動的日誌顯示先中止5619進程(實際環境不是5619)再啓動,以下圖:

注意:若是沒有顯示上圖要注意是否正常中止原有進程。

 

2.1    FastDFS--storage安裝

分別在192.168.101.五、192.168.101.六、192.168.101.七、192.168.101.8上安裝storage。

 

注:初次安裝可只安裝一臺storage方便調試。安裝步驟是同樣的

 

3.1.1  配置

安裝成功後進入/etc/fdfs目錄:

拷貝一份新的storage配置文件:

cp storage.conf.sample storage.conf

 

修改storage.conf

vi storage.conf

group_name=group1

base_path=/home/yuqing/FastDFS改成:base_path=/home/FastDFS

store_path0=/home/yuqing/FastDFS改成:store_path0=/home/FastDFS/fdfs_storage

#若是有多個掛載磁盤則定義多個store_path,以下

#store_path1=.....

#store_path2=......
tracker_server=192.168.101.3:22122   #配置tracker服務器:IP

#若是有多個則配置多個tracker

tracker_server=192.168.101.4:22122

 

#配置http端口

http.server_port=80

 

3.1.2  啓動

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

啓動的日誌顯示先中止8931進程(實際環境不是8931)再啓動,以下圖:

注意:若是沒有顯示上圖要注意是否正常中止原有進程。

 

4.1    上傳圖片測試

4.1.1  經過fdfs_test程序

astDFS安裝成功可經過/usr/bin/fdfs_test測試上傳、下載等操做。

 

修改/etc/fdfs/client.conf

 

base_path=/home/fastdfs

tracker_server=192.168.101.3:22122

tracker_server=192.168.101.4:22122

 

使用格式:

/usr/bin/fdfs_test 客戶端配置文件地址  upload  上傳文件

 

好比將/home下的圖片上傳到FastDFS中:

/usr/bin/fdfs_test /etc/fdfs/client.conf upload /home/tomcat.png

 

打印以下日誌:

This is FastDFS client test program v5.05

 

Copyright (C) 2008, Happy Fish / YuQing

 

FastDFS may be copied only under the terms of the GNU General

Public License V3, which may be found in the FastDFS source kit.

Please visit the FastDFS Home Page http://www.csource.org/

for more detail.

 

[2015-05-18 02:07:10] DEBUG - base_path=/home/FastDFS, connect_timeout=30, network_timeout=60, tracker_server_count=1, anti_steal_token=0, anti_steal_secret_key length=0, use_connection_pool=0, g_connection_pool_max_idle_time=3600s, use_storage_id=0, storage server id count: 0

 

tracker_query_storage_store_list_without_group:

         server 1. group_name=, ip_addr=192.168.101.5, port=23000

         server 2. group_name=, ip_addr=192.168.101.6, port=23000

 

group_name=group1, ip_addr=192.168.101.5, port=23000

storage_upload_by_filename

group_name=group1, remote_filename=M00/00/00/wKhlBVVY2M-AM_9DAAAT7-0xdqM485.png

source ip address: 192.168.101.5

file timestamp=2015-05-18 02:07:11

file size=5103

file crc32=3979441827

example file url: http://192.168.101.5/group1/M00/00/00/wKhlBVVY2M-AM_9DAAAT7-0xdqM485.png

storage_upload_slave_by_filename

group_name=group1, remote_filename=M00/00/00/wKhlBVVY2M-AM_9DAAAT7-0xdqM485_big.png

source ip address: 192.168.101.5

file timestamp=2015-05-18 02:07:11

file size=5103

file crc32=3979441827

example file url: http://192.168.101.5/group1/M00/00/00/wKhlBVVY2M-AM_9DAAAT7-0xdqM485_big.png

 

 

http://192.168.101.5/group1/M00/00/00/wKhlBVVY2M-AM_9DAAAT7-0xdqM485_big.png就是文件的下載路徑。

對應storage服務器上的

/home/fastdfs/fdfs_storage/data/00/00/wKhlBVVY2M-AM_9DAAAT7-0xdqM485_big.png文件。

 

因爲如今尚未和nginx整合沒法使用http下載。

 

4.1.2  經過java Client API

完整的測試方法參考測試源代碼。

public class FastdfsClientTest {

   

    //客戶端配置文件

    public String conf_filename = "F:\\workspace_indigo\\fastdfsClient\\src\\cn\\itcast\\fastdfs\\cliennt\\fdfs_client.conf";

    //本地文件,要上傳的文件

    public String local_filename = "F:\\develop\\upload\\linshiyaopinxinxi_20140423193847.xlsx";

 

//上傳文件

    @Test

    public void testUpload() {

       

        for(int i=0;i<100;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();

       }

        }

    }

 

}

相關文章
相關標籤/搜索