高可用高性能分布式文件系统FastDFS实践Java程序
在前篇 高可用高性能分布式文件系统FastDFS进阶keepalived+nginx对多tracker进行高可用热备 中已介绍搭建高可用的分布式文件系统架构。
那怎么在程序中调用,其实网上有很多栗子,这里在他们的基础上作个简单的介绍。
下载源码并加入本地仓库
官网Java客户端源代码:https://github.com/happyfish100/fastdfs-client-java
打开源码后 执行maven install 将代码打成jar到本地maven仓库(这步可自行 google)
示例源码
然后创建一个Demo工程,这里采用spring mvc模式创建。
在pom中加入引用 maven中依赖jar包
<!-- fastdfs上传下载图片 路径和上面的pom中对应 -->
<dependency>
<groupId>org.csource</groupId>
<artifactId>fastdfs-client-java</artifactId>
<version>1.27-SNAPSHOT</version>
</dependency>
fastdfs-client.properties
在resources的properties文件夹中创建配置文件
fastdfs-client.properties
fastdfs.connect_timeout_in_seconds =
fastdfs.network_timeout_in_seconds =
fastdfs.charset = UTF-
fastdfs.http_anti_steal_token = false
fastdfs.http_secret_key = FastDFS1234567890
fastdfs.http_tracker_http_port = fastdfs.tracker_servers = 10.0.11.201:,10.0.11.202:,10.0.11.203:
创建FastDFSClient工具类
package com.james.utils; import org.csource.common.MyException;
import org.csource.common.NameValuePair;
import org.csource.fastdfs.*; import java.io.BufferedOutputStream;
import java.io.IOException;
import java.net.URLDecoder; /**
* Created by James on 2015/11/14.
* FastDFS文件上传
*/
public class FastDFSClientUtils {
private TrackerClient trackerClient = null;
private TrackerServer trackerServer = null;
private StorageServer storageServer = null;
private StorageClient1 storageClient = null; public FastDFSClientUtils(String conf) throws Exception {
if (conf.contains("classpath:")) {
String path = this.getClass().getResource("/").getPath();
conf = conf.replace("classpath:", URLDecoder.decode(path, "UTF-8"));
}
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) {
String result = null;
try {
result = storageClient.upload_file1(fileName, extName, metas);
} catch (IOException e) {
e.printStackTrace();
} catch (MyException e) {
e.printStackTrace();
}
return result;
} /**
* 上传文件,传fileName
*
* @param fileName 文件的磁盘路径名称 如:D:/image/aaa.jpg
* @return null为失败
*/
public String uploadFile(String fileName) {
return uploadFile(fileName, null, null);
} /**
* @param fileName 文件的磁盘路径名称 如:D:/image/aaa.jpg
* @param extName 文件的扩展名 如 txt jpg等
* @return null为失败
*/
public String uploadFile(String fileName, String extName) {
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) {
String result = null;
try {
result = storageClient.upload_file1(fileContent, extName, metas);
} catch (IOException e) {
e.printStackTrace();
} catch (MyException e) {
e.printStackTrace();
}
return result;
} /**
* 上传文件
*
* @param fileContent 文件的字节数组
* @return null为失败
* @throws Exception
*/
public String uploadFile(byte[] fileContent) throws Exception {
return uploadFile(fileContent, null, null);
} /**
* 上传文件
*
* @param fileContent 文件的字节数组
* @param extName 文件的扩展名 如 txt jpg png 等
* @return null为失败
*/
public String uploadFile(byte[] fileContent, String extName) {
return uploadFile(fileContent, extName, null);
} /**
* 文件下载到磁盘
*
* @param path 图片路径
* @param output 输出流 中包含要输出到磁盘的路径
* @return -1失败,0成功
*/
public int download_file(String path, BufferedOutputStream output) {
//byte[] b = storageClient.download_file(group, path);
int result = -1;
try {
byte[] b = storageClient.download_file1(path);
try {
if (b != null) {
output.write(b);
result = 0;
}
} catch (Exception e) {
} //用户可能取消了下载
finally {
if (output != null)
try {
output.close();
} catch (IOException e) {
e.printStackTrace();
}
}
} catch (Exception e) {
e.printStackTrace();
}
return result;
} /**
* 获取文件数组
*
* @param path 文件的路径 如group1/M00/00/00/wKgRsVjtwpSAXGwkAAAweEAzRjw471.jpg
* @return
*/
public byte[] download_bytes(String path) {
byte[] b = null;
try {
b = storageClient.download_file1(path);
} catch (IOException e) {
e.printStackTrace();
} catch (MyException e) {
e.printStackTrace();
}
return b;
} /**
* 删除文件
*
* @param group 组名 如:group1
* @param storagePath 不带组名的路径名称 如:M00/00/00/wKgRsVjtwpSAXGwkAAAweEAzRjw471.jpg
* @return -1失败,0成功
*/
public Integer delete_file(String group, String storagePath) {
int result = -1;
try {
result = storageClient.delete_file(group, storagePath);
} catch (IOException e) {
e.printStackTrace();
} catch (MyException e) {
e.printStackTrace();
}
return result;
} /**
* @param storagePath 文件的全部路径 如:group1/M00/00/00/wKgRsVjtwpSAXGwkAAAweEAzRjw471.jpg
* @return -1失败,0成功
* @throws IOException
* @throws Exception
*/
public Integer delete_file(String storagePath) {
int result = -1;
try {
result = storageClient.delete_file1(storagePath);
} catch (IOException e) {
e.printStackTrace();
} catch (MyException e) {
e.printStackTrace();
}
return result;
}
}
Java客户端文件上传、下载、删除和元数据获取测试:
package com.james.fdfs;
import org.junit.Test;
import java.io.File;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import com.james.utils.FastDFSClientUtils; public class FastDFSClientUtilsTest { /**
* 文件上传测试
*/
@Test
public void testUpload() {
File file = new File("C:\\Users\\James\\Pic\\share.jpg");
Map<String,String> metaList = new HashMap<String, String>();
metaList.put("width","1024");
metaList.put("height","768");
String fid = FastDFSClientUtils.uploadFile(file,file.getName(),metaList);
System.out.println("upload local file " + file.getPath() + " ok, fileid=" + fid);
//上传成功返回的文件ID: group1/M00/00/00/wKgAyVgFk9aAB8hwAA-8Q6_7tHw351.jpg
} /**
* 文件下载测试
*/
@Test
public void testDownload() {
int r = FastDFSClientUtils.download_file("group1/M00/00/00/wKgAyVgFk9aAB8hwAA-8Q6_7tHw351.jpg", new File("DownloadFile_fid.jpg"));
System.out.println(r == 0 ? "下载成功" : "下载失败");
} /**
* 文件删除测试
*/
@Test
public void testDelete() {
int r = FastDFSClientUtils.delete_file("group1/M00/00/00/wKgAyVgFk9aAB8hwAA-8Q6_7tHw351.jpg");
System.out.println(r == 0 ? "删除成功" : "删除失败");
}
}
如果没有什么问题将会看到打印的日志。
Net版本
net版本可参考另外一位网友代码:
https://github.com/huanzui/fastdfs.client.net
问题
现在分布式文件平台已经完成了搭建和代码测试,但实践过程中还是有几个问题:
1、上传到平台的文件名都是无规律的64base编码过的字符串,因此如果只作为如图片等文件存储是没有问题的,因为我们不关心其文件名,但如果作为要下载的内容,如附件,或安装包,下载时如果还是编码那无法直观的知道此文件是做什么的,是要转换为正确的文件名。
解决:关于这个问题,网上有方法是通过nginx,利用域名和FID拼出url,然后在url后面增加一个参数,指定原始文件名。
例如:http://121.14.161.48:9030/group2/M00/00/89/eQ6h3FKJf_PRl8p4AUz4wO8tqaA688.apk?attname=filename.apk
在Nginx上进行如下配置,这样Nginx就会截获url中的参数attname,在Http响应头里面加上字段 Content-Disposition “attachment;filename=$arg_attname”。
这里只提供了一个方案,具体内容其实还需要一个篇幅来介绍,有时间再写吧。
2、实际用的时候我们其实是想按业务还将不同文件放在不同的文件夹中的,比如聊天文件,文档文件,还有临时文件 有时需要定时清理的,但分布式文件平台是没法指定文件夹的。
解决:最常用的做法是自己实现一个文件对应库,将上传的文件名,时间,对应的业务等信息与最终的文件路径对应起来,这样就可以作任何逻辑了,但缺点是非常麻烦。
FastDFS没有看到保存到指定的2级目录的API,但可以保存到指定的group,可以指定某个group为哪个业务用。但这样会破坏整个FastDFS的分布式结构,造成某个group非常巨大,而且不容易扩容。实际使用时还会有其它业务的内容进入到此group。
3、大文件如何断点续传?
如果文件大于100M,则需要断点续传的功能了,FastDFS对于大文件来说是有点吃力的,但还是可以实现,根据网友提供的方案来看就是需要客户进行切片上传,并且切片字节大小小于等于storage配置的buff_size,默认是256k,这一块需要自己实现。
高可用高性能分布式文件系统FastDFS实践Java程序的更多相关文章
- 高可用高性能分布式文件系统FastDFS进阶keepalived+nginx对多tracker进行高可用热备
在上一篇 分布式文件系统FastDFS如何做到高可用 中已经介绍了FastDFS的原理和怎么搭建一个简单的高可用的分布式文件系统及怎么访问. 高可用是实现了,但由于我们只设置了一个group,如果现在 ...
- 【FastDFS】如何打造一款高可用的分布式文件系统?这次我明白了!!
写在前面 前面我们学习了如何基于两台服务器搭建FastDFS环境,而往往在生产环境中,需要FastDFS做到高可用,那如何基于FastDFS打造一款高可用的分布式文件系统呢?别急,今天,我们就一起来基 ...
- 使用FastDFS打造一款高可用的分布式文件系统
FastDFS 介绍 参考: http://www.oschina.net/p/fastdfs FastDFS 是一个开源的分布式文件系统,它对文件进行管理,功能包括:文件存储.文件同步.文件访问(文 ...
- 分布式文件系统FastDFS动态扩容
当用户量越来越大,则集群中某个group总会到达其极限,这时就得扩展集群的容量了. FastDFS的扩容分为对group纵向扩容和横向扩容 纵向扩容 指在同一个group组中增加服务器,实现数据冗余, ...
- FastDFS是使用c语言编写的开源高性能分布式文件系统
FastDFS是什么 FastDFS是使用c语言编写的开源高性能分布式文件系统 是由淘宝开发平台部资深架构师余庆开发,FastDFS孵化平台板块 他对文件进行管理,功能包括文件存储,文件同步,文件访问 ...
- [转]分布式文件系统FastDFS架构剖析
[转]分布式文件系统FastDFS架构剖析 http://www.programmer.com.cn/4380/ 文/余庆 FastDFS是一款类Google FS的开源分布式文件系统,它用纯C语言实 ...
- 【转】分布式文件系统FastDFS架构剖析
FastDFS是一款类Google FS的开源分布式文件系统,它用纯C语言实现,支持Linux.FreeBSD.AIX等UNIX系统.它只能通过专有API对文件进行存取访问,不支持POSIX接口方式, ...
- 分布式文件系统FastDFS详解
上一篇文章<一次FastDFS并发问题的排查经历>介绍了一次生产排查并发问题的经历,可能有些人对FastDFS不是特别的了解,因此计划写几篇文章完整的介绍一下这个软件. 为什么要使用分布式 ...
- (转)淘淘商城系列——分布式文件系统FastDFS
http://blog.csdn.net/yerenyuan_pku/article/details/72801777 商品添加的实现,包括商品的类目选择,即商品属于哪个分类?还包括图片上传,对于图片 ...
随机推荐
- LODOP打印控件示例
一.lodop打印预览效果图 LODOP.PRINT_SETUP();打印维护效果图 LODOP.PREVIEW();打印预览图 二.写在前面 最近项目用到了LODOP的套打,主要用到两个地方,一是物 ...
- Collection源码图
java基础是否扎实,在于多读源码,比如集合 IO Socket 多线程并发包等 最近将集合框架的源码读了以下,总结了一些,下图所示
- 移动端IOS第三方输入法遮挡底部Input及android键盘回落留白问题
var interval; //消息框获取焦点 $('#J_text').focus(function(){ interval = setInterval(function() { scrollToE ...
- JavaScript的DOM编程--09--节点的替换
节点的替换: 1). replaceChild(): 把一个给定父元素里的一个子节点替换为另外一个子节点 var reference = element.replaceChild(newChild,o ...
- e.target和this的区别
```e.target与this的区别 event.target表示发生点击事件的元素this表示注册点击事件的元素 this 等于 e.currentTarget 指的是现在的目标this是所有原生 ...
- esp8266 SDK开发之GPIO中断
先秀一下自己焊的板子,黑的开关用于复位,蓝的开关用于烧录程序. 首先要明确的是esp8622的大多数管脚都有多个功能, 比如可以用来当做GPIO管脚,还可以用来当做SPI管脚. 如下图所示 使用PIN ...
- 如何实现虚拟机(VirtualBox)中的Ubuntu与Windows XP间的数据共享
环境: 主机是Windows XP系统 虚拟机与Ubuntu的版本分别为: VirtualBox-3.2.12-68302-Win ubuntu-10.10-desktop-i386 前提:已安装Vi ...
- Linux入门篇(四)——Vim的使用与Bash
这一系列的Linux入门都是本人在<鸟哥的Linux私房菜>的基础上总结的基本内容,主要是记录下自己的学习过程,也方便大家简要的了解 Linux Distribution是Ubuntu而不 ...
- JS大小写字母转换
var a = "ABCd"; console.log(a.toLowerCase());//转换成小写 console.log(a.toUpperCase());//转换成大写
- angular4.0快速import依赖路径
快捷键:ALT + ENTER 直接import对应的依赖路径