FastDFS 配置, 用户上传的图片存储

参照 https://www.cnblogs.com/chiangchou/p/fastdfs.html#_labelTop#undefined

* 下载fastdfs安装

git clone https://github.com/happyfish100/fastdfs

链接:https://pan.baidu.com/s/1V8M82oszYctd90nTbdsHEA 
提取码:jrnx 
复制这段内容后打开百度网盘手机App,操作更方便哦

tar xvzf xxx.tar.gz -C ./

# ./make.sh
# ./make.sh install

* 下载libfastcommon安装

wget https://github.com/happyfish100/libfastcommon/archive/V1.0.7.tar.gz

链接:https://pan.baidu.com/s/1Apqf5WK8j8UKO9Kwym-S4g 
提取码:1qrl 
复制这段内容后打开百度网盘手机App,操作更方便哦

tar xvzf xxx.tar.gz -C ./

# ./make.sh
# ./make.sh install

 libfastcommon.so 安装到了/usr/lib64/libfastcommon.so,但是FastDFS主程序设置的lib目录是/usr/local/lib,所以需要创建符号链接, 要是能配置安装路径就好。

# ln -s /usr/lib64/libfastcommon.so /usr/local/lib/libfastcommon.so
# 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 

* 安装后的配置文件 

默认安装方式安装后的相应文件与目录
 ** 服务脚本:

/etc/init.d/fdfs_storaged   # 默认使用23000端口
/etc/init.d/fdfs_tracker    # 默认使用22122端口

 ** 配置文件

/etc/fdfs/client.conf    
    # 客户端配置这个文件,在这里指定 tracker server的ip + port,对于客户端忽略另外2个配置文件
    # 客户端不需要指定 storage存储服务器在哪里

/etc/fdfs/storage.conf   
    # 存储服务器配置这个文件,对于存储服务器忽略其他2个配置文件

/etc/fdfs/tracker.conf   
    # tracker server 配置这个文件 bind_addr=0.0.0.0, 
    # 注意如果配置127.0.0.1, 客户端必须跟tracker server 是同一台服务器才能通信

  ** 命令行工具

$ ls /usr/local/bin/fdfs_*
/usr/local/bin/fdfs_appender_test   /usr/local/bin/fdfs_delete_file    /usr/local/bin/fdfs_storaged  /usr/local/bin/fdfs_upload_appender
/usr/local/bin/fdfs_appender_test1  /usr/local/bin/fdfs_download_file  /usr/local/bin/fdfs_test      /usr/local/bin/fdfs_upload_file
/usr/local/bin/fdfs_append_file     /usr/local/bin/fdfs_file_info      /usr/local/bin/fdfs_test1
/usr/local/bin/fdfs_crc32           /usr/local/bin/fdfs_monitor        /usr/local/bin/fdfs_trackerd
 

* 配置跟踪服务器 tracker server

/etc/fdfs/tracker.conf

traker server需要指定存储在位置, 客户端web访问存储服务器http端口, 还有监听哪些客户端.

bind_addr=0.0.0.0
# bind_addr=127.0.0.1  

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

# HTTP port on this tracker server                                                                                                                            
http.server_port=8090
 

* 配置存储服务器 

/etc/fdfs/storage.conf

stroage server  需要指定自身服务绑定ip +port,  tracker server的IP + port, 客户端web访问存储服务器http端口,

 还有存储路径

# bind an address of this host                                                                                                                                
# empty for bind all addresses of this host                                                                                                                   
bind_addr=0.0.0.0

# if bind an address of this host when connect to other servers                                                                                               
# (this storage server as a client)                                                                                                                           
# true for binding the address configed by above parameter: "bind_addr"                                                                                       
# false for binding any address of this host                                                                                                                  
client_bind=true

# the storage server port                                                                                                                                     
port=23000
 

# tracker_server can ocur more than once, and tracker_server format is                                                                                        
#  "host:port", host can be hostname or ip address                                                                                                            
tracker_server=47.93.??.??:22122

 

# the port of the web server on this storage server                                                                                                           
http.server_port=8090

 

# the base path to store data and log files                                                                                                                   
base_path=/home/mzh/fastdfs/storage   
# 如果路径不存在需要手动创建 mkdir -p /home/mzh/fastdfs/storage

# store_path#, based 0, if store_path0 not exists, it's value is base_path                                                                                    
# the paths must be exist                                                                                                                                     
store_path0=/home/mzh/fastdfs/storage
# store_path1=/home/mzh/fastdfs2  
 

* 配置客户端

客户端只需要知道 tracker server 服务 从哪里访问 ip + port

# tracker_server can ocur more than once, and tracker_server format is                                                                                        
#  "host:port", host can be hostname or ip address                                                                                                            
# tracker_server=127.0.0.1:22122                                                                                                                                                    tracker_server=47.93.??.???:22122
 

* 启动服务

 ** 在tracker server上

/etc/init.d/fdfs_trackerd start
设置开机启动 
vim /etc/rc.d/rc.local
加入配置:
/etc/init.d/fdfs_trackerd start 
** 在storage server上
/etc/init.d/fdfs_storaged start

同样设置开机启动

sudo echo  '/etc/init.d/fdfs_storaged start' >> /etc/rc.d/rc.local

 

查看服务启动了没有:

