职位上的调动导致自己很长一段时间没有做总结了,忙里偷闲来说点什么吧
关于这个做总结的事情,想了很久还是选择抛弃一些,毕竟所有的都记录下来的话所花费的时间实在太长了,所以之后会选择一些技术点或者感觉值得写下来的
先来说一下这个fastDFS分布式文件系统吧
什么是fastDFS
- fastDFS是一个底层使用C语言编写并且开源的分布式文件系统
作用
- 可以统一管理存储服务器集群,统一管理存储读取文件
fastDFS工作流程(原理)
- fastDFS一共分为三部分,client客户端(项目),trackerServer管理端,storageServer存储端
- client客户端(项目):使用fastDFS的jar包中的api来存储文件,调用fastDFS
- tarckerServer管理端:管理storageServer存储端集群,管理端如果死掉存储段无法使用,所有管理端都是做双击热备,就是一台主机,一台备机,他们之间做心跳检测机制,平时主机工作,备机向主机发送ping命令,主机接收到后返回pong命令,如果一段时间内没有返回,备机会认为主机死掉会代替主机工作,这样可以保证管理端的高可用,管理端还有负载均衡的功能,可以平均分配请求给存储端,这样就可以承载高并发的存储需求
- storageServer存储端:他负责具体存储文件,也是两台为一组,一台主机一台备机,之间双机热备,做心跳检测,保证存储端的高可用,存储端做冗余备份,就是我们向存储端主机存储内容,主机会向备机发送内容,主机和备机存储的内容是一样的,所以主机即使坏掉,备机也有同样的内容可以使用,这种叫做容灾配置,存储端理论上存储容量是无限的,因为两台为一组机器可以无限扩展
- 流程:我们项目存储的时候先调用管理端,管理端会给我们分配一台存储端的IP地址和端口,我们向存储端机器存储文件,存储端会给我们返回存储后的地址以及文件名,文件名会自动被存储端重命名,防止文件重名
优缺点:
优点:
- 管理端有负载均衡的功能,可以承载高并发的存储需求
- 存储端可以无限扩展,理论上存储容量是无限的,扩展性好
- 存储端和管理端都使用的心跳检测机制,保证了服务器的高可用
- 存储端做了冗余存储,所以即使有机器坏掉也不会丢数据,容灾性好
缺点:
- fastDFS结构复杂,会使项目中的复杂度变高,并且搭建fastDFS会使用很多服务器
这就开始说项目中的运用吧
添加fastDFS的依赖
<dependency>
<groupId>org.csource.fastdfs</groupId>
<artifactId>fastdfs</artifactId>
<version>1.2</version>
</dependency>
由于fastDFS客户端的jar包并没有在中央仓库中,所以需要手动下载jar包到本地仓库中
mvn install:install-file -DgroupId=org.csource.fastdfs -DartifactId=fastdfs -Dversion=1.2 -Dpackaging=jar -Dfile=d:\setup\fastdfs_client_v1.20.jar
配置fastDFS官方提供的配置文件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=192.168.200.128:22122
#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
#HTTP settings
http.tracker_server_port=80
#use "#include" directive to include HTTP other settiongs
##include http.conf
这里我们需要更改管理端的IP地址和端口,存储端是靠管理端来管理的,所以我们配置管理端就可以了
tracker_server=192.168.200.128:22122
如果是集群的话可以多配置几个
先来做一个文件存储的Demo
package com.ayyy.core.text;
import org.csource.common.NameValuePair;
import org.csource.fastdfs.ClientGlobal;
import org.csource.fastdfs.StorageClient1;
import org.csource.fastdfs.TrackerClient;
import org.csource.fastdfs.TrackerServer;
public class TestFastDFS {
public static void main(String[] args) throws Exception {
//1. 加载配置文件
ClientGlobal.init("D:\\intelijIdeaWorkSpace3\\fastDFSDemo\\src\\main\\resources\\fdfs_client.conf");
//2. 创建管理端对象
TrackerClient trackerClient = new TrackerClient();
//3. 通过管理端对象获取连接
TrackerServer connection = trackerClient.getConnection();
//4. 创建存储端对象
StorageClient1 storageClient = new StorageClient1(connection, null);
//创建文件属性信息对象数组
NameValuePair[] meta_list = new NameValuePair[3];
meta_list[0] = new NameValuePair("fileName","kaola");
meta_list[1] = new NameValuePair("ExtName","jpg");
meta_list[2] = new NameValuePair("zuozhe","zj");
//5. 上传文件
String path = storageClient.upload_file1("E:\\image\\Koala.jpg", "jpg", meta_list);
System.out.println("======" + path);
}
}
Demo应该很容易就能看懂
那就来运用到项目中
创建一个自定义工具类,封装了频繁创建客户端和存储端的步骤,提供上传方法
package com.ayy.core.util;
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(byte[] bytes, long size, 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);
}
}
创建配置文件application.properties,这里配置的是服务器IP地址
FILE_SERVER_URL=http://192.168.200.128/
配置springMVC的多媒体解析器
<!-- 配置多媒体解析器 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="UTF-8"></property>
<!-- 设定文件上传的最大值5MB,5*1024*1024 -->
<property name="maxUploadSize" value="5242880"></property>
</bean>
控制层
package com.ayy.core.controller;
import com.ayy.core.pojo.entity.Result;
import com.ayy.core.util.FastDFSClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
@RestController
@RequestMapping("/upload")
public class UploadController {
@Value("${FILE_SERVER_URL}")
private String FILE_SERVER;
@RequestMapping("/uploadFile")
public Result uploadFile(MultipartFile file) throws Exception{
try {
FastDFSClient fastDFSClient = new FastDFSClient("classpath:fastDFS/fdfs_client.conf");
String path= fastDFSClient.uploadFile(file.getBytes(), file.getSize(), file.getOriginalFilename());
return new Result(true,FILE_SERVER+path);
} catch (Exception e) {
e.printStackTrace();
return new Result(false,"上传失败");
}
}
}
加载application.properties配置文件,通过@Value注解赋值给变量FILE_SERVER
@Value("${FILE_SERVER_URL}") private String FILE_SERVER;
MulttipartFile是springMVC专门用来接收上传文件的,MulttipartFile默认是不开启的,必须要有多媒体解析器,上面已经配置过了
public Result uploadFile(MultipartFile file)
创建自定义工具类,加载配置文件
FastDFSClient fastDFSClient = new FastDFSClient("classpath:fastDFS/fdfs_client.conf");
调用工具类的文件上传方法,(文件类型,文件大小,文件名称)
String path= fastDFSClient.uploadFile(file.getBytes(), file.getSize(), file.getOriginalFilename());
上传文件,返回文件保存的路径和文件名
return new Result(true,FILE_SERVER+path);