首页 技术 正文
技术 2022年11月19日
0 收藏 349 点赞 3,627 浏览 9254 个字

一、FastDFS

FastDFS是用c语言编写的一款开源的分布式文件系统。FastDFS为互联网量身定制,充分考虑了冗余备份、负载均衡、线性扩容等机制,并注重高可用、高性能等指标,使用FastDFS很容易搭建一套高性能的文件服务器集群提供文件上传、下载等服务。

FastDFS服务端有两个角色:跟踪器(tracker)和存储节点(storage)。跟踪器主要做调度工作,在访问上起负载均衡的作用。

存储节点存储文件,完成文件管理的所有功能:就是这样的存储、同步和提供存取接口,FastDFS同时对文件的metadata进行管理。所谓文件的meta data就是文件的相关属性,以键值对(key value)方式表示,如:width=1024,其中的key为width,value为1024。文件metadata是文件属性列表,可以包含多个键值对。

跟踪器和存储节点都可以由一台或多台服务器构成。跟踪器和存储节点中的服务器均可以随时增加或下线而不会影响线上服务。其中跟踪器中的所有服务器都是对等的,可以根据服务器的压力情况随时增加或减少。

为了支持大容量,存储节点(服务器)采用了分卷(或分组)的组织方式。存储系统由一个或多个卷组成,卷与卷之间的文件是相互独立的,所有卷的文件容量累加就是整个存储系统中的文件容量。一个卷可以由一台或多台存储服务器组成,一个卷下的存储服务器中的文件都是相同的,卷中的多台存储服务器起到了冗余备份和负载均衡的作用。

在卷中增加服务器时,同步已有的文件由系统自动完成,同步完成后,系统自动将新增服务器切换到线上提供服务。

当存储空间不足或即将耗尽时,可以动态添加卷。只需要增加一台或多台服务器,并将它们配置为一个新的卷,这样就扩大了存储系统的容量。

FastDFS中的文件标识分为两个部分:卷名和文件名,二者缺一不可。

二、上传交互过程

1.client询问tracker上传到的storage,不需要附加参数;

2.tracker返回一台可用的storage;

3.client直接和storage通讯完成文件上传。

三、下载交互过程

四、搭建环境文件下载列表

1.FastDFS_v5.05.tar.gz

2.nginx-1.14.0.tar.gz

3.fastdfs-nginx-module_v1.16.tar.gz

4.fastdfs_client_v1.20.jar

5.libfastcommonV1.0.7.tar.gz

下载 链接:https://pan.baidu.com/s/16KjMc0Reyqg_nyM2sa92wg 提取码:9bbv

五、搭建FastDFS环境

1. 上传所有下载的压缩源码包到服务器

根据自己的喜好,上传到指定目录,一般上传目录为: /usr/local/

2. 安装FastDFS之前,需要先安装一些工具包

# nginx是c编写的,对于解压的源码进行编译,所以首先设置编译环境: gcc
# 执行: (-y: 不询问)
yum -y install gcc
yum -y install gcc-c++
# Perl库,包括 perl 兼容的正则表达式库
yum install -y pcre pcre-devel
# zlib库提供了很多种压缩和解压缩的方式
yum install -y zlib zlib-devel
# OpenSSL 是一个强大的安全套接字层密码库
yum install -y openssl openssl-devel
# libevent工具包
yum install -y libevent

3. 安装libfastcommon工具包

tar -zxvf libfastcommonV1.0.7.tar.gz
cd libfastcommon1.0.7
./make.sh
./make.sh install
cp /usr/lib64/libfastcommon.so /usr/lib/
# 查看lib下是否存在libfastcommon.so,如果存在则证明安装成功
ll |grep libfast*

4. 安装Tracker服务

