天天看點

V 11 FastDFS

一、概念:

<a href="https://github.com/happyfish100/fastdfs" target="_blank">https://github.com/happyfish100/fastdfs</a>

FastDFS is an open source high performance distributed file system (DFS). It's major functions include: file storing, file syncing and file accessing, and design for high capacity and load balance.

<a href="https://github.com/happyfish100/fastdfs-client-java" target="_blank">https://github.com/happyfish100/fastdfs-client-java</a>

<a href="https://github.com/happyfish100/fastdfs-nginx-module" target="_blank">https://github.com/happyfish100/fastdfs-nginx-module</a>

FastDFS(fast distribution filesystem)是一款開源的輕量級分布式FS,純C實作,支援linux、freeBSD等類unix系統,類似googleFS,不是通用的FS,隻能通過專有API通路,目前提供了C、java、php、.NET的API,為網際網路應用量身定做,追求高性能和高擴充性,可看作是基于檔案的key-valu pair存儲系統,稱作分布式檔案存儲服務更為合适,更适合存儲中小檔案,如4KB-500MB;最早在ali,是易道用車架構師餘慶寫的,現應用在jd、taobao、58、uc、51cto;

分布式FS(支援FUSE;不支援FUSE);

分布式FS(把檔案分片存放;按原本檔案存放);

<a href="https://s2.51cto.com/wyfs02/M02/8C/D4/wKiom1h57PuSxow_AAA8dLS7U-0626.jpg" target="_blank"></a>

注:

fuse,FS in userspace,使用者空間檔案系統,是Linux中用于挂載某些網絡空間,如SSH,到本地檔案系統的子產品,在SourceForge上可以找到相關内容;三種模式:核心模式、使用者空間模式、mount工具;

fastdfs兩種角色(tracker、storage):

tracker server(追蹤伺服器,主要做排程工作,在通路中起LB作用,在記憶體中記錄叢集中group和storage server的狀态資訊,是連接配接client和storage server的樞紐,因為相關資訊全部在記憶體中,tracker server的性能非常高(它本身所需負載很小),一個較大的叢集中(如上百個group)有3台就足夠);

storage server(存儲伺服器,檔案和檔案屬性(如metadata)都儲存在該server上);

<a href="https://s4.51cto.com/wyfs02/M02/8C/D1/wKioL1h57Q2QaLiRAABRGo3L7ZU929.jpg" target="_blank"></a>

storage裡同組内的兩個server裡存儲的内容完全相同(相當于raid1);

storage叢集中,組(或叫卷)群組之間不通信是互相獨立的,storage主動向tracker彙報狀态,所有組的容量累加就是整個存儲系統中的檔案容量,一個組可由一台或多台storage server組成,一個組内的所有單個存儲伺服器中的檔案都是相同的,一組中的多台存儲伺服器起到了備援備份和負載均衡的作用,在組中增加伺服器時,同步已有的檔案由系統自動完成,同步完成後,系統自動将新增伺服器切換到線上提供服務,當存儲空間不足或即将耗盡時,可動态添加組,隻需要增加一台或多台伺服器,并将它們配置為一個新的組,這樣就擴大了存儲系統的容量;

client與tracker聯系,由tracker配置設定storage給client(類似http的301跳轉);

擴充整個fastdfs叢集容量,在storage裡加group即可;

fastdfs上傳機制:

上傳檔案時,file_id由storage server生成并傳回給client,file_id包含了組名、磁盤、目錄、檔案名,storage server可直接根據file_id定位到檔案;

<a href="https://s4.51cto.com/wyfs02/M00/8C/D6/wKiom1h6KYmDgwHWAABVxMRG1Uo130.jpg" target="_blank"></a>

fastdfs存儲檔案位置file_id:

file_id包含了組名、磁盤、目錄、檔案名,storageserver可直接根據file_id定位到檔案;

<a href="https://s4.51cto.com/wyfs02/M00/8C/D1/wKioL1h57TDAJerZAAAsmohRnuM437.jpg" target="_blank"></a>

