什麼是分布式檔案系統
分布式檔案系統(Distributed File System)是指檔案系統管理的實體存儲資源不一定直接連接配接在本地節點上,而是通過計算機網絡與節點相連。
通俗來講:
- 傳統檔案系統管理的檔案就存儲在本機。
- 分布式檔案系統管理的檔案存儲在很多機器,這些機器通過網絡連接配接,要被統一管理。無論是上傳或者通路檔案,都需要通過管理中心來通路。
什麼是FastDFS
FastDFS是由淘寶的餘慶先生所開發的一個輕量級、高性能的開源分布式檔案系統。用純C語言開發,功能豐富:
- 檔案存儲
- 檔案同步
- 檔案通路(上傳、下載下傳)
- 存取負載均衡
- 線上擴容
适合有大容量存儲需求的應用或系統。同類的分布式檔案系統有谷歌的GFS、HDFS(Hadoop)、TFS(淘寶)等。
架構圖
FastDFS兩個主要的角色:Tracker Server 和 Storage Server 。
- Tracker Server:跟蹤伺服器,主要負責排程storage節點與client通信,在通路上起負載均衡的作用,和記錄storage節點的運作狀态,是連接配接client和storage節點的樞紐。
- Storage Server:存儲伺服器,儲存檔案和檔案的meta data(中繼資料),每個storage server會啟動一個單獨的線程主動向Tracker cluster中每個tracker server報告其狀态資訊,包括磁盤使用情況,檔案同步情況及檔案上傳下載下傳次數統計等資訊
- Group:檔案組,多台Storage Server的叢集。上傳一個檔案到同組内的一台機器上後,FastDFS會将該檔案即時同步到同組内的其它所有機器上,起到備份的作用。不同組的伺服器,儲存的資料不同,而且互相獨立,不進行通信。
- Tracker Cluster:跟蹤伺服器的叢集,有一組Tracker Server(跟蹤伺服器)組成。
- Storage Cluster :存儲叢集,有多個Group組成。
單節點FastDFS安裝
GCC用來對C語言代碼進行編譯運作,使用yum指令安裝:
yum -y install gcc
安裝unzip便于解壓
安裝libevent
yum -y install libevent
将所需要的壓縮包下載下傳到/usr/local/myfastdfs下
連結:https://pan.baidu.com/s/1tIbSyyjD3o6-stQZn_lXWA
提取碼:cltv
mkdir -p /usr/local/myfastdfs
cd /usr/local/myfastdfs
安裝libfastcommon
unzip libfastcommon-master.zip
cd libfastcommon-master
編譯并且安裝:
./make.sh
./make.sh install
安裝fastdfs
cd ..
tar -zxvf FastDFS_v5.08.tar.gz
cd FastDFS
./make.sh
./make.sh install
如果安裝成功,會看到/etc/init.d/下看到提供的腳本檔案:
ll /etc/init.d/ | grep fdfs
-
是tracker啟動腳本fdfs_trackerd
-
是storage啟動腳本fdfs_storaged
能夠在 /etc/fdfs/ 目錄下看到預設的配置檔案模闆:
ll /etc/fdfs/
-
是tracker的配置檔案模闆tarcker.conf.sample
-
是storage的配置檔案模闆storage.conf.sample
-
是用戶端的配置檔案模闆client.conf.sample
配置并啟動tracker服務
FastDFS的tracker和storage在剛剛的安裝過程中,都已經被安裝了,是以我們安裝這兩種角色的方式是一樣的。不同的是,兩種需要不同的配置檔案。
我們要啟動tracker,就修改剛剛看到的
tarcker.conf
,并且啟動
fdfs_trackerd
腳本即可。
1)首先将模闆檔案複制
cp /etc/fdfs/tracker.conf.sample /etc/fdfs/tracker.conf
2)修改複制後的配置檔案:
vim /etc/fdfs/tracker.conf
修改的内容如下:
修改為你的存儲日志和資料的根目錄
mkdir -p /myfastdfs/tracker
3)啟動和停止
檢查FastDFS Tracker Server是否啟動成功:
ps -ef | grep fdfs_trackerd
設定tracker服務開機啟動:
chkconfig fdfs_trackerd on
配置并啟動storage服務
1)首先将模闆檔案複制
cp /etc/fdfs/storage.conf.sample /etc/fdfs/storage.conf
2)修改複制後的配置檔案:
vim /etc/fdfs/storage.conf
修改的内容如下:
base_path=/myfastdfs/storage # 資料和日志檔案存儲根目錄
store_path0=/myfastdfs/storage # 第一個存儲目錄
tracker_server=192.168.130.128:22122 # tracker伺服器IP和端口
mkdir -p /myfastdfs/storage
3)啟動和停止
設定storage服務開機啟動:
chkconfig fdfs_storaged on
使用nginx通路FastDFS
為什麼需要用Nginx通路?
FastDFS通過Tracker伺服器,将檔案放在Storage伺服器存儲,但是同組存儲伺服器之間需要進入檔案複制,有同步延遲的問題。
假設Tracker伺服器将檔案上傳到了192.168.4.125,上傳成功後檔案ID已經傳回給用戶端。此時FastDFS存儲叢集機制會将這個檔案同步到同組存儲192.168.4.126,在檔案還沒有複制完成的情況下,用戶端如果用這個檔案ID在192.168.4.126上取檔案,就會出現檔案無法通路的錯誤。
而fastdfs-nginx-module可以重定向檔案連接配接到檔案上傳時的源伺服器取檔案,避免用戶端由于複制延遲導緻的檔案無法通路錯誤
安裝nginx和fastdfs-nginx-module子產品
上傳fastdfs-nginx-module安裝包
cd /usr/local/myfastdfs/
# 解壓fastdfs-nginx-module:
tar -zxvf fastdfs-nginx-module_v1.16.tar.gz
cd fastdfs-nginx-module/src/
#編輯config
vim config
使用以下底行指令:
:%s+/usr/local/+/usr/+g
配置nginx與FastDFS關聯配置檔案
複制 fastdfs-nginx-module 源碼中的配置檔案到/etc/fdfs 目錄, 并修改
cp /usr/local/myfastdfs/fastdfs-nginx-module/src/mod_fastdfs.conf /etc/fdfs/
vi /etc/fdfs/mod_fastdfs.conf
修改以下配置:
connect_timeout=10 # 用戶端通路檔案連接配接逾時時長(機關:秒)
tracker_server=192.168.130.128:22122 # tracker服務IP和端口
url_have_group_name=true # 通路連結字首加上組名
store_path0=/myfastdfs/storage # 檔案存儲路徑
複制 FastDFS 的部配置設定置檔案到/etc/fdfs 目錄
cd /usr/local/myfastdfs/FastDFS/conf/
cp http.conf mime.types /etc/fdfs/
安裝nginx的依賴庫
yum -y install gcc pcre pcre-devel zlib zlib-devel openssl openssl-devel
上傳nginx安裝包
cd /usr/local/myfastdfs/
#解壓nginx
tar -zxvf nginx-1.10.0.tar.gz
cd nginx-1.10.0
配置FastDFS 子產品
./configure --prefix=/opt/nginx --sbin-path=/usr/bin/nginx --add-module=/usr/local/myfastdfs/fastdfs-nginx-module/src
編譯并安裝
啟動nginx:
nginx # 啟動nginx
nginx -s stop # 停止nginx
nginx -s reload # 重新載入配置檔案
java用戶端測試
引入依賴
<?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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.sunyuqi</groupId>
<artifactId>fastdfs-test</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>com.github.tobato</groupId>
<artifactId>fastdfs-client</artifactId>
<version>1.26.1-RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
引入配置類
package com.sunyuqi.config;
import com.github.tobato.fastdfs.FdfsClientConfig;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableMBeanExport;
import org.springframework.context.annotation.Import;
import org.springframework.jmx.support.RegistrationPolicy;
@Configuration
@Import(FdfsClientConfig.class)
/**
* 解決jmx重複注冊bean的問題
*/
@EnableMBeanExport(registration = RegistrationPolicy.IGNORE_EXISTING)
public class FastClientImporter {
}
application.yml
fdfs:
so-timeout: 1501 # 逾時時間
connect-timeout: 601 # 連接配接逾時時間
thumb-image: # 縮略圖
width: 60
height: 60
tracker-list: # tracker位址:你的虛拟機伺服器位址+端口(預設是22122)
- 192.168.130.128:22122
引導類
package com.sunyuqi;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class UploadApplicatiton {
public static void main(String[] args) {
SpringApplication.run(UploadApplicatiton.class, args);
}
}
測試類
package test;
import com.github.tobato.fastdfs.domain.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import com.sunyuqi.UploadApplicatiton;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = UploadApplicatiton.class)
public class FdfsTest {
@Autowired
private FastFileStorageClient storageClient;
@Test
public void testUpload() throws FileNotFoundException {
File file = new File("C:\\Users\\Lenovo\\Desktop\\電腦桌面\\66-2005221A045500.jpg");
// 上傳
StorePath storePath = this.storageClient.uploadFile(
new FileInputStream(file), file.length(), "jpg", null);
// 帶分組的路徑
System.out.println(storePath.getFullPath());
// 不帶分組的路徑
System.out.println(storePath.getPath());
}
}
運作測試類結果
通路第一個路徑 即可通路到上傳的圖檔