FastDFS

原理

是一个开源的轻量级分布式文件系统,对文件进行管理。

  • 功能:
    文件存储,文件访问,文件上传下载。

  • 单次存储大小:
    4k–500M

  • 好处:
    解决了大量存储和负载均衡问题。

  • 用处:
    适合文件类的在线服务,比如视频网站腾讯视频,相册看图网站图虫。

二、服务端两个角色:

  • 跟踪器(tracker):
    做调度工作,起到负载均衡的作用。管理集群,也可以实现集群。每个tracker阶段地位平等,负责收集storage的状态。

  • 存储节点(storage):
    实际保存文件storage分为多个组,每个组保存的文件时不同的。
    每个组有多个组成员,组成员保存的内容是一样的,组成员地位平等,没有主从概念。

三、执行流程

1.初始化连接配置
2.创建一个Tracker客户端,目的是为了连接Tracker,获取TrackerServer对象
3.通过TrackerServer创建一个Storage客户端对象,该对象用来操作Storage
4.通过Storage实现上传下载
5.获取文件上传下载信息

实践

安装环境

yum install git gcc gcc-c++ make automake autoconf libtool pcre pcre-deval zlib zlib-devel openssl-devel wget vim -y

关闭防火墙

systemctl disable firewalld

下载

git clone https://gitee.com/fastdfs100/libfastcommon.git --depth 1
cd libfastcommon
./make.sh && ./make.sh install
git clone https://gitee.com/fastdfs100/fastdfs.git --depth 1
cd fastdfs
./make.sh && ./make.sh install
git clone https://gitee.com/fastdfs100/fastdfs-nginx-module.git --depth 1
cd fastdfs-nginx-module
./make.sh && ./make.sh install

修改配置文件

mkdir -p /data/fastdfs # -p 如果存在
# 这步时创建文件夹,就是存储路径的文件,名字自己定义,要和后面的保持一致# 对于 tracker.conf
base_path=/data/fastdfs # 路径设置,自定义路径,其中文件夹要自己创建必须存在# 对于 storage.conf
base_path=/data/fastdfs # 存储根路径路径
store_path0=/data/fastdfs # 存储路径设置
tracker_server=172.22.45.70:22122 # 一般设置内网路径,自己通过ifconfig -a 查询内网地址,不能使127.0.0.1
# 尝试过服务器外网不行,可能我是轻量级服务器的原因
http.server_port=8989 # 设置访问端口,默认是8888,但是由于我是宝塔面板,要进行修改# 对于client.conf 配置
tracker_server=172.22.45.70:22122# 对于mod_fastdfs.config 这是nginx模块默认的配置文件
tracker_server=172.22.45.70:22122
store_path0=/data/fastdfs

记住复制 源文件中的 tracker.conf 、 storage.conf 、 mod_fastdfs.config (都放在)他们的 解压文件下面。复制到/etc/fdfs

# 启动
/usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf# 启动storage服务
/usr/bin/fdfs_storaged /etc/fdfs/storage.conf# 存储图片
fdfs_upload_file /etc/fdfs/client.conf /1.JPG#删除图片
fdfs_delete_file /etc/fdfs/client.conf group1/M00/00/00/roup1/M00/00/00/rBYtRmIEre2AYPGYAABx01H1i_o128.JPG# 停止
killall fdfs_trackerd
killall fdfs_storaged# 查看运行情况
ps -ef|grep tracker
ps -ef|grep storage

配置ngnix

tracker_server=172.22.45.70:22122
url_have_group_name = true
store_path0=/data/fastdfs

nginx 添加模块步骤

先查看之前的安装设置项:

/www/server/nginx/sbin/nginx -V

会显示如下:(你不一定跟我版本一样,所以你显示可能会与我有所差别)

