Docker + Spring Boot + FastDFS 搭建一套分布式文件服务器,太强了!
上一篇:使用MySQL,请用好 JSON 这张牌!
首先说一下从零开始自己去搭一个fastdfs挺麻烦,后来看到有人把做好的 docker 镜像传出来了,那搭建起来就很容易了
1.第一步安装docker:
在 root 权限下
yum install -y docker-io #安装docker
service docker star #启动docker
docker -v # 查看docker版本
2. 拉取镜像
docker pull qbanxiaoli/fastdfs
启动 fastdfs
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
IP 后面是你的服务器公网ip或者虚拟机的IP,-e WEB_PORT=80 指定 nginx 端口
测试fastdfs是否搭建成功
docker exec -it fastdfs /bin/bash
echo "Hello FastDFS!">index.html
fdfs_test /etc/fdfs/client.conf upload index.html
能返回 url 就意见搭建成功,搜索公众号互联网架构师回复“2T”,送你一份惊喜礼包。
这样 fastdfs 就搭建好啦。
下面进入 Spring Boot 整合部分
<groupId>com.github.tobato</groupId><artifactId>fastdfs-client</artifactId>
<version>1.26.2</version>
在 Spring Boot 启动类上加:
@Import(FdfsClientConfig.class)
@EnableMBeanExport(registration = RegistrationPolicy.IGNORE_EXISTING)
创建FastDFSClient工具类
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.*;@Component
public class FastDFSClient {private static Logger log =LoggerFactory.getLogger(FastDFSClient.class);private static FastFileStorageClient fastFileStorageClient;private static FdfsWebServer fdfsWebServer;@Autowiredpublic 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;}}
配置yml文件,搜索公众号互联网架构师回复“2T”,送你一份惊喜礼包。
# 分布式文件系统fastdfs配置
fdfs:# socket连接超时时长soTimeout: 1500# 连接tracker服务器超时时长connectTimeout: 600pool:# 从池中借出的对象的最大数目max-total: 153# 获取连接时的最大等待毫秒数100max-wait-millis: 102# 缩略图生成参数,可选thumbImage:width: 150height: 150# 跟踪服务器tracker_server请求地址,支持多个,这里只有一个,如果有多个在下方加- x.x.x.x:porttrackerList:- 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 # 最大支持请求大小
**测试类**@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"); }}
运行测试类
返回的 url 就是能访问的地址
最后一张看效果
原文链接:https://blog.csdn.net/qq_37759106/article/details/82981023
感谢您的阅读,也欢迎您发表关于这篇文章的任何建议,关注我,技术不迷茫!小编到你上高速。
· END ·
最后,关注公众号互联网架构师,在后台回复:2T,可以获取我整理的 Java 系列面试题和答案,非常齐全。
正文结束
推荐阅读 ↓↓↓
1.心态崩了!税前2万4,到手1万4,年终奖扣税方式1月1日起施行~
2.深圳一普通中学老师工资单曝光,秒杀程序员,网友:敢问是哪个学校毕业的?
3.从零开始搭建创业公司后台技术栈
4.程序员一般可以从什么平台接私活?
5.清华大学:2021 元宇宙研究报告!
6.为什么国内 996 干不过国外的 955呢?
7.这封“领导痛批95后下属”的邮件,句句扎心!
8.15张图看懂瞎忙和高效的区别!
Docker + Spring Boot + FastDFS 搭建一套分布式文件服务器,太强了!相关推荐
- spring boot ELK搭建
ELK简介 ELK是Elasticsearch+Logstash+Kibana简称 Elasticsearch 是一个分布式的搜索和分析引擎,可以用于全文检索.结构化检索和分析,并能将这三者结合起来. ...
- 【Spring Boot】使用Spring Boot来搭建Java web项目以及开发过程
[Spring Boot]使用Spring Boot来搭建Java web项目以及开发过程 一.Spring Boot简介 Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来 ...
- Spring Boot框架搭建
目录 一.Spring Boot概述 二.Spring Boot的优点 三.Spring Boot框架搭建 一.Spring Boot概述 Spring Boot 是 Spring 框架的一个新的子项 ...
- Spring Boot + ELK搭建日志监控框架
Spring Boot + ELK搭建日志监控框架 准备ELK三件套 Elasticsearch+Logstash+Kibana 下载地址:https://www.elastic.co/cn/ ...
- 基于Intelli J IDE的Spring Boot 环境搭建
一 Spring Boot的介绍 Spring Boot是一个全新的框架,是用来简化新Spring应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置. ...
- Spring Boot快速搭建入门程序
一.快速搭建入门程序 第一步 新增Spring-Boot-starter-parent依赖[父级项目的web依赖] <parent><groupId>org.springfra ...
- Spring boot maven 搭建框架
Spring Boot: 目的:这个框架帮助开发者更容易地创建基于Spring的应用程序和服务,使得pring开发者能够最快速地获得所需要的Spring功能. 优点:完全不需要XML配置,让sprin ...
- mysql的每隔1分钟定时_简单易用,spring boot集成quartz,实现分布式定时任务
什么是quartz? Quartz是一个完全由 Java 编写的开源任务调度框架. 我们经常会遇到一些问题: 想每个月27号,提醒信用卡还款: 想每隔1小时,提醒一下,累了,站起来活动一下: 想每个月 ...
- 《SpringCloud超级入门》Spring Boot项目搭建步骤(超详细)《六》
目录 编写第一个 REST 接口 读取配置文件 profiles 多环境配置 热部署 actuator 监控 自定义 actuator 端点 统一异常处理 异步执行 随机端口 编译打包 在 Sprin ...
- Spring Boot快速搭建Web开发框架(Hibernate+Thymeleaf)
前言 Spring Boot的Web项目搭建参考: [Spring Boot系列]1. 项目搭建之一 整体介绍 Hibernate: ORM框架, 使用JPA统一接口操作数据库 基于MySQL数据库, ...
最新文章
- Java笔试面试总结—try、catch、finally语句中有return 的各类情况
- Vue指令v-for之遍历输出JavaScript数组,json对象的几种方式
- 经典php代码,10个非常经典的php代码片段.doc
- 计算机对农业领域带来的变革,智慧农业将给农业带来哪些变化?
- CountDownLatch的理解和使用
- 计算机导论上机模拟,计算机导论模拟考试题6份完整版.doc
- 【线段树】Traffic Jams in the Land(CF498D)
- Coursera自动驾驶课程第3讲:Self-Driving Hardware and Software Architectures
- 万能电商Banner素材模板,一切产品为王
- windows连接远程服务器报错‘SSH‘ 不是内部或外部命令,也不是可运行的程序 或批处理文件 解决方案
- ue4渲染速度太慢_技术汇丨如何在UE4中实现最佳性能和高质量视觉效果
- 【杂学笔记-表观遗传学-updating】
- html期末作业代码网页设计——蛋糕甜品店(4页) web期末作业设计网页_甜品美食大学生网页设计作业成品
- VS2019提示“未能完成操作,不支持此接口”
- 扦插机器人_如何看待我国农业采摘机器人发展趋势?
- 使用机器学习预测天气_使用机器学习来预测患者是否会再次入院
- 成也苹果败也苹果,曾经女首富身价缩水一半
- 基于多传感器数据融合的全自动泊车系统研究与应用(文献综述)
- Windows家庭版如何打开本地组策略编辑器
- storm源码分析研究(十三)