FastDFS 分布文件系统

1.定义:FastDFS分布式文件系统是用c语言编写的一款开源的分布式文件系统,考虑了冗余备份、负载均衡、线性扩容等机制,提供文件上传、下载,存储,同步,FastDFS架构包括 Tracker server(追踪、调度服务器)和Storage server(存储服务器)。

2..原理:客户端请求Tracker server进行文件上传、下载,通过Tracker server调度最终由Storage server完成文件上传和下载。

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

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

5.FastDFS与Tomcat对比:

  • FastDFS相比于Tomcat :不存在单点故障问题、支持容灾。
  • FastDFS相比于Tomcat:集群 (安全) 保存图片过多时扩容   分配合理   。
  • FastDFS相比于Tomcat: 具有备份功能、不怕磁盘损坏。

6.FastDFS安装及配置(Linux环境下):https://www.2cto.com/kf/201708/669886.html

7.FastDFS使用(Myeclipse)

  • 配置文件client.conf

  • 工具类FastDFSClient
package com.taotao.common.utils;

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;

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);
    }
}
  • 图片上传时调用FastDFSClient对应服务
package com.taotao.service.impl;

import java.io.IOException;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.taotao.common.pojo.PictureResult;
import com.taotao.common.utils.FastDFSClient;
import com.taotao.service.PictureService;

@Service
public class PictureServiceImpl implements PictureService {
	
	//192.168.64.128
	@Value("${IMAGE_SERVER_BASE_URL}")
	private String IMAGE_SERVER_BASE_URL;

	@Override
	public PictureResult uploadPic(MultipartFile picFile) {
		System.out.println("PictureServiceImpl.uploadPic()=========================");
		PictureResult result=new PictureResult();
		//判断图片是否为空
		if(picFile.isEmpty()){
			System.out.println("picFile is empty==============================");
			result.setError(1);
			result.setMessage("there must be a picture at least");
			return result;
		}
		//上传图片到服务器
		try {
			String originalFilename = picFile.getOriginalFilename();
			String extName = originalFilename.substring(originalFilename.lastIndexOf(".")+1);
			FastDFSClient client=new FastDFSClient("classpath:properties/client.conf");
			String url=client.uploadFile(picFile.getBytes(),extName);
			url=IMAGE_SERVER_BASE_URL+url;
			System.out.println("url:"+url);
			result.setError(0);
			result.setUrl(url);
		}  catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			result.setError(1);
			result.setMessage("upload pictures failed");
		}
		
		return result;
	}

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值