JavaEE进阶——FastDFS实现分布式文件系统

FastDFS

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

FastDFS服务端有两个角色:跟踪器(tracker)和存储节点(storage)。跟踪器主要做调度工作,在访问上起负载均衡的作用。 
存储节点存储文件,完成文件管理的所有功能:就是这样的存储、同步和提供存取接口,FastDFS同时对文件的metadata进行管理。所谓文件的meta data就是文件的相关属性,以键值对(key value)方式表示,如:width=1024,其中的key为width,value为1024。文件metadata是文件属性列表,可以包含多个键值对。 
跟踪器和存储节点都可以由一台或多台服务器构成。跟踪器和存储节点中的服务器均可以随时增加或下线而不会影响线上服务。其中跟踪器中的所有服务器都是对等的,可以根据服务器的压力情况随时增加或减少。 
为了支持大容量,存储节点(服务器)采用了分卷(或分组)的组织方式。存储系统由一个或多个卷组成,卷与卷之间的文件是相互独立的,所有卷的文件容量累加就是整个存储系统中的文件容量。一个卷可以由一台或多台存储服务器组成,一个卷下的存储服务器中的文件都是相同的,卷中的多台存储服务器起到了冗余备份和负载均衡的作用。 
在卷中增加服务器时,同步已有的文件由系统自动完成,同步完成后,系统自动将新增服务器切换到线上提供服务。 
当存储空间不足或即将耗尽时,可以动态添加卷。只需要增加一台或多台服务器,并将它们配置为一个新的卷,这样就扩大了存储系统的容量。 
FastDFS中的文件标识分为两个部分:卷名和文件名,二者缺一不可。

上传交互过程

  1. client询问tracker上传到的storage,不需要附加参数;
  2. tracker返回一台可用的storage;
  3. client直接和storage通讯完成文件上传。

下载交互过程

搭建环境文件下载列表

  1. FastDFS_v5.05.tar.gz
  2. nginx-1.14.0.tar.gz
  3. fastdfs-nginx-module_v1.16.tar.gz
  4. fastdfs_client_v1.20.jar
  5. libfastcommonV1.0.7.tar.gz

搭建FastDFS环境

1、上传所有下载的压缩源码包

选择CentOS6.4系统,具体安装见JavaEE进阶——CentOS开发环境搭建

2、安装FastDFS之前,先安装libevent工具包

yum -y install libevent
  • 1

3、安装libfastcommon工具包。

tar -zxvf libfastcommonV1.0.7.tar.gz
cd libfastcommon1.0.7 
./make.sh
./make.sh install
cp /usr/lib64/libfastcommon.so /usr/lib/
ll |grep libfast* # 查看lib下是否存在libfastcommon.so
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

4、安装Tracker服务