fastdfs下載下傳機制:

<a href="https://s2.51cto.com/wyfs02/M00/8C/D6/wKiom1h6KZqhYYwWAABGvSOsjBk722.jpg" target="_blank"></a>

fastdfs同步機制:

<a href="https://s4.51cto.com/wyfs02/M01/8C/D1/wKioL1h57Zeg-Rm4AAA3HrSyCdc811.jpg" target="_blank"></a>

fastdfs查找檔案:

<a href="https://s5.51cto.com/wyfs02/M02/8C/D1/wKioL1h57iOiHcQGAAA8szYX9AQ531.jpg" target="_blank"></a>

二、操作:

node1(test5;192.168.23.133);

node2(test6;192.168.23.134);

node{1,2}既作為tracker server也作為storageserver;

<a href="https://github.com/happyfish100/libfastcommon" target="_blank">https://github.com/happyfish100/libfastcommon</a>

[root@test5 ~]# git clone https://github.com/happyfish100/libfastcommon   #(或下載下傳release版本,libfastcommon-1.0.7.tar.gz)

Initialized empty Git repository in/root/libfastcommon/.git/

remote: Counting objects: 1839, done.

remote: Compressing objects: 100% (12/12),done.

remote: Total 1839 (delta 4), reused 0(delta 0), pack-reused 1827

Receiving objects: 100% (1839/1839), 559.00KiB | 250 KiB/s, done.

Resolving deltas: 100% (1299/1299), done.

[root@test5 ~]# cd libfastcommon/

[root@test5 libfastcommon]# ./make.sh

[root@test5 libfastcommon]# ./make.sh install

<a href="https://github.com/happyfish100/fastdfs/" target="_blank">https://github.com/happyfish100/fastdfs/</a>

fastdfs-5.08.tar.gz

[root@test5 libfastcommon]# cd

[root@test5 ~]# tar xf fastdfs-5.08.tar.gz

[root@test5 ~]# cd fastdfs-5.08

[root@test5 fastdfs-5.08]# ./make.sh

[root@test5 fastdfs-5.08]# ./make.sh install

[root@test5 fastdfs-5.08]# cd

[root@test5 ~]# ll /etc/fdfs/   #(三個樣例配置檔案,client.conf.sample,storage.conf.sample,tracker.conf.sample)

total 20

-rw-r--r--. 1 root root 1461 Jan 12 16:57client.conf.sample

-rw-r--r--. 1 root root 7927 Jan 12 16:57storage.conf.sample

-rw-r--r--. 1 root root 7200 Jan 12 16:57tracker.conf.sample

[root@test5 ~]# ls /usr/bin/fdfs_*   #(衆多的操作指令)

/usr/bin/fdfs_appender_test   /usr/bin/fdfs_delete_file    /usr/bin/fdfs_storaged  /usr/bin/fdfs_upload_appender

/usr/bin/fdfs_appender_test1  /usr/bin/fdfs_download_file  /usr/bin/fdfs_test      /usr/bin/fdfs_upload_file

/usr/bin/fdfs_append_file     /usr/bin/fdfs_file_info      /usr/bin/fdfs_test1

/usr/bin/fdfs_crc32           /usr/bin/fdfs_monitor        /usr/bin/fdfs_trackerd

[root@test5 ~]# ll/etc/init.d/{fdfs_storaged,fdfs_trackerd}  #(啟動腳本)

-rwxr-xr-x. 1 root root 918 Jan 12 16:57/etc/init.d/fdfs_storaged

-rwxr-xr-x. 1 root root 920 Jan 12 16:57/etc/init.d/fdfs_trackerd

[root@test5 ~]# mkdir -pv /data/{fdfs_tracker,fdfs_store/{base,store}}

mkdir: created directory `/data'

mkdir: created directory`/data/fdfs_tracker'

mkdir: created directory`/data/fdfs_storage'

mkdir: created directory`/data/fdfs_storage/base'

