Docker + FastDFS + Spring Boot 一键式搭建分布式文件服务器
程序IT圈 文章源自JAVA秀-https://www.javaxiu.com/20442.html
点击上方蓝色字体,选择“标星公众号”文章源自JAVA秀-https://www.javaxiu.com/20442.html
优质文章,第一时间送达文章源自JAVA秀-https://www.javaxiu.com/20442.html
作者:kalibiubiubiu文章源自JAVA秀-https://www.javaxiu.com/20442.html
blog.csdn.net/qq_37759106/article/details/82981023文章源自JAVA秀-https://www.javaxiu.com/20442.html
首先说一下从零开始自己去搭一个fastdfs挺麻烦,后来看到有人把做好的 docker 镜像传出来了,那搭建起来就很容易了文章源自JAVA秀-https://www.javaxiu.com/20442.html
有服务器的可以自己在服务器上玩玩,没有的可以新建一个centos7.5虚拟机玩玩,遇到虚拟机不能上网和换阿里云的源的问题可以参考https://blog.csdn.net/qq_37759106/article/details/82985113这篇文章文章源自JAVA秀-https://www.javaxiu.com/20442.html
PS:更多 Docker 和 Spring Boot 的文章可以关注微信公众号「Java后端」回复「666」下载技术栈手册。文章源自JAVA秀-https://www.javaxiu.com/20442.html
1.第一步安装docker:文章源自JAVA秀-https://www.javaxiu.com/20442.html
在 root 权限下文章源自JAVA秀-https://www.javaxiu.com/20442.html
yum install -y docker-io #安装dockerservice docker star #启动dockerdocker -v # 查看docker版本
文章源自JAVA秀-https://www.javaxiu.com/20442.html2. 拉取镜像文章源自JAVA秀-https://www.javaxiu.com/20442.html
docker pull qbanxiaoli/fastdfs
文章源自JAVA秀-https://www.javaxiu.com/20442.html启动 fastdfs文章源自JAVA秀-https://www.javaxiu.com/20442.html
docker run -d --restart=always --privileged=true --net=host --name=fastdfs -e IP=192.168.127.131 -e WEB_PORT=80 -v ${HOME}/fastdfs:/var/local/fdfs qbanxiaoli/fastdfs
文章源自JAVA秀-https://www.javaxiu.com/20442.htmlIP 后面是你的服务器公网ip或者虚拟机的IP,-e WEB_PORT=80 指定 nginx 端口
测试fastdfs是否搭建成功文章源自JAVA秀-https://www.javaxiu.com/20442.html
docker exec -it fastdfs /bin/bashecho "Hello FastDFS!">index.htmlfdfs_test /etc/fdfs/client.conf upload index.html
文章源自JAVA秀-https://www.javaxiu.com/20442.html文章源自JAVA秀-https://www.javaxiu.com/20442.html
能返回 url 就意见搭建成功文章源自JAVA秀-https://www.javaxiu.com/20442.html
文章源自JAVA秀-https://www.javaxiu.com/20442.html
这样 fastdfs 就搭建好啦文章源自JAVA秀-https://www.javaxiu.com/20442.html
下面进入 Spring Boot 整合部分文章源自JAVA秀-https://www.javaxiu.com/20442.html
<groupId>com.github.tobato</groupId> <artifactId>fastdfs-client</artifactId><version>1.26.2</version>
文章源自JAVA秀-https://www.javaxiu.com/20442.html在 Spring Boot 启动类上加文章源自JAVA秀-https://www.javaxiu.com/20442.html
@Import(FdfsClientConfig.class)@EnableMBeanExport(registration = RegistrationPolicy.IGNORE_EXISTING)
文章源自JAVA秀-https://www.javaxiu.com/20442.html文章源自JAVA秀-https://www.javaxiu.com/20442.html
创建FastDFSClient工具类文章源自JAVA秀-https://www.javaxiu.com/20442.html
package com.yd.client.common; import com.github.tobato.fastdfs.conn.FdfsWebServer;import com.github.tobato.fastdfs.domain.StorePath;import com.github.tobato.fastdfs.proto.storage.DownloadByteArray;import com.github.tobato.fastdfs.service.FastFileStorageClient;import org.apache.commons.io.FilenameUtils;import org.apache.commons.lang3.StringUtils;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Component;import org.springframework.web.multipart.MultipartFile; import java.io.*; @Componentpublic class FastDFSClient { private static Logger log =LoggerFactory.getLogger(FastDFSClient.class); private static FastFileStorageClient fastFileStorageClient; private static FdfsWebServer fdfsWebServer; @Autowired public void setFastDFSClient(FastFileStorageClient fastFileStorageClient, FdfsWebServer fdfsWebServer) { FastDFSClient.fastFileStorageClient = fastFileStorageClient; FastDFSClient.fdfsWebServer = fdfsWebServer; } /** * @param multipartFile 文件对象 * @return 返回文件地址 * @author qbanxiaoli * @description 上传文件 */ public static String uploadFile(MultipartFile multipartFile) { try { StorePath storePath = fastFileStorageClient.uploadFile(multipartFile.getInputStream(), multipartFile.getSize(), FilenameUtils.getExtension(multipartFile.getOriginalFilename()), null); return storePath.getFullPath(); } catch (IOException e) { log.error(e.getMessage()); return null; } } /** * @param multipartFile 图片对象 * @return 返回图片地址 * @author qbanxiaoli * @description 上传缩略图 */ public static String uploadImageAndCrtThumbImage(MultipartFile multipartFile) { try { StorePath storePath = fastFileStorageClient.uploadImageAndCrtThumbImage(multipartFile.getInputStream(), multipartFile.getSize(), FilenameUtils.getExtension(multipartFile.getOriginalFilename()), null); return storePath.getFullPath(); } catch (Exception e) { log.error(e.getMessage()); return null; } } /** * @param file 文件对象 * @return 返回文件地址 * @author qbanxiaoli * @description 上传文件 */ public static String uploadFile(File file) { try { FileInputStream inputStream = new FileInputStream(file); StorePath storePath = fastFileStorageClient.uploadFile(inputStream, file.length(), FilenameUtils.getExtension(file.getName()), null); return storePath.getFullPath(); } catch (Exception e) { log.error(e.getMessage()); return null; } } /** * @param file 图片对象 * @return 返回图片地址 * @author qbanxiaoli * @description 上传缩略图 */ public static String uploadImageAndCrtThumbImage(File file) { try { FileInputStream inputStream = new FileInputStream(file); StorePath storePath = fastFileStorageClient.uploadImageAndCrtThumbImage(inputStream, file.length(), FilenameUtils.getExtension(file.getName()), null); return storePath.getFullPath(); } catch (Exception e) { log.error(e.getMessage()); return null; } } /** * @param bytes byte数组 * @param fileExtension 文件扩展名 * @return 返回文件地址 * @author qbanxiaoli * @description 将byte数组生成一个文件上传 */ public static String uploadFile(byte[] bytes, String fileExtension) { ByteArrayInputStream stream = new ByteArrayInputStream(bytes); StorePath storePath = fastFileStorageClient.uploadFile(stream, bytes.length, fileExtension, null); return storePath.getFullPath(); } /** * @param fileUrl 文件访问地址 * @param file 文件保存路径 * @author qbanxiaoli * @description 下载文件 */ public static boolean downloadFile(String fileUrl, File file) { try { StorePath storePath = StorePath.praseFromUrl(fileUrl); byte[] bytes = fastFileStorageClient.downloadFile(storePath.getGroup(), storePath.getPath(), new DownloadByteArray()); FileOutputStream stream = new FileOutputStream(file); stream.write(bytes); } catch (Exception e) { log.error(e.getMessage()); return false; } return true; } /** * @param fileUrl 文件访问地址 * @author qbanxiaoli * @description 删除文件 */ public static boolean deleteFile(String fileUrl) { if (StringUtils.isEmpty(fileUrl)) { return false; } try { StorePath storePath = StorePath.praseFromUrl(fileUrl); fastFileStorageClient.deleteFile(storePath.getGroup(), storePath.getPath()); } catch (Exception e) { log.error(e.getMessage()); return false; } return true; } // 封装文件完整URL地址 public static String getResAccessUrl(String path) { String url = fdfsWebServer.getWebServerUrl() + path; log.info("上传文件地址为:\n" + url); return url; } }
文章源自JAVA秀-https://www.javaxiu.com/20442.html配置yml文件文章源自JAVA秀-https://www.javaxiu.com/20442.html
# 分布式文件系统fastdfs配置fdfs: # socket连接超时时长 soTimeout: 1500 # 连接tracker服务器超时时长 connectTimeout: 600 pool: # 从池中借出的对象的最大数目 max-total: 153 # 获取连接时的最大等待毫秒数100 max-wait-millis: 102 # 缩略图生成参数,可选 thumbImage: width: 150 height: 150 # 跟踪服务器tracker_server请求地址,支持多个,这里只有一个,如果有多个在下方加- x.x.x.x:port trackerList: - 192.168.127.131:22122 # # 存储服务器storage_server访问地址 web-server-url: http://192.168.127.131/ spring: http: multipart: max-file-size: 100MB # 最大支持文件大小 max-request-size: 100MB # 最大支持请求大小
文章源自JAVA秀-https://www.javaxiu.com/20442.html测试类@RunWith(SpringRunner.class)@SpringBootTestpublic class FileClientApplicationTests { @Test public void Upload() { String fileUrl = this.getClass().getResource("/test.jpg").getPath(); File file = new File(fileUrl); String str = FastDFSClient.uploadFile(file); FastDFSClient.getResAccessUrl(str); } @Test public void Delete() { FastDFSClient.deleteFile("group1/M00/00/00/rBEAClu8OiSAFbN_AAbhXQnXzvw031.jpg"); }}
文章源自JAVA秀-https://www.javaxiu.com/20442.html文章源自JAVA秀-https://www.javaxiu.com/20442.html
运行测试类文章源自JAVA秀-https://www.javaxiu.com/20442.html
文章源自JAVA秀-https://www.javaxiu.com/20442.html
返回的 url 就是能访问的地址文章源自JAVA秀-https://www.javaxiu.com/20442.html
文章源自JAVA秀-https://www.javaxiu.com/20442.html
文章源自JAVA秀-https://www.javaxiu.com/20442.html
技术交流群已成立扫描下方二维码备注 进群 ,很方便!
—————END—————推荐阅读:Nginx 除了负载均衡,还能做什么?因为一条SQL,我差点被祭天......高仿小米商城项目,拿来学习再好不过了!IntelliJ IDEA 2021.1正式发布!GitHub上最热门的Java开源项目最近面试BAT,整理一份面试资料《Java面试BAT通关手册》,覆盖了Java核心技术、JVM、Java并发、SSM、微服务、数据库、数据结构等等。获取方式:关注公众号并回复 java 领取,更多内容陆续奉上。文章源自JAVA秀-https://www.javaxiu.com/20442.html
明天见(。・ω・。)ノ♡文章源自JAVA秀-https://www.javaxiu.com/20442.html
阅读原文文章源自JAVA秀-https://www.javaxiu.com/20442.html

评论