FastDFS实现原理及流程

一、什么是FastDFS

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

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

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

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

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

二、服务端两个角色:

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

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

三、执行流程

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

测试代码:

1、pom配置(引入fastdfs配置即可):



    
        Fast111
        cn.cfg
        1.0-SNAPSHOT
        fastDFSDemo/Fast111/pom.xml
    
    4.0.0

    Fast2

    
        
            junit
            junit
            4.11
            test
        

        
            org.csource.fastdfs
            fastdfs
            1.2
        
      
    


2、Resources包下的配置(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.200.128: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

3、测试代码块(FastDFSTest.java):

import org.csource.fastdfs.ClientGlobal;
import org.csource.fastdfs.StorageClient;
import org.csource.fastdfs.TrackerClient;
import org.csource.fastdfs.TrackerServer;

public class TestFastDFS{
   public static String[] upload(String conFilename, String fileName) throws Exception{
        // 1.初始化连接配置
       ClientGlobal.init(conFilename);
       // 2.创建跟踪器客户端 tracker客户端
       TrackerClient trackerClient = new TrackerClient();
       //3. 通过tracker客户端连接  获得trackerServer
       TrackerServer trackerServer = trackerClient.getConnection();
       //4. 通过跟踪器客户端获取存储节点对象
       StorageClient storageClient = new StorageClient(trackerServer, null);
       //5.执行上传
       String[] strings = storageClient.upload_appender_file(fileName, null, null);
       //获取返回值
       return strings;
   }


    public static void main(String[] args) throws Exception {
        String tracker ="E:\\9.19fastDFSDemo\\Fast111\\Fast2\\src\\main\\resources\\fdfs_client.conf";
        //设置上传图片的本地路径
        String filerName="D:\\test\\ad53a60.jpg";
        String[] uploads = upload(tracker, filerName);
        for (String upload : uploads) {
            //打印返回值
            System.out.println(upload);
        }

    }
}

控制台打印结果:
FastDFS实现原理及流程_第3张图片
浏览器测试(输入端口号+组名+文件名):
FastDFS实现原理及流程_第4张图片

你可能感兴趣的:(框架)