mkdir: created directory `/data/fdfs_storage/store'

配置tracker server:

[root@test5 ~]# cd /etc/fdfs

[root@test5 fdfs]# cp tracker.conf.sample tracker.conf

[root@test5 fdfs]# vim tracker.conf

disabled=false

bind_addr=

port=22122

connect_timeout=30

network_timeout=60

base_path=/data/fdfs_tracker

max_connections=256

accept_threads=1

store_lookup=2   #(0: round robin; 1:specify group; 2: load balance, select the max free space group to upload file)

store_server=0   #(0: round robin (default);1: the first server order by ip address; 2: the first server order by priority(the minimal))

store_path=0   #(0: round robin; 2: loadbalance, select the max free space path to upload file)

download_server=0   #(0: round robin (default);1: the source storage server which the current file uploaded to)

reserved_storage_space =10%

run_by_group=

run_by_user=   #(不指定,則用哪個使用者運作的就預設是誰)

allow_hosts=*

sync_log_buff_interval = 10

check_active_interval = 120

thread_stack_size = 64KB

storage_ip_changed_auto_adjust = true

[root@test5 ~]# /etc/init.d/fdfs_trackerd start

Starting FastDFS tracker server:

[root@test5 ~]# lsof -i:22122

COMMAND   PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME

fdfs_trac 2379 root    5u IPv4  16322      0t0 TCP *:22122 (LISTEN)

配置storage server:

[root@test5 fdfs]# cp storage.conf.samplestorage.conf

[root@test5 fdfs]# vim storage.conf

group_name=group1

port=23000

heart_beat_interval=30

stat_report_interval=60

base_path=/data/fdfs_storage/base

buff_size = 256KB

work_threads=4

disk_rw_separated = true

disk_reader_threads = 1

disk_writer_threads = 1

store_path_count=1   #(path(disk or mount point) count, defaultvalue is 1)

store_path0=/data/fdfs_storage/store

subdir_count_per_path=256

tracker_server=192.168.23.133:22122

tracker_server=192.168.23.134:22122

[root@test5 ~]# /etc/init.d/fdfs_storaged start

Starting FastDFS storage server:

[root@test5 ~]# lsof -i:23000

fdfs_stor 2435 root    5u IPv4  85931      0t0 TCP *:inovaport1 (LISTEN)

fdfs_stor 2435 root   22u IPv4  85947      0t0 TCP test5:38563-&gt;test6:inovaport1 (ESTABLISHED)

以上在node{1,2}都配置;

在node1配置client:

[root@test5 fdfs]# cp client.conf.sample client.conf

[root@test5 fdfs]# vim client.conf

base_path=/tmp

上傳檔案:

[root@test5 fdfs]# fdfs_upload_file --help

Usage: fdfs_upload_file&lt;config_file&gt; &lt;local_filename&gt; [storage_ip:port] [store_path_index]

[root@test5 fdfs]# fdfs_upload_file /etc/fdfs/client.conf /etc/hosts  #(會傳回file_id)

group1/M00/00/00/wKgXhVh4i9qAbesQAAAAyL1v7g09358326

[root@test5 fdfs]# cat /etc/hosts

127.0.0.1  localhost localhost.localdomain localhost4 localhost4.localdomain4

::1        localhost localhost.localdomain localhost6 localhost6.localdomain6

192.168.23.134        test6

192.168.23.133        test5

[root@test5 fdfs]# cat /data/fdfs_storage/store/data/00/00/wKgXhVh4i9qAbesQAAAAyL1v7g09358326

[root@test5 fdfs]# md5sum /etc/hosts

dec92ea815e2828dde8b607c599b54d5  /etc/hosts

[root@test5 fdfs]# md5sum /data/fdfs_storage/store/data/00/00/wKgXhVh4i9qAbesQAAAAyL1v7g09358326

dec92ea815e2828dde8b607c599b54d5 /data/fdfs_storage/store/data/00/00/wKgXhVh4i9qAbesQAAAAyL1v7g09358326

[root@test5 fdfs]# fdfs_file_info --help

Usage: fdfs_file_info&lt;config_file&gt; &lt;file_id&gt;

[root@test5 fdfs]# fdfs_file_info /etc/fdfs/client.conf group1/M00/00/00/wKgXhVh4i9qAbesQAAAAyL1v7g09358326

source storage id: 0

source ip address: 192.168.23.133

file create timestamp: 2017-01-13 00:12:10

file size: 200

file crc32: 3178229261 (0xBD6FEE0D)

[root@test5 fdfs]# fdfs_delete_file --help

Usage: fdfs_delete_file&lt;config_file&gt; &lt;file_id&gt;

[root@test5 fdfs]# fdfs_delete_file /etc/fdfs/client.conf group1/M00/00/00/wKgXhVh4i9qAbesQAAAAyL1v7g09358326

[root@test5 fdfs]# fdfs_upload_appender --help

Usage:fdfs_upload_appender &lt;config_file&gt; &lt;local_filename&gt;

[root@test5 fdfs]# vim /tmp/append.txt

1

2

3

[root@test5 fdfs]# fdfs_upload_appender /etc/fdfs/client.conf /tmp/append.txt

group1/M00/00/00/wKgXhlh4llOET8jHAAAAAMmGRic964.txt

[root@test5 fdfs]# vim /tmp/append2.txt

4

5

6

[root@test5 fdfs]# fdfs_append_file /etc/fdfs/client.conf group1/M00/00/00/wKgXhlh4llOET8jHAAAAAMmGRic964.txt/tmp/append2.txt

[root@test5 fdfs]# cat /data/fdfs_storage/store/data/00/00/wKgXhlh4llOET8jHAAAAAMmGRic964.txt

[root@test5 ~]# fdfs_download_file --help

Usage: fdfs_download_file &lt;config_file&gt; &lt;file_id&gt; [local_filename] [&lt;download_offset&gt;&lt;download_bytes&gt;]

[root@test5 ~]# fdfs_download_file /etc/fdfs/client.conf group1/M00/00/00/wKgXhlh4llOET8jHAAAAAMmGRic964.txt

[root@test5 ~]# cat wKgXhlh4llOET8jHAAAAAMmGRic964.txt

[root@test5 ~]# fdfs_monitor /etc/fdfs/client.conf

[2017-01-13 01:01:57] DEBUG -base_path=/tmp, connect_timeout=30, network_timeout=60, tracker_server_count=2,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 idcount: 0

server_count=2, server_index=0

tracker server is 192.168.23.133:22122

group count: 1

Group 1:

group name = group1

disk total space = 17909 MB

disk free space = 5493 MB

trunk free space = 0 MB

storage server count = 2

active server count = 2

storage server port = 23000

storage HTTP port = 8888

store path count = 1

subdir count per path = 256

current write server index = 0

current trunk file id = 0

         Storage1:

                   id= 192.168.23.133

                   ip_addr= 192.168.23.133 (test5)  ACTIVE

                   httpdomain =

                   version= 5.08

                   jointime = 2017-01-12 23:21:24

                   uptime = 2017-01-12 23:44:11

                   totalstorage = 17909 MB

                   freestorage = 5493 MB

                   uploadpriority = 10

                   store_path_count= 1

                   subdir_count_per_path= 256

                   storage_port= 23000

                   storage_http_port= 8888

                   current_write_path= 0

                   sourcestorage id = 192.168.23.134

                   if_trunk_server= 0

                   connection.alloc_count= 256

                   connection.current_count= 1

                   connection.max_count= 2

                   total_upload_count= 1

                   success_upload_count= 1

                   total_append_count= 0

                   success_append_count= 0

                   total_modify_count= 0

                   success_modify_count= 0

                   total_truncate_count= 0

                   success_truncate_count= 0

                   total_set_meta_count= 0

                   success_set_meta_count= 0

                   total_delete_count= 1

                   success_delete_count= 1

                   total_download_count= 0

                   success_download_count= 0

                   total_get_meta_count= 0

                   success_get_meta_count= 0

                   total_create_link_count= 0

                   success_create_link_count= 0

                   total_delete_link_count= 0

                   success_delete_link_count= 0

                   total_upload_bytes= 200

                   success_upload_bytes= 200

                   total_append_bytes= 0

                   success_append_bytes= 0

                   total_modify_bytes= 0

                   success_modify_bytes= 0

                   stotal_download_bytes= 0

                   success_download_bytes= 0

                   total_sync_in_bytes= 12

                   success_sync_in_bytes= 12

                   total_sync_out_bytes= 0

                   success_sync_out_bytes= 0

                   total_file_open_count= 3

                   success_file_open_count= 3

                   total_file_read_count= 0

                   success_file_read_count= 0

                   total_file_write_count= 3

                   success_file_write_count= 3

                   last_heart_beat_time= 2017-01-13 01:01:56

                   last_source_update= 2017-01-13 00:29:41

                   last_sync_update= 2017-01-13 00:58:39

                   last_synced_timestamp= 2017-01-13 00:58:39 (0s delay)

         Storage2:

                   id= 192.168.23.134

                   ip_addr= 192.168.23.134 (test6)  ACTIVE

……

[root@test6 ~]# /etc/init.d/fdfs_storaged stop   #(在node2上停storageserver)

waiting for pid [2759] exit ...

pid [2759] exit.

[root@test5 ~]# fdfs_monitor /etc/fdfs/client.conf

                   ip_addr= 192.168.23.134 (test6)  OFFLINE

[root@test5 ~]# fdfs_monitor /etc/fdfs/client.conf delete group1 192.168.23.134   #(删除有問題node時要先停止該node服務)

[2017-01-13 01:12:28] DEBUG -base_path=/tmp, connect_timeout=30, network_timeout=60, tracker_server_count=2,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 idcount: 0

server_count=2, server_index=1

tracker server is 192.168.23.134:22122

delete storage servergroup1::192.168.23.134 success

                   ip_addr= 192.168.23.134 (test6)  DELETED

[root@test6 ~]# /etc/init.d/fdfs_storaged start   #(啟動node2的storaged服務)

                   ip_addr= 192.168.23.134 (test6)  ACTIVE

三、使用php_client、fastdfs-client-java、fastdfs-nginx-module:

1、php_client:

配置LNMP環境;

[root@test5 ~]# cd fastdfs-5.08/php_client/

[root@test5 php_client]# /usr/local/php/bin/phpize

Configuring for:

PHP Api Version:         20131106

Zend Module Api No:      20131226

Zend Extension Api No:   220131226

[root@test5 php_client]# ./configure --with-php-config=/usr/local/php/bin/php-config

[root@test5 php_client]# make &amp;&amp; make install

Build complete.

Don't forget to run 'make test'.

Installing shared extensions:    /usr/local/php/lib/php/extensions/no-debug-non-zts-20131226/

[root@test5 php_client]# ll/usr/local/php/lib/php/extensions/no-debug-non-zts-20131226/

total 2004

-rwxr-xr-x. 1 root root  346393 Jan 13 22:40fastdfs_client.so

-rwxr-xr-x. 1 root root 1112480 Jan 1322:15 opcache.a

-rwxr-xr-x. 1 root root  589060 Jan 13 22:15 opcache.so

[root@test5 php_client]# cat fastdfs_client.ini &gt;&gt; /etc/php.ini   #([root@test5php_client]# /usr/local/php/bin/php -i | grep php.ini,可用此檢視php.ini位置)

[root@test5 php_client]# vimfastdfs_test.php   #(php操作fastdfs的指令)

[root@test5 php_client]# /usr/local/php/bin/php fastdfs_test.php

5.08

fastdfs_tracker_make_all_connectionsresult: 1

delete file group1/M00/00/00/wKgXhlh5yi-AZL9sAAAAD61kmgs188.binreturn: 1

bool(true)

tracker_close_all_connections result: 1

2、fastdfs-client-java:

[root@test5 ~]# java -version   #(安裝java環境)

java version "1.8.0_111"

Java(TM) SE Runtime Environment (build1.8.0_111-b14)

Java HotSpot(TM) 64-Bit Server VM (build25.111-b14, mixed mode)

[root@test5 ~]# git clone https://github.com/happyfish100/fastdfs-client-java

Initialized empty Git repository in/root/fastdfs-client-java/.git/

remote: Counting objects: 60, done.

remote: Total 60 (delta 0), reused 0 (delta0), pack-reused 60

Unpacking objects: 100% (60/60), done.

[root@test5 ~]# cd fastdfs-client-java/src

[root@test5 src]# yum -y install ant

[root@test5 src]# which ant

/usr/bin/ant

[root@test5 src]# ant 

Buildfile: build.xml

init:

compile:

   [mkdir] Created dir: /root/fastdfs-client-java/src/build/classes

   [javac] Compiling 32 source files to/root/fastdfs-client-java/src/build/classes

   [javac] This version of java does not support the classic compiler;upgrading to modern

   [javac] Note: Some input files use unchecked or unsafe operations.

   [javac] Note: Recompile with -Xlint:unchecked for details.

jar:

     [jar] Building jar: /root/fastdfs-client-java/src/build/fastdfs_client.jar

BUILD SUCCESSFUL

Total time: 4 seconds

[root@test5 src]# ll build   #(生成build/fastdfs_client.jar)

total 100

drwxr-xr-x. 3 root root  4096 Jan 13 23:12 classes

-rw-r--r--. 1 root root 94216 Jan 13 23:12fastdfs_client.jar  

[root@test5 src]# cd build

[root@test5 build]# java -cp fastdfs_client.jar org.csource.fastdfs.test.TestClient /etc/fdfs/client.conf /etc/resolv.conf   #(-cp classpath)

java.version=1.8.0_111

network_timeout=60000ms

charset=ISO8859-1

file length: 14

store storage servers count: 2

1. 192.168.23.133:23000

2. 192.168.23.134:23000

upload_file time used: 75 ms

group_name: group1, remote_filename: M00/00/00/wKgXhlh50OaAIv-2AAAADv4ZzcQ659.txt

source_ip_addr = 192.168.23.134, file_size= 14, create_timestamp = 2017-01-13 23:19:02, crc32 = -31863356

storage servers count: 1

1. 192.168.23.134:23000

set_metadata time used: 6 ms

set_metadata success

author Mike

bgcolor #000000

heigth 768

title Untitle

width 1024

download_file time used: 2 ms

file length:14

this is a test

upload_file time used: 46 ms

slave file group_name: group1,remote_filename: M00/00/00/wKgXhlh50OaAIv-2AAAADv4ZzcQ659-part1.txt

source_ip_addr = 192.168.23.134, file_size= 20, create_timestamp = 2017-01-13 23:19:02, crc32 = -31863356

delete_file time used: 2 ms

Delete file success

group_name: group1, remote_filename:M00/00/00/wKgXhVh50MmAQEs9AAAAXJXZQnk07.conf

source_ip_addr = 192.168.23.133, file_size= 92, create_timestamp = 2017-01-13 23:18:33, crc32 = -1780923783

file url:http://192.168.23.134/group1/M00/00/00/wKgXhVh50MmAQEs9AAAAXJXZQnk07.conf

Download file success

upload_file time used: 57 ms

slave file group_name: group1,remote_filename: M00/00/00/wKgXhVh50MmAQEs9AAAAXJXZQnk07-part2.conf

group name: group1, remote filename:M00/00/00/wKgXhlh50OeAH48FAAAAXJXZQnk39.conf

source_ip_addr = 192.168.23.134, file_size= 92, create_timestamp = 2017-01-13 23:19:03, crc32 = -1780923783

upload_file time used: 59 ms

slave file group_name: group1,remote_filename: M00/00/00/wKgXhlh50OeAH48FAAAAXJXZQnk39-part3.conf

active test to storage server: true

active test to tracker server: true

[root@test5 build]# java -cp fastdfs_client.jar org.csource.fastdfs.test.Monitor /etc/fdfs/client.conf

disk total space = 17909MB

disk free space = 3229 MB

                   storageid = 192.168.23.133

                   ip_addr= 192.168.23.133  ACTIVE

                   totalstorage = 17909MB

                   freestorage = 3229MB

                   sourceip_addr =

                   if_trunk_server= false

                   conntion.alloc_count  = 256

                   conntion.current_count  = 1

                   conntion.max_count  = 3

                   total_upload_count= 13

                   success_upload_count= 13

                   total_set_meta_count= 6

                   success_set_meta_count= 6

                   total_delete_count= 11

                   success_delete_count= 11

                   total_download_count= 6

                   success_download_count= 6

                   total_get_meta_count= 2

                   success_get_meta_count= 2

                   total_upload_bytes= 189858

                   success_upload_bytes= 189858

                   total_download_bytes= 214

                   success_download_bytes= 214

                   total_sync_in_bytes= 405

                   success_sync_in_bytes= 405

                   total_file_open_count= 29

                   success_file_open_count= 29

                   total_file_read_count= 8

                   success_file_read_count= 8

                   total_file_write_count= 21

                   success_file_write_count= 21

                   last_heart_beat_time= 2017-01-13 23:22:26

                   last_source_update= 2017-01-13 23:18:32

                   last_sync_update= 2017-01-13 23:18:40

                   last_synced_timestamp= 2017-01-13 23:19:02 (0s delay)

                   storageid = 192.168.23.134

                   ip_addr= 192.168.23.134  ACTIVE

3、fastdfs-nginx-module:

注:生産中每個storage server上都要裝fastdfs-nginx-module;

[root@test5 ~]# git clone https://github.com/happyfish100/fastdfs-nginx-module

Initialized empty Git repository in/root/fastdfs-nginx-module/.git/

remote: Counting objects: 52, done.

remote: Total 52 (delta 0), reused 0 (delta0), pack-reused 52

Unpacking objects: 100% (52/52), done.

在LNMP環境上操作:

[root@test5 nginx-1.8.0]# ./configure   --prefix=/usr   --sbin-path=/usr/sbin/nginx   --conf-path=/etc/nginx/nginx.conf   --error-log-path=/var/log/nginx/error.log  --http-log-path=/var/log/nginx/access.log   --pid-path=/var/run/nginx/nginx.pid    --lock-path=/var/lock/nginx.lock   --user=nginx   --group=nginx   --with-http_ssl_module   --with-http_flv_module   --with-http_stub_status_module   --with-http_gzip_static_module  --http-client-body-temp-path=/var/tmp/nginx/client/  --http-proxy-temp-path=/var/tmp/nginx/proxy/   --http-fastcgi-temp-path=/var/tmp/nginx/fcgi/  --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi  --http-scgi-temp-path=/var/tmp/nginx/scgi   --with-pcre--add-module=../fastdfs-nginx-module/src/

[root@test5 nginx-1.8.0]# make   #(隻make不make install,make install後會覆寫之前的安裝,僅把nginx-1.8.0/objs/nginx的二進制指令覆寫掉之前的/usr/sbin/nginx)

[root@test5 nginx-1.8.0]# ll -h objs/nginx

-rwxr-xr-x. 1 root root 5.4M Jan 14 00:10objs/nginx

[root@test5 nginx-1.8.0]# ll -h /usr/sbin/nginx

-rwxr-xr-x. 1 root root 5.3M Jan 13 21:33/usr/sbin/nginx

[root@test5 nginx-1.8.0]# cp objs/nginx /usr/sbin/nginx

cp: overwrite `/usr/sbin/nginx'? y

