FastDFS 是一个开源的高性能分布式文件系统(DFS)。 它的主要功能包括:文件存储,文件同步和文件访问,以及高容量和负载平衡。主要解决了海量数据存储问题,特别适合以中小文件(建议范围:4KB < file_size <500MB)为载体的在线服务。
FastDFS开源地址: https://hub.docker.com/r/season/fastdfs/
FastDFS 系统有三个角色:跟踪服务器(Tracker Server)、存储服务器(Storage Server)和客户端(Client)。
Storage Server:存储服务器,主要提供容量和备份服务;以 group 为单位,每个 group 内可以有多台 storage server,数据互为备份。
操作系统:CentOS7 X64
FastDFS: /season/fastdfs
下载
netstat -aon | grep 22122
systemctl stop firewalld
关闭centos 7新防火墙 如下图
find / -name selinux
vi /etc/sysconfig/selinux
docker run -ti -d --name trakcer -v ~/tracker_data:/fastdfs/tracker/data --net=host season/fastdfs tracker
docker run -tid --name storage -v ~/storage_data:/fastdfs/storage/data -v ~/store_path:/fastdfs/store_path --net=host -e TRACKER_SERVER:192.168.229.129:22122 season/fastdfs storage
docker exec -it storage bash
docker cp storage:/fdfs_conf/storage.conf ~/
vi storage.conf
docker cp ~/storage.conf storage:/fdfs_conf/
docker run -ti --name fdfs_sh --net=host season/fastdfs sh
docker cp ~/storage.conf fdfs_sh:/fdfs_conf/
fdfs_monitor storage.conf
[root@bogon fdfs]# ls /usr/bin | grep fdfs
fdfs_appender_test
fdfs_appender_test1
fdfs_append_file
fdfs_crc32
fdfs_delete_file
fdfs_download_file
fdfs_file_info
fdfs_monitor
fdfs_storaged
fdfs_test
fdfs_test1
fdfs_trackerd
fdfs_upload_appender
fdfs_upload_file
<repositories>
<repository>
<id>sn</id>
<name>sn</name>
<url>https://oss.sonatype.org/content/repositories/releases/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.luhuiguo</groupId>
<artifactId>fastdfs-spring-boot-starter</artifactId>
<version>0.2.0</version>
</dependency>
</dependencies>
server:
port: 8899
fdfs:
#连接世界
connect-timeout: 10000
#响应时间
so-timeout: 3000
tracker-list:
- 192.168.229.129:22122
spring:
datasource:
url: jdbc:mysql://localhost/accounts
password: ps123456
username: root
driver-class-name: com.mysql.jdbc.Driver
# 上传文件限制大小(字节)
http:
multipart:
max-file-size: 10485760
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form method="post" enctype="multipart/form-data" action="fupload">
文件<input type="file" name="myFile" />
<input type="submit" value="上传" />
</form>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<a href="/fdownload/1">下载</a>
</body>
</html>
@SpringBootApplication
public class FileMain {
public static void main(String[] args) {
SpringApplication.run(FileMain.class);
}
}
@RestController
public class Upload {
@Autowired
private FastFileStorageClient storageClient;
@Autowired
JdbcTemplate jdbcTemplate;
/** 获取后缀 :FilenameUtils.getExtension()
* 获取文件名:myFile.getOriginalFilename();
* @param myFile 从浏览器传过来
* @return
*/
//上传
@PostMapping("fupload")
public String upload(@RequestParam("myFile") MultipartFile myFile) throws IOException {
String extName= FilenameUtils.getExtension(myFile.getOriginalFilename());
StorePath storePath = storageClient.uploadFile("group1",myFile.getInputStream(),myFile.getSize(),extName);
String sql="insert into myfile(filename,groupName,filepath) values (?,?,?)";
jdbcTemplate.update(sql,myFile.getOriginalFilename(),storePath.getGroup(),storePath.getPath());
return storePath.getFullPath();
}
//下载
@GetMapping("/fdownload/{id}")
public void download(@PathVariable String id, HttpServletResponse httpServletResponse) throws IOException {
List query = jdbcTemplate.query("select * from myfile where fileid = "+id, new ColumnMapRowMapper());
Map map= (Map) query.get(0);
//解决中文乱码
String fileName=URLDecoder.decode(map.get("filename").toString(),"UTF-8");
String groupName=map.get("groupName").toString();
String filePath=map.get("filepath").toString();
//设置响应头,告诉浏览器下载的文件名
httpServletResponse.setHeader("Content-Disposition","attachment; filename="+fileName+"");
//将文件内容输出到浏览器
byte[] bytes = storageClient.downloadFile(groupName, filePath);
httpServletResponse.getOutputStream().write(bytes);
}
}
测试访问 http://localhost:8899/upload.html