文章详情

短信预约-IT技能 免费直播动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

如何使用 docker compose 搭建 fastDFS文件服务器

2024-04-02 19:55

关注

上篇文章给大家介绍了使用docker compose安装FastDfs文件服务器的实例详解

今天给大家介绍如何使用 docker compose 搭建 fastDFS文件服务器,内容详情如下所示:

平台 :Mac M1

备注:关于 IP Address

关于 docker 的网络模式,上述文中提到了 docker 的 Host 模式

如果启动容器的时候使用host模式,那么这个容器将不会获得一个独立的Network Namespace,而是和宿主机共用一个 Network Namespace。容器将不会虚拟出自己的网卡,配置自己的 IP 等,而是使用宿主机的 IP 和端口。但是,容器的其他方面,如文件系统、进程列表等还是和宿主机隔离的。

问题在于:使用宿主机的 IP 和端口的话,配置文件中 IP 填写 localhost的话按理说能访问到容器,然而事实上却不行。个人理解(如果理解有问题的话烦请指正)的 IP Address填写方法如下:

启动 tracker 时控制台输出:

192.168.64.2的网络为:

 

192.168.65.4的网络为:

文件目录


├── docker-compose.yaml
├── nginx
│   └── nginx.conf
├── storage
│   └── data
└── tracker
│   └── conf
│       └── client.conf
└── store_path

./docker-compose.yaml


version: "2"
services:
	fastdfs-tracker:
		hostname: fastdfs-tracker
		container_name: fastdfs-tracker
		image: season/fastdfs:1.2
		network_mode: "host"
		command: tracker
		volumes:
		  - ./tracker/data:/fastdfs/tracker/data
		  - ./tracker/conf:/etc/fdfs
	fastdfs-storage:
		hostname: fastdfs-storage
		container_name: fastdfs-storage
		image: season/fastdfs:1.2
		network_mode: "host"
		volumes:
		  - ./storage/data:/fastdfs/storage/data
		  - ./store_path:/fastdfs/store_path
		environment:
		  - TRACKER_SERVER=192.168.64.2:22122
		command: storage
		depends_on:
		  - fastdfs-tracker
	fastdfs-nginx:
		hostname: fastdfs-nginx
		container_name: fastdfs-nginx
		image: season/fastdfs:1.2
		network_mode: "host"
		volumes:
		  - ./nginx/nginx.conf:/etc/nginx/conf/nginx.conf
		  - ./store_path:/fastdfs/store_path
		environment:
		  - TRACKER_SERVER=192.168.64.2:22122
		command: nginx

./tracker/conf/client.conf


# connect timeout in seconds
# default value is 30s
connect_timeout=30
# network timeout in seconds
# default value is 30s
network_timeout=60

# the base path to store log files
base_path=/fastdfs/client

# tracker_server can ocur more than once, and tracker_server format is
#  "host:port", host can be hostname or ip address
# 需要修改此处 ip
tracker_server=192.168.64.2:22122

#standard log level as syslog, case insensitive, value list:
### emerg for emergency
### alert
### crit for critical
### error
### warn for warning
### notice
### info
### debug
log_level=info

# if use connection pool
# default value is false
# since V4.05
use_connection_pool = false

# connections whose the idle time exceeds this time will be closed
# unit: second
# default value is 3600
# since V4.05
connection_pool_max_idle_time = 3600

# if load FastDFS parameters from tracker server
# since V4.05
# default value is false
load_fdfs_parameters_from_tracker=false

# if use storage ID instead of IP address
# same as tracker.conf
# valid only when load_fdfs_parameters_from_tracker is false
# default value is false
# since V4.05
use_storage_id = false

# specify storage ids filename, can use relative or absolute path
# same as tracker.conf
# valid only when load_fdfs_parameters_from_tracker is false
# since V4.05
storage_ids_filename = storage_ids.conf

#HTTP settings
http.tracker_server_port=80

#use "#include" directive to include HTTP other settiongs
##include http.conf

./nginx/nginx.conf


#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;

events {
	worker_connections  1024;
}