[root@test5 nginx-1.8.0]# cd ../fastdfs-nginx-module/src

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

[root@test5 src]# cp /root/fastdfs-5.08/conf/{anti-steal.jpg,http.conf,mime.types} /etc/fdfs/

[root@test5 src]# touch /var/log/mod_fastdfs.log

[root@test5 src]# chown nginx.nginx /var/log/mod_fastdfs.log

[root@test5 src]# vim /etc/nginx/nginx.conf   #(location/group1/M00對應mod_fastdfs.conf中url_have_group_name = true,預設為false)

       location / {

           root   html;

           index  index.php index.htmlindex.htm;

       }

        location /group1/M00 {

                root /data/fdfs_storage/store;

                ngx_fastdfs_module;

        }

[root@test5 src]# vim /etc/fdfs/mod_fastdfs.conf   #(url_have_group_name= true對應nginx.conf中location /group1/M00)

url_have_group_name = true

log_filename=/var/log/mod_fastdfs.log

response_mode=proxy   #(response mode when the file not exist inthe local file system. proxy: get the content from other storage server, thensend to client. redirect: redirect to the original storage server (HTTP Headeris Location))

[root@test5 src]# /etc/init.d/nginx restart

ngx_http_fastdfs_set pid=112824

nginx: the configuration file/etc/nginx/nginx.conf syntax is ok

