Linux部署Docker + FastDFS集群 +Springboot

Linux部署Docker + FastDFS集群 +Springboot

版本号修改人修改日期
V1.0JackFang2019年11月1日

1.0版本简介:nginx+keepalived 实现对外提供vip访问,达到负载均衡、高可用的模式。

  • 3 组tracker
  • 3 个storage节点
  • 1 组group
  • 轮询存储策略
  • Keepalived+Nginx高可用+负载均衡
  • Nginx缩略图+缓存

架构图:

在这里插入图片描述

0、环境准备:

名称IP
服务器系统Centos7
服务器1(主)192.168.100.128
服务器2(备)192.168.100.129
服务器3(备)192.168.100.130
对外统一IP(vip)192.168.100.120

备注:分别在服务器1、2、3执行以下操作(可通过打包镜像后复制):

1、拉取fastdfs docker镜像

docker pull morunchang/fastdfs

#查看已下载的docker镜像列表
docker images
在这里插入图片描述

1.1开启防火墙端口:22122、23000、9101或者关闭防火墙

firewall-cmd --zone=public --add-port=22122/tcp --permanent
firewall-cmd --zone=public --add-port=23000/tcp --add-port=9101/tcp --permanent
firewall-cmd --reload

2、安装fastdfs tracker

2.1创建目录

mkdir -p /home/bdxhdata/fastdfs/tracker/conf /home/bdxhdata/fastdfs/tracker/data
mkdir -p /home/bdxhdata/fastdfs/storage/conf /home/bdxhdata/fastdfs/storage/data

2.2安装tracker

#Tracker创建+启动

cd /home/bdxhdata/fastdfs/tracker/conf
touch tracker.conf

tracker.conf:(文件内容如下)

disabled=false
bind_addr=
port=22122
connect_timeout=30
network_timeout=30
base_path=/data/fast_data
max_connections=256
accept_threads=1
work_threads=4
store_lookup=2
store_group=group1
store_server=0
store_path=0
download_server=0
reserved_storage_space = 10%
log_level=info
run_by_group=
run_by_user=
allow_hosts=*
sync_log_buff_interval = 10
check_active_interval = 120
thread_stack_size = 64KB
storage_ip_changed_auto_adjust = true
storage_sync_file_max_delay = 86400
storage_sync_file_max_time = 300
use_trunk_file = false 
slot_min_size = 256
slot_max_size = 16MB
trunk_file_size = 64MB
trunk_create_file_advance = false
trunk_create_file_time_base = 02:00
trunk_create_file_interval = 86400
trunk_create_file_space_threshold = 20G
trunk_init_check_occupying = false
trunk_init_reload_from_binlog = false
trunk_compress_binlog_min_interval = 0
use_storage_id = false
storage_ids_filename = storage_ids.conf
id_type_in_filename = ip
store_slave_file_use_link = false
rotate_error_log = false
error_log_rotate_time=00:00
rotate_error_log_size = 0
log_file_keep_days = 0
use_connection_pool = false
connection_pool_max_idle_time = 3600
http.server_port=8080
http.check_alive_interval=30
http.check_alive_type=tcp
http.check_alive_uri=/status.html

在这里插入图片描述

2.3运行tracker1的docker容器

docker run -itd --name tracker1 --net=host --privileged=true --restart always \
-v /etc/localtime:/etc/localtime \
-v /home/bdxhdata/fastdfs/tracker:/data/fast_data/ \
-v /home/bdxhdata/fastdfs/tracker/conf/tracker.conf:/etc/fdfs/tracker.conf \
morunchang/fastdfs sh tracker.sh

2.4查看启动日志 是否有报错

docker logs tracker1

如下图所示表示启动成功:
在这里插入图片描述

3、安装fastdfs storage

在服务器1、2、3分别执行以下操作:

3.1 Storage创建+启动

cd /home/bdxhdata/fastdfs/storage/conf
touch storage.conf nginx.conf mod_fastdfs.conf storage.sh client.conf

创建配置文件:

storage.conf:(文件内容如下)