$ sudo netstat -unltp|grep fdfs
tcp        0      0 0.0.0.0:23000           0.0.0.0:*               LISTEN      18306/fdfs_storaged 
tcp        0      0 0.0.0.0:22122           0.0.0.0:*               LISTEN      1258/fdfs_trackerd 

我这里 storage服务,tracker服务器运行在同一台服务器上。

 

在storage server上, 查看确认能连接上tracker server

/usr/bin/fdfs_monitor /etc/fdfs/storage.conf

[2018-12-24 10:39:17] DEBUG - base_path=/home/mzh/fastdfs/storage, 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

server_count=1, server_index=0

tracker server is 47.93.??.???:22122

group count: 1

Group 1:

 

store_path0路径下创建了一些文件

$ ls ~/fastdfs/storage/data/
00  09  12  1B  24  2D  36  3F  48  51  5A  63  6C  75  7E  87  90  99  A2  AB  B4  BD  C6  CF  D8  E1  EA  F3  FC
01  0A  13  1C  25  2E  37  40  49  52  5B  64  6D  76  7F  88  91  9A  A3  AC  B5  BE  C7  D0  D9  E2  EB  F4  FD
02  0B  14  1D  26  2F  38  41  4A  53  5C  65  6E  77  80  89  92  9B  A4  AD  B6  BF  C8  D1  DA  E3  EC  F5  fdfs_storaged.pid
03  0C  15  1E  27  30  39  42  4B  54  5D  66  6F  78  81  8A  93  9C  A5  AE  B7  C0  C9  D2  DB  E4  ED  F6  FE
04  0D  16  1F  28  31  3A  43  4C  55  5E  67  70  79  82  8B  94  9D  A6  AF  B8  C1  CA  D3  DC  E5  EE  F7  FF
05  0E  17  20  29  32  3B  44  4D  56  5F  68  71  7A  83  8C  95  9E  A7  B0  B9  C2  CB  D4  DD  E6  EF  F8  storage_stat.dat
06  0F  18  21  2A  33  3C  45  4E  57  60  69  72  7B  84  8D  96  9F  A8  B1  BA  C3  CC  D5  DE  E7  F0  F9  sync
07  10  19  22  2B  34  3D  46  4F  58  61  6A  73  7C  85  8E  97  A0  A9  B2  BB  C4  CD  D6  DF  E8  F1  FA
08  11  1A  23  2C  35  3E  47  50  59  62  6B  74  7D  86  8F  98  A1  AA  B3  BC  C5  CE  D7  E0  E9  F2  FB

 

* 文件上传测试

在客户端上进行,确认能连接到 tracker server

$ telnet 47.93.??.??? 22122
Trying 47.93.??.???...
Connected to 47.93.??.???.
Escape character is '^]'.
quit
exit
Connection closed by foreign host.

用自带命令行工具测试上传

$ /usr/local/bin/fdfs_upload_file /etc/fdfs/client.conf ~/Cal.php 
group1/M00/00/00/rBG2b1wgSOaAKr3lAAANeqDq1ZI965.php

 

* nginx配置 (storage server)

/opt/nginx/conf/nginx.conf

    # FastDFS storage                                                                                                                                         
    server {
        listen       8090;
        server_name  localhost;

        #charset koi8-r;                                                                                                                                      

        #access_log  logs/host.access.log  main;                                                                                                              
        location / {
           root /home/mzh/fastdfs/storage;
           index index.html index.htm;
        }

        location /group1/M00 {
          alias /home/mzh/fastdfs/storage/data;
        }

        #error_page  404              /404.html;                                                                                                              

	# redirect server error pages to the static page /50x.html                                                                                            
        #                                                                                                                                                     
        error_page   500 502 503 504  /50x.html;
	location = /50x.html {
            root   html;
	}

    }

sudo /opt/nginx/sbin/nginx -t
the configuration file /opt/nginx/conf/nginx.conf syntax is ok
configuration file /opt/nginx/conf/nginx.conf test is successful

sudo /opt/nginx/sbin/nginx -s reload

 

/usr/bin/fdfs_upload_file /etc/fdfs/client.conf /home/mzh/Pictures/2018120602.jpg
group1/M00/00/00/rBG2b1wgSg-AEMjQAAB7JvXwOQM909.jpg
 

然后通过浏览器打开

http://47.93.??.???:8090/group1/M00/00/00/rBG2b1wgSg-AEMjQAAB7JvXwOQM909.jpg

换一个客户端

 /usr/local/bin/fdfs_upload_file /etc/fdfs/client.conf /home/ubuntu/Pictures/2018120602.jpg
group1/M00/00/00/rBG2b1wgSuyAfe9YAAB7JvXwOQM696.jpg
 

http://47.93.??.???:8090/group1/M00/00/00/rBG2b1wgSuyAfe9YAAB7JvXwOQM696.jpg

说明上传成功

都是同一个图片,但是文件名不同

 

* 原理

Tracker Server:跟踪服务器,主要做调度工作,起到均衡的作用;负责管理所有的 storage server和 group,每个 storage 在启动后会连接 Tracker,告知自己所属 group 等信息,并保持周期性心跳。

  Storage Server:存储服务器,主要提供容量和备份服务;以 group 为单位,每个 group 内可以有多台 storage server,数据互为备份。

  Client:客户端,上传下载数据的服务器,也就是我们自己的项目所部署在的服务器。

tracker server起到中介的作用

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

fareast_mzh

打赏个金币

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值