1. 程式人生 > >Centos7上安裝FastDFS及Springboot整合FastDFS

Centos7上安裝FastDFS及Springboot整合FastDFS

一、概念

tracker-server:

跟蹤伺服器, 主要做排程工作, 起負載均衡的作用。 在記憶體中記錄叢集中所有儲存組和儲存伺服器的狀態資訊, 是客戶端和資料伺服器互動的樞紐。 相比GFS中的master更為精簡, 不記錄檔案索引資訊, 佔用的記憶體量很少。

storage-server:

儲存伺服器( 又稱:儲存節點或資料伺服器) , 檔案和檔案屬性( metadata) 都儲存到儲存伺服器上。 Storage server直接利用OS的檔案系統呼叫管理檔案。

group:

組, 也可稱為卷。 同組內伺服器上的檔案是完全相同的 ,同一組內的storage server之間是對等的, 檔案上傳、 刪除等操作可以在任意一臺storage server上進行 。

meta data:

檔案相關屬性,鍵值對( Key Value Pair) 方式,如:width=1024,heigth=768 。

二、部署

192.168.1.177安裝fastdfs的tracker節點,以及nginx反向代理伺服器用於下載服務。

192.168.1.188,192.168.1.189安裝fastdfs的storage節點,預設分一組,一組內兩臺機器互為備份.

注意:為了做到高可用,一個group建議分為兩臺以上的機器。

三、編譯和安裝所需的依賴包

# yum install make cmake gcc gcc-c++

四.安裝 libfastcommon,這裡是通過wget下載(我喜歡這種方式)。

1、下載:

wget https://github.com/happyfish100/libfastcommon/archive/V1.0.7.tar.gz

 

2、解壓

將 libfastcommonV1.0.7.tar.gz解壓至/usr/local/下:

tar -zxvf libfastcommonV1.0.7.tar.gz -C /usr/local/

3、切換目錄到:/usr/local/libfastcommon-1.0.7/ 下,接著進行編譯和安裝;

cd /usr/local/libfastcommon-1.0.7/
./make.sh           #編譯
./make.sh install   #安裝

安裝後的截圖:

4、libfastcommon安裝好後會在/usr/lib64 目錄下生成  libfastcommon.so 庫檔案;

  

  注意:由於FastDFS程式引用usr/lib目錄所以需要將/usr/lib64下的庫檔案拷貝至/usr/lib下。

cp libfastcommon.so /usr/lib

五、安裝tracker,即安裝FastDFS

1、下載安裝 FastDFS,這裡也是通過wget下載。

wget https://github.com/happyfish100/fastdfs/archive/V5.05.tar.gz

2、將5.05.tar.gz 解壓至/usr/local/下

tar -zxvf V5.05.tar.gz -C /usr/local

3、切換目錄到: /usr/local/fastdfs-5.05/ 下即解壓後的目錄,編譯和安裝;

cd /usr/local/fastdfs-5.05
./make.sh
./make.sh install

安裝後的截圖:

4、安裝成功將安裝目錄下的conf下的檔案拷貝到/etc/fdfs/下;

