主頁 > 軟體設計 > 學習FastDFS這一篇就夠了

學習FastDFS這一篇就夠了

2020-09-10 07:45:45 軟體設計

目錄

    • 第一章 FastDFS簡介
      • 1.1、FastDFS的簡介
      • 1.2、FastDFS的發展歷史
      • 1.3、FastDFS的整體架構
      • 1.4、FastDFS的使用用戶
      • 1.5、FastDFS的官方網址
    • 第二章 FastDFS單實體部署
      • 2.1、環境準備
      • 2.2、安裝依賴
      • 2.3、安裝libfastcommon庫
      • 2.4、安裝FastDFS
      • 2.5、配置FastDFS
      • 2.6、啟動FastDFS
      • 2.7、重啟FastDFS
      • 2.8、測驗FastDFS
      • 2.9、關閉FastDFS
      • 2.10、開啟FastDFS外部訪問
    • 第三章 FastDFS高可用集群
      • 3.1、環境準備
      • 3.2、架構目標
      • 3.3、tracker配置
      • 3.4、storage配置
      • 3.5、mod_fastdfs配置
      • 3.6、安裝兩個tracker上的nginx
      • 3.7、啟動所有服務進行測驗
      • 3.8、統一訪問路徑
      • 3.9、如何保證主nginx高可用
    • 第四章 FastDFS的JavaAPI
      • 4.1、工程搭建
      • 4.2、上傳檔案
      • 4.3、下載檔案
      • 4.4、洗掉檔案


配套資料,免費下載
鏈接:https://pan.baidu.com/s/17n_VVnk0geU_fv3789_6Tg
提取碼:kzvy
復制這段內容后打開百度網盤手機App,操作更方便哦

第一章 FastDFS簡介

1.1、FastDFS的簡介

FastDFS是一個開源的輕量級分布式檔案系統,為互聯網應用量身定做,簡單、靈活、高效,采用C語言開發,由阿里巴巴開發并開源,

FastDFS對檔案進行管理,功能包括:檔案存盤、檔案同步、檔案訪問(檔案上傳、檔案下載、檔案洗掉)等,解決了大容量檔案存盤的問題,特別適合以檔案為載體的在線服務,如相冊網站、檔案網站、圖片網站、視頻網站等等,

FastDFS充分考慮了冗余備份、線性擴容等機制,并注重高可用、高性能等指標,使用FastDFS很容易搭建一套高性能的檔案服務器集群提供檔案上傳、下載等服務,

1.2、FastDFS的發展歷史

  • 2008年4月專案正式啟動

  • 2008年7月推出V1.00

  • 2010年8月推出V2.00

  • 2011年6月推出V3.00

  • 2012年10月推出V4.00

  • 2013年12月推出V5.00

  • 2019年10月推出V6.00

FastDFS系統架構從第一個版本發布后一直沒有大的調整,高版本完全兼容低版本的資料,可以做到平滑升級,推薦更新升級到最新版本,

1.3、FastDFS的整體架構

FastDFS檔案系統由兩大部分構成,一個是客戶端,一個是服務端,

客戶端通常指我們的程式,比如我們的Java程式去連接FastDFS、操作FastDFS,那我們的Java程式就是一個客戶端,FastDFS提供專有API訪問,目前提供了C、Java和PHP幾種編程語言的API,用來訪問FastDFS檔案系統,

服務端由兩個部分構成:一個是跟蹤器(tracker),一個是存盤節點(storage),

跟蹤器(tracker)主要做調度作業,在記憶體中記錄集群中存盤節點storage的狀態資訊,是前端Client和后端存盤節點storage的樞紐,因為相關資訊全部在記憶體中,Tracker server的性能非常高,一個較大的集群(比如上百個group)中有3臺就足夠了,

存盤節點(storage)用于存盤檔案,包括檔案和檔案屬性(meta data)都保存到存盤服務器磁盤上,完成檔案管理的所有功能:檔案存盤、檔案同步和提供檔案訪問等,

