簡介
FastDFS 是一個開源的高性能分佈式文件系統(DFS)。 它的主要功能包括:文件存儲,文件同步和文件訪問,以及高容量和負載平衡。主要解決了海量數據存儲問題,特別適合以中小文件(建議範圍:4KB < file_size <500MB)爲載體的在線服務。
FastDFS開源地址: https://hub.docker.com/r/season/fastdfs/
FastDFS 系統有三個角色:跟蹤服務器(Tracker Server)、存儲服務器(Storage Server)和客戶端(Client)。
- Tracker Server:跟蹤服務器,主要做調度工作,起到均衡的作用;負責管理所有的 storage server和 group,每個 storage 在啓動後會連接 Tracker,告知自己所屬 group 等信息,並保持週期性心跳。
- 判斷有哪些存儲服務器(註冊中心)
- 判斷哪個存儲服務器的空間較大
- 判斷資源所在的存儲服務器(路由)
-
Storage Server:存儲服務器,主要提供容量和備份服務;以 group 爲單位,每個 group 內可以有多臺 storage server,數據互爲備份。
-
Client:客戶端,上傳下載數據的服務器,也就是我們自己的項目所部署在的服務器。
FastDFS環境
-
操作系統:CentOS7 X64
-
FastDFS: /season/fastdfs
-
下載
- docker pull season/fastdfs
- 查看fastdfs的端口是否被佔用
netstat -aon | grep 22122
- 關閉防火牆
systemctl stop firewalld
關閉centos 7新防火牆 如下圖
find / -name selinux
vi /etc/sysconfig/selinux
4. 執行reboot操作再重啓虛擬機重連客戶端
創建tracker服務器的容器
docker run -ti -d --name trakcer -v ~/tracker_data:/fastdfs/tracker/data --net=host season/fastdfs tracker
創建storage存儲服務器
- 指定tracker服務器的地址(通過環境變量)
- storage文件位置 /store_path
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
- 指定storage服務器註冊到trakcer服務器的ip地址未生效,需要手動設置
- 進入storage容器下,將fdfs_conf目錄下的storage.conf文件拷貝到root目錄下
docker exec -it storage bash
docker cp storage:/fdfs_conf/storage.conf ~/
- 編輯storage.conf文件,將地址指上
vi storage.conf
- 將編輯好的文件再拷貝回去
docker cp ~/storage.conf storage:/fdfs_conf/
- 啓動fastdfs客戶端
- 將storage服務器的配置文件拷貝到客戶端 fdfs_sh:/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
- 這時storage存儲服務器已註冊到trakcer服務器上,開啓8888端口,接下來我們就可以實現上傳下載文件
演示上傳
- FastDFS命令行
[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
- 上傳文件必須指定配置文件
測試上傳下載文件(IDEA篇)
- 依賴
- fastdfs這裏引用了私服倉庫上的jar包,需指上地址
<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>
- application.yml文件
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
-
數據庫新建myfile表
-
做一個簡單的html 上傳、下載
- HTML靜態文件放置在resources目錄下的static/public目錄
<!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>
- main方法
@SpringBootApplication
public class FileMain {
public static void main(String[] args) {
SpringApplication.run(FileMain.class);
}
}
- controller層
@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);
}
}