cp /usr/local/fastdfs-5.05/conf/* /etc/fdfs/

六、配置和啟動tracker

(1)切換目錄到: /etc/fdfs/ 目錄下;

(2)拷貝一份新的tracker配置檔案

cp tracker.conf.sample tracker.conf

(3)修改tracker.conf ;   vim tracker.conf

base_path=/home/yuqing/fastdfs  改為:  base_path=/home/fastdfs
http.server_port  改為: 80
base_path=/home/fastdfs
http.server_port=80  #配置http埠

(4)建立 /home/fastdfs 目錄

mkdir /home/fastdfs

(5)啟動tracker,執行如下命令:

/usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf restart

注意:在/home/fastdfs/ 目錄下生成兩個目錄, 一個是資料,一個是日誌;

七、配置和啟動storage

由於上面已經安裝過FastDFS,這裡只需要配置storage就好了;

(1)切換目錄到: /etc/fdfs/ 目錄下;

(2)拷貝一份新的storage配置檔案

cp storage.conf.sample storage.conf

(3)修改storage.conf ;   vim storage.conf

group_name=group1 #配置組名
base_path=/home/yuqing/fastdfs    改為:  base_path=/home/fastdfs

#store存放檔案的位置(store_path)
store_path0=/home/yuqing/fastdfs  改為: store_path0=/home/fdfs_storage 
#如果有多個掛載磁碟則定義多個store_path,如下
#store_path1=.....
#store_path2=......

#配置tracker伺服器:IP
tracker_server=192.168.172.20:22122
#如果有多個則配置多個tracker
#tracker_server=192.168.101.4:22122

#配置http埠
http.server_port=88

 (4)建立 /home/fdfs_storage 目錄

mkdir /home/fdfs_storage

(5)啟動storage, 執行命令如下:

/usr/bin/fdfs_storaged /etc/fdfs/storage.conf restart

啟動完成後進入 /home/fdfs_storage/data 目錄下,顯示目錄如下:

八、使用FastDFS自帶工具測試

(1)切換目錄到 /etc/fdfs/ 目錄下;

(2)拷貝一份新的client配置檔案

cp client.conf.sample client.conf

(3)修改client.conf ;   vim client.conf,修改基本路徑和tracker_server如下:

 

  注意:若tracker有多個,可以配置多個,如下:

  #tracker_server=......

  #tracker_server=......
(4)拷貝一張圖片wo.jpg 到Centos伺服器上的 / 目錄下;

(5)進行測試,執行如下:(執行測試程式,讀取/etc/fdfs/client.conf 檔案,上傳/home目錄下的.jpg檔案)

/usr/bin/fdfs_test /etc/fdfs/client.conf upload /home/wo.jpg

結果如下,表示搭建成功;

以上圖中的檔案地址:http://192.168.172.20/group1/M00/00/00/wKisFFpBG9eAHaQvAAAWKd1hQR4158_big.jpg  對應storage伺服器上的/home/fdfs_storage/data/00/00/wKisFFpBG9eAHaQvAAAWKd1hQR4158_big.jpg檔案;

由於現在還沒有和nginx整合無法使用http下載。

九、FastDFS 和nginx整合

1 在tracker上安裝 nginx

 在每個tracker上安裝nginx,的主要目的是做負載均衡及實現高可用。如果只有一臺tracker可以不配置nginx。

 一個tracker對應多個storage,通過nginx對storage負載均衡;

 2 在storage 上安裝nginx

1.下載 fastdfs-nginx-module,這裡是通過wget下載(我喜歡這種方式)。

wget https://github.com/happyfish100/fastdfs-nginx-module/archive/10d1729c043c18550258cd6cb56cbf337624a397.zip

注意:fastdfs-nginx-module與FastDFS版本必須對應,否則會報錯,在nginx安裝時,對應的版本可以參考github上的說明。

github地址: https://github.com/happyfish100/fastdfs-nginx-module/blob/master/HISTORY

出現問題解決方案文件地址:https://www.jianshu.com/p/218913cccc95

由於我們的FastDFS是5.05,所以我們對應的使用1.17版本。

2、解壓 fastdfs-nginx-module.tar.gz 到 /usr/local目錄下;

unzip -o master.zip -d /usr/local

3、切換目錄到: /usr/local/fastdfs-nginx-module-master/src/ 目錄下

cd /usr/local/fastdfs-nginx-module-master/src/

4、修改config檔案,將檔案中的所有 /usr/local/ 路徑改為 /usr/

 

修改完成後:

5、將fastdfs-nginx-module-/src下的mod_fastdfs.conf拷貝至/etc/fdfs/下

cp mod_fastdfs.conf /etc/fdfs/

6、並修改 /etc/fdfs/mod_fastdfs.conf 的內容;vi /etc/fdfs/mod_fastdfs.conf

 base_path=/tmp 修改為 base_path=/home/fastdfs

base_path=/home/fastdfs
tracker_server=192.168.172.20:22122 
#tracker_server=192.168.172.20:22122 #(多個tracker配置多行)
url_have_group_name=true        #url中包含group名稱
store_path0=/home/fdfs_storage  #指定檔案儲存路徑(上面配置的store路徑)

(7)將libfdfsclient.so拷貝至/usr/lib下

cp /usr/lib64/libfdfsclient.so /usr/lib/

(8)建立nginx/client目錄

mkdir -p /var/temp/nginx/client

十、Nginx的安裝

1、下載:

wget-c https://nginx.org/download/nginx-1.10.1.tar.gz

(2)解壓 nginx-1.10.1.tar.gz 到 /usr/local目錄下;

tar -zxvf nginx-1.10.1.tar.gz -C /usr/local/

(3)安裝nginx的依賴庫

yum install gcc-c++
yum install pcre
yum install pcre-devel
yum install zlib
yum install zlib-devel
yum install openssl
yum install openssl-devel

(4)進入nginx解壓的目錄下:cd /usr/local/nginx-1.10.1/

(5)加入模組命令配置

./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

該處為命令,所以需要拷貝出去執行。特備註意裡面的path所寫路徑要與伺服器真正的路徑對應,特別是--add-module=/usr/local/fastdfs-nginx-module/src要與fastdfs-nginx-module真正安裝的路徑對應。

(6)編譯並安裝

make && make install

安裝成功後檢視生成的目錄,如下所示:

 

(7)拷貝配置檔案到 /etc/fdfs 下;

cd /usr/local/fastdfs-5.05/conf
cp http.conf mime.types /etc/fdfs/

(8)修改nginx配置檔案

mkdir /usr/local/nginx/logs # 建立logs目錄
cd /usr/local/nginx/conf/
vim nginx.conf

做如下的修改:

  

  

說明

  (a.) server_name指定本機ip;

  (b.) location /group1/M00/:group1為nginx 服務FastDFS的分組名稱,M00是FastDFS自動生成編號,對應store_path0=/home/fdfs_storage,如果FastDFS定義store_path1,這裡就是M01

 

 (9)nginx的啟動

cd /usr/local/nginx/sbin/
./nginx 

查詢nginx程序

  

(10) fastdfs 和 nginx 服務的開機自啟動: http://www.cnblogs.com/yufeng218/p/8215381.html

十一、在瀏覽器中訪問上傳到fastDFS的圖片

因為Centos系統有防火牆,需要先關閉掉,才可以在瀏覽器中訪問;

(1)CentOS 7.0預設使用的是firewall作為防火牆;若沒有啟用iptables 作為防火牆,則使用以下方式關閉防火牆:

systemctl stop firewalld.service #停止firewall
systemctl disable firewalld.service #禁止firewall開機啟動
firewall-cmd --state #檢視預設防火牆狀態(關閉後顯示notrunning,開啟後顯示running)

  

(2)若已經啟用iptables作為防火牆,則使用以下方式關閉:

service iptables stop  #臨時關閉防火牆
chkconfig iptables off #永久關閉防火牆

防火牆的相關連結:http://www.jb51.net/article/101576.htm

           http://www.linuxidc.com/Linux/2015-05/117473.htm

 (3)在谷歌瀏覽器中訪問剛才上傳的圖片:

剛才上傳的圖片地址為:http://192.168.172.20/group1/M00/00/00/wKisFFpBG9eAHaQvAAAWKd1hQR4158_big.jpg

參考文章:

https://www.cnblogs.com/yufeng218/p/8111961.html

https://www.jianshu.com/p/218913cccc95

十三、總結:

在本次安裝中,所有安裝包解壓後都放到 /usr/local/目錄下,然後在該目錄下進行編譯與安裝(install),安裝完畢後會對應的在/usr/lib64/中生成.so包,然後需要對應的將其拷貝到/usr/lib/目錄下。安裝完FastDFS後,tracker、storage的配置檔案都需要拷貝到/etc/fdfs/目錄下,包含:tracker.conf、storage.conf、client.conf,FastDFS安裝nginx,需要安裝nginx、fastdfs-nginx-module,他們也是將原始碼解壓到/usr/local/目錄下,配置檔案mod_fastdfs.conf需要配置tracker的地址、檔案的儲存路徑,nginx在安裝時需要加入fastdfs-nginx-module模組,通過命令,在nginx.conf配置檔案中加入為FastDfs的server,其中包括外界訪問的ip地址,還有FastDfs的storage的group地址。

十四、關於springboot整合FastDFS

請參考該文章:https://blog.csdn.net/ityouknow/article/details/79078175

十五、常用命令

啟動tracker

/usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf restart

重啟tracker

/usr/bin/restart.sh /usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf

啟動storage

/usr/bin/fdfs_storaged /etc/fdfs/storage.conf restart

重啟storage

/usr/bin/restart.sh /usr/bin/fdfs_storaged /etc/fdfs/storage.conf

啟動nginx

cd /usr/local/nginx/sbin/
./nginx 

重啟nginx

cd /usr/local/nginx/sbin/
./nginx -t
./nginx -s reload

nginx檢視日誌:

/usr/local/nginx/logs

十六、最終總結

通過上述步驟搭建的fdsfDfs系統,當通過nginx通過http訪問檔案時,在不加校驗,即不加Token時是沒有問題的,當開啟校驗後,就會出現問題,nginx報各種錯,訪問不到檔案。一開始推斷是由於每個模組版本的問題導致的,但是這時沒有發現用java寫的生成token的程式本身就有問題,而通過官方給的java客戶端訪問也是不成功,因為本身官網的生成token的程式也有問題,因為生成token時,檔案路徑不應加上group。而最終我還是重現按照官網教程進行了各模組的安裝。

官網教程地址:https://github.com/happyfish100/fastdfs/wiki

接下來我就按照官網教程進行了重新安裝,注意教程中所有安裝完的效果都是在啟動或重啟tracker、storage、nginx,關閉防火牆的前提下進行。特別注意在生成token時,檔案路徑不要攜帶group。

十七、http校驗

參考文章:https://www.cnblogs.com/Leo_wl/p/7705158.html 

官網未找到相關說明

1、進入/etc/fdfs

cd /etc/fdfs

2、修改http.conf

vim /etc/fdfs/http.conf

修改內容如下:

http.mime_types_filename=mime.types

# 設定為true表示開啟token驗證
http.anti_steal.check_token=true

# 設定token失效的時間單位為秒(s)
http.anti_steal.token_ttl=900
      
# 金鑰,跟客戶端即java程式中配置檔案的fastdfs.http_secret_key保持一致
http.anti_steal.secret_key=123456

# 如果token檢查失敗,返回的頁面
http.anti_steal.token_check_fail=/home/error.jpg
    
# if support multi regions for HTTP Range
# default value is true
http.multi_range.enabed = true

重啟服務

2、配置客戶端,即java

connect_timeout = 2
network_timeout = 30
charset = UTF-8
http.tracker_http_port = 8888
http.anti_steal_token = true
http.secret_key = 123456

tracker_server = 192.168.32.129:22122

注意:這裡有http.tracker_http_port埠好應與/usr/local/nginx/conf/nginx.conf中的listen中埠號一致,即與storage配置檔案/etc/fdfs/storage.conf中的http.server_port=8888埠號一致。

3、啟動客戶端程式,即可以通過校驗訪問檔案,例如如下地址:

http://192.168.32.129:8888/group1/M00/00/00/wKgggVvASFGAUzvbAADvriHGzsI112.png?token=4edd27279a76e01450b5f65500fd12d4&ts=1539328079

十八、客戶端

FastDFSClient.java內容如下:


import org.csource.common.MyException;
import org.csource.common.NameValuePair;
import org.csource.fastdfs.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.ClassPathResource;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.time.Instant;
import java.util.Properties;

import static org.springframework.util.StringUtils.getFilename;

/**
 xi
 */
