docker快速安装fastdfs服务springboot访问「建议收藏」

docker快速安装fastdfs服务springboot访问「建议收藏」拉取镜像dockerpullmorunchang/fastdfs运行tracker跟踪器dockerrun-d–nametracker–net=hostmorunchang/fastdfsshtracker.sh运行storage存储器【注意:修改IP为自己的IP端口不变】dockerrun-d–namestorage–net=host-eTRACKER_IP=192.168.61.200:22122-eGROUP_NAME=gr

大家好,又见面了,我是你们的朋友全栈君。

  • 拉取镜像
docker pull morunchang/fastdfs
  • 运行tracker 跟踪器
 docker run -d --name tracker --net=host morunchang/fastdfs sh tracker.sh
  • 运行storage 存储器【注意:修改IP为自己的IP 端口不变】
  docker run -d --name storage --net=host -e TRACKER_IP=192.168.61.200:22122 -e GROUP_NAME=group1 morunchang/fastdfs sh storage.sh
  • nginx的配置
docker exec -it storage  /bin/bash
cd data
vi nginx/conf/nginx.conf
  • 将红色框的内容添加进去(后面提供了可复制的内容)

docker快速安装fastdfs服务springboot访问「建议收藏」

location /group1/M00 {
	   proxy_next_upstream http_502 http_504 error timeout invalid_header;
		 proxy_cache http-cache;
		 proxy_cache_valid  200 304 12h;
		 proxy_cache_key $uri$is_args$args;
		 proxy_pass http://fdfs_group1;
		 expires 30d;
	 }

编辑完后:wq退出编辑

  • 然后退出docker
exit
  •  重启storage服务
docker restart storage

至此服务安装完毕.

借鉴博客:使用Docker快速搭建FastDFS_米斯特尔.W-CSDN博客_docker fastdfs搭建

集成springboot

<dependency>
            <groupId>com.github.tobato</groupId>
            <artifactId>fastdfs-client</artifactId>
            <version>1.26.2</version>
        </dependency>
# 分布式文件系统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.0.1:22122
  #
  # 存储服务器storage_server访问地址
  web-server-url: http://192.168.0.1/
在springboot启动类上加

@Import(FdfsClientConfig.class)
@EnableMBeanExport(registration = RegistrationPolicy.IGNORE_EXISTING)

docker快速安装fastdfs服务springboot访问「建议收藏」

工具类:

package com.itheima.util;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.HashSet;
import java.util.Set;

import com.github.tobato.fastdfs.domain.MataData;
import com.github.tobato.fastdfs.domain.StorePath;
import com.github.tobato.fastdfs.proto.storage.DownloadByteArray;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import com.github.tobato.fastdfs.service.FastFileStorageClient;

/**
 * FastDFS客户端包装类
 *
 * @author CL
 *
 */
@Component
public class FdfsClientWrapper {

    @Autowired
    private FastFileStorageClient fastFileStorageClient;

    public String uploadFile(MultipartFile file) throws IOException {
        if (file != null) {
            byte[] bytes = file.getBytes();
            long fileSize = file.getSize();
            String originalFilename = file.getOriginalFilename();
            String extension = originalFilename.substring(originalFilename.lastIndexOf(".") + 1);
            return this.uploadFile(bytes, fileSize, extension);
        }
        return null;
    }

    /**
     * 文件上传
     *
     * @param bytes     文件字节
     * @param fileSize  文件大小
     * @param extension 文件扩展名
     * @return 返回文件路径(卷名和文件名)
     */
    public String uploadFile(byte[] bytes, long fileSize, String extension) {
        ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
        // 元数据
        Set<MataData> metaDataSet = new HashSet<MataData>();
        metaDataSet.add(new MataData("dateTime", LocalDateTime.now().toString()));
        StorePath storePath = fastFileStorageClient.uploadFile(bais, fileSize, extension, metaDataSet);
        return storePath.getFullPath();
    }

    /**
     * 下载文件
     *
     * @param filePath 文件路径
     * @return 文件字节
     * @throws IOException
     */
    public byte[] downloadFile(String filePath) throws IOException {
        byte[] bytes = null;
        if (StringUtils.isNotBlank(filePath)) {
            String group = filePath.substring(0, filePath.indexOf("/"));
            String path = filePath.substring(filePath.indexOf("/") + 1);
            DownloadByteArray byteArray = new DownloadByteArray();
            bytes = fastFileStorageClient.downloadFile(group, path, byteArray);
        }
        return bytes;
    }

