FastDFS服务器

分布式文件系统

  • 分布式文件系统(Distributed File System)是指文件系统管理的物理存储资源不一定直接连接在本地节点上,而是通过计算机网络与节点相连。
    通俗来讲
    • 传统文件系统管理的文件就存储在本机。
    • 分布式文件系统管理的文件存储在很多机器,这些机器通过网络连接,要被统一管理。无论是上传或者访问文件,都需要通过管理中心来访问

什么是FastDFS

  • FastDFS是由淘宝的余庆先生所开发的一个轻量级、高性能的开源分布式文件系统。用纯C语言开发,功能丰富:
    • 文件存储
    • 文件同步
    • 文件访问(上传、下载)
    • 存取负载均衡
    • 在线扩容
  • 适合有大容量存储需求的应用或系统。同类的分布式文件系统有谷歌的GFS、HDFS(Hadoop)、TFS(淘宝)等。

FastDFS的架构

结构图

在这里插入图片描述

  • FastDFS两个主要的角色:Tracker Server 和 Storage Server 。
    • Tracker Server:跟踪服务器,主要负责调度storage节点与client通信,在访问上起负载均衡的作用,和记录storage节点的运行状态,是连接client和storage节点的枢纽。
    • Storage Server:存储服务器,保存文件和文件的meta data(元数据),每个storage server会启动一个单独的线程主动向Tracker cluster中每个tracker server报告其状态信息,包括磁盘使用情况,文件同步情况及文件上传下载次数统计等信息
    • Group:文件组,多台Storage Server的集群。上传一个文件到同组内的一台机器上后,FastDFS会将该文件即时同步到同组内的其它所有机器上,起到备份的作用。不同组的服务器,保存的数据不同,而且相互独立,不进行通信
    • Tracker Cluster:跟踪服务器的集群,有一组Tracker Server(跟踪服务器)组成。
    • Storage Cluster :存储集群,有多个Group组成。

上传和下载流程

上传:
在这里插入图片描述

  • Client通过Tracker server查找可用的Storage server。
  • Tracker server向Client返回一台可用的Storage server的IP地址和端口号。
  • Client直接通过Tracker server返回的IP地址和端口与其中一台Storage server建立连接并进行文件上传。
  • 上传完成,Storage server返回Client一个文件ID,文件上传结束。

下载
在这里插入图片描述

  • Client通过Tracker server查找要下载文件所在的的Storage server。
  • Tracker server向Client返回包含指定文件的某个Storage server的IP地址和端口号。
  • Client直接通过Tracker server返回的IP地址和端口与其中一台Storage server建立连接并指定要下载文件。
  • 下载文件成功。

下载安装包即安装教程

java 客户端

余庆先生提供了一个Java客户端,但是作为一个C程序员,写的java代码可想而知。而且已经很久不维护了。

这里推荐一个开源的FastDFS客户端,支持最新的SpringBoot2.0。

地址

在这里插入图片描述

  • 依赖
    <dependency>
        <groupId>com.github.tobato</groupId>
        <artifactId>fastdfs-client</artifactId>
        <version>1.26.2</version>
    </dependency>
    
  • 配置
    @Configuration
    @Import(FdfsClientConfig.class)
    // 解决jmx重复注册bean的问题
    @EnableMBeanExport(registration = RegistrationPolicy.IGNORE_EXISTING)
    public class FastClientImporter {
    }
    
    编写yaml
    fdfs:
      so-timeout: 1501
      connect-timeout: 601
      thumb-image: # 缩略图
        width: 60
        height: 60
      tracker-list: # tracker地址
        - 192.168.56.101:22122
    

test

@RunWith(SpringRunner.class)
@SpringBootTest(classes = LyUploadService.class)
public class FdfsTest {

    @Autowired
    private FastFileStorageClient storageClient;

    @Autowired
    private ThumbImageConfig thumbImageConfig;

    @Test
    public void testUpload() throws FileNotFoundException {
        File file = new File("D:\\test\\baby.png");
        // 上传并且生成缩略图
        StorePath storePath = this.storageClient.uploadFile(
                new FileInputStream(file), file.length(), "png", null);
        // 带分组的路径
        System.out.println(storePath.getFullPath());
        // 不带分组的路径
        System.out.println(storePath.getPath());
    }

    @Test
    public void testUploadAndCreateThumb() throws FileNotFoundException {
        File file = new File("D:\\test\\baby.png");
        // 上传并且生成缩略图
        StorePath storePath = this.storageClient.uploadImageAndCrtThumbImage(
                new FileInputStream(file), file.length(), "png", null);
        // 带分组的路径
        System.out.println(storePath.getFullPath());
        // 不带分组的路径
        System.out.println(storePath.getPath());
        // 获取缩略图路径
        String path = thumbImageConfig.getThumbImagePath(storePath.getPath());
        System.out.println(path);
    }
}

Utils

配置好

 @Autowired
    private FastFileStorageClient fastFileStorageClient;

    /**
     * 上传DFS图片
     *
     * @param acImage
     * @return
     * @throws IOException
     */
    public String DfsFiles(MultipartFile acImage) {

        StorePath storePath = null;
        try {
            storePath = fastFileStorageClient.uploadFile(acImage.getInputStream(), acImage.getSize(), StringUtils.substringAfterLast(acImage.getOriginalFilename(), "."), null);
            return  "http:// 192.168.56.101/" + storePath.getFullPath();
        } catch (IOException e) {
            return null;
        }

    }

liunx 安装

  • 如果ip被修改
    解决方法
    • cd /etc/fdfs
      在这里插入图片描述

    ip更改后修改配置文件

    • cd /etc/fdfs
    • vim storage.conf 更改ip
    • vim /mod_fastdfs.conf 更改ip
    • service - stop
    • service - start
      fdfs_trackerd
      fdfs_storaged
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值