fast dfs--分布式文件服务器

概述

FastDFS是c语言编写的一款开源分布式文件服务器;阿里巴巴出品;

架构

  1. FastDFS架构包括Tracker Server 和storage server进行文件上传下载.通过Tracker Server进行调度,最终由storage server完成文件上传和下载;
  2. Tracker Server作用:负载均衡和调度(管理storage 群),在文件上传时根据一些策略找到storage server提供上传服务;也可以实现集群;报告机制:storage会定时向Tracker 报告状态;

文件上传流程

  1. storage定时向tracker上报状态信息
  2. 客户端上传请求到tracker
  3. tracker查询是否有可用信息
  4. 返回给客户端IP和端口
  5. 上传(访问storage)
  6. 生成file_id
  7. 将内容存储到磁盘
  8. 返回file_id(路径信息和文件名)
  9. 存储文件信息到数据库

file_id组成

  1. group1--组名:文件上传后所在storage组名称
  2. M00--虚拟磁盘路径
  3. 02/44--数据两级目录,用于存储文件的文件夹
  4. 文件名(服务器地址ip,时间戳,文件大小,随机数,文件拓展名密文)

文件下载流程

  1. storage定时向tracker上报状态信息
  2. 客户端下载请求到tracker
  3. tracker查询是否有可用信息
  4. 返回信息
  5. 访问storage
  6. 查找文件
  7. 返回文件

单机FastDFS安装

资料下载

链接:https://pan.baidu.com/s/1lrUq9Zp1GhJXKHIe7nfXIg  提取码:asfa 

参考资料的:《centos安装FastDFS.md》

测试main

package cn.bufanli;

import org.csource.fastdfs.*;

/**
 * @author BuShuangLi
 * @date 2019/3/18
 */
public class Test {
     public static void main(String[] args) throws Exception {
          //1.加载配置文件 改成你自己的
          ClientGlobal.init("D:\\fast_dfs_\\src\\main\\resources\\fdfs_client.conf");
          //2.构建管理者客户端
          TrackerClient client = new TrackerClient();
          //3.连接服务端管理者
          TrackerServer connection = client.getConnection();
          //4.声明存储服务端
          StorageServer storageServer=null;
          //5.获取存储服务器的客户端对象
          StorageClient storageClient =new StorageClient(connection,storageServer);
          //6.上传文件 改成你的地址upload_appender_file(local_filename 文件地址,file_ext_name 后缀,,meta_list 扩展信息尺寸分辨率等等属性)
          String[] jpgs = storageClient.upload_appender_file("D:\\壁纸\\1.jpg", "jpg", null);
          //jpgs 显示上传的结果 file_id
          for (String jpg : jpgs) {

               System.out.println(jpg);

          }
     }

}

将资料的.jar文件使用maven安装到本地仓库

pom.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>cn.bufanli</groupId>
    <artifactId>fast_dfs_</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>
    <!-- https://mvnrepository.com/artifact/org.csource/fastdfs-client-java -->
  <dependencies>
      <!-- https://mvnrepository.com/artifact/org.csource/fastdfs-client-java -->
      <dependency>
          <groupId>org.csource.fastdfs</groupId>
          <artifactId>fastdfs</artifactId>
          <version>1.2</version>
      </dependency>
  </dependencies>


</project>

配置文件 fdfs_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 需要指定 ip:22122
tracker_server=IP:PORT
#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

reserved_storage_space= 10%
#HTTP settings
http.tracker_server_port=80

#use "#include" directive to include HTTP other settiongs
##include http.conf

上传文件工具类

package cn.bufanli;

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);
	}
}

js

app.service("uploadService",function ($http) {
    //上传文件
    this.uploadFile=function () {
        var formdata = new FormData()
        formdata.append('file',file.files[0]);// file 文件上传框name
        console.log(formdata+"-----")
       return $http({
           url:'../upload',
           method:'POST',
           data:formdata, //上传的文件
           headers:{'Content-Type':undefined },//指定上传类型
           transformRequest: angular.identity
       })
    }

})

页面

<tr>
                             
    <td>图片</td>
    
    <td>
        <input type="file" id="file"/>
        <button ng-click="uploadFile">上传</button>
        <img src="{{entity.pic}}" width="200px" height="100px"/>
    </td>

</tr>	

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值