<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          Docker + FastDFS + Spring Boot 一鍵式搭建分布式文件服務(wù)器

          共 13327字,需瀏覽 27分鐘

           ·

          2021-05-24 16:28

          作者:kalibiubiubiu

          blog.csdn.net/qq_37759106/article/details/82981023

          首先說一下從零開始自己去搭一個(gè)fastdfs挺麻煩,后來看到有人把做好的 docker 鏡像傳出來了,那搭建起來就很容易了

          有服務(wù)器的可以自己在服務(wù)器上玩玩,沒有的可以新建一個(gè)centos7.5虛擬機(jī)玩玩,遇到虛擬機(jī)不能上網(wǎng)和換阿里云的源的問題可以參考https://blog.csdn.net/qq_37759106/article/details/82985113這篇文章

          PS:更多 Docker 和 Spring Boot 的文章可以關(guān)注微信公眾號(hào)「Java后端」回復(fù)「666」下載技術(shù)棧手冊(cè)。

          1.第一步安裝docker:

           在 root 權(quán)限下

          yum install -y docker-io #安裝docker
          service docker star #啟動(dòng)docker
          docker -v # 查看docker版本

          2. 拉取鏡像

          docker pull qbanxiaoli/fastdfs

          啟動(dòng) 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 后面是你的服務(wù)器公網(wǎng)ip或者虛擬機(jī)的IP,-e WEB_PORT=80 指定 nginx 端口

          測(cè)試fastdfs是否搭建成功

          docker exec -it fastdfs /bin/bash
          echo "Hello FastDFS!">index.html
          fdfs_test /etc/fdfs/client.conf upload index.html

          能返回 url 就意見搭建成功

          這樣 fastdfs 就搭建好啦

          下面進(jìn)入 Spring Boot 整合部分

          <groupId>com.github.tobato</groupId>
             <artifactId>fastdfs-client</artifactId>
          <version>1.26.2</version>
          在 Spring Boot 啟動(dòng)類上加
          @Import(FdfsClientConfig.class)
          @EnableMBeanExport(registration = RegistrationPolicy.IGNORE_EXISTING)

          創(chuàng)建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;
           
              @Autowired
              public void setFastDFSClient(FastFileStorageClient fastFileStorageClient, FdfsWebServer fdfsWebServer) {
                  FastDFSClient.fastFileStorageClient = fastFileStorageClient;
                  FastDFSClient.fdfsWebServer = fdfsWebServer;
              }
           
              /**
               * @param multipartFile 文件對(duì)象
               * @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 圖片對(duì)象
               * @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 文件對(duì)象
               * @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 圖片對(duì)象
               * @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數(shù)組
               * @param fileExtension 文件擴(kuò)展名
               * @return 返回文件地址
               * @author qbanxiaoli
               * @description 將byte數(shù)組生成一個(gè)文件上傳
               */

              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文件
          # 分布式文件系統(tǒng)fastdfs配置
          fdfs:
            # socket連接超時(shí)時(shí)長(zhǎng)
            soTimeout: 1500
            # 連接tracker服務(wù)器超時(shí)時(shí)長(zhǎng)
            connectTimeout: 600
            pool:
              # 從池中借出的對(duì)象的最大數(shù)目
              max-total: 153
              # 獲取連接時(shí)的最大等待毫秒數(shù)100
              max-wait-millis: 102
            # 縮略圖生成參數(shù),可選
            thumbImage:
              width: 150
              height: 150
            # 跟蹤服務(wù)器tracker_server請(qǐng)求地址,支持多個(gè),這里只有一個(gè),如果有多個(gè)在下方加- x.x.x.x:port
            trackerList:
              - 192.168.127.131:22122
            #
            # 存儲(chǔ)服務(wù)器storage_server訪問地址
            web-server-url: http://192.168.127.131/
            spring:
              http:
                multipart:
                  max-file-size: 100MB # 最大支持文件大小
                  max-request-size: 100MB # 最大支持請(qǐng)求大小

          測(cè)試類

          @RunWith(SpringRunner.class)
          @SpringBootTest
          public 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");
            }
          }
          運(yùn)行測(cè)試類
          返回的 url 就是能訪問的地址

          點(diǎn)擊閱讀全文前往微服務(wù)電商教程
          瀏覽 50
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  岛国免费AV | 91jiuse视频 | 国产精品99久久久久久久久久久久 | A V在线免费播放 | 91精品国产专区 |