分布式文件服务器FastDFS小案例

一。下载镜像

  1. 阿里云容器仓库下载

  2. 容器镜像下载

[root@192 ~]# docker image pull registry.cn-hangzhou.aliyuncs.com/tdenergys/fastdfs
[root@192 ~]# docker image list
REPOSITORY                                            TAG                 IMAGE ID            CREATED             SIZE
dubbo-admin                                           latest              4b43bc0f997e        3 weeks ago         360MB
mysql                                                 5.6                 27e29668a08a        7 weeks ago         256MB
zookeeper                                             latest              64e049ee9478        6 months ago        148MB
registry.cn-hangzhou.aliyuncs.com/tdenergys/fastdfs   latest              63ca62a9f44c        18 months ago       457MB
[root@192 ~]# docker image tag registry.cn-hangzhou.aliyuncs.com/tdenergys/fastdfs fastdfs
[root@192 ~]# docker image rm registry.cn-hangzhou.aliyuncs.com/tdenergys/fastdfs
Untagged: registry.cn-hangzhou.aliyuncs.com/tdenergys/fastdfs:latest
Untagged: registry.cn-hangzhou.aliyuncs.com/tdenergys/fastdfs@sha256:9aae50225ded5ec4f309e01dbcdf113c3d961fba3b071587ff49c9efde9376d1
[root@192 ~]# docker image list
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
dubbo-admin         latest              4b43bc0f997e        3 weeks ago         360MB
mysql               5.6                 27e29668a08a        7 weeks ago         256MB
zookeeper           latest              64e049ee9478        6 months ago        148MB
fastdfs             latest              63ca62a9f44c        18 months ago       457MB
[root@192 ~]# 

  3. 运行tracker

[root@192 ~]# docker container run -d --name fastdfs_tracker -v /data/fast_data/:/data/fast_data/ --net=host fastdfs sh tracker.sh
6e1390cee5c81cb59a8d44a2cb45d3b8f5209f8fe60e5a095bd9aac770f4c074
[root@192 ~]# 

-d:后天运行

--name :命名容器

-v :主机文件夹挂载到容器里

--net:网络类型,host只有主机和容器互通

  4. 运行storage

[root@192 ~]# docker container run -d --name fastdfs_storage -e TRACKER_IP=10.20.0.129:22122 -v /data/fast_data/:/data/fast_data/ --net=host -e GROUP_NAME=group1 -e STORAGE_PORT=23000 -e HTTP_PORT=8888 fastdfs sh storage.sh
9fead632e2fd644aeb43cd3b6b568bcb507545eecf39fc4462ca6d2ead76bfe7
[root@192 ~]# 

-e:环境变量,即运行参数

二。新建案例

  1. 新建工程:fastDFSdemo,并继承parent父工程的依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>parent</artifactId>
        <groupId>com.zgh</groupId>
        <version>1.0-SNAPSHOT</version>
        <relativePath>../parent/pom.xml</relativePath>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>fastDFSdemo</artifactId>


</project>

  2. 新建配置文件:fdfs_client.properties ,并修改ip地址

# 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=/home/fastdfs

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

tracker_server=10.20.0.129: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. 新建java文件

import org.csource.fastdfs.*;

public class dfs {
    public static void main(String[] args) throws Exception {
        // 1、加载配置文件,配置文件中的内容就是 tracker 服务的地址。
        ClientGlobal.init("E:\\IdeaProjects\\Demo\\fastDFSdemo\\src\\main\\resources\\fdfs_client.properties");
        // 2、创建一个 TrackerClient 对象。直接 new 一个。
        TrackerClient trackerClient = new TrackerClient();
        // 3、使用 TrackerClient 对象创建连接,获得一个 TrackerServer 对象。
        TrackerServer trackerServer = trackerClient.getConnection();
        // 4、创建一个 StorageServer 的引用,值为 null
        StorageServer storageServer = null;
        // 5、创建一个 StorageClient 对象,需要两个参数 TrackerServer 对象、StorageServer 的引用
        StorageClient storageClient = new StorageClient(trackerServer, storageServer);
        // 6、使用 StorageClient 对象上传图片。
        //扩展名不带“.”
        String[] strings = storageClient.upload_file("E:\\IdeaProjects\\Demo\\fastDFSdemo\\src\\main\\resources\\timg.jpg", "jpg",
                null);
        // 7、返回数组。包含组名和图片的路径。
        for (String string : strings) {
            System.out.println(string);
        }
    }
}

  4. 运行结果

  5. 浏览器打开可获取图片:http://10.20.0.129:8888/group1/M00/00/00/ChQAgVxoFdiABydJAABVAWt0ukc025.jpg

 

posted @ 2019-02-16 21:58  Python++  阅读(308)  评论(0编辑  收藏  举报