第五十二章:FastDFS分散式檔案系統
阿新 • • 發佈:2018-12-27
大綱
FastDFS架構包括Tracker server 和 Storage server 。客戶端請求Tracker server 進行檔案上傳、下載,通過Ttacker server 排程,最總由Storage server 完成檔案上傳和下載。
Tracher server 作用是負載均衡和排程,通過Tracker server 在檔案上傳時可以根據一些策略找到Storage server 提供的檔案上傳服務。可以將Tracker稱為追蹤伺服器或排程伺服器。
Storage server 的作用是檔案儲存,,客戶端上傳的檔案最終儲存在Storage伺服器上,不過Storage server 沒有實現自己的檔案系統,而是利用作業系統的檔案系統來管理檔案。可以將Storage稱為儲存伺服器。
6.在Nginx中安裝FastDFS模組
1.上傳fastdfs-nginx-module_v1.16.tar.gz到/usr/local目錄
2.解壓,進入fastdfs-nginx-module/src目錄
3.修改config檔案:把/usr/local改成/usr(共3處)
4.cp mod_fastdfs.conf /etc/fdfs/
5.修改/etc/fdfs/mod_fastdfs.conf內容
vim /etc/fdfs/mod_fastdfs.conf
base_path=/home/FastDFS
tracker_server=192.168.70.130:22122
# tracker_server=192.168.70.133:22122(多個tracker配置多行)
url_have_group_name=true #URL中包含group名稱
store_path0=/home/FastDFS/fdfs_storage #指定檔案儲存路徑
6.在Nginx的解壓目錄下重新生成MakeFile
./configure \
--prefix=/usr/local/nginx \
--pid-path=/var/run/nginx/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--with-http_gzip_static_module \
--http-client-body-temp-path=/var/temp/nginx/client \
--http-proxy-temp-path=/var/temp/nginx/proxy \
--http-fastcgi-temp-path=/var/temp/nginx/fastcgi \
--http-uwsgi-temp-path=/var/temp/nginx/uwsgi \
--http-scgi-temp-path=/var/temp/nginx/scgi \
--add-module=/usr/local/fastdfs-nginx-module/src
7.重新編譯,還是執行make命令
8.用objs目錄下的nginx檔案替換/usr/local/nginx/sbin目錄下的nginx檔案
保險起見可以把被替換的/usr/local/nginx/sbin/nginx程式檔案備份一下
如果看到Nginx檔案忙無法替換的提示,則停止Nginx程式
/usr/local/nginx/sbin/nginx -s quit
9.在Nginx中加入如下配置
server {
listen 80;
server_name 192.168.70.130;
location /group1/M00/{
ngx_fastdfs_module;
}
}
10.啟動Nginx
[ [email protected] conf]# /usr/local/nginx/sbin/nginx
ngx_http_fastdfs_set pid=10013
11.瀏覽器訪問
#PRG=/usr/local/bin/fdfs_trackerd PRG=/usr/bin/fdfs_trackerd #if [ ! -f /usr/local/bin/stop.sh ]; then # echo "file /usr/local/bin/stop.sh does not exist!" # exit 2 #fi if [ ! -f /usr/local/fdfs/stop.sh ]; then echo "file /usr/local/fdfs/stop.sh does not exist!" exit 2 fi #if [ ! -f /usr/local/bin/restart.sh ]; then # echo "file /usr/local/bin/restart.sh does not exist!" # exit 2 #fi if [ ! -f /usr/local/fdfs/restart.sh ]; then echo "file /usr/local/fdfs/restart.sh does not exist!" exit 2 fi #stop() { # /usr/local/bin/stop.sh $CMD # RETVAL=$? # return $RETVAL #} stop() { /usr/local/fdfs/stop.sh $CMD RETVAL=$? return $RETVAL } #restart() { # /usr/local/bin/restart.sh $CMD & #} restart() { /usr/local/fdfs/restart.sh $CMD & }
3.修改/etc/init.d/fdfs_storaged
#PRG=/usr/local/bin/fdfs_storaged
PRG=/usr/bin/fdfs_storaged
#if [ ! -f /usr/local/bin/stop.sh ]; then
# echo "file /usr/local/bin/stop.sh does not exist!"
# exit 2
#fi
if [ ! -f /usr/local/fdfs/stop.sh ]; then
echo "file /usr/local/fdfs/stop.sh does not exist!"
exit 2
fi
#if [ ! -f /usr/local/bin/restart.sh ]; then
# echo "file /usr/local/bin/restart.sh does not exist!"
# exit 2
#fi
if [ ! -f /usr/local/fdfs/restart.sh ]; then
echo "file /usr/local/fdfs/restart.sh does not exist!"
exit 2
fi
#stop() {
# /usr/local/bin/stop.sh $CMD
# RETVAL=$?
# return $RETVAL
#}
stop() {
/usr/local/fdfs/stop.sh $CMD
RETVAL=$?
return $RETVAL
}
#restart() {
# /usr/local/bin/restart.sh $CMD &
#}
restart() {
/usr/local/fdfs/restart.sh $CMD &
}
4.新增服務
chkconfig --add fdfs_trackerd
chkconfig --add fdfs_storaged
## 5.設定Nginx開機自動啟動
mkdir /usr/local/nginx/logs
vim /usr/local/nginx/conf/nginx.conf
#pid logs/nginx.pid;
pid /usr/local/nginx/logs/nginx.pid;
vim /lib/systemd/system/nginx.service
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true
[Install]
WantedBy=multi-user.target
systemctl enable nginx.service
7.示例程式碼
//1.宣告一個字串變數,用來儲存tracker.conf配置檔案的路徑
//參照基準為類路徑根目錄
String configPath = "/tracker.conf";
//2.動態獲取配置檔案的絕對路徑
String absolutePath = FastDFSClientTest.class.getResource(configPath).getPath();
//3.呼叫ClientGlobal類的靜態方法執行初始化操作
ClientGlobal.init(absolutePath);
//4.建立TrackerClient物件
TrackerClient trackerClient = new TrackerClient();
//5.建立TrackerServer物件
TrackerServer trackerServer = trackerClient.getConnection();
//6.建立StorageServer物件,可以為null
StorageServer storageServer = null;
//7.建立StorageClient物件
StorageClient storageClient = new StorageClient(trackerServer, storageServer);
//8.上傳檔案
String[] resultArray = storageClient.upload_file("C:/Users/Lenovo/Desktop/girls/bbb.jpg", "jpg", null);
for (String result : resultArray) {
System.out.println(result);
}
八、SpringMVC環境下實現表單上傳的檔案上傳到FastDFS
1.依賴
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.6.8</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.10.RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jsp-api</artifactId>
<version>2.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>org.csource</groupId>
<artifactId>fastdfs-client-java</artifactId>
<version>1.27-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
2.準備tracker.conf
tracker_server=192.168.70.200:22122
3.準備SpringMVC環境
<mvc:annotation-driven/>
<bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/"/>
<property name="suffix" value=".jsp"/>
</bean>
<context:component-scan base-package="com.atguigu.fdfs.controller"/>
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="UTF-8"/>
</bean>
4.建立工廠類
private String confLocation;
public void setConfLocation(String confLocation) {
//原始:classpath:tracker.conf
//需要:/tracker.conf
int beginIndex = confLocation.indexOf(":") + 1;
this.confLocation = "/" + confLocation.substring(beginIndex);
}
@Override
public StorageClient getObject() throws Exception {
//1.初始化工作
//※需要把confPath從基於類路徑根目錄的相對路徑轉換為真實物理路徑
String confAbsolutePath = FastDFSStorageClientFatory.class.getResource(confLocation).getPath();
System.err.println("☆☆☆"+confAbsolutePath+"☆☆☆");
ClientGlobal.init(confAbsolutePath);
//2.建立StorageClient
TrackerClient trackerClient = new TrackerClient();
TrackerServer trackerServer = trackerClient.getConnection();
StorageServer storageServer = null;
StorageClient storageClient = new StorageClient(trackerServer, storageServer);
System.err.println("☆☆☆"+storageClient.toString()+"☆☆☆");
return storageClient;
}
@Override
public Class<?> getObjectType() {
// TODO Auto-generated method stub
return StorageClient.class;
}
@Override
public boolean isSingleton() {
// TODO Auto-generated method stub
return true;
}
5.在Spring配置檔案中配置工廠的bean
<bean id="fastDFSStorageClientFatory" class="com.fdfs.factory.FastDFSStorageClientFatory">
<property name="confLocation" value="classpath:tracker.conf"/>
</bean>
6.在Controller中自動裝配StorageClient
@Autowired
private StorageClient storageClient;