nginx version: nginx/1.18.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC)
built with OpenSSL 1.1.1g  21 Apr 2020
TLS SNI support enabled
configure arguments: --user=www --group=www --prefix=/www/server/nginx --add-module=/www/server/nginx/src/ngx_devel_kit --add-module=/www/server/nginx/src/lua_nginx_module --add-module=/www/server/nginx/src/ngx_cache_purge --add-module=/www/server/nginx/src/nginx-sticky-module --with-openssl=/www/server/nginx/src/openssl --with-pcre=pcre-8.43 --with-http_v2_module --with-stream --with-stream_ssl_module --with-stream_ssl_preread_module --with-http_stub_status_module --with-http_ssl_module --with-http_image_filter_module --with-http_gzip_static_module --with-http_gunzip_module --with-ipv6 --with-http_sub_module --with-http_flv_module --with-http_addition_module --with-http_realip_module --with-http_mp4_module --with-ld-opt=-Wl,-E --with-cc-opt=-Wno-error --with-ld-opt=-ljemalloc --with-http_dav_module --add-module=/www/server/nginx/src/nginx-dav-ext-module

其中我是使用宝塔面板的,所以按照nginx需要编译安装,自己配置其中的文件路径,添加模块,建议按照1.16版本

注意把 configure arguments: 后面的配置项复制一下,例如我这把下面的内容都复制下来了

--user=www --group=www --prefix=/www/server/nginx --add-module=srclib/ngx_devel_kit --add-module=srclib/lua_nginx_module --add-module=srclib/ngx_cache_purge --add-module=srclib/nginx-sticky-module --with-openssl=srclib/openssl --with-pcre=srclib/pcre-8.43 --with-http_v2_module --with-stream --with-stream_ssl_module --with-stream_ssl_preread_module --with-http_stub_status_module --with-http_ssl_module --with-http_image_filter_module --with-http_gzip_static_module --with-http_gunzip_module --with-ipv6 --with-http_sub_module --with-http_flv_module --with-http_addition_module --with-http_realip_module --with-http_mp4_module --with-ld-opt=-Wl,-E --with-cc-opt=-Wno-error --with-http_dav_module --add-module=srclib/nginx-dav-ext-module

切换到你的宝塔安装目录,输入如下命令

cd /www/server/nginx/src

现在把你要安装的模块添加进去(例如我要添加的是fastdfs-nginx-module这个模块),你仔细看我下面的命令已经在最后面加了一个--add-module=/usr/local/fastdfs-nginx-module/src

./configure --user=www --group=www --prefix=/www/server/nginx --add-module=/www/server/nginx/src/ngx_devel_kit --add-module=/www/server/nginx/src/lua_nginx_module --add-module=/www/server/nginx/src/ngx_cache_purge --add-module=/www/server/nginx/src/nginx-sticky-module --with-openssl=/www/server/nginx/src/openssl --with-pcre=pcre-8.43 --with-http_v2_module --with-stream --with-stream_ssl_module --with-stream_ssl_preread_module --with-http_stub_status_module --with-http_ssl_module --with-http_image_filter_module --with-http_gzip_static_module --with-http_gunzip_module --with-ipv6 --with-http_sub_module --with-http_flv_module --with-http_addition_module --with-http_realip_module --with-http_mp4_module --with-ld-opt=-Wl,-E --with-cc-opt=-Wno-error --with-ld-opt=-ljemalloc --with-http_dav_module --add-module=/www/server/nginx/src/nginx-dav-ext-module  --add-module=/usr/local/fastdfs-nginx-module/src/

执行完上面的命令之后make一下

make

安装 重新安装

make install

注意,此时你看它会自动给你备份了原来安装的,有个***.old文件,而且你本次安装原来的配置不会失效

ll /www/server/nginx/sbin

重启nginx

/www/server/nginx/sbin/nginx -s stop    #注意,这是停止
/www/server/nginx/sbin/nginx            #注意,这是启动,不带任何参数的
/www/server/nginx/sbin/nginx reload # 重启

ok,大功告成,如果你不是宝塔安装的nginx,也可以以此类推解决,关键是要找到nginx的configure,修改里面的设置项,再make就行了

nginx 配置文件

server {listen 8989;server_name localhost;location ~/group[0-9]/ {ngx_fastdfs_module;}error_page 500 502 503 504 /50x.html;location = /50x.html {root html;}
}

访问 记住文件名字一定准确

wget http://172.22.45.70:8989/group1/M00/00/00/rBYtRmIEre2AYPGYAABx01H1i_o128.JPG

使用宝塔面板的网站反向代理进行网页访问

大坑

  • 由于我是使用了宝塔面试,占用了我的8888端口,记得在storage上把端口改成其他端口,同理在nginx上也要修改