public class FastDFSClient {
    private static final Logger logger = LoggerFactory.getLogger(FastDFSClient.class);

    private static TrackerClient trackerClient;
    private static TrackerServer trackerServer;
    private static StorageServer storageServer;
    private static StorageClient storageClient;

    static {
        try {
            ClientGlobal.init("fdfs_client.conf");
            trackerClient = new TrackerClient();
            trackerServer = trackerClient.getConnection();
            storageServer = trackerClient.getStoreStorage(trackerServer);
        } catch (Exception e) {
            logger.error("FastDFS Client Init Fail!", e);
        }
    }

    public static String[] upload(FastDFSFile file) {
        logger.info("File Name: " + file.getName() + "File Length:" + file.getContent().length);

        NameValuePair[] meta_list = new NameValuePair[1];
        meta_list[0] = new NameValuePair("author", file.getAuthor());

        long startTime = System.currentTimeMillis();
        String[] uploadResults = null;
        try {
            storageClient = new StorageClient(trackerServer, storageServer);
            uploadResults = storageClient.upload_file(file.getContent(), file.getExt(), meta_list);
        } catch (IOException e) {
            logger.error("IO Exception when uploadind the file:" + file.getName(), e);
        } catch (Exception e) {
            logger.error("Non IO Exception when uploadind the file:" + file.getName(), e);
        }
        logger.info("upload_file time used:" + (System.currentTimeMillis() - startTime) + " ms");

        if (uploadResults == null) {
            logger.error("upload file fail, error code:" + storageClient.getErrorCode());
        }
        String groupName = uploadResults[0];
        String remoteFileName = uploadResults[1];

        logger.info("upload file successfully!!!" + "group_name:" + groupName + ", remoteFileName:" + " " + remoteFileName);
        return uploadResults;
    }

