fastdfs搭建和使用
阿新 • • 發佈:2018-11-24
目錄
前言
安裝
安裝外掛
yum install -y libevent cd /usr/local mkdir fastFDS cd fastFDS wget https://github.com/happyfish100/libfastcommon/archive/V1.0.7.tar.gz mv V1.0.7.tar.gz libfastcommon-1.0.7.tar.gz tar zxvf libfastcommon-1.0.7.tar.gz cd libfastcommon-1.0.7/ ./make.sh ./make.sh install ln -s /usr/lib64/libfastcommon.so /usr/local/lib/libfastcommon.so ln -s /usr/lib64/libfastcommon.so /usr/lib/libfastcommon.so ln -s /usr/lib64/libfdfsclient.so /usr/local/lib/libfdfsclient.so ln -s /usr/lib64/libfdfsclient.so /usr/lib/libfdfsclient.so
tracker
cd /usr/local/fastFDS wget https://github.com/happyfish100/fastdfs/archive/V5.05.tar.gz mv V5.05.tar.gz FastDFS_v5.05.tar.gz tar zxvf FastDFS_v5.05.tar.gz cd fastdfs-5.05/ ./make.sh ./make.sh install # 建立一個存放data和log的目錄 mkdir -p /opt/fastdfs/tracker/data-and-log # 配置 cp /etc/fdfs/tracker.conf.sample /etc/fdfs/tracker.conf vim /etc/fdfs/tracker.conf 修改為: base_path=/opt/fastdfs/tracker/data-and-log # 啟動tracker服務: /usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf # 重啟tracker服務: /usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf restart # 檢視是否有 tracker 程序: ps aux | grep tracker
storage
如果 storage 單獨安裝的話,那上面安裝的所有步驟都要在走一遍,只是到了編輯配置檔案的時候,編輯的是 storage.conf 而已
mkdir -p /opt/fastdfs/storage/data-and-log mkdir -p /opt/fastdfs/storage/images-data cp /etc/fdfs/storage.conf.sample /etc/fdfs/storage.conf vim /etc/fdfs/storage.conf 修改: base_path=/opt/fastdfs/storage/data-and-log store_path0=/opt/fastdfs/storage/images-data # 圖片實際存放路徑,如果有多個,這裡可以有多行: # store_path0=/opt/fastdfs/storage/images-data0 # store_path1=/opt/fastdfs/storage/images-data1 # store_path2=/opt/fastdfs/storage/images-data2 # 指定 tracker 伺服器的 IP 和埠 tracker_server=192.168.63.128:22122 # 啟動 storage 服務 /usr/bin/fdfs_storaged /etc/fdfs/storage.conf # 重啟 storage 服務: /usr/bin/fdfs_storaged /etc/fdfs/storage.conf restart # 檢視是否有 storage 程序: ps aux | grep storage
用自帶的 client 進行測試
mkdir -p /opt/fastdfs/client/data-and-log
cp /etc/fdfs/client.conf.sample /etc/fdfs/client.conf
vim /etc/fdfs/client.conf
修改:
base_path=/opt/fastdfs/client/data-and-log
# 指定 tracker 伺服器的 IP 和埠
tracker_server=192.168.63.128:22122
# 下載並上傳檔案
cd /opt
wget http://img4.imgtn.bdimg.com/it/u=379667247,2270287117&fm=200&gp=0.jpg
mv 'u=379667247,2270287117&fm=200&gp=0.jpg' ceshi.jpg
/usr/bin/fdfs_test /etc/fdfs/client.conf upload /opt/ceshi.jpg
想要檢視結果必須安裝nginx
cd /usr/local
wget http://nginx.org/download/nginx-1.11.8.tar.gz
wget http://jaist.dl.sourceforge.NET/project/fastdfs/FastDFS%20Nginx%20Module%20Source%20Code/fastdfs-nginx-module_v1.16.tar.gz
mkdir -p /opt/setups/FastDFS
tar zxvf fastdfs-nginx-module_v1.16.tar.gz -C /opt/setups/FastDFS/fastdfs-nginx-module
vim /opt/setups/FastDFS/fastdfs-nginx-module/src/config
修改:
CORE_INCS="$CORE_INCS /usr/local/include/fastdfs /usr/local/include/fastcommon/"改為:
CORE_INCS="$CORE_INCS /usr/include/fastdfs /usr/include/fastcommon/"
# 下面的一行也去掉local
cp /usr/local/fastDFS/fastdfs-5.05/conf/http.conf /etc/fdfs
cp /usr/local/fastDFS/fastdfs-5.05/conf/mime.types /etc/fdfs
# 安裝 Nginx 依賴包
yum install -y gcc gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel
# 預設幾個資料夾,方便等下安裝的時候有些檔案可以進行存放
mkdir -p /opt/fastdfs/fastdfs-nginx-module/data-and-log
mkdir -p /usr/local/nginx /var/log/nginx /var/temp/nginx /var/lock/nginx
tar zxvf nginx-1.11.8.tar.gz
cd nginx-1.11.8
./configure --prefix=/usr/local/nginx --pid-path=/var/local/nginx/nginx.pid --lock-path=/var/lock/nginx/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=/opt/setups/FastDFS/fastdfs-nginx-module/src
make
make install
cp /opt/setups/FastDFS/fastdfs-nginx-module/src/mod_fastdfs.conf /etc/fdfs
vim /etc/fdfs/mod_fastdfs.conf
base_path=/opt/fastdfs/fastdfs-nginx-module/data-and-log
tracker_server=192.168.63.128:22122
url_have_group_name = true
store_path0=/opt/fastdfs/storage/images-data
<!--圖片實際存放路徑,如果有多個,這裡可以有多行:-->
<!--store_path0=/opt/fastdfs/storage/images-data0-->
<!--store_path1=/opt/fastdfs/storage/images-data1-->
<!--store_path2=/opt/fastdfs/storage/images-data2-->
vim /usr/local/nginx/conf/nginx.conf
user root;
listen 80;
# 訪問本機
server_name 192.168.1.114;
# 攔截包含 /group1/M00 請求,使用 fastdfs 這個 Nginx 模組進行轉發
location /group1/M00 {
ngx_fastdfs_module;
}
# 停掉防火牆:
service iptables stop
# 啟動:
/usr/local/nginx/sbin/nginx
# 重啟:
/usr/local/nginx/sbin/nginx -s reload
# 停止
/usr/local/nginx/sbin/nginx -s stop
訪問nginx:
192.168.63.128
檢視程序:
ps aux | grep nginx
錯誤資訊:
vim /var/log/nginx/error.log
備份:
cp /usr/local/tracker/fastdfs-5.05/conf/http.conf /etc/fdfs
cp /usr/local/tracker/fastdfs-5.05/conf/mime.types /etc/fdfs
使用示例
引用
必須先安裝到本地才可以引用
mvn install:install-file -DgroupId=org.csource.fastdfs -DartifactId=fastdfs -Dversion=1.2 -Dpackaging=jar -Dfile=d:\setup\fastdfs_client_v1.20.jar
<dependency>
<groupId>org.csource.fastdfs</groupId>
<artifactId>fastdfs</artifactId>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
</dependency>
配置
springMvc.xml
<!--配置可以直接讀取的配置-->
<context:property-placeholder location="classpath:config/application.properties" />
<!-- 配置多媒體解析器 -->
<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>
fastdfs引數
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=/opt/fastdfs/tracker/data-and-log # tracker_server can ocur more than once, and tracker_server format is # "host:port", host can be hostname or ip address tracker_server=192.168.63.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 ```
自定義工具類
FastDFSClient.java
package com.alvin.utils;
import org.apache.commons.io.FilenameUtils;
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[] file, String fileName, long fileSize) throws Exception {
NameValuePair[] metas = new NameValuePair[3];
metas[0] = new NameValuePair("fileName", fileName);
metas[1] = new NameValuePair("fileSize", String.valueOf(fileSize));
metas[2] = new NameValuePair("fileExt", FilenameUtils.getExtension(fileName));
String result = storageClient.upload_file1(file, FilenameUtils.getExtension(fileName), metas);
return result;
}
public String uploadFile(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);
}
}
使用
package com.alvin.controller;
import com.alvin.pojo.entity.Result;
import com.alvin.utils.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_URL;
@RequestMapping("/uploadFile")
public Result upload(MultipartFile file) {
String fileName = file.getOriginalFilename();
try {
FastDFSClient fastDFSClient = new FastDFSClient("classpath:fastDFS/fdfs_client.conf");
String path = fastDFSClient.uploadFile(file.getBytes(), fileName, file.getSize());
String url = FILE_SERVER_URL + path;
return new Result(true, url);
} catch (Exception e) {
e.printStackTrace();
return new Result(false, "上傳失敗");
}
}
}