02.fastDFS分布式文件上传下载解决方案

fastDFS 分布式文件上传解决方案

一、fastDFS介绍

1). 特点

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

2). 架构

FastDFS 架构包括 Tracker serverStorage server。客户端请求 Tracker server 进行文件上传、下载,通过 Tracker server 调度最终由 Storage server 完成文件上传和下载。

Tracker server 作用是负载均衡和调度,通过 Tracker server 在文件上传时可以根据一些策略找到 Storage server 提供文件上传服务。可以将 tracker 称为追踪服务器或调度服务器。

Storage server 作用是文件存储,客户端上传的文件最终存储在 Storage 服务器上,Storageserver 没有实现自己的文件系统而是利用操作系统 的文件系统来管理文件。可以将storage称为存储服务器。

3). 服务端的两个角色


02.fastDFS分布式文件上传下载解决方案_第1张图片
fastDFS1.png

1. Tracker:管理集群,tracker 也可以实现集群。每个 tracker 节点地位平等。收集 Storage 集群的状态。

2. Storage:实际保存文件 Storage 分为多个组,每个组之间保存的文件是不同的。每个组内部可以有多个成员,组成员内部保存的内容是一样的,组成员的地位是一致的,没有主从的概念。

二、文件上传下载

1). 上传流程


02.fastDFS分布式文件上传下载解决方案_第2张图片
文件上传时序图.png

客户端上传文件后存储服务器将文件 ID 返回给客户端,此文件 ID 用于以后访问该文件的索引信息。文件索引信息包括:组名,虚拟磁盘路径,数据两级目录,文件名。
group1/M00/02/44/wKgDrE34E8wAAAAAGxx.xx

  1. 组名:文件上传后所在的 storage 组名称,在文件上传成功后有storage服务器返回,需要客户端自行保存。
  2. 虚拟磁盘路径:storage 配置的虚拟路径,与磁盘选项 store_path*对应。如果配置了store_path0 则是 M00,如果配置了store_path1 则是 M01,以此类推。
  3. 数据两级目录:storage 服务器在每个虚拟磁盘路径下创建的两级目录,用于存储数据文件。
  4. 文件名:与文件上传时不同。是由存储服务器根据特定信息生成,文件名包含:源存储服务器 IP 地址、文件创建时间戳、文件大小、随机数和文件拓展名等信息。

2). 下载流程


02.fastDFS分布式文件上传下载解决方案_第3张图片
文件下载时序图.png

3). 最简单的fastDFS架构


02.fastDFS分布式文件上传下载解决方案_第4张图片
最简单的fastDFS架构.png

四、FastDFS安装

推荐请有经验的运维开发人员安装!开发中如果使用的为别人配置好的虚拟器,请一定要选择 我已移动该虚拟机!, 否则会出现配置问题!

FastDFS安装部署

密码:oox9; 依赖jar包也在分享的文件中!maven中央仓库没有,需要手动安装到本地仓库或者私服

mvn install:install-file -DgroupId=org.csource.fastdfs -DartifactId=fastdfs  -Dversion=1.2 -Dpackaging=jar -Dfile=d:\setup\fastdfs_client_v1.20.jar

五、FastDFS使用案例

1). 项目结构图


02.fastDFS分布式文件上传下载解决方案_第5张图片
fastDFS使用案例.png
  1. Controller
package com.lingting.controller;

import com.lingting.utils.FastDFSClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/** 文件上传解析器 */
@RestController
@RequestMapping("/file")
public class UploadController {

    /** 这里的依赖注入 在 springMVC中已经配置了,写了这个注解
     * 框架会自动将对应值进行注入 */
    @Value("${FILE_SERVER_URL}")
    private String FILE_SERVER_URL;

