分布式存储系统FastDFS

帅比萌擦擦* 提交于 2020-01-19 04:29:15

软件的安装、配置

安装列表

fastdfs的安装需要centos,我在ubuntu上安装没有成功
在这里插入图片描述

安装所需的依赖包

安装前可使用dpkg -l | grep gcc命令来查看依赖包是否已经安装,如果没有,使用下列命令来安装相应的依赖包

yum -y install libevent //Libevent 是一个用C语言编写的、轻量级的开源高性能事件通知库
yum -y install pop* //安装popler,popt等库
yum -y install openssl* //使得nginx不仅支持http,还支持https
yum install -y gcc gcc-c++ //安装gcc,用于编译nginx

安装libfastcommon

将libfastcommon-1.0.7.tar.gz解压至指定目录(所有安装包都在/home/sword/distributed-storage/),然后使用以下命令来安装。

./make.sh
./make.sh install

libfastcommon安装好后会自动将库文件拷贝至/usr/lib64下,由于FastDFS程序引用usr/lib目录所以需要将/usr/lib64下的库文件拷贝至/usr/lib下

cp /usr/lib64/libfastcommon.so /usr/lib/

安装FastDFS

将FastDFS_v5.05.tar.gz解压到指定目录,然后使用以下命令来安装

./make.sh
./make.sh install

安装完成后在/etc/fdfs下,将三个文件:
client.conf.sample
storage.conf.sample
tracker.conf.sample
都修改为:
client.conf
storage.conf
tracker.conf

配置tracker服务器,并启动

修改/etc/fdfs/tracker.conf

//下面这个路径需要自己新建
base_path=/home/sword/Desktop/distributed-storage/storage-data
http.server_port=8080

配置完成后使用下面命令行启动tracker服务

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

使用下面的命令查看是否启动成功

ps -e|grep fdfs_trackerd

如果成功,则会有

[root@localhost bin]# ps -e|grep fdfs_trackerd
 15425 ?        00:00:00 fdfs_trackerd

配置Storage服务器,并启动

修改/etc/fdfs/tracker.conf

group_name=group1
base_path=/home/sword/Desktop/distributed-storage/storage-data
store_path0=/home/sword/Desktop/distributed-storage/storage-data
tracker_server=192.168.217.144:22122
//如果有多个则配置多个tracker
//tracker_server=192.168.217.145:22122

配置完成后使用下面命令启动Storage服务

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

使用下面的命令查看tracker和storage是否都启动成功

[root@localhost bin]# ps -e|grep fdfs_
 15425 ?        00:00:00 fdfs_trackerd
 24134 pts/0    00:00:00 fdfs_storaged

修改Client,以测试图片上传

修改/etc/fdfs/client.conf

base_path=/home/sword/Desktop/distributed-storage/storage-data
tracker_server=192.168.217.144:22122
//tracker_server=192.168.217.145:22122

使用以下命令上传图片

[root@localhost distributed-storage]# /usr/bin/fdfs_test /etc/fdfs/client.conf upload 1.jpg

结果如下

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.

[2020-01-05 02:58:15] DEBUG - base_path=/home/sword/Desktop/distributed-storage/storage-data, 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.217.144, port=23000

group_name=group1, ip_addr=192.168.217.144, port=23000
storage_upload_by_filename
group_name=group1, remote_filename=M00/00/00/wKjZkF4Q4EeANDdQAAGNecD0vxY622.jpg
source ip address: 192.168.217.144
file timestamp=2020-01-05 02:58:15
file size=101753
file crc32=3237265174
example file url: http://192.168.217.144/group1/M00/00/00/wKjZkF4Q4EeANDdQAAGNecD0vxY622.jpg
storage_upload_slave_by_filename
group_name=group1, remote_filename=M00/00/00/wKjZkF4Q4EeANDdQAAGNecD0vxY622_big.jpg
source ip address: 192.168.217.144
file timestamp=2020-01-05 02:58:15
file size=101753
file crc32=3237265174
example file url: http://192.168.217.144/group1/M00/00/00/wKjZkF4Q4EeANDdQAAGNecD0vxY622_big.jpg

FDFS和Nginx组合

FastDFS的HTTP服务较为简单,无法提供负载均衡等高性能的服务,所以FastDFS的作者为我们提供了Nginx上使用的FastDFS模块。
新建一个nginx的安装目录(nginx_LJ),将fastdfs-nginx-module,nginx-1.9.3,ngx_cache_purge-2.3,pcre-8.37,zlib-1.2.3解压,目前的目录结构是
在这里插入图片描述
修改/home/sword/Desktop/distributed-storage/fastdfs-nginx-module/src/中的config文件(好像不需要改,在下一步建立软连接就可以了,不改的话,make的时候会出现找不到fdfs_define.h的错误)

