FastDFS入门

FastDFS
参考资料:https://github.com/happyfish100/fastdfs
参考资料:https://www.oschina.net/p/fastdfs
一、简介
FastDFS高性能的分布式文件系统,主要功能包含文件的存储、同步、访问(上传和下载),特别适用于以文件为主体的网络站点(图片分享和视频分享)
FastDFS包含两个角色
Tracker(追踪器 调度服务器):调度和文件访问的负载均衡
Storage(存储器 存储服务器):文件的管理(存储、同步、访问接口)和文件的元数据
支持水平扩展,不会对在线服务造成任何影响
存储服务器通过 卷/ 组组织管理,不同的卷管理不同的文件,一个卷管理一台或者多台存储服务器,并且这些存储服务器之间互相备份
存储容量=所有卷容量之和
文件在集群中标示=卷名+文件名
架构图:
二、环境搭建(单机版)
1. 克隆虚拟器
vi /etc/sysconfig/network-scripts/ifcfg-eth0
fastdfs.md to fastdfs.pdf by  MARKDOWN-THEMEABLE-PDF
Page 2/5 © Copyright Thursday, May 10, 2018, 5:54 PM by COMPANYNAME
# 删除两行
UUID
HWADDR
2. 安装
yum install gcc-c++ perl-devel pcre-devel openssl-devel zlib-devel wget
wget https://github.com/happyfish100/fastdfs/archive/V5.11.tar.gz
wget https://github.com/happyfish100/libfastcommon/archive/V1.0.36.tar.gz
tar -zxvf V1.0.36.tar.gz
cd libfastcommon-1.0.36/
./make.sh && ./make.sh install
tar -zxvf V5.11.tar.gz
cd fastdfs-5.11/
./make.sh && ./make.sh install
3. 修改配置文件
cd /etc/fdfs
[root@baizhi fdfs]# cp tracker.conf.sample tracker.conf
[root@baizhi fdfs]# cp storage.conf.sample storage.conf
[root@baizhi fdfs]# cp client.conf.sample client.conf
vi tracker.conf
# 修改
base_path=/data/fastdfs/tracker
vi storage.conf
# 修改
base_path=/data/fastdfs/storage
store_path0=/data/fastdfs/storage/store
tracker_server=192.168.43.136:22122
# 创建目录
[root@baizhi fdfs]# mkdir -p /data/fastdfs/tracker
[root@baizhi fdfs]# mkdir -p /data/fastdfs/storage/store
vi client.conf
# 修改
base_path=/tmp
tracker_server=192.168.43.136:22122
4. 启动
# 启动tracker server
[root@baizhi fdfs]# fdfs_trackerd /etc/fdfs/tracker.conf start
# 再启动storage server
[root@baizhi fdfs]# fdfs_storaged /etc/fdfs/storage.conf start
5. 测试
# 监控指令
fdfs_monitor /etc/fdfs/client.conf
fastdfs.md to fastdfs.pdf by  MARKDOWN-THEMEABLE-PDF
Page 3/5 © Copyright Thursday, May 10, 2018, 5:54 PM by COMPANYNAME
# 上传文件
fdfs_upload_file /etc/fdfs/client.conf /root/1.png
# 下载文件
fdfs_download_file /etc/fdfs/client.conf group1/M00/00/00/wKgriFr0YmeAI_lcABr-dch7j3Q658.png /root/2.png
# 删除文件
三、JAVA API
1. 将fastdfs-client-java安装到Maven本地仓库
mvn install
2. 准备配置文件fdfs_client.conf
# 文件内容如下
tracker_server = 192.168.43.136:22122
3. 测试代码
import org.csource.common.MyException;
import org.csource.common.NameValuePair;
import org.csource.fastdfs.*;
import org.junit.Before;
import org.junit.Test;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
/**
* Created by Administrator on 2018/5/10.
*/
public class FastDFSJavaApiTest {
StorageClient client = null;
/**
*  单元测试方法之前执行
*
* @throws IOException
* @throws MyException
*/
@Before
public void before() throws IOException, MyException {
//  加载配置文件
ClientGlobal.init("fdfs_client.conf");
TrackerClient trackerClient = new TrackerClient();
TrackerServer trackerServer = trackerClient.getConnection();
//  通过 client 对象操作分布式文件系统
client = new StorageClient(trackerServer, null);
}
/**
*  测试文件上传
*/
@Test
public void testUpload() throws IOException, MyException {
//  参数一:本地文件路径 参数二 :  文件的扩展名 参数三:元数据列表
//String[] str = client.upload_file("C:\\Users\\Administrator\\Desktop\\1.png", "png", null);
String[] str = client.upload_file("C:\\Users\\Administrator\\Desktop\\1.png", "png",
new NameValuePair[]{new NameValuePair("width","1080"),new NameValuePair("author","zs")}
);
for (String s : str) {
System.out.println(s);
}
}
/**
*  文件下载
fastdfs.md to fastdfs.pdf by  MARKDOWN-THEMEABLE-PDF
Page 4/5 © Copyright Thursday, May 10, 2018, 5:54 PM by COMPANYNAME
* @throws IOException
* @throws MyException
*/
@Test
public void testDownload() throws IOException, MyException {
byte[] b = client.download_file("group1", "M00/00/00/wKgriFr0ebmAI1gBABr-dch7j3Q224.png");
FileOutputStream fileOutputStream = new FileOutputStream("e:\\aa.png");
fileOutputStream.write(b);
fileOutputStream.close();
}
/**
*  文件删除
* @throws IOException
* @throws MyException
*/
@Test
public void testDelete() throws IOException, MyException {
client.delete_file("group1", "M00/00/00/wKgriFr0ebmAI1gBABr-dch7j3Q224.png");
}
@Test
public void testGetFileInfo() throws IOException, MyException {
FileInfo fileInfo = client.get_file_info("group1", "M00/00/00/wKgriFr0e4qAB0ndABr-dch7j3Q248.png");
System.out.println(fileInfo.getCrc32() + " | " +fileInfo.getCreateTimestamp() + " | "+fileInfo.getFileSize());
}
@Test
public void testGetMetadata() throws IOException, MyException {
try {
NameValuePair[] nameValuePairs = client.get_metadata("group1", "M00/00/00/wKgriFr0e4qAB0ndABr-dch7j3Q248.png");
for (NameValuePair nameValuePair : nameValuePairs) {
System.out.println(nameValuePair.getName() +"|"+nameValuePair.getValue());
}
} catch (IOException e) {
e.printStackTrace();
} catch (MyException e) {
e.printStackTrace();
}
}
}
四、Spring Boot整合
1. 创建spring boot工程
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.8.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.7</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.github.tobato</groupId>
<artifactId>fastdfs-client</artifactId>
<version>1.26.2</version>
fastdfs.md to fastdfs.pdf by  MARKDOWN-THEMEABLE-PDF
Page 5/5 © Copyright Thursday, May 10, 2018, 5:54 PM by COMPANYNAME
</dependency>
</dependencies>
2. 配置入口类
@Import(FdfsClientConfig.class)
//  解决 jmx 重复注册 bean 的问题
@EnableMBeanExport(registration = RegistrationPolicy.IGNORE_EXISTING)
@SpringBootApplication
public class SpringbootFastdfsDemoApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootFastdfsDemoApplication.class, args);
}
}
3. 配置文件
# ===================================================================
#  分布式文件系统 FDFS 配置
# ===================================================================
fdfs:
so-timeout: 1501
connect-timeout: 601
thumb-image: # 缩略图生成参数
width: 150
height: 150
tracker-list: #TrackerList 参数 , 支持多个
- 192.168.43.136:22122
4. 测试代码
package com.baizhi;
import com.github.tobato.fastdfs.domain.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import com.github.tobato.fastdfs.service.GenerateStorageClient;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringbootFastdfsDemoApplicationTests {
@Autowired
private FastFileStorageClient storageClient;
@Test
public void contextLoads() throws FileNotFoundException {
File file = new File("E:\\aa.png");
FileInputStream inputStream = new FileInputStream(file);
StorePath storePath = storageClient.uploadFile(inputStream, file.length(), "png", null);
System.out.println(storePath.getGroup() + " | " + storePath.getPath());
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值