    @RequestMapping("/upload")
    public String upload(MultipartFile file) {

        // 1. 取文件的扩展名
        String originalFilename = file.getOriginalFilename();
        String extName = originalFilename.substring(originalFilename.lastIndexOf(".") + 1);
        try {
            // 2. 创建一个FastDFS的客户端, FastDFS 类是封装好的一个工具类,将fastDFS的接口封装好了
            FastDFSClient fastDFSClient = new FastDFSClient("classpath:fdfs_client.conf");

            // 3. 执行上传处理
            String path = fastDFSClient.uploadFile(file.getBytes(), extName);

            // 4. 拼接返回的url和ip地址,拼装成完整的url
            String url = FILE_SERVER_URL + path;
            System.out.println(url);
            return url;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return "null";
    }
}
  1. utils
package com.lingting.utils;

import org.csource.common.NameValuePair;
import org.csource.fastdfs.*;

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);
    }

    /**
     * 上传文件方法
     * 

Title: uploadFile

*

Description:

* @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); } /** * 上传文件方法 *

Title: uploadFile

*

Description:

* @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); } }
  1. fdfs_client.conf
# connect timeout in seconds
# default value is 30s
connect_timeout=30

# network timeout in seconds
# default value is 30s
network_timeout=60

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

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

#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

# if use connection pool
# default value is false
# since V4.05
use_connection_pool = false

# connections whose the idle time exceeds this time will be closed
# unit: second
# default value is 3600
# since V4.05
connection_pool_max_idle_time = 3600

# if load FastDFS parameters from tracker server
# since V4.05
# default value is false
load_fdfs_parameters_from_tracker=false

# 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 V4.05
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 V4.05
storage_ids_filename = storage_ids.conf


#HTTP settings
http.tracker_server_port=80

#use "#include" directive to include HTTP other settiongs
##include http.conf
  1. application.properties
FILE_SERVER_URL=http://192.168.25.133/
  1. springmvc.xml



    
    
    
    
    
    
    
    
        
        
        
    

  1. web.xml


    
    
        CharacterEncodingFilter
        org.springframework.web.filter.CharacterEncodingFilter
        
            encoding
            utf-8
        
        
            forceEncoding
            true
        
    
    
        CharacterEncodingFilter
        /*
    

    
        springmvc
        org.springframework.web.servlet.DispatcherServlet
        
        
            contextConfigLocation
            classpath:springmvc.xml
        
        1
    

    
        springmvc
        *.do
    

  1. index.html
  1. pom.xml依赖
war


    
    
        org.springframework
        spring-context
        4.2.4.RELEASE
    
    
        org.springframework
        spring-beans
        4.2.4.RELEASE
    
    
        org.springframework
        spring-webmvc
        4.2.4.RELEASE
    
    
        org.springframework
        spring-jdbc
        4.2.4.RELEASE
    
    
        org.springframework
        spring-aspects
        4.2.4.RELEASE
    
    
        org.springframework
        spring-jms
        4.2.4.RELEASE
    
    
        org.springframework
        spring-context-support
        4.2.4.RELEASE
    
    
        org.springframework
        spring-test
        4.2.4.RELEASE
    

    
    
        org.csource.fastdfs
        fastdfs
        1.2
    
    
        commons-fileupload
        commons-fileupload
        1.3.1
    
    
        junit
        junit
        4.12
    

六、FastDFS独立小Demo

1). 项目结构


02.fastDFS分布式文件上传下载解决方案_第6张图片
fastDFSDemo.png

2). 代码

  1. pom.xml依赖
jar

    
        org.csource.fastdfs
        fastdfs
        1.2
    

  1. fdfs_client.conf配置文件
connect_timeout=30
network_timeout=60
base_path=/home/fastdfs
tracker_server=192.168.25.133:22122
log_level=info
use_connection_pool = false
connection_pool_max_idle_time = 3600
load_fdfs_parameters_from_tracker=false
use_storage_id = false
storage_ids_filename = storage_ids.conf
http.tracker_server_port=80
  1. FastDFSDemo
import org.csource.fastdfs.*;

/**
 * FastDFS 入门案例
 * */
public class FastDFSDemo {

    public static void main(String[] args) throws Exception{

        // 1. 加载配置文件,配置文件的内容就是tracker服务的地址
        ClientGlobal.init("E:\\JavaWebSSMWorkspaces\\pinyougou\\fastDFSDemo\\src\\main\\resources\\fdfs_client.conf");

        // 2. 创建一个TrackerClient对象。
        TrackerClient trackerClient = new TrackerClient();

        // 3. 使用TrackerClient对象创建连接,获得一个TrackServer对象
        TrackerServer trackerServer = trackerClient.getConnection();

        // 4. 创建一个StorageServer的引用,值为null
        StorageServer storageServer = null;

        // 5、创建一个 StorageClient 对象,需要两个参数 TrackerServer 对象、StorageServer 的引用
        StorageClient storageClient = new StorageClient(trackerServer, storageServer);

        // 6、使用 StorageClient 对象上传图片。扩展名不带“.”

        String[] strings = storageClient.upload_file("G:\\图片\\4K\\117068_original_5760x3240.jpg", "jpg", null);

        // 7、返回数组。包含组名和图片的路径。
        for (String string : strings) {
            System.out.println(string);
        }

    }
}

你可能感兴趣的:(02.fastDFS分布式文件上传下载解决方案)