nginx: configuration file/etc/nginx/nginx.conf test is successful

Stopping nginx:                                           [  OK  ]

Starting nginx: ngx_http_fastdfs_setpid=112918

                                                          [  OK  ]

[root@test5 src]# cd

[root@test5 ~]# fdfs_upload_file /etc/fdfs/client.conf DSC_0171.JPG

group1/M00/00/00/wKgXhVh545iAP6ETADyH-kmtQ2U888.JPG

<a href="http://192.168.23.133/group1/M00/00/00/wKgXhVh545iAP6ETADyH-kmtQ2U888.JPG" target="_blank">http://192.168.23.133/group1/M00/00/00/wKgXhVh545iAP6ETADyH-kmtQ2U888.JPG</a>

<a href="https://s2.51cto.com/wyfs02/M00/8C/D4/wKiom1h57jnw4teuAADGZZMbWQc684.jpg" target="_blank"></a>

附:

Copyright (C) 2008 Happy Fish / YuQing

FastDFS may be copied only under the termsof the GNU General Public License V3, which may be found in the FastDFS sourcekit. Please visit the FastDFS Home Page for more detail. English language:http://english.csource.org/ Chinese language: http://www.csource.org/

FastDFS is an open source high performancedistributed file system. It's major functions include: file storing, filesyncing and file accessing (file uploading and file downloading), and it canresolve the high capacity and load balancing problem. FastDFS should meet therequirement of the website whose service based on files such as photo sharingsite and video sharing site.