    /**
     * 删除文件
     *
     * @param filePath 文件路径
     */
    public void deleteFile(String filePath) {
        if (StringUtils.isNotBlank(filePath)) {
            fastFileStorageClient.deleteFile(filePath);
        }
    }

}

测试接口:


import com.itheima.util.FdfsClientWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
public class TestController {
    private final FdfsClientWrapper fdfsClientWrapper;

    @Autowired
    public TestController(FdfsClientWrapper fdfsClientWrapper) {
        this.fdfsClientWrapper = fdfsClientWrapper;
    }

    @RequestMapping("upload")
    public String upload(@RequestParam MultipartFile file) {
        String filePath = null;
        try {
            filePath = fdfsClientWrapper.uploadFile(file);
        } catch (IOException e) {
            return "上传文件失败";
        }
        return filePath;
    }

    @RequestMapping("del")
    public String del(@RequestParam String filePath) {
        fdfsClientWrapper.deleteFile(filePath);
        return "删除成功";
    }
}

docker快速安装fastdfs服务springboot访问「建议收藏」

访问地址:

fastdfs服务器IP:8080/group1/M00/00/00/rBMvdGFAtNGAVy55AAyEK5YJQm8841.png

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/149484.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • docker 启动容器出现 Exited[通俗易懂]

    有时候在启动容器的时候,启动没报错,但是在执行dockerps-a时发现刚启动的容器状态为Exited(1),这个时候查看日志dockerlogs-f-t–tail20容器ID,发现报chown:changingownershipof’.’:Permissiondenied提示没有权限,这个时候将容器删除,在执行容器启动的命令中加入–privi…

    2022年4月17日
    1.3K
  • docker菜鸟教程_k8s部署docker镜像

    docker菜鸟教程_k8s部署docker镜像前记:最近跟着哔站码神之路做了一个SpringBoot练手项目,第一次操作碰到了很多困难和问题,尤其是在部署部分,走了很多弯路,这里写下自己的部署过程,供大家参考,也欢迎大家提出宝贵的意见。哔站码神视频链接:https://www.bilibili.com/video/BV1Gb4y1d7zb?p=36我的网站:www.zhangshidi.space前置知识以下知识点希望大家首先搜一搜,读一读,有一个大概的了解。什么是Linux以及掌握Linux的一些基本指令。什么是docke

    2022年10月19日
    3
  • Kubernetes各版本对应支持的docker版本列表

    Kubernetes各版本对应支持的docker版本列表Kubernetes主要做Docker的容器化管理,总结一下如何查看k8s对应支持的docker版本的方法。在GitHub可以查看所有Kubernetets版本信息:https://github.com/kubernetes/kubernetes/releases截止2019.08.09最新的版本支持信息:Kubernetes1.15.2–>Docker版本1.13…

    2025年6月26日
    4
  • Docker总结(配合阿里云容器镜像服务)「建议收藏」

    Docker总结(配合阿里云容器镜像服务)「建议收藏」Docker是个很好的工具,刚开始用觉得还没虚拟环境好用,随着深入了解,越发觉得Docker好用,今天就来总结一下使用心得。一、Docker基础1、背景知识1)docker是什么?Docker属于Linux容器的一种封装,提供简单易用的容器使用接口。它是目前最流行的Linux容器解决方案。Docker将应用程序与该程序的依赖,打包在一个文件里面。运行这个…

    2022年5月24日
    65
  • docker下载安装教程_vmware mac版本

    docker下载安装教程_vmware mac版本前言Docker提供轻量的虚拟化,你能够从Docker获得一个额外抽象层,你能够在单台机器上运行多个Docker微容器,而每个微容器里都有一个微服务或独立应用,例如你可以将Tomcat运行在一个D

    2022年7月28日
    12
  • 群晖docker mysql_Watchtower – 群晖自动更新 Docker 映像与容器

    群晖docker mysql_Watchtower – 群晖自动更新 Docker 映像与容器群晖的Docker功能非常丰富,不过也有不完美的地方,映像和容器更新比较麻烦,比如我的Docker容器运行了十几个,如果通过手动更新非常繁琐,容器还需要重新配置本文就介绍如何通过watchtower全自动更新Docker映像,并保留原始配置重新运行容器。watchtower是一个可以监控正在运行的容器镜像是否有更新的工具,当本地镜像与远端镜像有差异的时候,可以自动使用当前容器的运行参数以新镜像重…

    2025年6月13日
    5

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号