1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > Docker + FastDFS + Spring Boot 一键式搭建分布式文件服务器

Docker + FastDFS + Spring Boot 一键式搭建分布式文件服务器

时间:2021-06-14 02:32:07

相关推荐

Docker + FastDFS + Spring Boot 一键式搭建分布式文件服务器

点击上方蓝色“方志朋”,选择“设为星标”回复“666”获取独家整理的学习资料!

首先说一下从零开始自己去搭一个fastdfs挺麻烦,后来看到有人把做好的 docker 镜像传出来了,那搭建起来就很容易了

有服务器的可以自己在服务器上玩玩,没有的可以新建一个centos7.5虚拟机玩玩,遇到虚拟机不能上网和换阿里云的源的问题可以参考/qq_37759106/article/details/82985113这篇文章

PS:更多 Docker 和 Spring Boot 的文章可以关注微信公众号「Java后端」回复「666」下载技术栈手册。

1.第一步安装docker:

在 root 权限下

yuminstall -y docker-io#安装dockerservice docker star#启动dockerdocker -v# 查看docker版本

2. 拉取镜像

dockerpull qbanxiaoli/fastdfs

启动 fastdfs

dockerrun -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/bashecho"Hello FastDFS!">index.htmlfdfs_test /etc/fdfs/client.confuploadindex.html

能返回 url 就意见搭建成功

这样 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工具类

packagecom.mon;importcom.github.tobato.fastdfs.conn.FdfsWebServer;importcom.github.tobato.fastdfs.domain.StorePath;importcom.github.tobato.fastdfs.proto.storage.DownloadByteArray;importcom.github.tobato.fastdfs.service.FastFileStorageClient;mons.io.FilenameUtils;mons.lang3.StringUtils;importorg.slf4j.Logger;importorg.slf4j.LoggerFactory;importorg.springframework.beans.factory.annotation.Autowired;importorg.ponent;importorg.springframework.web.multipart.MultipartFile;importjava.io.*;@ComponentpublicclassFastDFSClient{privatestaticLogger log =LoggerFactory.getLogger(FastDFSClient.class);privatestaticFastFileStorageClient fastFileStorageClient;privatestaticFdfsWebServer fdfsWebServer;@AutowiredpublicvoidsetFastDFSClient(FastFileStorageClient fastFileStorageClient, FdfsWebServer fdfsWebServer){FastDFSClient.fastFileStorageClient = fastFileStorageClient;FastDFSClient.fdfsWebServer = fdfsWebServer;}/***@parammultipartFile 文件对象*@return返回文件地址*@authorqbanxiaoli*@description上传文件*/publicstaticStringuploadFile(MultipartFile multipartFile){try{StorePath storePath = fastFileStorageClient.uploadFile(multipartFile.getInputStream(), multipartFile.getSize(), FilenameUtils.getExtension(multipartFile.getOriginalFilename()),null);returnstorePath.getFullPath();}catch(IOException e) {log.error(e.getMessage());returnnull;}}/***@parammultipartFile 图片对象*@return返回图片地址*@authorqbanxiaoli*@description上传缩略图*/publicstaticStringuploadImageAndCrtThumbImage(MultipartFile multipartFile){try{StorePath storePath = fastFileStorageClient.uploadImageAndCrtThumbImage(multipartFile.getInputStream(), multipartFile.getSize(), FilenameUtils.getExtension(multipartFile.getOriginalFilename()),null);returnstorePath.getFullPath();}catch(Exception e) {log.error(e.getMessage());returnnull;}}/***@paramfile 文件对象*@return返回文件地址*@authorqbanxiaoli*@description上传文件*/publicstaticStringuploadFile(File file){try{FileInputStream inputStream =newFileInputStream(file);StorePath storePath = fastFileStorageClient.uploadFile(inputStream, file.length(), FilenameUtils.getExtension(file.getName()),null);returnstorePath.getFullPath();}catch(Exception e) {log.error(e.getMessage());returnnull;}}/***@paramfile 图片对象*@return返回图片地址*@authorqbanxiaoli*@description上传缩略图*/publicstaticStringuploadImageAndCrtThumbImage(File file){try{FileInputStream inputStream =newFileInputStream(file);StorePath storePath = fastFileStorageClient.uploadImageAndCrtThumbImage(inputStream, file.length(), FilenameUtils.getExtension(file.getName()),null);returnstorePath.getFullPath();}catch(Exception e) {log.error(e.getMessage());returnnull;}}/***@parambytes byte数组*@paramfileExtension 文件扩展名*@return返回文件地址*@authorqbanxiaoli*@description将byte数组生成一个文件上传*/publicstaticStringuploadFile(byte[] bytes, String fileExtension){ByteArrayInputStream stream =newByteArrayInputStream(bytes);StorePath storePath = fastFileStorageClient.uploadFile(stream, bytes.length, fileExtension,null);returnstorePath.getFullPath();}/***@paramfileUrl 文件访问地址*@paramfile 文件保存路径*@authorqbanxiaoli*@description下载文件*/publicstaticbooleandownloadFile(String fileUrl, File file){try{StorePath storePath = StorePath.praseFromUrl(fileUrl);byte[] bytes = fastFileStorageClient.downloadFile(storePath.getGroup(), storePath.getPath(),newDownloadByteArray());FileOutputStream stream =newFileOutputStream(file);stream.write(bytes);}catch(Exception e) {log.error(e.getMessage());returnfalse;}returntrue;}/***@paramfileUrl 文件访问地址*@authorqbanxiaoli*@description删除文件*/publicstaticbooleandeleteFile(String fileUrl){if(StringUtils.isEmpty(fileUrl)) {returnfalse;}try{StorePath storePath = StorePath.praseFromUrl(fileUrl);fastFileStorageClient.deleteFile(storePath.getGroup(), storePath.getPath());}catch(Exception e) {log.error(e.getMessage());returnfalse;}returntrue;}// 封装文件完整URL地址publicstaticStringgetResAccessUrl(String path){String url = fdfsWebServer.getWebServerUrl() + path;log.info("上传文件地址为:\n"+ url);returnurl;}}

配置yml文件

# 分布式文件系统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)@SpringBootTestpublicclassFileClientApplicationTests{@TestpublicvoidUpload(){String fileUrl =this.getClass().getResource("/test.jpg").getPath();File file =newFile(fileUrl);String str = FastDFSClient.uploadFile(file);FastDFSClient.getResAccessUrl(str);}@TestpublicvoidDelete(){FastDFSClient.deleteFile("group1/M00/00/00/rBEAClu8OiSAFbN_AAbhXQnXzvw031.jpg");}}

运行测试类

返回的 url 就是能访问的地址

作者:kalibiubiubiu

/qq_37759106/article/details/82981023

热门内容:955 不加班的公司名单:955.WLB工作 4 年,从阿里巴巴辞职到了国企百度最近开源了分布式配置中心,名叫BRCC最近面试BAT,整理一份面试资料《Java面试BAT通关手册》,覆盖了Java核心技术、JVM、Java并发、SSM、微服务、数据库、数据结构等等。获取方式:点“在看”,关注公众号并回复666领取,更多内容陆续奉上。

明天见(。・ω・。)ノ♡

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。