ngx_addon_name=ngx_http_fastdfs_module
HTTP_MODULES="$HTTP_MODULES ngx_http_fastdfs_module"
NGX_ADDON_SRCS="$NGX_ADDON_SRCS $ngx_addon_dir/ngx_http_fastdfs_module.c"
CORE_INCS="$CORE_INCS /usr/include/fastdfs /usr/include/fastcommon/"
CORE_LIBS="$CORE_LIBS -L/usr/local/lib -lfastcommon -lfdfsclient"
CFLAGS="$CFLAGS -D_FILE_OFFSET_BITS=64 -DFDFS_OUTPUT_CHUNK_SIZE='256*1024' -DFDFS_MOD_CONF_FILENAME='\"/etc/fdfs/mod_fastdfs.conf\"'"

使用三个命令分别建立三个软连接

ln -sv /usr/include/fastcommon /usr/local/include/fastcommon
ln -sv /usr/include/fastdfs /usr/local/include/fastdfs
ln -sv /usr/lib64/libfastcommon.so /usr/local/lib/libfastcommon.so

然后使用下面命令进行安装

./configure --prefix=/home/sword/Desktop/distributed-storage/nginx_LJ --add-module=/home/sword/Desktop/distributed-storage/fastdfs-nginx-module/src --add-module=/home/sword/Desktop/distributed-storage/ngx_cache_purge-2.3 --with-pcre=/home/sword/Desktop/distributed-storage/pcre-8.37 --with-zlib=/home/sword/Desktop/distributed-storage/zlib-1.2.3
make&make install

使用Nginx来访问FastDFS中的文件

修改nginx.conf中的用户,否则在访问的时候会出现无法连接的情况。找到设置user的地方(文件的开头位置)

user root;

添加location