整合Java

导入包

<dependency><groupId>net.oschina.zcx7878</groupId><artifactId>fastdfs-client-java</artifactId><version>1.27.0.0</version>
</dependency><!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
<dependency><groupId>org.apache.commons</groupId><artifactId>commons-lang3</artifactId><version>3.12.0</version>
</dependency>

配置文件

fastdfs.connect_timeout_in_seconds=10
fastdfs.network_timeout_in_seconds=30
fastdfs.charset=UTF-8
fastdfs.tracker_servers=120.24.43.3:22122
fastdfs.http_tracker_http_port=8989

工具类

package com.peng.utils;import com.peng.Test;
import org.csource.common.NameValuePair;
import org.csource.fastdfs.*;import java.util.Properties;/*** @description:* @projectName: FastDFS* @see: com.peng.utils* @author: pc* @createTime: 2022/2/10 17:50* @version: 1.0*/
public class FastDFSUtils {private FastDFSUtils() {}private static final Properties properties = new Properties();private static final StorageClient storageClient;static {try {Properties properties = new Properties();properties.load(Test.class.getClassLoader().getResourceAsStream("fdfs.properties"));ClientGlobal.initByProperties(properties);// 使用的就是配置文件的地址生成的客服端TrackerClient trackerClient = new TrackerClient();TrackerServer trackerServer = trackerClient.getConnection();StorageServer storageServer = trackerClient.getStoreStorage(trackerServer);storageClient = new StorageClient(trackerServer, storageServer);} catch (Exception e) {e.printStackTrace();// 初始化代码块异常,抛出错误,停止虚拟机throw new ExceptionInInitializerError(e);}}/*** @desc 删除文件* @param group* @param path* @return boolean* @author pengshi* @date 2022/2/10 18:29**/public static boolean delete(String group, String path) {try {int stat = storageClient.delete_file(group, path);return stat == 0;} catch (Exception e) {e.printStackTrace();// 删除失败return false;}}/*** @desc 下载文件* @param group 组名* @param path 文件路径以及文件名字* @return byte文件二进制数据* @author pengshi* @date 2022/2/10 18:18**/public static byte[] download(String group, String path) {try {return storageClient.download_file(group, path);} catch (Exception e) {e.printStackTrace();// 下载失败return null;}}/*** @desc 根据组名和文件信息查询元数据数组* @param group 组名* @param path 文件路径以及文件名字* @return NameValuePair* @author pengshi* @date 2022/2/10 18:21**/public static NameValuePair[] getMetaData(String group, String path) {try {return storageClient.get_metadata(group, path);}catch (Exception e) {e.printStackTrace();return null;}}/*** @desc 上传文件数据* @param data* @param extName* @param nvps* @return String 文件路径* @author pengshi* @date 2022/2/10 18:17**/public static String uploadFile(byte[] data, String extName, NameValuePair[] nvps) {try {String[] result = storageClient.upload_file(data, extName, nvps);String path = result[0] + "/" + result[1];return path;} catch (Exception e) {e.printStackTrace();// 上传失败return null;}}
}

FastDFS 原理 以及 简单使用相关推荐

  1. python装饰器实例-Python装饰器原理与简单用法实例分析

    本文实例讲述了Python装饰器原理与简单用法.分享给大家供大家参考,具体如下: 今天整理装饰器,内嵌的装饰器.让装饰器带参数等多种形式,非常复杂,让人头疼不已.但是突然间发现了装饰器的奥秘,原来如此 ...

  2. Java注解的基本概念和原理及其简单实用

