当前位置: 首页 > 后端技术 > Java

Docker+SpringBoot+FastDFS搭建分布式文件服务器,太强大了!

时间:2023-04-01 22:28:32 Java

首先告诉大家,从头开始搭建一个fastdfs是非常麻烦的。后来看到有人上传了做好的docker镜像,所以搭建起来非常简单。1.第一步安装docker:在root权限下yuminstall-ydocker-io#安装dockerservicedockerstar#启动dockerdocker-v#查看docker版本2.拉取镜像dockerpullqbanxiaoli/fastdfsstartfastdfsdockerrun-d--restart=always--privileged=true--net=host--name=fastdfs-eIP=192.168.127.131-eWEB_PORT=80-v${HOME}/fastdfs:/var/local/fdfsqbanxiaoli/fastdfsIP后跟你的服务器公网ip或者虚拟机IP,-eWEB_PORT=80指定nginx端口,测试fastdfs是否搭建成功。dockerexec-itfastdfs/bin/bashecho"HelloFastDFS!">index.htmlfdfs_test/etc/fdfs/client.confuploadindex.html可以返回url,至此fastdfs构建成功构建完成后进入SpringBoot集成部分com.github.tobatofastdfs-client1.26.2在SpringBoot启动类(FdfsClientConfig.class)中添加@Import@EnableMBeanExport(registration=RegistrationPolicy.IGNORE_EXISTING)创建FastDFSClient工具类包com.yd.client.common;importcom.github.tobato.fastdfs.conn.FdfsWebServer;导入com.github.tobato.fastdfs.domain.StorePath;导入com.github.tobato.fastdfs.proto.storage.DownloadByteArray;导入com.github.tobato.fastdfs.service.FastFileStorageClient;导入org.apache.commons。io.FilenameUtils;导入org.apache.commons.lang3.StringUtils;导入org.slf4j.Logger;导入org.slf4j.LoggerFactory;导入org.springframework.beans.factory.annotation.Autowired;导入org.springframework.stereotype.Component;importorg.springframework.web.multipart.MultipartFile;importjava.io.*;@ComponentpublicclassFastDFSClient{privatestaticLoggerlog=LoggerFactory.getLogger(FastDFSClient.class);私有静态FastFileStorageClientfastFileStorageClient;私有静态FdfsWebServerfdfsWebServer;@AutowiredpublicvoidsetFastDFSClient(FastFileStorageClientfastFileStorageClient,FdfsWebServerfdfsWebServer){FastDFSClient.fastFileStorageClient=fastFileStorageClient;FastDFSClient.fdfsWebServer=fdfsWebServer;}/***@parammultipartFile文件对象*@return返回文件地址*@authorqbanxiaoli*@description上传文件*/publicstaticStringuploadFile(MultipartFilemultipartFile){try{StorePathstorePath=fastFileStorageClient.uploadFile(multipartFile.getInputStream(),multipartFile.getSize(),FilenameUtils.getExtension(multipartFile.getOriginalFilename()),null);返回storePath.getFullPath();}catch(IOExceptione){log.error(e.getMessage());返回空值;}}/***@parammultipartFile图片对象*@return返回图片地址*@authorqbanxiaoli*@description上传缩略图*/publicstaticStringuploadImageAndCrtThumbImage(MultipartFilemultipartFile){try{StorePathstorePath=fastFileStorageClient.uploadImageAndCrtThumbImage(multipartFile.getInputStream(),multipartFile.getSize(),FilenameUtils.getExtension(multipartFile.getOriginalFilename()),null);返回storePath.getFullPath();}catch(Exceptione){log.error(e.getMessage());返回空值;}}/***@paramfile文件对象*@return返回文件地址*@authorqbanxiaoli*@description上传文件*/publicstaticStringuploadFile(Filefile){try{FileInputStreaminputStream=newFileInputStream(file);StorePathstorePath=fastFileStorageClient.uploadFile(inputStream,file.length(),FilenameUtils.getExtension(file.getName()),null);返回storePath.getFullPath();}catch(Exceptione){log.error(e.getMessage());返回空值;}}/***@paramfile图片对象*@return返回图片地址*@authorqbanxiaoli*@description上传缩略图*/publicstaticStringuploadImageAndCrtThumbImage(Filefile){try{FileInputStreaminputStream=newFileInputStream米(文件);StorePathstorePath=fastFileStorageClient.uploadImageAndCrtThumbImage(inputStream,file.length(),FilenameUtils.getExtension(file.getName()),null);返回storePath.getFullPath();}赶上(异常e){日志。错误(e.getMessage());返回空值;}}/***@parambytes字节数组*@paramfileExtension文件扩展名*@return返回文件地址*@authorqbanxiaoli*@description从字节数组生成文件上传*/publicstaticStringuploadFile(byte[]bytes,StringfileExtension){ByteArrayInputStreamstream=newByteArrayInputStream(bytes);StorePathstorePath=fastFileStorageClient.uploadFile(stream,bytes.length,fileExtension,null);returnstorePath.getFullPath(}/***@paramfileUrl文件访问地址*@paramfile文件保存路径*@authorqbanxiaoli*@description下载文件*/publicstaticbooleandownloadFile(StringfileUrl,Filefile){try{StorePathstorePath=StorePath.praseFromUrl(fileUrl);byte[]bytes=fastFileStorageClient.downloadFile(storePath.getGroup(),storePath.getPath(),newDownloadByteArray());FileOutputStreamstream=newFileOutputStream(文件);stream.write(字节);}catch(Exceptione){log.error(e.getMessage());返回假;}返回真;}/***@paramfileUrl文件访问地址*@authorqbanxiaoli*@description删除文件*/publicstaticbooleandeleteFile(StringfileUrl){if(StringUtils.isEmpty(fileUrl)){returnfalse;}尝试{StorePathstorePath=StorePath.praseFromUrl(fileUrl);fastFileStorageClient.deleteFile(storePath.getGroup(),storePath.getPath());}catch(Exceptione){log.error(e.getMessage());返回假;}返回真;}//封装文件完整URL地址publicstaticStringgetResAccessUrl(Stringpath){Stringurl=fdfsWebServer.getWebServerUrl()+path;log.info("上传文件地址为:\n"+url);返回网址;}}配置yml文件#分布式文件系统fastdfs配置fdfs:#socket连接超时soTimeout:1500#连接跟踪器服务器超时connectTimeout:600pool:#从池中借出的最大对象数max-total:153#获取时的最大等待毫秒数aconnection100max-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#最大支持的请求大小**Testclass**@RunWith(SpringRunner.class)@SpringBootTestpublicclassFileClientApplicationTests{@TestpublicvoidUpload(){StringfileUrl=this.getClass().getResource("/test.jpg").getPath();文件file=newFile(fileUrl);字符串str=FastDFSClient.uploadFile(文件);FastDFSClient.getResAccessUrl(str);}@TestpublicvoidDelete(){FastDFSClient.deleteFile("group1/M00/00/00/rBEAClu8OiSAFbN_AAbhXQnXzvw031.jpg");}}运行测试类返回的url就是可以访问的地址

猜你喜欢