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();

       }

        }

    }

 

}

相关文章
相关标签/搜索