disabled=false
group_name=group1
bind_addr=
client_bind=true
port=23000
connect_timeout=30
network_timeout=30
heart_beat_interval=30
stat_report_interval=60
base_path=/data/fast_data
max_connections=256
buff_size = 256KB
accept_threads=1
work_threads=4
disk_rw_separated = true
disk_reader_threads = 1
disk_writer_threads = 1
sync_wait_msec=50
sync_interval=0
sync_start_time=00:00
sync_end_time=23:59
write_mark_file_freq=500
store_path_count=1
store_path0=/data/fast_data
subdir_count_per_path=256
tracker_server=192.168.100.128:22122
tracker_server=192.168.100.129:22122
tracker_server=192.168.100.130:22122
log_level=debug
run_by_group=
run_by_user=
allow_hosts=*
file_distribute_path_mode=0
file_distribute_rotate_count=100
fsync_after_written_bytes=0
sync_log_buff_interval=10
sync_binlog_buff_interval=10
sync_stat_file_interval=300
thread_stack_size=512KB
upload_priority=10
if_alias_prefix=
check_file_duplicate=0
file_signature_method=hash
key_namespace=FastDFS
keep_alive=0
use_access_log = true
rotate_access_log = false
access_log_rotate_time=00:00
rotate_error_log = false
error_log_rotate_time=00:00
rotate_access_log_size = 0
rotate_error_log_size = 0
log_file_keep_days = 0
file_sync_skip_invalid_record=false
use_connection_pool = false
connection_pool_max_idle_time = 3600
http.domain_name=
http.server_port=9101

nginx.conf:(文件内容如下)

