欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  网络运营

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

程序员文章站 2022-06-22 16:11:23
上篇文章给大家介绍了使用docker compose安装fastdfs文件服务器的实例详解今天给大家介绍如何使用 docker compose 搭建 fastdfs文件服务器,内容详情如下所示:平台...

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

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

平台 :mac m1

备注:关于 ip address

,上述文中提到了 docker 的 host 模式

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

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

启动 tracker 时控制台输出:

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

192.168.64.2的网络为:

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

192.168.65.4的网络为:

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

文件目录

├── 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;
    }

    /**
     * 封装文件完整 url 地址
     *
     * @param path
     * @return
     */
    public string getresaccessurl(string path) {
        return fdfswebserver.getwebserverurl() + path;
    }
}

fastdfscontroller.java

@restcontroller
@requestmapping("/fast-dfs")
public class fastdfscontroller {
    /**
     * @param file
     * @return
     * @throws ioexception
     */
    @postmapping("")
    @transactional
    public void uploadfile(multipartfile file, string cuisineid) throws ioexception {
        string s = fastdfsutil.uploadfile(file);
        string resaccessurl = fastdfsutil.getresaccessurl(s);
    }

    /**
     * @param response
     * @throws ioexception
     */
    @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());
    }

    /**
     * 流媒体的方式播放视频,只能从头看到尾,不能手动点击重新看已经看过的内容
     * @param filepath
     * @param response
     * @throws ioexception
     */
    @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内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!