    public static FileInfo getFile(String groupName, String remoteFileName) {
        try {
            storageClient = new StorageClient(trackerServer, storageServer);
            return storageClient.get_file_info(groupName, remoteFileName);
        } catch (IOException e) {
            logger.error("IO Exception: Get File from Fast DFS failed", e);
        } catch (Exception e) {
            logger.error("Non IO Exception: Get File from Fast DFS failed", e);
        }
        return null;
    }

    public static InputStream downFile(String groupName, String remoteFileName) {
        try {
            storageClient = new StorageClient(trackerServer, storageServer);
            byte[] fileByte = storageClient.download_file(groupName, remoteFileName);
            InputStream ins = new ByteArrayInputStream(fileByte);
            return ins;
        } catch (IOException e) {
            logger.error("IO Exception: Get File from Fast DFS failed", e);
        } catch (Exception e) {
            logger.error("Non IO Exception: Get File from Fast DFS failed", e);
        }
        return null;
    }

    public static void deleteFile(String groupName, String remoteFileName)
            throws Exception {
        storageClient = new StorageClient(trackerServer, storageServer);
        int i = storageClient.delete_file(groupName, remoteFileName);
        logger.info("delete file successfully!!!" + i);
    }

    public static String getTrackerUrl() throws IOException {
        return "http://" + trackerServer.getInetSocketAddress().getHostString() + ":" + ClientGlobal.getG_tracker_http_port() + "/";
    }