worker_processes  1;
error_log  /data/fast_data/logs/nginx-error.log;

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  /data/fast_data/logs/nginx-access.log  main;
    sendfile        on;
    keepalive_timeout  65;

    server {
        listen       9101;
        server_name  localhost;

        location / {
            root   html;
            index  index.html index.htm;
        }

        location ~ /group1/M00 {
                    root /data/fast_data/data;
                    ngx_fastdfs_module;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

mod_fastdfs.conf:(文件内容如下)

connect_timeout=30
network_timeout=30
base_path=/data/fast_data
load_fdfs_parameters_from_tracker=true
storage_sync_file_max_delay = 86400
use_storage_id = false
storage_ids_filename = storage_ids.conf
tracker_server=192.168.100.128:22122
tracker_server=192.168.100.129:22122
tracker_server=192.168.100.130:22122
storage_server_port=23000
group_name=group1
url_have_group_name = true
store_path_count=1
store_path0=/data/fast_data
log_level=info
log_filename=
response_mode=proxy
if_alias_prefix=
flv_support = true
flv_extension = flv
group_count = 0
#include http.conf

storage.sh:(文件内容如下)

#!/bin/sh
/data/fastdfs/storage/fdfs_storaged /etc/fdfs/storage.conf
/etc/nginx/sbin/nginx
tail -f /data/fast_data/logs/storaged.log

client.conf:(文件内容如下)

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

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

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

# tracker_server can ocur more than once, and tracker_server format is
#  "host:port", host can be hostname or ip address

tracker_server=192.168.100.128:22122
tracker_server=192.168.100.129:22122
tracker_server=192.168.100.130: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

3.2运行storage1的docker容器

docker run -d --name storage1 --net=host --privileged=true --restart always \
-v /etc/localtime:/etc/localtime \
-v /home/bdxhdata/fastdfs/storage/data:/data/fast_data/ \
-v /home/bdxhdata/fastdfs/storage/conf/storage.sh:/storage.sh \
-v /home/bdxhdata/fastdfs/storage/conf/storage.conf:/etc/fdfs/storage.conf \
-v /home/bdxhdata/fastdfs/storage/conf/nginx.conf:/etc/nginx/conf/nginx.conf  \
-v /home/bdxhdata/fastdfs/storage/conf/mod_fastdfs.conf:/etc/fdfs/mod_fastdfs.conf \
-v /home/bdxhdata/fastdfs/storage/conf/client.conf:/data/fastdfs/conf/client.conf \
morunchang/fastdfs sh storage.sh

在这里插入图片描述

3.3查看启动日志 是否有报错

docker logs storage1

如果启动失败,提示:

fail, errno: 2, error info: No such file or directorye: 970, open file /etc/fdfs/storage.conf
" fail, ret code: 24] ERROR - file: process_ctrl.c, line: 230, load conf file "/etc/fdfs/storage.conf
: not found 3: storage.sh: /etc/nginx/sbin/nginx
tail: cannot open ‘/data/fast_data/logs/storaged.log’ for reading: No such file or directory
tail: no files remaining

在这里插入图片描述

#删除失败容器storage1

docker stop storage1
docker rm storage1

然后上面的语句先删除这句
-v /home/bdxhdata/fastdfs/storage/conf/storage.sh:/storage.sh \

执行如下代码:

docker run -d --name storage1 --net=host --privileged=true --restart always \
-v /etc/localtime:/etc/localtime \
-v /home/bdxhdata/fastdfs/storage/data:/data/fast_data/ \
-v /home/bdxhdata/fastdfs/storage/conf/storage.conf:/etc/fdfs/storage.conf \
-v /home/bdxhdata/fastdfs/storage/conf/nginx.conf:/etc/nginx/conf/nginx.conf  \
-v /home/bdxhdata/fastdfs/storage/conf/mod_fastdfs.conf:/etc/fdfs/mod_fastdfs.conf \
-v /home/bdxhdata/fastdfs/storage/conf/client.conf:/data/fastdfs/conf/client.conf \
morunchang/fastdfs sh storage.sh

查看是否正常启动

docker ps

提示正常启动,再删除storage1
在这里插入图片描述

docker stop storage1
docker rm storage1

检查下/home/bdxhdata/fastdfs/storage/conf 目录下的配置文件中的tracker_server是否被覆盖了清空了,如果被覆盖清空了把 3.1的配置覆盖回去。
在这里插入图片描述
在这里插入图片描述

覆盖回去之后,把3.2 中run完整的语句再执行一次。

docker ps

查看是否正常启动,如下图所示表示成功启动。

在这里插入图片描述
正常启动后,进入容器、启动storage、启动Nginx、查看日志

docker exec -it storage1  /bin/bash
/data/fastdfs/storage/fdfs_storaged /etc/fdfs/storage.conf
/etc/nginx/sbin/nginx
tail -f /data/fast_data/logs/storaged.log

如下图所示表示启动成功:
在这里插入图片描述

Ctrl+c 退出查看日志模式。

3.5查看FastDFS监控状态 (服务器1中运行)

退出容器,查看storage状态。
exit
docker exec -it storage1 fdfs_monitor /data/fastdfs/conf/client.conf

如下图所示表示storage已经连接成功:ACTIVE 表示storage是活跃状态(OFFLINE:离线)
在这里插入图片描述

参数说明:
tracker_server_count:3 --表示2个Tracker Server
tracker server is 198.168.1.128:22122 --表示Leader Tracker
group count: 1 --表示有1个group
group name = group1 --组名称是group1
storage server count = 3 --组内有2个storage
active server count = 3 --活动的storage有2个
storage server port = 23000 --storage的端口
storage HTTP port = 9101 --storage的文件访问端口
store path count = 1 --storage只挂了一个存储目录
total_upload_count = 11 --总共上传了多少个文件
total_upload_bytes = 691405 --总共上传了多少字节
success_upload_bytes = 691405 --成功上传了多少字节
total_download_count = 2 --总共下载了多少文件(使用java客户端)

4、测试上传文件到FastDFS

docker exec -it storage1 bash
cd /data/fastdfs/conf
fdfs_test client.conf upload anti-steal.jpg

如下图所示表示上传成功:

在这里插入图片描述
找到红圈中的地址:
图片的相对地址:M00/00/00/wKhkgF2_8fGAIsnTAABdrZgsqUU312.jpg
图片的本服务器访问地址:
http://192.168.100.128/group1/M00/00/00/wKhkgF2_8fGAIsnTAABdrZgsqUU312.jpg

由于storage1的http端口配置为9101,所以真正的访问地址是:
http://192.168.100.128:9101/group1/M00/00/00/wKhkgF2_8fGAIsnTAABdrZgsqUU312.jpg
在这里插入图片描述

文件上传成功后,同时会自动同步到storage2,storage3,因此也可以通过storage2的IP,访问文件。

129服务器:
http://192.168.100.129:9101/group1/M00/00/00/wKhkgF2_8fGAIsnTAABdrZgsqUU312.jpg
130服务器:
http://192.168.100.130:9101/group1/M00/00/00/wKhkgF2_8fGAIsnTAABdrZgsqUU312.jpg

4.1下载文件

退出容器,进入 root目录
exit
cd /root
用curl -O(大写o)+ 图片地址下载测试:
curl -O
http://192.168.100.128:9101/group1/M00/00/00/wKhkgF2_8fGAIsnTAABdrZgsqUU312.jpg
在这里插入图片描述

同时在服务器129、130测试
能下载文件就表示fastdfs同步文件成功,也表示fastdfs安装配置成功。

5、使用Nginx生成缩略图

5.1 nginx集成 image_filter

#进入容器,安装依赖模块

docker exec -it storage1  /bin/bash
apt-get update
apt-get install libgd2-xpm-dev
apt-get install -y libgd-dev
cd /usr/local

#重新安装ngxin(为了加装image_filter模块)
wget https://nginx.org/download/nginx-1.13.4.tar.gz
在这里插入图片描述

#解压与加载模块
tar -zxvf nginx-1.13.4.tar.gz
cd nginx-1.13.4/

./configure --prefix=/etc/nginx --add-module=/data/fastdfs-nginx-module/src --with-http_image_filter_module

make
make install

5.2 修改nginx.conf 配置

worker_processes  1;
error_log  /data/fast_data/logs/nginx-error.log;

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  /data/fast_data/logs/nginx-access.log  main;
    sendfile        on;
    keepalive_timeout  65;
  
    server {
        listen       9101;
        server_name  localhost;
	client_max_body_size 100m;

        location / {
            root   html;
            index  index.html index.htm;
        }

	location ~ '^/group1/M00/([0-9A-F]{2})/([0-9A-F]{2})/(.*)_([\d]+)x([\d]+)\.(jpg|jpeg|png)$'{
            root /data/fast_data/thumb;
            #ngx_fastdfs_module;

            set $width $4;
            set $height $5;
	    set $image_path $1/$2;
	    set $image_name $3.$6;
	    set $cache_path  $3_$4x$5.$6;  #临时文件地址
	    
            if (-f $document_root$cache_path) {
		rewrite /group1/M00/(.+)\.(jpg|gif|png)@(\d+)w_(\d+)h_(\d+)Q_([rc])$ $3_$4x$5.$6;
		break;
	    }

            if (!-f $document_root$cache_path) {
		set $image_uri thumb_image/$image_path/$image_name?width=$width&height=$height;
		proxy_pass http://127.0.0.1:$server_port/$image_uri;
		break;
	    }

	    proxy_store $document_root/$cache_path;
	    proxy_store_access user:rw group:rw all:r;
	    proxy_temp_path  /data/fast_data/cache;
	    proxy_set_header Host $host;
	    expires  10d; # 设置图片过期时间10天
        }

	location /thumb_image {
	    #原图目录
	    alias /data/fast_data/data/;
	    image_filter resize $arg_width $arg_height;
	    image_filter_jpeg_quality 25;                                   # 压缩质量
	    image_filter_buffer 20m;
	    #try_files $image_path/$image_name=404 /data/fast_data/404.jpg;
	    # 如果原始图片大小起过1m,则会报415错误
	    #不允许外部访问
	    allow 127.0.0.0/8;
	    deny all;
	}

        location ~ /group1/M00 {
            root /data/fast_data/data;
            ngx_fastdfs_module;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

	location = /favicon.ico {
	    log_not_found off;
	    access_log off;
	}
    }
}

5.3 创建缩略图、缓存目录

cd /data/fast_data/
mkdir -p cache thumb
chmod 777 cache
chmod 777 thumb

#重启nginx
/etc/nginx/sbin/nginx -s stop
/etc/nginx/sbin/nginx

查看nginx 日志
tail -500f /data/fast_data/logs/nginx-access.log

生成缩略图示例:
http://192.168.100.128:9101/group1/M00/00/00/wKhkgF2_8fGAIsnTAABdrZgsqUU312_400x400.jpg
在这里插入图片描述

6、安装keepalived实现高可用

6.1 下载与配置

yum -y install keepalived

vi /etc/keepalived/keepalived.conf

#keepalived.conf 配置:

! Configuration File for keepalived
global_defs {
	router_id jackfang
}

vrrp_script check_nginx {
    script "/etc/keepalived/nginx_check.sh"         
    interval 2                            
    weight 2                  
}
vrrp_instance VI_1 {
    state MASTER
    interface eth33
    virtual_router_id 100
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    track_script {
        check_nginx
    }
    virtual_ipaddress {
        192.168.100.120
    }
}

6.2 创建检测nginx脚本

vi /etc/keepalived/nginx_check.sh

#!/bin/bash
A=`ps -C nginx -no-header |wc -l`
if [ $A -eq 0 ];then
/etc/nginx/sbin/nginx
sleep 2
if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then
        killall keepalived
fi
fi

添加权限:
chmod +x /etc/keepalived/nginx_check.sh

service keepalived start
service keepalived restart
service keepalived stop

#查看keepalived运行状态
service keepalived status
在这里插入图片描述

mkdir -p /etc/sysconfig/

cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
ln -s /usr/local/sbin/keepalived /usr/sbin/
ln -s /usr/local/keepalived/sbin/keepalived /sbin/

#启动keepalived
/usr/local/keepalived/sbin/keepalived

7、nginx负载均衡

7.1 进入tracker1容器,修改nginx配置

docker exec -it tracker1 /bin/bash
cd /etc/nginx/conf/
rm nginx.conf
touch nginx.conf
vi nginx.conf

worker_processes  1;
error_log  /data/fast_data/logs/nginx-error.log;

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  /data/fast_data/logs/nginx-access.log  main;
    sendfile        on;
    keepalive_timeout  65;

    upstream fastdfs {  
	server 192.168.100.128:9101 weight=1;
	server 192.168.100.129:9101 weight=1;
   	server 192.168.100.130:9101 weight=1;
    }
   server {  
	listen       80;  
	server_name  fastdfs.com;  
	#charset koi8-r;  
	#access_log  logs/host.access.log  main;  
	location / {  
	    proxy_pass   http://fastdfs;  
	    index  index.html index.htm;  
	}

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

	location = /favicon.ico {
	    log_not_found off;
	    access_log off;
	}
    }
}

#重启racker nginx
/etc/nginx/sbin/nginx -s stop
/etc/nginx/sbin/nginx

8、打包storager1容器为本地镜像

将已经配置好的容器打包成镜像可以移植到其他服务器使用

查看容器id
docker ps
打包到本地镜像images
docker commit -a “jackfang” -m “This is fastdfs colony image” 207b59676d3d storage/fastdfs
查看images
docker images

保存为压缩文件实现复制共享
docker save -o storageImage storage/fastdfs

加载本地镜像
docker load < storageImage

9、Springboot集成Fastdfs

9.1 pom依赖

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

9.2 application.properties

# 分布式文件系统fastdfs配置
# socket连接超时时长
fdfs.soTimeout=1500
# 连接tracker服务器超时时长
fdfs.connect-timeout=600
# 从池中借出的对象的最大数目
fdfs.pool.max-total=153
# 获取连接时的最大等待毫秒数100
fdfs.pool.max-wait-millis=102
# 缩略图生成参数,可选
fdfs.thumb-image.height=400
fdfs.thumb-image.width=400
# 跟踪服务器tracker_server请求地址,支持多个,如果有多个在下方加- x.x.x.x:port
fdfs.tracker-list=192.168.100.128:22122,192.168.100.129:22122,192.168.100.130:22122
# 存储服务器storage_server访问地址
fdfs.web-server-url=http://192.168.100.120/

9.3 启动类注入

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

9.4 FastDFSClient工具类

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 文件对象
     * @return 返回文件地址
     * @author jackfang
     * @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 图片对象
     * @return 返回图片地址
     * @author jackfang
     * @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 文件对象
     * @return 返回文件地址
     * @author jackfang
     * @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 图片对象
     * @return 返回图片地址
     * @author jackfang
     * @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数组
     * @param fileExtension 文件扩展名
     * @return 返回文件地址
     * @author jackfang
     * @description 将byte数组生成一个文件上传
     */
    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 jackfang
     * @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 jackfang
     * @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;
    }

}

