分佈式文件系統怎麼做?終於有個人把分佈式文件上傳講清楚了

{"type":"doc","content":[{"type":"heading","attrs":{"align":null,"level":1},"content":[{"type":"text","text":"FastDFS概念","attrs":{}}]},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"FastDFS是開源的輕量級分佈式文件系統,實現","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"文件管理,","attrs":{}},{"type":"text","text":" 主要功能:","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"文件存儲","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"文件同步","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"文件訪問","attrs":{}},{"type":"text","text":"(文件上傳,文件下載)","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"解決了大容量存儲和負載均衡的問題,特別適合以文件爲載體的在線服務:相冊網站,視頻網站","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"FastDFS爲互聯網量身定製,充分考慮了","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"冗餘備份,負載均衡,線性擴容","attrs":{}},{"type":"text","text":"等機制,並注重高可用,高性能等指標,使用FastDFS可以很方便地搭建一套高性能的","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"文件服務器集羣","attrs":{}},{"type":"text","text":"提供文件上傳,下載等服務","attrs":{}}]}]}],"attrs":{}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"FastDFS文件系統架構","attrs":{}}]},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"FastDFS服務端有兩個角色:","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"跟蹤器(tracker):","attrs":{}},{"type":"text","text":" 主要做調度工作,在訪問上起負載均衡作用","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":2,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"跟蹤器和存儲節點都可以由一臺服務器或多臺服務器構成,跟蹤器和存儲節點中的服務器可以隨時增加或下線而不會影響下線服務.","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":2,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"跟蹤器中所有服務都是對等的,可以根據服務器的壓力情況隨時增加或減少","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"存儲節點(storage):","attrs":{}},{"type":"text","text":" 存儲文件,完成文件管理的所有功能","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":2,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"就是這樣的存儲","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":2,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"同步存儲接口","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":2,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"提供存儲接口","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":2,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"FastDFS同時對","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"文件metadata","attrs":{}},{"type":"text","text":"進行管理,文件metadata是文件屬性列表,可以包含多個鍵值對","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":3,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"文件metadata:","attrs":{}},{"type":"text","text":" 文件的相關屬性,以鍵值對方式表示","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":3,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"爲了支持大容量,存儲節點採用分卷的組織方式","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":4,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"存儲系統由一個卷或多個卷組成,卷與卷之間的文件是相互獨立的,所有卷的文件容量累加就是整個存儲系統的文件容量","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":4,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"一個卷可以由一臺或多臺存儲服務器組成,一個卷下的存儲服務器中文件都是相同的,卷中的多臺服務器起到了冗餘備份和負載均衡作用","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":4,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在卷中增加服務器時,同步已有的文件由系統自動完成,同步完成後,系統自動將新增服務器切換到線上提供服務","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":4,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"當存儲空間不足或即將耗盡時,可以動態添加捲,只需要增加一臺或多臺服務器,配置一個新的卷,這樣擴大存儲系統的容量","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":4,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"FastDFS ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"文件標識","attrs":{}},{"type":"text","text":"分爲兩部分:","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":5,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"卷名","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":5,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"文件名","attrs":{}}]}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"高可用要有崩潰恢復的能力\n服務集羣要有同步的功能\n否則就要有負載均衡\n","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"上傳交互過程","attrs":{}}]},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"client詢問tracker上傳到的storage,不需要附加參數","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"tracker返回一臺可用的storage","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"client直接和storage通訊完成文件上傳","attrs":{}}]}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"client爲使用FastDFS的調用方,client也是一臺服務器,對tracker和對storage的調用均爲服務器間的調用\n","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"下載交互過程","attrs":{}}]},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"client詢問tracker下載文件的storage,參數爲文件標識(卷名和文件名)","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"tracker返回一臺可用的storage","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"client直接和storage通訊完成文件下載","attrs":{}}]}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"client爲使用FastDFS的調用方,client也是一臺服務器,對tracker和對storage的調用均爲服務器間的調用\n","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"FastDFS結合Nginx","attrs":{}}]},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"使用FastDFS部署分佈式文件系統時,通過FastDFS的客戶端API進行文件的上傳,下載,刪除等操作,同時通過FastDFS和HTTP服務器來提供HTTP服務.但是FastDFS的HTTP服務較爲簡單,無法提供負載均衡等高性能的服務.需要使用FastDFS的Nginx模塊彌補這一缺陷","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"FastDFS通過tracker服務器,將文件放在storage服務器存儲,但是同組之間的服務器需要複製文件,有延遲的問題,可以通過fastdfs-nginx-module可以重定向連接到源服務器取文件,避免客戶端由於複製延遲的問題,出現錯誤","attrs":{}}]}]}],"attrs":{}},{"type":"heading","attrs":{"align":null,"level":1},"content":[{"type":"text","text":"基於Docker安裝FastDFS","attrs":{}}]},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"環境準備:","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"libfastcommon:","attrs":{}},{"type":"text","text":" FastDFS分離出的一些公用函數包","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"FastDFS:","attrs":{}},{"type":"text","text":" FastDFS本體","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"fastdfs-nginx-module:","attrs":{}},{"type":"text","text":" FastDFS和nginx的關聯模塊","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"nginx:","attrs":{}},{"type":"text","text":" nginx1.15.4","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"創建工作目錄:","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在Linux中創建","attrs":{}}]}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":" /usr/local/docker/fastdfs/environment\n\n /usr/local/docker/fastdfs:用於存放docker-compose.yml配置文件及FastDFS數據卷\n /usr/local/docker/fastdfs/environment:用於存放Dockerfile鏡像配置文件及FastDFS所需環境\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"在 /usr/local/docker/fastdfs/environment目錄中創建Dockerfile","attrs":{}}]}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"dockerfile"},"content":[{"type":"text","text":"# 更新數據源\nWORKDIR /etc/apt\nRUN echo 'deb http://mirrors.aliyun.com/ubuntu/ xenial main restricted universe multiverse' > sources.list\nRUN echo 'deb http://mirrors.aliyun.com/ubuntu/ xenial-security main restricted universe multiverse' >> sources.list\nRUN echo 'deb http://mirrors.aliyun.com/ubuntu/ xenial-updates main restricted universe multiverse' > sources.list\nRUN echo 'deb http://mirrors.aliyun.com/ubuntu/ xenial-backports main restricted universe multiverse' > sources.list\nRUN apt-get update\n\n# 安裝依賴\nRUN apt-get install make gcc libpcre3-dev zliblg-dev --assume-yes\n\n# 複製工具包\nADD fastdfs-5.11.tar.gz /usr/local/src\nADD fastdfs-nginx-module_v1.16.tar.gz /usr/local/src\nADD libfastcommon.tar.gz /usr/local/src\nADD nginx-1.15.4.tar.gz /usr/local/src\n\n# 安裝libfastcommon\nWORKDIR /usr/local/src/libfastcommon\nRUN ./make.sh && ./make.sh install\n\n# 安裝 FastDFS\nWORKDIR /usr/local/src/fastdfs-5.11\nRUN ./make.sh && ./make.sh install\n\n# 配置FastDFS tracker\nADD tracker.conf /etc/fdfs\nRUN mkdir -p /fastdfs/tracker\n\n# 配置FastDFS storage\nADD storage.conf /etc/fdfs\nRUN mkdir -p /fastdfs/storage\n\n# 配置FastDFS客戶端\nADD client.conf /etc/fdfs\n\n# 配置fastdfs-nginx-module\nADD config /usr/local/src/fastdfs-nginx-modules/src\n\n# FastDFS與Nginx集成\nWORKDIR /usr/local/src/nginx-1.13.6\nRUN ./configure --add-module=/usr/local/src/fastdfs-nginx-module/src\nRUN make && make install\nADD mod_fastdfs.conf /etc/fdfs\n\nWORKDIR /usr/local/src/fastdfs-5.11/conf\nRUN cp http.conf mime.types /etc/fdfs/\n\n# 配置Nginx\nADD nginx.conf /usr/local/nginx/conf\n\nCOPY entrypoint.sh /usr/local/bin/\nENTRYPOINT [\"/usr/local/bin/entrypoint.sh\"]\n\nWORKDIR /\nEXPOSE 8888\nCMD [\"/bin/bash\"]\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"在/usr/local/docker/fastdfs/environment創建entrypoint.sh,創建完成的後要執行 chmod +x entrypoint.sh命令文件纔可以使用","attrs":{}}]}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"shell"},"content":[{"type":"text","text":"# !/bin/sh\n/etc/init.d/fdfs_trackerd start\n/etc/init.d/fdfs_storaged start\n/usr/local/nginx/sbin/nginx -g 'daemon off;'\n","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"相關配置文件","attrs":{}}]},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"tracker.conf:","attrs":{}},{"type":"text","text":" FastDFS跟蹤器配置,容器路徑爲:/etc/fdfs,修改:","attrs":{}}]}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"conf"},"content":[{"type":"text","text":"base_path=/fastdfs/tracker\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"storage.conf:","attrs":{}},{"type":"text","text":" FastDFS存儲節點配置,容器路徑爲:/etc/fdfs,修改:","attrs":{}}]}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"conf"},"content":[{"type":"text","text":"base_path=/fastdfs/storage\nstore_path0=/fastdfs/storage\ntracker_server=192.168.32.255:22122\nhttp.server_port=8888\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"client.conf:","attrs":{}},{"type":"text","text":" FastDFS客戶端配置,容器中路徑爲:/etc/fdfs,修改:","attrs":{}}]}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"conf"},"content":[{"type":"text","text":"base_path=/fastdfs/tracker\ntracker_server=192.168.32.255:22122\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"config:","attrs":{}},{"type":"text","text":" fastdfs-nginx-module配置文件,容器中路徑爲:/usr/local/src/fastdfs-nginx-module/src,修改:","attrs":{}}]}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"conf"},"content":[{"type":"text","text":"# 修改前\nCORE_INCS=\"$CORE_INCS /usr/local/include/fastdfs /usr/local/include/fastcommon/\"\nCORE_LIBS=\"$CORE_LIBS -L/usr/local/lib -lfastcommon -lfdfsclient\"\n\n# 修改後\nCORE_INCS=\"$CORE_INCS /usr/include/fastdfs /usr/include/fastcommon/\"\nCORE_LIBS=\"$CORE_LIBS -L/usr/lib -lfastcommon -lfdfsclient\"\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"mod_fastdfs.conf:","attrs":{}},{"type":"text","text":" fastdfs-nginx-module配置文件,容器中路徑:/usr/local/src/fastdfs-nginx-module/src,修改:","attrs":{}}]}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"conf"},"content":[{"type":"text","text":"connect_timeout=10\ntracker_server=192.168.75.128:22122\nurl_have_group_name=true\nstore_path0=/fastdfs/storage\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"nginx.conf:","attrs":{}},{"type":"text","text":" Nginx配置文件,容器中的路徑爲:/usr/local/src/nginx-1.15.4/conf,修改:","attrs":{}}]}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"conf"},"content":[{"type":"text","text":"user root;\nworker_processes 1;\n\nevents {\n worker_connections 1024;\n}\n\nhttp{\n include mime.types;\n defaulte_type application/octet-stream;\n\n sendfile on;\n\n keepalive_timeout 65;\n\n server{\n listen 8888;\n server_name localhost;\n\n location ~/group([0-9])/M00{\n ngx_fastddfs_module;\n }\n\n error_page 500 502 503 504 /50x.html\n location = /50x.html {\n root html;\n }\n }\n}\n","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"啓動容器","attrs":{}}]},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"docker-compose.yml:","attrs":{}},{"type":"text","text":" 在/usr/local/docker/fastdfs文件夾中創建docker-compose.yml","attrs":{}}]}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"yml"},"content":[{"type":"text","text":"version: '3.1'\nservices:\n fastdfs:\n build: environment\n restart: always\n container_name: fastdfs\n volumes:\n - ./storage:/fastdfs/storage\n network_mode: host # 網絡模式:主機模式--將所有端口映射到主機,Docker容器與宿主機共享端口,即端口一致\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"執行命令,使文件編譯生效","attrs":{}}]}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"linux"},"content":[{"type":"text","text":"docker-compose up -d\n","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"測試上傳","attrs":{}}]},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"交互式進入容器:","attrs":{}}]}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"linux"},"content":[{"type":"text","text":"docker exec -it fastdfs /bin/bash\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"測試文件上傳:","attrs":{}},{"type":"text","text":" 在/usr/bin目錄中執行(","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"第1個","attrs":{}},{"type":"text","text":"是二進制可執行文件客戶端,","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"第2個","attrs":{}},{"type":"text","text":"是客戶端的客戶端配置文件,","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"第3個","attrs":{}},{"type":"text","text":"是需要上傳的文件)","attrs":{}}]}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"linux"},"content":[{"type":"text","text":"/usr/bin/fdfs_upload_file /etc/fdfs/client.conf /usr/local/src/fastdfs-5.11/INSTALL\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"服務器反饋上傳地址:","attrs":{}},{"type":"text","text":" 文件的上傳路徑(非地址),通過在瀏覽器輸入","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"Ngnix的訪問地址+文件上傳路徑","attrs":{}},{"type":"text","text":"即可訪問服務器上的文件","attrs":{}}]}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"linux"},"content":[{"type":"text","text":"group1/M00/00/00/wKliyyfhHHkjsio986777\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"測試Nginx訪問:","attrs":{}},{"type":"text","text":" 通過在瀏覽器輸入","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"Ngnix的訪問地址+文件上傳路徑","attrs":{}},{"type":"text","text":"即可訪問服務器上的文件","attrs":{}}]}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"url"},"content":[{"type":"text","text":"http://192.168.32.255:8888/group1/M00/00/00/wKliyyfhHHkjsio986777\n","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":1},"content":[{"type":"text","text":"配置 FastDFS Java客戶端","attrs":{}}]},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"創建項目:","attrs":{}},{"type":"text","text":" 創建項目名爲myshop-service-upload的服務提供者項目","attrs":{}}]}]}],"attrs":{}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"安裝FastDFS Java客戶端","attrs":{}}]},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"從github上git clone FastDFS項目代碼:","attrs":{}}]}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"git"},"content":[{"type":"text","text":"git clone https://github.com/happyfish100/fastdfs-client-java.git\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"配置到本地倉庫:","attrs":{}},{"type":"text","text":" 在項目目錄的target包下有項目的jar文件","attrs":{}}]}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"cmd"},"content":[{"type":"text","text":"mvn clean install\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"將項目jar文件上傳到Nexus中","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"在項目中添加依賴:","attrs":{}}]}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"xml"},"content":[{"type":"text","text":"\n\n org.csource\n fastdfs-client-java\n 1.27-SNAPSHOT\n\n","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"創建FastDFS工具類","attrs":{}}]},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"定義文件存儲服務接口:","attrs":{}}]}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"java"},"content":[{"type":"text","text":"package com.oxford.myshop.service.upload.fastdfs;\n\npublic interface StorageService{\n /**\n *上傳文件\n *\n *@param data 文件的二進制符\n *@param extName 擴展名\n *@return 上傳成功後返回生成文件的id,失敗則返回null\n */\n public String upload(byte[] data,String extName);\n \n /**\n *刪除文件\n *\n *@param fileId 被刪除的文件id\n *@return 刪除成功後返回0,失敗後返回錯誤代碼\n */\n public int delete(String fileId);\n}\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"實現文件存儲服務接口:","attrs":{}}]}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"java"},"content":[{"type":"text","text":"public class FastDFSStorageService implements StorageService,InitializingBean{\n private static final Logger logger=LoggerFactory.getLogger(FastDFSStorageService.class);\n\n private TrackerClient trackerClient;\n\n @Value(\"${storage.fastdfs.tracker_server}\")\n\n @Override\n public String upload(byte[] data,String extName){\n TrackerServer trackerServer=null;\n StorageServer storageServer=null;\n StorageClient storageClient=null;\n try{\n NameValuePair[] meta_list=null; // new NameValuePair[0]\n\n trackerServer=trackerClient.getConnection();\n if(trackerServer==null){\n logger.error(\"getConnection return null\");\n }\n storageServer=trackerClient.getStoreStorage(trackerServer);\n storageClient1=new StorageClient1(trackerServer,storageServer);\n String fileId=storageClient1.upload_file1(data,extName,meta_list);\n logger.debug(\"uploaded file \",fileId);\n return fileId;\n }catch(Exception ex){\n logger.error(\"Uploaded fail\",ex);\n return null;\n }finally{\n if(storageServer!=null){\n try{\n storageServer.close();\n }catch(IOException e){\n e.printStackTrace();\n }\n }\n if(trackeServer!=null){\n try{\n trackeServer.close();\n }catch(IOException e){\n e.printStackTrace();\n }\n }\n storageClient1=null;\n }\n }\n\n @Override\n public int delete(String fileId){\n TrackerServer trackerServer=null;\n StorageServer storageServer=null;\n StorageClient storageClient=null;\n int index=fileId.indexOf('/');\n String groupName=fileId.substring(0,index);\n try{\n trackerServer=trackerClient.getConnection();\n if(trackerServer==null){\n logger.error(\"getConnection return null\");\n }\n storageServer=trackerClient.getStoreStorage(trackerServer,groupName);\n storageClient1=new StorageClient1(trackerServer,storageServer);\n int result=storageClient1.delete_file1(fileId);\n return result;\n }catch(Exception ex){\n logger.error(\"Delete fail\",ex);\n return 1;\n }finally{\n ifreturn fileId;\n }catch(Exception ex){\n logger.error(\"Uploaded fail\",ex);\n return null;\n }finally{\n if(storageServer!=null){\n try{\n storageServer.close();\n }catch(IOException e){\n e.printStackTrace();\n }\n }\n if(trackeServer!=null){\n try{\n trackeServer.close();\n }catch(IOException e){\n e.printStackTrace();\n }\n }\n storageClient1=null;\n }\n }\n @Override\n public void afterPropertiesSet() throws Exxception{\n File confFile=File.createTempFile(\"fastdfs\",\".conf\");\n PrintWriter confWriter=new PrintWriter(new FileWriter(confFile));\n confWriter.println(\"tracker_server=\"+trackerServer);\n confWriter.close();\n ClientGlobal.init(confFile.getAbsolutePath());\n confFile.delete();\n TrackerGroup trackerGroup=ClientGlobal.g_tracker_group;\n trackerClient=new TrackerClient(trackerGroup)\n\n logger.info(\"Init FastDFS with tracker_server : {}\",trackerServer);\n }\n}\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"文件存儲服務工廠","attrs":{}}]}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"java"},"content":[{"type":"text","text":"public class StorageFactory implements FactoryBean{\n @Autowired\n private AutowireCapableBeanFactory acbf;\n\n /**\n * 存儲服務的類型,僅支持FastDFS\n */\n @Value(\"${storage.type}\")\n private String type;\n \n private Map> classMap;\n\n public StorageFactory(){\n classMap=new HashMap<>();\n classMap.put(\"fastdfs\",FastDFSStorageService.class);\n } \n\n @Override\n public StorageService getObject() throws Exception{\n Class extends StorageService> clazz=classMap.get(type);\n if(class==null){\n throw new RuntimeException(\"Unsupported storage type [\"+type+\"],valid are\"+ classMap.keySet());\n }\n\n StorageService bean=clazz.newInstance();\n acbf.autowireBean(bean);\n acbf.initializeBean(bean,bean.getClass().getSimpleName());\n return bean;\n }\n\n @Override\n public Class> getObjectType(){\n return StorageService.class;\n }\n\n @Override\n public boolean isSingleton(){\n return true;\n }\n}\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"配置文件存儲服務工廠類","attrs":{}}]}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"java"},"content":[{"type":"text","text":"/**\n * Java配置方式定義StorageFactory的bean,使其可以被依賴注入\n */\n @Configuration\n public classs FastDFSConfiguration{\n @Bean\n public StorageFactory storageFactory(){\n return new StorageFactory();\n }\n }\n","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"創建FastDFS控制器","attrs":{}}]},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"增加雲配置:","attrs":{}},{"type":"text","text":" application.yml","attrs":{}}]}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"yml"},"content":[{"type":"text","text":"# SpringBoot Application\nspring:\n application:\n name: myshop-service-upload\n\n# FastDFS Configuration\nfastdfs.base.url: htttp//192.168.32.255:8888/\nstorage:\n type: fastdfs\n fastdfs:\n tracker_server: 192.168.32.255:22122\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"控制器代碼","attrs":{}}]}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"java"},"content":[{"type":"text","text":"@CrossOrigin(origins=\"*\",maxAge=3600)\n@RestController\npublic class UploadController{\n @Value(\"${fastdfs.base.url}\")\n private String FASTDFS_BASE_URL;\n\n @Autowired\n private StorageService storageService;\n\n @RequestMapping(value=\"upload\",method=RequestMethod.POST)\n public Map upload(MultipartFile dropFile,MultipartFile[] editorFiles){\n Map result=new HashMap<>();\n\n //DropZone上傳\n if(dropFile!=null){\n result.put(\"fileName\",writeFile(dropFile));\n }\n\n //wangEditor上傳\n if(editorFiles != null && editorFiles.length > 0){\n List fileNames=new ArrayList<>();\n\n for(MultipartFile editorFile:editorFiles){\n fileNames.add(writeFile(editorFile));\n }\n\n result.put(\"error\",0);\n result.put(\"data\",fileNames);\n }\n return result;\n }\n \n /**\n *將圖片寫入指定目錄\n */\n private String writeFile(MultipartFile multipartFile){\n // 獲取文件後綴\n String oName=multipartFile.getOriginalFilename();\n String exName=oName.substring(oName.lastIndexOf(\".\")+1);\n \n // 文件存放路徑\n String url=null;\n try{\n String uploadUrl=storageService.upload(multipartFile.getBytes(),exName);\n url=FASTDFS_BASE_URL+uploadUrl;\n }catch(IOException e){\n e.printStackTrace();\n }\n \n // 返回文件完整路徑\n return url;\n }\n}\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"創建SpringBoot Application,運行執行分佈式文件上傳項目","attrs":{}}]}]}],"attrs":{}}]}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章