FastDFS has two roles: tracker and storage.The tracker takes charge of scheduling and load balancing for file access. Thestorage store files and it's function is file management including: filestoring, file syncing, providing file access interface. It also manage the metadata which are attributes representing as key value pair of the file. Forexample: width=1024, the key is "width" and the value is"1024".

The tracker and storage contain one or moreservers. The servers in the tracker or storage cluster can be added to orremoved from the cluster by any time without affecting the online services. Theservers in the tracker cluster are peer to peer.

The storarge servers organizing by the filevolume/group to obtain high capacity. The storage system contains one or morevolumes whose files are independent among these volumes. The capacity of thewhole storage system equals to the sum of all volumes' capacity. A file volumecontains one or more storage servers whose files are same among these servers.The servers in a file volume backup each other, and all these servers are loadbalancing. When adding a storage server to a volume, files already existing inthis volume are replicated to this new server automatically, and when thisreplication done, system will switch this server online to providing storageservices.

When the whole storage capacity isinsufficiency, you can add one or more volumes to expand the storage capacity.To do this, you need to add one or more storage servers.

The identification of a file is composed oftwo parts: the volume name and the file name.

Client test code use client library pleaserefer to the directory: client/test.

本文轉自 chaijowin 51CTO部落格,原文連結:http://blog.51cto.com/jowin/1891995,如需轉載請自行聯系原作者