http {
	include       mime.types;
	default_type  application/octet-stream;

	#log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
	#                  '$status $body_bytes_sent "$http_referer" '
	#                  '"$http_user_agent" "$http_x_forwarded_for"';

	#access_log  logs/access.log  main;

	sendfile        on;
	#tcp_nopush     on;

	#keepalive_timeout  0;
	keepalive_timeout  65;

	#gzip  on;

	server {
		listen       9800;
		server_name  localhost;

		#charset koi8-r;

		#access_log  logs/host.access.log  main;

		# 修改部分
		location / {
			root /fastdfs/store_path/data;
			ngx_fastdfs_module;
		}

		#error_page  404              /404.html;

		# redirect server error pages to the static page /50x.html
		#
		error_page   500 502 503 504  /50x.html;
		location = /50x.html {
			root   html;
		}
	}
}

SpringBoot 集成 fastDFS

添加依赖


<dependency>
	<groupId>com.github.tobato</groupId>
	<artifactId>fastdfs-client</artifactId>
	<version>1.27.2</version>
</dependency>

 

applicaiton.yaml


# 分布式文件系统配置
fdfs:
  #根据自己的ip进行更改
  ip: 192.168.64.2
  #socket连接超时时长
  soTimeout: 1500
  connectTimeout: 600
  #支持多个
  trackerList:
    - ${fdfs.ip}:22122
  # fastDFS 中的 nginx 的 ip 和 port
  # IDEA 提示使用 https, 
  # nginx 配置 SSL 请移步:
  web-server-url: http://${fdfs.ip}:9800/

FastDFSConfig.java


@Configuration
// 导入FastDFS-Client组件
@Import(FdfsClientConfig.class)
// 解决jmx重复注册bean的问题
@EnableMBeanExport(registration = RegistrationPolicy.IGNORE_EXISTING)
public aspect FastDFSConfig {
}

FastDFSUtil.java


@Component
public class FastDFSUtil {
    @Resource
    private FastFileStorageClient fastFileStorageClient;
    @Resource
    private FdfsWebServer fdfsWebServer;

    public String uploadFile(MultipartFile file) throws IOException {
        StorePath storePath = fastFileStorageClient.uploadFile(file.getInputStream(), file.getSize(), FilenameUtils.getExtension(file.getOriginalFilename()), null);
        String fullPath = storePath.getFullPath();
        getResAccessUrl(fullPath);
        return fullPath;
    }

    public 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) {
            e.printStackTrace();
            return null;
        }
    }

    public byte[] downloadFile(String filePath) {
        StorePath storePath = StorePath.parseFromUrl(filePath);
        return fastFileStorageClient.downloadFile(storePath.getGroup(), storePath.getPath(), new DownloadByteArray());
    }

    public Boolean deleteFile(String filePath) {
        if (StringUtils.isEmpty(filePath)) {
            return false;
        }
        try {
            StorePath storePath = StorePath.parseFromUrl(filePath);
            fastFileStorageClient.deleteFile(storePath.getGroup(), storePath.getPath());
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    
    public String getResAccessUrl(String path) {
        return fdfsWebServer.getWebServerUrl() + path;
    }
}

FastDFSController.java


@RestController
@RequestMapping("/fast-dfs")
public class FastDFSController {
    
    @PostMapping("")
    @Transactional
    public void uploadFile(MultipartFile file, String cuisineId) throws IOException {
        String s = fastDfsUtil.uploadFile(file);
        String resAccessUrl = fastDfsUtil.getResAccessUrl(s);
    }

    
    @GetMapping("")
    public void downloadFile(String filePath, HttpServletResponse response) throws IOException {
        byte[] bytes = fastDfsUtil.downloadFile(filePath);

        String[] split = filePath.split("/");
        String fileName = split[split.length - 1];
        // 设置强制下载不打开
        response.setContentType("application/force-download");

        fileName = URLEncoder.encode(fileName, StandardCharsets.UTF_8);
        response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
        IOUtils.write(bytes, response.getOutputStream());
    }

    
    @GetMapping("/play")
    public void streamMedia(String filePath, HttpServletResponse response) throws IOException {
        byte[] bytes = fastDfsUtil.downloadFile(filePath);
        IOUtils.copy(new ByteArrayInputStream(bytes), response.getOutputStream());
        response.flushBuffer();
    }
}

到此这篇关于如何使用 docker compose 搭建 fastDFS文件服务器的文章就介绍到这了,更多相关docker compose 搭建 fastDFS内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

阅读原文内容投诉

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     801人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     348人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     311人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     432人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     220人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
位置:首页-资讯-服务器
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