      一.注解的基本概念和原理及其简单实用 注解(Annotation)提供了一种安全的类似注释的机制,为我们在代码中添加信息提供了一种形式化得方法,使我们可以在稍后某个时刻方便的使用这些数据(通过解析 ...

  3. 深入了解RabbitMQ工作原理及简单使用

    深入了解RabbitMQ工作原理及简单使用 RabbitMQ系列文章 RabbitMQ在Ubuntu上的环境搭建 深入了解RabbitMQ工作原理及简单使用 RabbitMQ交换器Exchange介绍 ...

  4. 编译原理(简单自动词法分析器LEX)

    编译原理(简单自动词法分析器LEX)源程序下载地址:  http://files.cnblogs.com/files/hujunzheng/%E6%B1%87%E7%BC%96%E5%8E%9F%E7 ...

  5. java 排序原理_简单选择排序算法原理及java实现(超详细)

    简单选择排序的原理 简单选择排序的原理非常简单,即在待排序的数列中寻找最大(或者最小)的一个数,与第 1 个元素进行交换,接着在剩余的待排序的数列中继续找最大(最小)的一个数,与第 2 个元素交换.以 ...

  6. 深入解读RabbitMQ工作原理及简单使用

    深入解读RabbitMQ工作原理及简单使用 RabbitMQ系列目录 RabbitMQ在Ubuntu上的环境搭建 深入解读RabbitMQ工作原理及简单使用 Rabbit的几种工作模式介绍与实践 Ra ...

  7. PHP的分页原理和简单实例

    最新PHP的分页原理和简单实例 以下是三零网为大家整理的最新PHP的分页原理和简单实例的文章,希望大家能够喜欢! 1.前言 分页显示是一种非常常见的浏览和显示大量数据的方法,属于web编程中最常处理的 ...

  8. 左倾红黑树的原理及简单实现

    (注:以下图片全部源于<算法 第4版>) 左倾红黑树的原理及简单实现 左倾红黑树的简介 左倾红黑树的定义 左倾红黑树与2-3树的对比 左倾红黑树的颜色表示 左倾红黑树的一些基本操作 1.颜 ...

  9. (python)小最的电脑上安装了一个机器翻译软件,他经常用这个软件来翻译英语文章这个翻译软件的原理很简单,它只是从头到尾,依次将每个英文单词用对应的中文含义来替换。对于每个英文单词,软件会先在内存中

    问题描述 小最的电脑上安装了一个机器翻译软件,他经常用这个软件来翻译英语文章 这个翻译软件的原理很简单,它只是从头到尾,依次将每个英文单词用对应的中文含义 来替换.对于每个英文单词,软件会先在内存中查 ...

  10. VGA成像原理与简单实现

    VGA成像原理与简单实现 前言 一.VGA成像原理 二.VGA成像时序详解 三.VGA成像设计 四.VGA控制器Verilog设计实现 五.TB文件展示 六.仿真结果分析 前言 本篇文章简单记录VGA ...

最新文章

  1. 如何更好地玩转GitHub?
  2. Python使用pandas读取两个或者多个excel文件(xlsx)并进行数据连接(join)合并两个或者多个excel的信息
  3. cocos2d-x 2.0启用HD高清图片支持
  4. 我们前端忙成狗人家后端写SQL?
  5. 代码和产品发布的几种方式
  6. 说说报表工具的无编码定制能力
  7. 底部菜单 点击突起_iOS开发之上下文交互菜单(UIContextMenuInteraction)
  8. 从美图、4399生根,到趣店、瑞幸落户,厦门离中国的西雅图还有多远?
  9. Spring MVC Rest 学习 一
  10. 判断给定森林中有多少棵树特别版
  11. Java实验二 计算机 1501 王奕开 18
  12. 0320 关于构建之法前三章的读后感
  13. (崩溃整理)EDEM多体动力学EDEM-ADAMS耦合步骤,及报错问题分析
  14. 台式电脑怎么组装步骤_台式机组装教程,详细教您台式机怎么组装
  15. 右键菜单项删除 pycharm vs
  16. linux系统单网卡绑定双IP的方法
  17. [IMX6Q][Android5.1]移植笔记 --- Kernel启动无法挂载文件系统
  18. sicily 9562 SUME
  19. 孙溟㠭书法篆刻《寿》
  20. CSPJ2021考完了,你,AK了吗?

热门文章

  1. 对目录的操作(opendir)
  2. Debug查看汉字机内码
  3. html5标签参考手册
  4. Odoo12功能模块文档整理
  5. 对医疗类网站的seo优化方法细致探讨
  6. 画出清明上河图的代码_制作阴影立体动态图文图的代码【清明上河图】
  7. 2016年大学计算机期末笔试题目,2017年计算机基础大一考试题「附答案」
  8. c语言中sqrt取整,69. Sqrt(x) 求根号再取整
  9. IAR8.3.2破解说明
  10. android 上位机下位机串口通讯,上位机下位机串口通信.doc