分布式文件系统怎么做?终于有个人把分布式文件上传讲清楚了

{"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":{}}]}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章