tar -zxvf FastDFS_v5.05.tar.gz
cd FastDFS
./make.sh
./make.sh install  
cd /usr/bin/
ll fdfs_*  # 查看编译结果是否产生fdfs相关文件
cp FastDFS/conf/* /etc/fdfs/  # 拷贝配置文件到/etc/fdfs/目录下
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

修改配置文件:

vim /etc/fdfs/tracker.conf
  • 1

启动tracker:

/usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf # 启动Tracker服务
/usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf restart # 重启Tracker服务
  • 1
  • 2

5、安装Storage服务

如果是在不同的服务器安装,需要在新的机器编译FastDFS_v5.05.tar.gz源码包,不需要配置Tracker服务而已。 
修改配置文件:

vim /etc/fdfs/storage.conf
  • 1




启动storage服务:

扫描二维码关注公众号,回复: 1614364 查看本文章
/usr/bin/fdfs_storaged /etc/fdfs/storage.conf # 启动Storage服务
/usr/bin/fdfs_storaged /etc/fdfs/storage.conf restart # 重启Storage服务
  • 1
  • 2

测试服务

1、修改配置文件/etc/fdfs/client.conf


2、启动测试

/usr/bin/fdfs_test /etc/fdfs/client.conf upload anti-steal.jpg
  • 1

搭建nginx提供http服务

  1. 解压插件压缩包fastdfs-nginx-module_v1.16.tar.gz
  2. 修改/root/fastdfs-nginx-module/src/config文件,把其中的local去掉。 
  3. 对nginx重新config,添加Fastdfs-nginx-module:

    mkdir -p /var/temp/nginx
    
    ./configure \
    --prefix=/usr/local/nginx \
    --pid-path=/var/run/nginx/nginx.pid \
    --lock-path=/var/lock/nginx.lock \
    --error-log-path=/var/log/nginx/error.log \
    --http-log-path=/var/log/nginx/access.log \
    --with-http_gzip_static_module www.cnzhaotai.com \
    --http-client-body-temp-path=/var/temp/nginx/client \
    --http-proxy-temp-path=/var/temp/nginx/proxy \
    --http-fastcgi-temp-path=/var/temp/nginx/fastcgi \
    --http-uwsgi-temp-path=/var/temp/nginx/uwsgi \
    --http-scgi-temp-path=/var/temp/nginx/scgi \
    --add-module=/root/fastdfs-nginx-module/src # 指定自己的路径
    
    make
    make install
  4. 1把/root/fastdfs-nginx-module/src/mod_fastdfs.conf文件复制到/etc/fdfs目录下,进行编辑: 



  5. nginx的配置

    server {
            listen       80;
            server_name  192.168.74.129;
    
            location /group1/M00/{
  6. 将libfdfsclient.so拷贝至/usr/lib下:

    cp /usr/lib64/libfdfsclient.so www.mcyllpt.com /usr/lib/
    • 1
  7. 启动nginx。 

测试http服务是否成功

  1. 上传图片:

    /usr/bin/fdfs_test /etc/fdfs/client.conf upload www.078881.cn /root/anti-steal.jpg
    • 1

    2018-06-16-14-18-13.jpg

  2. 访问命令行输出网址:http://192.168.74.129/group1/M00/00/00/wKhKgVslH5GAKcLdAAB1_3EXRGc833_big.png 
  3. 如果不行,检查22122和23000端口防火墙是否关闭,或者临时关闭防火墙:

    service iptables stop  # 临时关闭防火墙
    • 1

Java使用FastDFS

官方提供一个jar包:fastdfs_client_v1.20.jar。如果使用maven管理,可以添加:

<!-- https://mvnrepository.com/artifact/net.oschina.zcx7878/fastdfs-client-java -->
<dependency>
    <groupId>net.oschina.zcx7878</groupId> <artifactId>fastdfs-client-java</artifactId> <version>1.27.0.0</version> </dependency>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

使用方法: 
1. 把FastDFS提供的jar包添加到工程中 
2. 初始化全局配置。加载一个配置文件。 
3. 创建一个TrackerClient对象。 
4. 通过TrackerClient获得一个TrackerServer对象。 
5. 声明一个StorageServer对象,null。 
6. 通过TrackerServer对象和StorageServer对象获得一个StorageClient对象。 
7. 直接调用StorageClient对象方法上传文件即可。 
创建配置文件client.conf:

tracker_server=192.168.74.129:22122
  • 1

测试Java代码:

public class FastdfsTest {

    @Test public void testUpload() throws IOException, MyException { ClientGlobal.init("E:\\Intelljidea\\taotao\\taotao-manager\\taotao-manager-web\\src\\main\\resources\\properties\\client.conf"); TrackerClient trackerClient = new TrackerClient(www.leyouzaixian2.com); TrackerServer trackerServer = trackerClient.getConnection(); StorageServer storageServer = null; StorageClient storageClient = new StorageClient(trackerServer, storageServer); String[] strings = storageClient.upload_file("C:\\Users\\os\\Pictures\\十分妹子.jpg", "jpg", null); for (String string : strings) { System.out.println(string); 

测试结果: 
 

FastDFS工具类:FastDFSClient.java

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(www.leyouzaixian2.com 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); }

猜你喜欢

转载自www.cnblogs.com/qwangxiao/p/9192513.html