    /**
     2      * 獲取訪問伺服器的token,拼接到地址後面
     3      *
     4      * @param filepath 檔案路徑 M00/00/00/wKgzgFnkTPyAIAUGAAEoRmXZPp876.jpeg
     5      * @param httpSecretKey 金鑰
     6      * @return 返回token,如: token=078d370098b03e9020b82c829c205e1f&ts=1508141521
     7      */
    public static String getToken(String filepath){
         int ts;
         String token = null;
         String file_url="";
         try {
             if (ClientGlobal.g_anti_steal_token) {
                 ts = (int) (System.currentTimeMillis() / 1000);
                 String key=ClientGlobal.g_secret_key;
                 token = ProtoCommon.getToken(filepath, ts, ClientGlobal.g_secret_key);
                 file_url += "?token=" + token + "&ts=" + ts;
             }
         } catch (UnsupportedEncodingException e) {
             e.printStackTrace();
         } catch (NoSuchAlgorithmException e) {
             e.printStackTrace();
         } catch (MyException e) {
             e.printStackTrace();
         }
        return file_url;
     }
}

web程式如下:


import com.hongdaoai.datastore.common.annotation.SysLog;
import com.hongdaoai.datastore.common.controller.BaseController;
import com.hongdaoai.datastore.fastdfs.FastDFSClient;
import com.hongdaoai.datastore.fastdfs.FastDFSFile;
import lombok.extern.slf4j.Slf4j;
import org.csource.fastdfs.StorageClient1;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.math.BigDecimal;
import java.util.Arrays;

@RestController
@RequestMapping("hdzy/datastore")
@Scope("prototype") //設定該類不是單例
@Slf4j
public class DataFileController extends BaseController {
    private static final Logger logger = LoggerFactory.getLogger(DataFileController.class);

    /**
     * 上傳檔案
     * @return
     */
    @SysLog(isLog = true)
    @PostMapping("/book/upload")
    @ResponseBody
    public Object fileUploadForBook(@RequestParam("file") MultipartFile multipartFile) throws IOException {
        String[] fileAbsolutePath={};
        String fileName=multipartFile.getOriginalFilename();
        String ext = fileName.substring(fileName.lastIndexOf(".") + 1);
        byte[] file_buff = null;
        InputStream inputStream=multipartFile.getInputStream();
        if(inputStream!=null){
            int len1 = inputStream.available();
            file_buff = new byte[len1];
            inputStream.read(file_buff);
        }
        inputStream.close();
        FastDFSFile file = new FastDFSFile(fileName, file_buff, ext);
        try {
            fileAbsolutePath = FastDFSClient.upload(file);  //upload to fastdfs
        } catch (Exception e) {
            logger.error("upload file Exception!",e);
        }
        if (fileAbsolutePath==null) {
            logger.error("upload file failed,please upload again!");
        }
        String group_name=fileAbsolutePath[0];
        String remote_filename=fileAbsolutePath[1];
//        String path=FastDFSClient.getTrackerUrl()+fileAbsolutePath[0]+ "/"+fileAbsolutePath[1];
        String path=FastDFSClient.getTrackerUrl()+group_name+ StorageClient1.SPLIT_GROUP_NAME_AND_FILENAME_SEPERATOR+remote_filename+FastDFSClient.getToken(remote_filename);
        return path;
    }
}

fdfs_client.conf內容如下:

connect_timeout = 2
network_timeout = 30
charset = UTF-8
http.tracker_http_port = 8888
http.anti_steal_token = true
http.secret_key = 123456

tracker_server = 192.168.32.129:22122