tar -zxvf FastDFS_v5.05.tar.gz
cd FastDFS
./make.sh
./make.sh install
cd /usr/bin/
# 查看编译结果是否产生fdfs相关文件
ll fdfs_*
# 拷贝配置文件到/etc/fdfs/目录下
cp FastDFS/conf/* /etc/fdfs/

修改配置文件

vim /etc/fdfs/tracker.conf

将base_path的值修改为自己的路径,路径必须存在

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

启动tracker服务

# 启动tracker服务
/usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf
# 重启Tracker服务
# /usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf restart

5. 安装Storage服务

注意: 本事例是在同一台服务器上面安装的所有服务,如果是在不同的服务器安装,需要在新的机器编译FastDFS_v5.05.tar.gz源码包,不需要配置Tracker服务。

修改配置文件

vim /etc/fdfs/storage.conf

找到下面的几个属性,修改为自己的路径

# storage服务日志存放路径,路径必须存在
base_path=/home/yuqing/fastdfs
# 文件的保存路径,路径必须存在
store_path0=/home/yuqing/fastdfs
# 指定tracker服务器的ip及端口号,注意不能写localhost或者127.0.0.1
tracker_server=192.168.154.128:22122

启动storage服务

# 启动Storage服务
/usr/bin/fdfs_storaged /etc/fdfs/storage.conf
# 重启Storage服务
# /usr/bin/fdfs_storaged /etc/fdfs/storage.conf restart

六、测试服务

1. 修改配置文件

vim /etc/fdfs/client.conf

找到下列属性,修改为自己的路径

# 客户端日志文件存放的路径
base_path=/home/yuqing/fastdfs/client
# 指定tracker服务器的ip及端口号,注意不能写localhost或者127.0.0.1
tracker_server=192.168.154.128:22122

2. 启动测试

usr/bin/fdfs_test /etc/fdfs/client.conf upload /etc/fdfs/anti-steal.jpg

出现如下下列信息,则表示测试成功

storage_upload_slave_by_filename
group_name=group1, remote_filename=M00/00/00/wKiagFzeZb6AaD8mAABdrZgsqUU942_big.jpg
source ip address: 192.168.154.128
file timestamp=2019-05-17 15:41:50
file size=23981
file crc32=2553063749
example file url: http://192.168.154.128/group1/M00/00/00/wKiagFzeZb6AaD8mAABdrZgsqUU942_big.jpg

七、搭建nginx提供http服务

1. 解压插件压缩包fastdfs-nginx-module_v1.16.tar.gz

cd /usr/local
tar -zxvf fastdfs-nginx-module_v1.16.tar.gz

2. 修改/usr/local/fastdfs-nginx-module/src/config,把其中的local/都去掉

3. 安装nginx,如果已经安装则直接进入到下一步骤

cd /usr/local/
tar -zxvf nginx-1.14.0.tar.gz

这里只需要解压即可,不用立即安装

4. nginx添加Fastdfs-nginx-module模块支持

如果服务器已经安装nginx需要对其重新进行config,不管安装与否都可执行下列命令进行安装

cd /usr/local/nginx-1.14.0mkdir -p /var/temp/nginx./configure \
--prefix=/usr/local/nginx \
--pid-path=/var/run/nginx/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--with-http_gzip_static_module \
--http-client-body-temp-path=/var/temp/nginx/client \
--http-proxy-temp-path=/var/temp/nginx/proxy \
--http-fastcgi-temp-path=/var/temp/nginx/fastcgi \
--http-uwsgi-temp-path=/var/temp/nginx/uwsgi \
--http-scgi-temp-path=/var/temp/nginx/scgi \
--add-module=/usr/local/fastdfs-nginx-module/src # 指定为自己的路径makemake install

5. 把/usr/local/fastdfs-nginx-module/src/mod_fastdfs.conf文件复制到/etc/fdfs目录下,并对其进行编辑

cp -rf /usr/local/fastdfs-nginx-module/src/mod_fastdfs.conf /etc/fdfs/mod_fastdfs.conf
vim /etc/fdfs/mod_fastdfs.conf

找到下列属性,并修改为自己的配置

# 日志存放路径
base_path=/tmp
# 实际的tracker服务器地址
tracker_server=192.168.154.128:22122
# 路径是否包含组名
url_have_group_name = true
# 图片、文件保存路径
store_path0=/home/yuqing/fastdfs

6. 修改nginx的配置

cd /usr/local/nginx/conf/
vim nginx.conf

修改如下内容

server {
listen 80; #端口
server_name 192.168.154.128;
location /group1/M00 { # 多个组 ~/group[1-3]
root /home/yuqing/fastdfs/data; #存储数据
ngx_fastdfs_module;
}
....
}

7. 将libfdfsclient.so拷贝至/usr/lib下

cp -rf /usr/lib64/libfdfsclient.so /usr/lib/libfdfsclient.so

8. 启动nginx

# 注意:执行./nginx启动nginx,这里可以-c指定加载的nginx配置文件,如下
# ./nginx -c /usr/local/nginx/conf/nginx.conf
# 如果不指定-c,nginx在启动时默认加载conf/nginx.conf文件,
# 此文件的地址也可以在编译安装nginx时指定./configure的参数(--conf-path= 指向配置文件(nginx.conf))
cd /usr/local/nginx
# 启动命令
./nginx -c /usr/local/nginx/conf/nginx.conf
# 停止命令
./nginx -s stop
# 全部停止
./nginx -s quit
# 重启ngnix
./ngnix -s reload
# 查看ngnix 状态
netstat -tupln | grep ngnix

八、测试http服务是否成功

1. 上传图片

/usr/bin/fdfs_test /etc/fdfs/client.conf upload /root/anti-steal.jpg

2. 出现如下信息表示上传成功

tracker_query_storage_store_list_without_group:
server 1. group_name=, ip_addr=192.168.154.128, port=23000group_name=group1, ip_addr=192.168.154.128, port=23000
storage_upload_by_filename
group_name=group1, remote_filename=M00/00/00/wKiagFzegJuABMsEAABdrZgsqUU340.jpg
source ip address: 192.168.154.128
file timestamp=2019-05-17 17:36:27
file size=23981
file crc32=2553063749
example file url: http://192.168.154.128/group1/M00/00/00/wKiagFzegJuABMsEAABdrZgsqUU340.jpg
storage_upload_slave_by_filename
group_name=group1, remote_filename=M00/00/00/wKiagFzegJuABMsEAABdrZgsqUU340_big.jpg
source ip address: 192.168.154.128
file timestamp=2019-05-17 17:36:27
file size=23981
file crc32=2553063749
example file url: http://192.168.154.128/group1/M00/00/00/wKiagFzegJuABMsEAABdrZgsqUU340_big.jpg

3. 打开浏览器输入上面的url地址,如果出现图片信息,则表示http服务成功

九、Java使用FastDFS

1. jar包的应用

官方提供一个jar包:fastdfs_client_v1.20.jar,上面有提供这个包的下载地址,直接放到项目的lib包里面即可。如果是用maven进行管理,可以添加下面的依赖

<dependency>
<groupId>net.oschina.zcx7878</groupId>
<artifactId>fastdfs-client-java</artifactId>
<version>1.27.0.0</version>
</dependency>

2.使用方法

1.把FastDFS提供的jar包添加到工程中

2.初始化全局配置。加载一个配置文件。

3.创建一个TrackerClient对象。

4.通过TrackerClient获得一个TrackerServer对象。

5.声明一个StorageServer对象,null。

6.通过TrackerServer对象和StorageServer对象获得一个StorageClient对象。

7.直接调用StorageClient对象方法上传文件即可。

3.测试工程

创建配置文件

文件名称自定义,本测试案例为client.conf,文件内容如下

# Fastdfs访问地址和端口号
tracker_server=192.168.154.128:22122

FastDFS工具类

将本工具类放到工程的任何一个目录即可,代码如下:

import org.csource.common.NameValuePair;
import org.csource.fastdfs.ClientGlobal;
import org.csource.fastdfs.StorageClient1;
import org.csource.fastdfs.StorageServer;
import org.csource.fastdfs.TrackerClient;
import org.csource.fastdfs.TrackerServer;/**
* @ClassName FastDFSClient
* @Description
* @Author ZhangYue
* @Date 2019/5/20 18:02
* @Version 1.0
*/
public class FastDFSClient {
private TrackerClient trackerClient = null;
private TrackerServer trackerServer = null;
private StorageServer storageServer = null;
private StorageClient1 storageClient = null; public FastDFSClient(String conf) throws Exception {
if (conf.contains("classpath:")) {
conf = conf.replace("classpath:", this.getClass().getResource("/").getPath());
}
ClientGlobal.init(conf);
trackerClient = new TrackerClient();
trackerServer = trackerClient.getConnection();
storageServer = null;
storageClient = new StorageClient1(trackerServer, storageServer);
} /**
* 上传文件方法
* <p>Title: uploadFile</p>
* <p>Description: </p>
* @param fileName 文件全路径
* @param extName 文件扩展名,不包含(.)
* @param metas 文件扩展信息
* @return
* @throws Exception
*/
public String uploadFile(String fileName, String extName, NameValuePair[] metas) throws Exception {
String result = storageClient.upload_file1(fileName, extName, metas);
return result;
} public String uploadFile(String fileName) throws Exception {
return uploadFile(fileName, null, null);
} public String uploadFile(String fileName, String extName) throws Exception {
return uploadFile(fileName, extName, null);
} /**
* 上传文件方法
* <p>Title: uploadFile</p>
* <p>Description: </p>
* @param fileContent 文件的内容,字节数组
* @param extName 文件扩展名
* @param metas 文件扩展信息
* @return
* @throws Exception
*/
public String uploadFile(byte[] fileContent, String extName, NameValuePair[] metas) throws Exception { String result = storageClient.upload_file1(fileContent, extName, metas);
return result;
} public String uploadFile(byte[] fileContent) throws Exception {
return uploadFile(fileContent, null, null);
} public String uploadFile(byte[] fileContent, String extName) throws Exception {
return uploadFile(fileContent, extName, null);
}
}

编写测试代码进行测试

测试代码如下:

import org.csource.common.MyException;
import org.csource.fastdfs.ClientGlobal;
import org.csource.fastdfs.StorageClient;
import org.csource.fastdfs.StorageServer;
import org.csource.fastdfs.TrackerClient;
import org.csource.fastdfs.TrackerServer;
import org.junit.Test;import java.io.IOException;public class FastdfsTest {
@Test
public void testUpload1() throws IOException, MyException {
ClientGlobal.init("D:\\Workspaces\\file-storage\\src\\main\\resources\\client.conf"); TrackerClient trackerClient = new TrackerClient();
TrackerServer trackerServer = trackerClient.getConnection(); StorageServer storageServer = null;
StorageClient storageClient = new StorageClient(trackerServer, storageServer); String[] strings = storageClient.upload_file("C:\\Users\\Alan\\Desktop\\30kb图片.jpg", "jpg", null);
for (String string : strings) {
System.out.println(string);
}
}}

如出现如下测试结果,表示java程序编写成功

group1
M00/00/00/wKiagFzjaB6Ach2pAABH6l_9yHw101.jpg

浏览器访问

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,091
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,568
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,416
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,189
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,825
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,908