9.5 测试图片上传

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.util.ResourceUtils;

@SpringBootTest
class FastDFSClientTest {

    @Test
    void uploadFile() throws Exception{
        String path = FastDFSClient.uploadFile(ResourceUtils.getFile("classpath:test.jpg"));
        System.out.println(FastDFSClient.getResAccessUrl(path));
    }
}

获得图片地址:group1/M00/00/00/wKhkgF2_5OWAI6BSAAWnGtMBl6Q804.jpg
拼接上服务器后的地址:
http://192.168.100.120/group1/M00/00/00/wKhkgF2_5OWAI6BSAAWnGtMBl6Q804.jpg
访问:
在这里插入图片描述

9.6 测试生成缩略图

测试生成300x300的缩略图:
http://192.168.100.120/group1/M00/00/00/wKhkgF2_5OWAI6BSAAWnGtMBl6Q804_300x300.jpg
测试结果:
在这里插入图片描述

10、相关操作命令

# 查看docker容器
docker ps -a

# 查看docker images 镜像
docker images

# 执行容器
docker run -it ubuntu:16.04 /bin/bash 

-i和-t的作用是帮助你进入交互模式,如果不加这部分,你将无法进入容器内部,更别说使用命令行了。/bin/bash的意思就是使用容器的命令行来输入命令。
-d表示的就是后台运行

#进入运行中的容器
#exec是进入已经存在的容器,run是创建新容器
docker exec -it c7188be9bd7b /bin/bash

#关闭storage容器
docker container stop storage

#启动storage容器
docker container start storage

#统计storage文件数量:
cd /home/bdxhdata/fastdfs/storage/data
ls -lR|grep "^-"|wc -l

#或者进入data目录下的sync目录,执行以下指令
cat binlog.000 | wc -l
binlog.000保存了storage中所有文件名
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值