1.4、FastDFS的使用用戶

  • UC(http://www.uc.cn/)
  • 支付寶(http://www.alipay.com/)
  • 京東商城(http://www.jd.com/)
  • 淘淘搜(http://www.taotaosou.com/)
  • 飛信(http://feixin.10086.cn/)
  • 趕集網(http://www.ganji.com/)
  • 淘米網(http://www.61.com/)
  • 迅雷(http://www.xunlei.com/)
  • 螞蜂窩(http://www.mafengwo.cn/)
  • 5173(http://www.5173.com/)
  • 華師京城教育云平臺(http://www.hsjdy.com.cn/)
  • 視友網(http://www.cuctv.com/)
  • 搜道網(http://www.sodao.com/)
  • 58同城(http://www.58.com/)
  • 商務聯盟網(http://www.biz72.com/)
  • 中青網(http://www.youth.cn/)
  • 保利威視(http://www.freeovp.com/)
  • 夢芭莎(http://www.moonbasa.com/)
  • 51CTO(http://www.51cto.com/)
  • 搜房網(http://www.soufun.com/)

1.5、FastDFS的官方網址

官方網址:點擊打開

第二章 FastDFS單實體部署

2.1、環境準備

  • 虛擬機的版本:VMware-workstation-full-15.5.6-16341506.exe
  • 系統鏡像版本:CentOS-6.10-x86_64-bin-DVD1.iso,全新安裝,桌面版,可上網
  • 系統記憶體大小:512MB,這里修改小點,方便后邊集群復制
  • 系統硬碟大小:20GB
  • 連接工具版本:SecureCRTSecureFX_HH_x64_7.0.0.326.zip

2.2、安裝依賴

[root@caochenlei ~]# yum install -y gcc gcc-c++ perl perl-devel openssl openssl-devel pcre pcre-devel zlib zlib-devel libevent libevent-devel

2.3、安裝libfastcommon庫

下載:

[root@caochenlei ~]# wget https://github.com/happyfish100/libfastcommon/archive/V1.0.43.tar.gz

解壓:

[root@caochenlei ~]# tar -zxvf V1.0.43.tar.gz

切換:

[root@caochenlei ~]# cd libfastcommon-1.0.43

編譯:

[root@caochenlei libfastcommon-1.0.43]# ./make.sh

安裝:

[root@caochenlei libfastcommon-1.0.43]# ./make.sh install
[root@caochenlei libfastcommon-1.0.43]# cd ~

2.4、安裝FastDFS

下載:

[root@caochenlei ~]# wget https://github.com/happyfish100/fastdfs/archive/V6.06.tar.gz

解壓:

[root@caochenlei ~]# tar -zxvf V6.06.tar.gz

切換:

[root@caochenlei ~]# cd fastdfs-6.06

編譯:

[root@caochenlei fastdfs-6.06]# ./make.sh

安裝:

[root@caochenlei fastdfs-6.06]# ./make.sh install

查看可執行檔案:

[root@caochenlei fastdfs-6.06]# ll /usr/bin/fdfs*

image-20200905183808335

查看組態檔

[root@caochenlei fastdfs-6.06]# ll /etc/fdfs/

image-20200905184005284

拷貝其它配置:

[root@caochenlei fastdfs-6.06]# cd conf

[root@caochenlei conf]# cp http.conf /etc/fdfs/
[root@caochenlei conf]# cp mime.types /etc/fdfs/

[root@caochenlei conf]# cd /etc/fdfs/

[root@caochenlei fdfs]# mv client.conf.sample client.conf
[root@caochenlei fdfs]# mv storage.conf.sample storage.conf
[root@caochenlei fdfs]# mv storage_ids.conf.sample storage_ids.conf
[root@caochenlei fdfs]# mv tracker.conf.sample tracker.conf

2.5、配置FastDFS

配置 tracker :

修改tracker.conf的以下幾項配置項:vi tracker.conf

  • #配置tracker存盤資料的目錄
    • base_path = /opt/fastdfs/tracker

創建相對應的檔案夾:

[root@caochenlei fdfs]# mkdir -p /opt/fastdfs/tracker

配置 storage :

修改storage.conf的以下幾項配置項:vi storage.conf

  • #storage存盤資料目錄
    • base_path = /opt/fastdfs/storage
  • #真正存放檔案的目錄
    • store_path0 = /opt/fastdfs/storage/files
  • #注冊當前存盤節點的跟蹤器地址
    • tracker_server = 192.168.239.128:22122

創建相對應的檔案夾:

[root@caochenlei fdfs]# mkdir -p /opt/fastdfs/storage
[root@caochenlei fdfs]# mkdir -p /opt/fastdfs/storage/files

2.6、啟動FastDFS

啟動 tracker :

[root@caochenlei fdfs]# fdfs_trackerd /etc/fdfs/tracker.conf

啟動 storage :

[root@caochenlei fdfs]# fdfs_storaged /etc/fdfs/storage.conf

查看啟動情況:

[root@caochenlei fdfs]# ps -ef | grep fdfs

image-20200905201134490

檢查監控資訊:

[root@caochenlei fdfs]# fdfs_monitor /etc/fdfs/storage.conf

查看資料目錄:

[root@caochenlei fdfs]# ls /opt/fastdfs/storage/files/data/

2.7、重啟FastDFS

重啟 tracker :

[root@caochenlei fdfs]# fdfs_trackerd /etc/fdfs/tracker.conf restart
waiting for pid [32335] exit ...
starting ...

重啟 storage :

[root@caochenlei fdfs]# fdfs_storaged /etc/fdfs/storage.conf restart
waiting for pid [32375] exit ...
starting ...

查看啟動情況:

[root@caochenlei fdfs]# ps -ef | grep fdfs

image-20200905202624605

2.8、測驗FastDFS

配置 client :

修改client.conf的以下幾項配置項:vi client.conf

  • #client存盤資料目錄

    • base_path = /opt/fastdfs/client
  • #注冊當前存盤節點的跟蹤器地址

    • tracker_server = 192.168.239.128:22122

創建相對應的檔案夾:

[root@caochenlei fdfs]# mkdir -p /opt/fastdfs/client

創建 a.txt :

[root@caochenlei fdfs]# echo "Hello,FastDFS" > a.txt

上傳 a.txt :

格式:fdfs_test /etc/fdfs/client.conf upload 檔案路徑

[root@caochenlei fdfs]# fdfs_test /etc/fdfs/client.conf upload /etc/fdfs/a.txt
This is FastDFS client test program v6.06

Copyright (C) 2008, Happy Fish / YuQing

FastDFS may be copied only under the terms of the GNU General
Public License V3, which may be found in the FastDFS source kit.
Please visit the FastDFS Home Page http://www.fastken.com/ 
for more detail.

[2020-09-05 20:39:01] DEBUG - base_path=/opt/fastdfs/client, connect_timeout=5, network_timeout=60, tracker_server_count=1, anti_steal_token=0, anti_steal_secret_key length=0, use_connection_pool=0, g_connection_pool_max_idle_time=3600s, use_storage_id=0, storage server id count: 0

tracker_query_storage_store_list_without_group: 
        server 1. group_name=, ip_addr=192.168.239.128, port=23000

group_name=group1, ip_addr=192.168.239.128, port=23000
storage_upload_by_filename
group_name=group1, remote_filename=M00/00/00/wKjvgF9ThuWAGUrIAAAADtHNnrs806.txt
source ip address: 192.168.239.128
file timestamp=2020-09-05 20:39:01
file size=14
file crc32=3519913659
example file url: http://192.168.239.128/group1/M00/00/00/wKjvgF9ThuWAGUrIAAAADtHNnrs806.txt
storage_upload_slave_by_filename
group_name=group1, remote_filename=M00/00/00/wKjvgF9ThuWAGUrIAAAADtHNnrs806_big.txt
source ip address: 192.168.239.128
file timestamp=2020-09-05 20:39:01
file size=14
file crc32=3519913659
example file url: http://192.168.239.128/group1/M00/00/00/wKjvgF9ThuWAGUrIAAAADtHNnrs806_big.txt

注意:group_name、remote_filename

地址格式,舉例如下圖:

image-20200905205221928

查看上傳后的資料檔案:

[root@caochenlei fdfs]# ll /opt/fastdfs/storage/files/data/00/00/
總用量 16
-rw-r--r--. 1 root root 14 9月   5 20:39 wKjvgF9ThuWAGUrIAAAADtHNnrs806_big.txt
-rw-r--r--. 1 root root 49 9月   5 20:39 wKjvgF9ThuWAGUrIAAAADtHNnrs806_big.txt-m
-rw-r--r--. 1 root root 14 9月   5 20:39 wKjvgF9ThuWAGUrIAAAADtHNnrs806.txt
-rw-r--r--. 1 root root 49 9月   5 20:39 wKjvgF9ThuWAGUrIAAAADtHNnrs806.txt-m

洗掉 a.txt :

格式:fdfs_delete_file /etc/fdfs/client.conf (group_name)/(remote_filename)

[root@caochenlei fdfs]# fdfs_delete_file /etc/fdfs/client.conf group1/M00/00/00/wKjvgF9ThuWAGUrIAAAADtHNnrs806.txt
[root@caochenlei fdfs]# ll /opt/fastdfs/storage/files/data/00/00/
總用量 8
-rw-r--r--. 1 root root 14 9月   5 20:39 wKjvgF9ThuWAGUrIAAAADtHNnrs806_big.txt
-rw-r--r--. 1 root root 49 9月   5 20:39 wKjvgF9ThuWAGUrIAAAADtHNnrs806_big.txt-m

[root@caochenlei fdfs]# fdfs_delete_file /etc/fdfs/client.conf group1/M00/00/00/wKjvgF9ThuWAGUrIAAAADtHNnrs806_big.txt
[root@caochenlei fdfs]# ll /opt/fastdfs/storage/files/data/00/00/
總用量 0

2.9、關閉FastDFS

關閉 tracker :

[root@caochenlei fdfs]# fdfs_trackerd /etc/fdfs/tracker.conf stop
waiting for pid [32441] exit ...
pid [32441] exit.

關閉 storage :

[root@caochenlei fdfs]# fdfs_storaged /etc/fdfs/storage.conf stop
waiting for pid [32453] exit ...
pid [32453] exit.

查看啟動情況:

[root@caochenlei fdfs]# ps -ef | grep fdfs

注意問題:

  1. 沒有搭建集群默認只有一個組group1
  2. 后綴名包含-m的為屬性檔案(meta)
  3. 在Linux中并沒有磁盤一說,M00是虛擬的,它其實就是data目錄
  4. 現在FastDFS已經安裝完成,但現在外部還不能訪問,有兩大原因:一是防火墻未關閉、二是FastDFS默認不支持外部訪問,想要訪問需要繼續往下學習

2.10、開啟FastDFS外部訪問

啟動 tracker :

[root@caochenlei fdfs]# fdfs_trackerd /etc/fdfs/tracker.conf

啟動 storage :

[root@caochenlei fdfs]# fdfs_storaged /etc/fdfs/storage.conf

關閉防火墻:

[root@caochenlei fdfs]# service iptables stop
[root@caochenlei fdfs]# chkconfig iptables off

回退根目錄:

[root@caochenlei fdfs]# cd ~

Nginx依賴:

[root@caochenlei ~]# yum install -y gcc gcc-c++ make libtool wget pcre pcre-devel zlib zlib-devel openssl openssl-devel

Nginx下載:

[root@caochenlei ~]# wget http://nginx.org/download/nginx-1.18.0.tar.gz

Nginx解壓:

[root@caochenlei ~]# tar -zxvf nginx-1.18.0.tar.gz

fastdfs-nginx-module下載:

[root@caochenlei ~]# wget https://github.com/happyfish100/fastdfs-nginx-module/archive/V1.22.tar.gz

fastdfs-nginx-module解壓:

[root@caochenlei ~]# tar -zxvf V1.22.tar.gz

#查看一下拓展模塊所在路徑,后邊會用到這個路徑
[root@caochenlei ~]# cd fastdfs-nginx-module-1.22/src/
[root@caochenlei src]# pwd
/root/fastdfs-nginx-module-1.22/src

#回退到根目錄,方便接下來的一系列安裝
[root@caochenlei src]# cd ~

Nginx及fastdfs-nginx-module安裝:

注意:因為這個模塊必須在Nginx的安裝的程序中才能添加,所有我們需要重新安裝一個Nginx,為了進行區分,我們把新安裝的Nginx取名為nginx_fdfs

[root@caochenlei ~]# cd nginx-1.18.0
[root@caochenlei nginx-1.18.0]# ./configure --prefix=/usr/local/nginx_fdfs --add-module=/root/fastdfs-nginx-module-1.22/src
[root@caochenlei nginx-1.18.0]# make && make install

注意:安裝完成后的路徑為:/usr/local/nginx_fdfs

Nginx命令(此處了解,先不要敲呢,跳過這步):

  • 普通啟動服務:/usr/local/nginx_fdfs/sbin/nginx
  • 組態檔啟動:/usr/local/nginx_fdfs/sbin/nginx -c /usr/local/nginx_fdfs/conf/nginx.conf
  • 暴力停止服務:/usr/local/nginx_fdfs/sbin/nginx -s stop
  • 優雅停止服務:/usr/local/nginx_fdfs/sbin/nginx -s quit
  • 檢查組態檔:/usr/local/nginx_fdfs/sbin/nginx -t
  • 重新加載配置:/usr/local/nginx_fdfs/sbin/nginx -s reload
  • 查看相關行程:ps -ef | grep nginx

FastDFS的Nginx訪問配置:

將 /root/fastdfs-nginx-module-1.22/src/mod_fastdfs.conf 拷貝到 /etc/fdfs/ 目錄下,這樣才能正常啟動Nginx

[root@caochenlei nginx-1.18.0]# cp /root/fastdfs-nginx-module-1.22/src/mod_fastdfs.conf /etc/fdfs/

修改mod_fastdfs.conf組態檔:

修改mod_fastdfs.conf的以下幾項配置項:vi /etc/fdfs/mod_fastdfs.conf

  • #mod_fastdfs存盤資料目錄

    • base_path=/opt/fastdfs/nginx_mod
  • #注冊當前存盤節點的跟蹤器地址

    • tracker_server=192.168.239.128:22122
  • #路徑中是否包含分組名稱,我們為true

    • url_have_group_name=true
  • #真正存放檔案的目錄

    • store_path0=/opt/fastdfs/storage/files

創建相對應的檔案夾:

[root@caochenlei nginx-1.18.0]# mkdir -p /opt/fastdfs/nginx_mod

配置Nginx的拓展模塊請求轉發:

[root@caochenlei nginx-1.18.0]# vi /usr/local/nginx_fdfs/conf/nginx.conf
location ~ /group[1-9]/M0[0-9] {	
     ngx_fastdfs_module;  
}

注意:ngx_fastdfs_module; #這個指令不是Nginx本身提供的,是擴展模塊提供的,根據這個指令找到FastDFS提供的Nginx模塊組態檔,然后找到Tracker,最終找到Stroager

啟動帶有Fastdfs模塊的Nginx:

[root@caochenlei nginx-1.18.0]# /usr/local/nginx_fdfs/sbin/nginx -c /usr/local/nginx_fdfs/conf/nginx.conf
ngx_http_fastdfs_set pid=35500

上傳一個檔案進行測驗驗證:

[root@caochenlei fdfs]# fdfs_test /etc/fdfs/client.conf upload /etc/fdfs/a.txt
This is FastDFS client test program v6.06

Copyright (C) 2008, Happy Fish / YuQing

FastDFS may be copied only under the terms of the GNU General
Public License V3, which may be found in the FastDFS source kit.
Please visit the FastDFS Home Page http://www.fastken.com/ 
for more detail.

[2020-09-05 21:41:43] DEBUG - base_path=/opt/fastdfs/client, connect_timeout=5, network_timeout=60, tracker_server_count=1, anti_steal_token=0, anti_steal_secret_key length=0, use_connection_pool=0, g_connection_pool_max_idle_time=3600s, use_storage_id=0, storage server id count: 0

tracker_query_storage_store_list_without_group: 
        server 1. group_name=, ip_addr=192.168.239.128, port=23000

group_name=group1, ip_addr=192.168.239.128, port=23000
storage_upload_by_filename
group_name=group1, remote_filename=M00/00/00/wKjvgF9TlZeAZpeTAAAADtHNnrs395.txt
source ip address: 192.168.239.128
file timestamp=2020-09-05 21:41:43
file size=14
file crc32=3519913659
example file url: http://192.168.239.128/group1/M00/00/00/wKjvgF9TlZeAZpeTAAAADtHNnrs395.txt
storage_upload_slave_by_filename
group_name=group1, remote_filename=M00/00/00/wKjvgF9TlZeAZpeTAAAADtHNnrs395_big.txt
source ip address: 192.168.239.128
file timestamp=2020-09-05 21:41:43
file size=14
file crc32=3519913659
example file url: http://192.168.239.128/group1/M00/00/00/wKjvgF9TlZeAZpeTAAAADtHNnrs395_big.txt

在虛擬機外部瀏覽器訪問上傳的檔案:

注意:直接輸入example file url: http://192.168.239.128/group1/M00/00/00/wKjvgF9TlZeAZpeTAAAADtHNnrs395.txt

image-20200905215213209

第三章 FastDFS高可用集群

3.1、環境準備

將第二章的單實體版本復制7個(關閉FastDFS服務,再關閉系統,退出虛擬機),然后依次啟動,選擇我已復制虛擬機,這樣就會為每一個虛擬機重新分配一塊網卡,這樣每天虛擬機就可以正常上網了,如果你要是有精力,你也可以按照第二章的方法,重新安裝這幾臺機器,但是這里為了省事,直接復制了,

學習這章電腦的環境:

記憶體至少8G+

硬碟至少255G+

關閉服務及系統命令:

[root@caochenlei ~]# /usr/local/nginx_fdfs/sbin/nginx -s quit
[root@caochenlei ~]# fdfs_trackerd /etc/fdfs/tracker.conf stop
[root@caochenlei ~]# fdfs_storaged /etc/fdfs/storage.conf stop
[root@caochenlei ~]# poweroff

復制完的效果如下圖:

image-20200906090522061

依次打開啟動虛擬機:

image-20200906090721672

注意:啟動后使用ifconfig查看當前虛擬機的IP地址

CentOS 6 64 bit - 副本 (1):192.168.239.136

CentOS 6 64 bit - 副本 (2):192.168.239.137

CentOS 6 64 bit - 副本 (3):192.168.239.138

CentOS 6 64 bit - 副本 (4):192.168.239.139

CentOS 6 64 bit - 副本 (5):192.168.239.140

CentOS 6 64 bit - 副本 (6):192.168.239.141

CentOS 6 64 bit - 副本 (7):192.168.239.142

使用連接工具進行連接:

image-20200906095358182

image-20200906095658625

3.2、架構目標

image-20200906095053224

3.3、tracker配置

其實tracker已經在單實體版本的時候就配置好了,跳過就行了,

3.4、storage配置

每兩個storage server為一組,共兩個組(group1 和 group2),一個組內有兩個storage server,


修改group1的storage1的storage.conf:vi /etc/fdfs/storage.conf

  • group_name = group1
  • tracker_server = 192.168.239.136:22122
  • tracker_server = 192.168.239.137:22122

修改group1的storage2的storage.conf:vi /etc/fdfs/storage.conf

  • group_name = group1
  • tracker_server = 192.168.239.136:22122
  • tracker_server = 192.168.239.137:22122

修改group2的storage3的storage.conf:vi /etc/fdfs/storage.conf

  • group_name = group2
  • tracker_server = 192.168.239.136:22122
  • tracker_server = 192.168.239.137:22122

修改group2的storage4的storage.conf:vi /etc/fdfs/storage.conf

  • group_name = group2
  • tracker_server = 192.168.239.136:22122
  • tracker_server = 192.168.239.137:22122

3.5、mod_fastdfs配置

修改group1的nginx1的mod_fastdfs.conf:vi /etc/fdfs/mod_fastdfs.conf

  • tracker_server = 192.168.239.136:22122

  • tracker_server = 192.168.239.137:22122

  • group_name = group1

  • group_count = 2

  • 在末尾增加2個組的具體資訊:

    • [group1]
      group_name=group1
      storage_server_port=23000
      store_path_count=1
      store_path0=/opt/fastdfs/storage/files
      
      [group2]
      group_name=group2
      storage_server_port=23000
      store_path_count=1
      store_path0=/opt/fastdfs/storage/files
      

修改group1的nginx2的mod_fastdfs.conf:vi /etc/fdfs/mod_fastdfs.conf

  • tracker_server = 192.168.239.136:22122

  • tracker_server = 192.168.239.137:22122

  • group_name = group1

  • group_count = 2

  • 在末尾增加2個組的具體資訊:

    • [group1]
      group_name=group1
      storage_server_port=23000
      store_path_count=1
      store_path0=/opt/fastdfs/storage/files
      
      [group2]
      group_name=group2
      storage_server_port=23000
      store_path_count=1
      store_path0=/opt/fastdfs/storage/files
      

修改group2的nginx3的mod_fastdfs.conf:vi /etc/fdfs/mod_fastdfs.conf

  • tracker_server = 192.168.239.136:22122

  • tracker_server = 192.168.239.137:22122

  • group_name = group2

  • group_count = 2

  • 在末尾增加2個組的具體資訊:

    • [group1]
      group_name=group1
      storage_server_port=23000
      store_path_count=1
      store_path0=/opt/fastdfs/storage/files
      
      [group2]
      group_name=group2
      storage_server_port=23000
      store_path_count=1
      store_path0=/opt/fastdfs/storage/files
      

修改group2的nginx4的mod_fastdfs.conf:vi /etc/fdfs/mod_fastdfs.conf

  • tracker_server = 192.168.239.136:22122

  • tracker_server = 192.168.239.137:22122

  • group_name = group2

  • group_count = 2

  • 在末尾增加2個組的具體資訊:

    • [group1]
      group_name=group1
      storage_server_port=23000
      store_path_count=1
      store_path0=/opt/fastdfs/storage/files
      
      [group2]
      group_name=group2
      storage_server_port=23000
      store_path_count=1
      store_path0=/opt/fastdfs/storage/files
      

3.6、安裝兩個tracker上的nginx

Nginx洗掉:

注意:之前已經下載了nginx安裝包并解壓安裝了,我們先洗掉以前安裝的,然后再安裝

[root@caochenlei ~]# rm -rf /usr/local/nginx_fdfs

注意:之前已經下載了nginx安裝包并解壓安裝了,我們先洗掉以前解壓的,然后再解壓

Nginx解壓:

[root@caochenlei ~]# rm -rf nginx-1.18.0
[root@caochenlei ~]# tar -zxvf nginx-1.18.0.tar.gz

Nginx安裝:

[root@caochenlei ~]# cd nginx-1.18.0
[root@caochenlei nginx-1.18.0]# ./configure
[root@caochenlei nginx-1.18.0]# make && make install

注意:安裝完成后的路徑為:/usr/local/nginx

Nginx配置:

[root@caochenlei nginx-1.18.0]# vi /usr/local/nginx/conf/nginx.conf
#配置負載均衡
upstream fastdfs_storage_server {
    server 192.168.239.138:80;
    server 192.168.239.139:80;
    server 192.168.239.140:80;
    server 192.168.239.141:80;
}

#配置請求路徑
location ~ /group[1-9]/M0[0-9] {
    proxy_pass http://fastdfs_storage_server;
}

Nginx命令:

  • 普通啟動服務:/usr/local/nginx/sbin/nginx
  • 組態檔啟動:/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
  • 暴力停止服務:/usr/local/nginx/sbin/nginx -s stop
  • 優雅停止服務:/usr/local/nginx/sbin/nginx -s quit
  • 檢查組態檔:/usr/local/nginx/sbin/nginx -t
  • 重新加載配置:/usr/local/nginx/sbin/nginx -s reload
  • 查看相關行程:ps -ef | grep nginx

3.7、啟動所有服務進行測驗

啟動兩個tracker:

[root@caochenlei nginx-1.18.0]# fdfs_trackerd /etc/fdfs/tracker.conf

啟動四個storage:

[root@caochenlei ~]# fdfs_storaged /etc/fdfs/storage.conf

啟動四個storage上的nginx:

[root@caochenlei ~]# /usr/local/nginx_fdfs/sbin/nginx -c /usr/local/nginx_fdfs/conf/nginx.conf

啟動兩個tracker上的nginx:

[root@caochenlei nginx-1.18.0]# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

打開瀏覽器,輸入以下幾個地址進行測驗:

注意:wKjvgF9TlZeAZpeTAAAADtHNnrs395.txt這個檔案是我們之前測驗的檔案,已經上傳上去了

兩個tracker上的訪問:

http://192.168.239.136/group1/M00/00/00/wKjvgF9TlZeAZpeTAAAADtHNnrs395.txt
http://192.168.239.137/group1/M00/00/00/wKjvgF9TlZeAZpeTAAAADtHNnrs395.txt

四個storage上的訪問:

http://192.168.239.138/group1/M00/00/00/wKjvgF9TlZeAZpeTAAAADtHNnrs395.txt
http://192.168.239.139/group1/M00/00/00/wKjvgF9TlZeAZpeTAAAADtHNnrs395.txt
http://192.168.239.140/group1/M00/00/00/wKjvgF9TlZeAZpeTAAAADtHNnrs395.txt
http://192.168.239.141/group1/M00/00/00/wKjvgF9TlZeAZpeTAAAADtHNnrs395.txt

3.8、統一訪問路徑

我們需要使用一臺單獨的nginx為兩臺tracker進行負載均衡,以此達到統一訪問路徑,這回配置的是192.168.239.142這臺機器

Nginx洗掉:

注意:之前已經下載了nginx安裝包并解壓安裝了,我們先洗掉以前安裝的,然后再安裝

[root@caochenlei ~]# rm -rf /usr/local/nginx_fdfs

注意:之前已經下載了nginx安裝包并解壓安裝了,我們先洗掉以前解壓的,然后再解壓

Nginx解壓:

[root@caochenlei ~]# rm -rf nginx-1.18.0
[root@caochenlei ~]# tar -zxvf nginx-1.18.0.tar.gz

Nginx安裝:

[root@caochenlei ~]# cd nginx-1.18.0
[root@caochenlei nginx-1.18.0]# ./configure
[root@caochenlei nginx-1.18.0]# make && make install

注意:安裝完成后的路徑為:/usr/local/nginx

Nginx配置:

[root@caochenlei nginx-1.18.0]# vi /usr/local/nginx/conf/nginx.conf
#配置負載均衡
upstream fastdfs_tracker_server {
    server 192.168.239.136:80;
    server 192.168.239.137:80;
}

#配置請求路徑
location ~ /group[1-9]/M0[0-9] {
    proxy_pass http://fastdfs_tracker_server;
}

Nginx命令:

  • 普通啟動服務:/usr/local/nginx/sbin/nginx
  • 組態檔啟動:/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
  • 暴力停止服務:/usr/local/nginx/sbin/nginx -s stop
  • 優雅停止服務:/usr/local/nginx/sbin/nginx -s quit
  • 檢查組態檔:/usr/local/nginx/sbin/nginx -t
  • 重新加載配置:/usr/local/nginx/sbin/nginx -s reload
  • 查看相關行程:ps -ef | grep nginx

Nginx啟動:

[root@caochenlei nginx-1.18.0]# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
[root@caochenlei nginx-1.18.0]# ps -ef | grep nginx
root       6309      1  0 11:20 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
nobody     6310   6309  0 11:20 ?        00:00:00 nginx: worker process                                          
root       6312   3016  0 11:20 pts/1    00:00:00 grep nginx

Nginx測驗:

打開瀏覽器,輸入http://192.168.239.142/group1/M00/00/00/wKjvgF9TlZeAZpeTAAAADtHNnrs395.txt,如果看到內容證明環境已經搭建完畢!

3.9、如何保證主nginx高可用

請您參考:學習Nginx這一篇就夠了中的高可用集群如何搭建的,

原理說明:準備兩臺Nginx,一主一備,每一臺上安裝一個keepalived,由keepalived負責監測當前機器上的nginx是否可用(其實是呼叫一個shell腳本不停的查看行程資訊),同時也會在兩個keepalived之間進行心跳監測,如果主節點宕機,則從節點自動跟上,訪問是通過虛擬IP進行訪問的,

第四章 FastDFS的JavaAPI

注意:我們進行JavaAPI測驗采用的是單實體版本,所以要將之前的單實體版本啟動并把所有服務開啟,這一步請參考第二章,還有一個問題需要注意,虛擬機的IP地址可能變化,如果一旦發生變化,那么之前配置的FastDFS就肯定不會啟動成功,雖然我們會配置了,但是我們為了偷懶,臨時把IP地址修改為之前的那個IP地址,命令如下(命令生效,請重啟一個視窗連接):

ifconfig eth0 192.168.239.128 netmask 255.255.255.0

4.1、工程搭建

工程名稱:FastDFSDemo

專案依賴:

  • fastdfs-client-java-1.28-SNAPSHOT.jar
  • log4j-1.2.17.jar
  • slf4j-api-1.7.26.jar
  • slf4j-log4j12-1.7.26.jar

單元測驗:Junit 4

測驗包名:com.caochenlei.fastdfs.demo

組態檔:在src中創建 fdfs_client.conf

connect_timeout = 2
network_timeout = 30
charset = UTF-8
http.tracker_http_port = 8080
http.anti_steal_token = no
http.secret_key = FastDFS1234567890

tracker_server = 192.168.239.128:22122

connection_pool.enabled = true
connection_pool.max_count_per_entry = 500
connection_pool.max_idle_time = 3600
connection_pool.max_wait_time_in_ms = 1000

4.2、上傳檔案

FastDFSDemo.java(全路徑:/FastDFSDemo/src/com/caochenlei/fastdfs/demo/FastDFSDemo.java)

/**
 * 上傳檔案
 */
@Test
public void fileUpload() {
	try {
		// 1.加載組態檔,默認去classpath下加載
		ClientGlobal.init("fdfs_client.conf");
		// 2.創建TrackerClient物件
		TrackerClient trackerClient = new TrackerClient();
		// 3.創建TrackerServer物件
		TrackerServer trackerServer = trackerClient.getTrackerServer();
		// 4.創建StorageServler物件
		StorageServer storageServer = trackerClient.getStoreStorage(trackerServer);
		// 5.創建StorageClient物件,這個物件完成對檔案的操作
		StorageClient storageClient = new StorageClient(trackerServer, storageServer);
		// 6.上傳檔案(第一個引數:本地檔案路徑、第二個引數:上傳檔案的后綴、第三個引數:檔案資訊)
		String[] uploadArray = storageClient.upload_file("a.txt", "txt", null);
		for (String str : uploadArray) {
			System.out.println(str);
		}
	} catch (IOException e) {
		e.printStackTrace();
	} catch (MyException e) {
		e.printStackTrace();
	}
}

運行結果:

group1
M00/00/00/wKjvgF9UbIGAV0ioAAAAHjXTgjs092.txt

4.3、下載檔案

FastDFSDemo.java(全路徑:/FastDFSDemo/src/com/caochenlei/fastdfs/demo/FastDFSDemo.java)

/**
 * 下載檔案
 */
@Test
public void fileDownload() {
	try {
		// 1.加載組態檔,默認去classpath下加載
		ClientGlobal.init("fdfs_client.conf");
		// 2.創建TrackerClient物件
		TrackerClient trackerClient = new TrackerClient();
		// 3.創建TrackerServer物件
		TrackerServer trackerServer = trackerClient.getTrackerServer();
		// 4.創建StorageServler物件
		StorageServer storageServer = trackerClient.getStoreStorage(trackerServer);
		// 5.創建StorageClient物件,這個物件完成對檔案的操作
		StorageClient storageClient = new StorageClient(trackerServer, storageServer);
		// 6.下載檔案(回傳0表示成功,其它均表示失敗)
		int num = storageClient.download_file("group1", "M00/00/00/wKjvgF9UbIGAV0ioAAAAHjXTgjs092.txt", "b.txt");
		System.out.println(num);
	} catch (IOException e) {
		e.printStackTrace();
	} catch (MyException e) {
		e.printStackTrace();
	}
}

運行結果:

0

4.4、洗掉檔案

FastDFSDemo.java(全路徑:/FastDFSDemo/src/com/caochenlei/fastdfs/demo/FastDFSDemo.java)

/**
 * 洗掉檔案
 */
@Test
public void fileDelete() {
	try {
		// 1.加載組態檔,默認去classpath下加載
		ClientGlobal.init("fdfs_client.conf");
		// 2.創建TrackerClient物件
		TrackerClient trackerClient = new TrackerClient();
		// 3.創建TrackerServer物件
		TrackerServer trackerServer = trackerClient.getTrackerServer();
		// 4.創建StorageServler物件
		StorageServer storageServer = trackerClient.getStoreStorage(trackerServer);
		// 5.創建StorageClient物件,這個物件完成對檔案的操作
		StorageClient storageClient = new StorageClient(trackerServer, storageServer);
		// 6.洗掉檔案(回傳0表示成功,其它均表示失敗)
		int num = storageClient.delete_file("group1", "M00/00/00/wKjvgF9UbIGAV0ioAAAAHjXTgjs092.txt");
		System.out.println(num);
	} catch (IOException e) {
		e.printStackTrace();
	} catch (MyException e) {
		e.printStackTrace();
	}
}

運行結果:

0

轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/1350.html

標籤:其他

上一篇:快取加速--Squid代理服務器應用(ACL 訪問控制、 Squid 日志分析、反向代理)

下一篇:!!Apache構建虛擬web主機

標籤雲
其他(157675) Python(38076) JavaScript(25376) Java(17977) C(15215) 區塊鏈(8255) C#(7972) AI(7469) 爪哇(7425) MySQL(7132) html(6777) 基礎類(6313) sql(6102) 熊猫(6058) PHP(5869) 数组(5741) R(5409) Linux(5327) 反应(5209) 腳本語言(PerlPython)(5129) 非技術區(4971) Android(4554) 数据框(4311) css(4259) 节点.js(4032) C語言(3288) json(3245) 列表(3129) 扑(3119) C++語言(3117) 安卓(2998) 打字稿(2995) VBA(2789) Java相關(2746) 疑難問題(2699) 细绳(2522) 單片機工控(2479) iOS(2429) ASP.NET(2402) MongoDB(2323) 麻木的(2285) 正则表达式(2254) 字典(2211) 循环(2198) 迅速(2185) 擅长(2169) 镖(2155) 功能(1967) .NET技术(1958) Web開發(1951) python-3.x(1918) HtmlCss(1915) 弹簧靴(1913) C++(1909) xml(1889) PostgreSQL(1872) .NETCore(1853) 谷歌表格(1846) Unity3D(1843) for循环(1842)

熱門瀏覽
  • 面試突擊第一季,第二季,第三季

    第一季必考 https://www.bilibili.com/video/BV1FE411y79Y?from=search&seid=15921726601957489746 第二季分布式 https://www.bilibili.com/video/BV13f4y127ee/?spm_id_fro ......

    uj5u.com 2020-09-10 05:35:24 more
  • 第三單元作業總結

    1.前言 這應該是本學期最后一次寫作業總結了吧。總體來說,對作業的節奏也差不多掌握了,作業做起來的效率也更高了。雖然和之前的作業一樣,作業中都要用到新的知識,但是相比之前,更加懂得了如何利用工具以及資料。雖然之間卡過殼,但總體而言,這幾次作業還算完成的比較好。 2.作業程序總結 相比前兩個單元,此單 ......

    uj5u.com 2020-09-10 05:35:41 more
  • 北航OO(2020)第四單元博客作業暨課程總結博客

    北航OO(2020)第四單元博客作業暨課程總結博客 本單元作業的架構設計 在本單元中,由于UML圖具有比較清晰的樹形結構,因此我對其中需要進行查詢操作的元素進行了包裝,在樹的父節點中存盤所有孩子的參考。考慮到性能問題,我采用了快取機制,一次查詢后盡可能快取已經遍歷過的資訊,以減少遍歷次數。 本單元我 ......

    uj5u.com 2020-09-10 05:35:48 more
  • BUAA_OO_第四單元

    一、UML決議器設計 ? 先看下題目:第四單元實作一個基于JDK 8帶有效性檢查的UML(Unified Modeling Language)類圖,順序圖,狀態圖分析器 MyUmlInteraction,實際上我們要建立一個有向圖模型,UML中的物件(元素)可能與同級元素連接,也可與低級元素相連形成 ......

    uj5u.com 2020-09-10 05:35:54 more
  • 6.1邏輯運算子

    邏輯運算子 1. && 短路與 運算式1 && 運算式2 01.運算式1為true并且運算式2也為true 整體回傳為true 02.運算式1為false,將不會執行運算式2 整體回傳為false 03.只要有一個運算式為false 整體回傳為false 2. || 短路或 運算式1 || 運算式2 ......

    uj5u.com 2020-09-10 05:35:56 more
  • BUAAOO 第四單元 & 課程總結

    1. 第四單元:StarUml檔案決議 本單元采用了圖模型決議UML。 UML檔案可以抽象為圖、子圖、邊的邏輯結構。 在實作中,圖的節點包括類、介面、屬性,子圖包括狀態圖、順序圖等。 采用了三次遍歷UML元素的方法建圖,第一遍遍歷建點,第二、三次遍歷設定屬性、連邊,實作圖物件的初始化。這里借鑒了一些 ......

    uj5u.com 2020-09-10 05:36:06 more
  • 談談我對C# 多型的理解

    面向物件三要素:封裝、繼承、多型。 封裝和繼承,這兩個比較好理解,但要理解多型的話,可就稍微有點難度了。今天,我們就來講講多型的理解。 我們應該經常會看到面試題目:請談談對多型的理解。 其實呢,多型非常簡單,就一句話:呼叫同一種方法產生了不同的結果。 具體實作方式有三種。 一、多載 多載很簡單。 p ......

    uj5u.com 2020-09-10 05:36:09 more
  • Python 資料驅動工具:DDT

    背景 python 的unittest 沒有自帶資料驅動功能。 所以如果使用unittest,同時又想使用資料驅動,那么就可以使用DDT來完成。 DDT是 “Data-Driven Tests”的縮寫。 資料:http://ddt.readthedocs.io/en/latest/ 使用方法 dd. ......

    uj5u.com 2020-09-10 05:36:13 more
  • Python里面的xlrd模塊詳解

    那我就一下面積個問題對xlrd模塊進行學習一下: 1.什么是xlrd模塊? 2.為什么使用xlrd模塊? 3.怎樣使用xlrd模塊? 1.什么是xlrd模塊? ?python操作excel主要用到xlrd和xlwt這兩個庫,即xlrd是讀excel,xlwt是寫excel的庫。 今天就先來說一下xl ......

    uj5u.com 2020-09-10 05:36:28 more
  • 當我們創建HashMap時,底層到底做了什么?

    jdk1.7中的底層實作程序(底層基于陣列+鏈表) 在我們new HashMap()時,底層創建了默認長度為16的一維陣列Entry[ ] table。當我們呼叫map.put(key1,value1)方法向HashMap里添加資料的時候: 首先,呼叫key1所在類的hashCode()計算key1 ......

    uj5u.com 2020-09-10 05:36:38 more
最新发布
  • 【中介者設計模式詳解】C/Java/JS/Go/Python/TS不同語言實作

    * 中介者模式是一種行為型設計模式,它可以用來減少類之間的直接依賴關系,
    * 將物件之間的通信封裝到一個中介者物件中,從而使得各個物件之間的關系更加松散。
    * 在中介者模式中,物件之間不再直接相互互動,而是通過中介者來中轉訊息。 ......

    uj5u.com 2023-04-20 08:20:47 more
  • 露天煤礦現場調研和交流案例分享

    他們集團的資訊化公司及研究院在一個礦區正在做智能礦山的統一平臺的 試點,專案投資大概1億,包括了礦山的各方面的內容,顯示得我們這次交流有點多余。他們2年前開始做智能礦山的規劃,有很多煤礦行業專家的加持,他們的描述是非常完美,但是去年底應該上線的平臺,現在還沒有看到影子。他們確實有很多場景需求,但是被... ......

    uj5u.com 2023-04-20 08:20:25 more
  • 《社區人員管理》實戰案例設計&個人案例分享

    設計是一個讓人夢想成真程序,開始編碼、測驗、除錯之前進行需求分析和架構設計,才能保證關鍵方面都做正確 ......

    uj5u.com 2023-04-20 08:20:17 more
  • 軟體架構生態化-多角色交付的探索實踐

    作為一個技術架構師,不僅僅要緊跟行業技術趨勢,還要結合研發團隊現狀及痛點,探索新的交付方案。在日常中,你是否遇到如下問題 “ 業務需求排期長研發是瓶頸;非研發角色感受不到研發技改提效的變化;引入ISV 團隊又擔心質量和安全,培訓周期長“等等,基于此我們探索了一種新的技術體系及交付方案來解決如上問題。 ......

    uj5u.com 2023-04-20 08:20:10 more
  • 【中介者設計模式詳解】C/Java/JS/Go/Python/TS不同語言實作

    * 中介者模式是一種行為型設計模式,它可以用來減少類之間的直接依賴關系,
    * 將物件之間的通信封裝到一個中介者物件中,從而使得各個物件之間的關系更加松散。
    * 在中介者模式中,物件之間不再直接相互互動,而是通過中介者來中轉訊息。 ......

    uj5u.com 2023-04-20 08:19:44 more
  • 露天煤礦現場調研和交流案例分享

    他們集團的資訊化公司及研究院在一個礦區正在做智能礦山的統一平臺的 試點,專案投資大概1億,包括了礦山的各方面的內容,顯示得我們這次交流有點多余。他們2年前開始做智能礦山的規劃,有很多煤礦行業專家的加持,他們的描述是非常完美,但是去年底應該上線的平臺,現在還沒有看到影子。他們確實有很多場景需求,但是被... ......

    uj5u.com 2023-04-20 08:19:07 more
  • 《社區人員管理》實戰案例設計&個人案例分享

    設計是一個讓人夢想成真程序,開始編碼、測驗、除錯之前進行需求分析和架構設計,才能保證關鍵方面都做正確 ......

    uj5u.com 2023-04-20 08:18:57 more
  • 軟體架構生態化-多角色交付的探索實踐

    作為一個技術架構師,不僅僅要緊跟行業技術趨勢,還要結合研發團隊現狀及痛點,探索新的交付方案。在日常中,你是否遇到如下問題 “ 業務需求排期長研發是瓶頸;非研發角色感受不到研發技改提效的變化;引入ISV 團隊又擔心質量和安全,培訓周期長“等等,基于此我們探索了一種新的技術體系及交付方案來解決如上問題。 ......

    uj5u.com 2023-04-20 08:18:49 more
  • 05單件模式

    #經典的單件模式 public class Singleton { private static Singleton uniqueInstance; //一個靜態變數持有Singleton類的唯一實體。 // 其他有用的實體變數寫在這里 //構造器宣告為私有,只有Singleton可以實體化這個類! ......

    uj5u.com 2023-04-19 08:42:51 more
  • 【架構與設計】常見微服務分層架構的區別和落地實踐

    軟體工程的方方面面都遵循一個最基本的道理:沒有銀彈,架構分層模型更是如此,每一種都有各自優缺點,所以請根據不同的業務場景,并遵循簡單、可演進這兩個重要的架構原則選擇合適的架構分層模型即可。 ......

    uj5u.com 2023-04-19 08:42:41 more