server {
        listen       8081;
        server_name  localhost;
        #charset koi8-r;
        #access_log  logs/host.access.log  main;
        location / {
            root   html;
            index  index.html index.htm;
        }
		location /group1/M00 {
			root /home/sword/Desktop/distributed-storage/storage-data/data;
			ngx_fastdfs_module;
	}

在/home/sword/Desktop/distributed-storage/FastDFS/conf/找到http.conf和mime.types,拷贝到/etc/fdfs目录下

cp http.conf /etc/fdfs/
cp mime.types /etc/fdfs/

在/home/sword/Desktop/distributed-storage/fastdfs-nginx-module/src/中找到mod_fastdfs.conf,拷贝到/etc/fdfs目录下

cp mod_fastdfs.conf /etc/fdfs/

修改这个文件的内容如下:

# connect timeout in seconds
# default value is 30s
connect_timeout=2

# network recv and send timeout in seconds
# default value is 30s
network_timeout=30

# the base path to store log files
base_path=/home/sword/Desktop/distributed-storage/

# if load FastDFS parameters from tracker server
# since V1.12
# default value is false
load_fdfs_parameters_from_tracker=true

# storage sync file max delay seconds
# same as tracker.conf
# valid only when load_fdfs_parameters_from_tracker is false
# since V1.12
# default value is 86400 seconds (one day)
storage_sync_file_max_delay = 86400

# if use storage ID instead of IP address
# same as tracker.conf
# valid only when load_fdfs_parameters_from_tracker is false
# default value is false
# since V1.13
use_storage_id = false

# specify storage ids filename, can use relative or absolute path
# same as tracker.conf
# valid only when load_fdfs_parameters_from_tracker is false
# since V1.13
storage_ids_filename = storage_ids.conf

# FastDFS tracker_server can ocur more than once, and tracker_server format is
#  "host:port", host can be hostname or ip address
# valid only when load_fdfs_parameters_from_tracker is true
tracker_server=192.168.217.144:22122

# the port of the local storage server
# the default value is 23000
storage_server_port=23000

# the group name of the local storage server
group_name=group1

# if the url / uri including the group name
# set to false when uri like /M00/00/00/xxx
# set to true when uri like ${group_name}/M00/00/00/xxx, such as group1/M00/xxx
# default value is false
url_have_group_name = true

# path(disk or mount point) count, default value is 1
# must same as storage.conf
store_path_count=1

# store_path#, based 0, if store_path0 not exists, it's value is base_path
# the paths must be exist
# must same as storage.conf
store_path0=/home/sword/Desktop/distributed-storage/storage-data
#store_path1=/home/yuqing/fastdfs1

# standard log level as syslog, case insensitive, value list:
### emerg for emergency
### alert
### crit for critical
### error
### warn for warning
### notice
### info
### debug
log_level=info

# set the log filename, such as /usr/local/apache2/logs/mod_fastdfs.log
# empty for output to stderr (apache and nginx error_log file)
log_filename=

# response mode when the file not exist in the local file system
## proxy: get the content from other storage server, then send to client
## redirect: redirect to the original storage server (HTTP Header is Location)
response_mode=proxy

# the NIC alias prefix, such as eth in Linux, you can see it by ifconfig -a
# multi aliases split by comma. empty value means auto set by OS type
# this paramter used to get all ip address of the local host
# default values is empty
if_alias_prefix=

# use "#include" directive to include HTTP config file
# NOTE: #include is an include directive, do NOT remove the # before include
#include http.conf

# if support flv
# default value is false
# since v1.15
flv_support = true

# flv file extension name
# default value is flv
# since v1.15
flv_extension = flv

# set the group count
# set to none zero to support multi-group
# set to 0  for single group only
# groups settings section as [group1], [group2], ..., [groupN]
# default value is 0
# since v1.14
group_count = 0

# group settings for group #1
# since v1.14
# when support multi-group, uncomment following section
[group1]
group_name=group1
storage_server_port=23000
store_path_count=1
store_path0=/home/sword/Desktop/distributed-storage/storage-data

# group settings for group #2
# since v1.14
# when support multi-group, uncomment following section as neccessary
#[group2]
#group_name=group2
#storage_server_port=23000
#store_path_count=2
#store_path0=/home/yuqing/fastdfs
#store_path1=/home/yuqing/fastdfs1

创建M00至storage存储目录的符号连接:

ln -s /home/sword/Desktop/distributed-storage/storage-data/data/ /home/sword/Desktop/distributed-storage/storage-data/data/M00

在/home/sword/Desktop/distributed-storage/storage-data/data/sync/目录下找到binlog.000文件,查看已经上传的文件

[root@localhost sync]# cat binlog.000
1578164295 C M00/00/00/wKjZkF4Q4EeANDdQAAGNecD0vxY622.jpg
1578164294 C M00/00/00/wKjZkF4Q4EeANDdQAAGNecD0vxY622.jpg-m
1578164295 C M00/00/00/wKjZkF4Q4EeANDdQAAGNecD0vxY622_big.jpg
1578164295 C M00/00/00/wKjZkF4Q4EeANDdQAAGNecD0vxY622_big.jpg-m

打开/home/sword/Desktop/distributed-storage/nginx_LJ/sbin/,启动nginx

./nginx

在浏览器中浏览nginx主页
在这里插入图片描述
通过nginx访问fdfs上的内容
在这里插入图片描述

程序操作FDFS

Python

from fdfs_client.client import *
import time

class FDFSClient:
    def __init__(self,config_file_path="client.conf"):
        self.config_file_path = config_file_path
        self.tracker = get_tracker_conf(self.config_file_path)
        self.client = Fdfs_client(self.tracker)

    #上传文件字节
    def upload_file_by_buffer(self,file_path=""):
        with open(file_path,"rb") as f:
            ret = self.client.upload_by_buffer(f.read())
            print(ret)
            return ret
    #上传文件
    def upload_file(self,file_path=""):
        ret = self.client.upload_by_filename(file_path)
        print(ret)
        return ret
    #下载文件字节,是不是可以实现以流的方式播放,有待验证
    def download_file_by_buffer(self,local_file="",remote_file_id=b"",offset=0,down_bytes=1024):
        ret = self.client.download_to_buffer(remote_file_id,offset=offset,down_bytes=down_bytes)
        with open(local_file,"wb") as f:
            f.write(ret["Content"])

    def download_file(self,local_file="",remote_file_id=b"",offset=0,down_bytes=1024):
        ret = self.client.download_to_file(local_file,remote_file_id)

    def delete_file(self,remote_file_id):
        ret = self.client.delete_file(remote_file_id)
        print(ret)


if __name__=="__main__":
    fdfsClient = FDFSClient()
    #ret = fdfsClient.upload_file("1.MP4")
    #ret = fdfsClient.upload_file_by_buffer("1.MP4")
    #remote_file_id = ret["Remote file_id"]
    #fdfsClient.download_file_by_buffer("test.MP4",remote_file_id=b'group1/M00/00/00/wKjZkF4TXKiAPiXhBx77kEARHrg3654480')
    fdfsClient.download_file("test.MP4",remote_file_id=b'group1/M00/00/00/wKjZkF4TZLiAY9ReBx77kEARHrg163.MP4')
    #fdfsClient.delete_file(b'group1/M00/00/00/wKjZkF4TXKiAPiXhBx77kEARHrg3654480')


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!