使用docker安装fastdfs


fastdfs

1、使用docker安装fastdfs

docker run -dti \
--network=host \
--name tracker \
-v /opt/fastdfs/tracker:/var/fdfs \
-v /etc/localtime:/etc/localtime \
delron/fastdfs tracker

docker run -dti  \
--network=host \
--name storage \
-e TRACKER_SERVER="0.0.0.0:22122" \
-v /opt/fastdfs/storage:/var/fdfs  \
-v /etc/localtime:/etc/localtime  \
delron/fastdfs storage

TRACKER_SERVER为tracker的地址

/etc/localtime同步时间

运行后,使用的端口为:

fdfs_storaged:23000

fdfs_trackerd: 22122

2、java操作fastdfs

2.1、引入依赖

先运行:

mvn install:install-file                           \
-Dfile="lib\fastdfs-client-java-1.27-SNAPSHOT.jar" \
-Dpackaging=jar                                    \
-DgroupId=org.csource                              \
-DartifactId=fastdfs-client-java                   \
-Dversion=1.27

再在pom.xml中添加:

<dependency>
    <groupId>org.csource</groupId>
    <artifactId>fastdfs-client-java</artifactId>
    <version>1.27</version>
</dependency>

此依赖通过远程仓库无法加载

2.2、配置初始化

初始化方式有多种

2.2.1、配置文件初始化

String tracker = GmallManageWebApplicationTests.class.getResource("/tracker.conf").getPath();// 配置文件路径
ClientGlobal.init(tracker);

tracker.conf:

# tracker服务器的地址,注意端口不是访问文件端口是通讯端口
tracker_server=192.168.2.128:22122
# 连接超时时间,针对socket套接字函数connect,默认为30秒
connect_timeout=30000
# 网络通讯超时时间,默认是60秒
network_timeout=60000

2.2.2、Properties初始化

Properties properties = new Properties();
properties.setProperty("fastdfs.connect_timeout_in_seconds", "30");
properties.setProperty("fastdfs.network_timeout_in_seconds", "60");
properties.setProperty("fastdfs.tracker_servers", "192.168.18.188:22122");
ClientGlobal.initByProperties(properties);

初始化参数中,tracker_server是必须的,其他是可选的。tracker_server的格式要是: host:port

2.2、获取StorageClient对象

TrackerClient trackerClient = new TrackerClient();
// 获得一个trackerServer的实例
TrackerServer trackerServer = trackerClient.getConnection();
// 通过tracker获得一个Storage链接客户端
StorageClient storageClient = new StorageClient(trackerServer,null);

StorageClient对象可以进行上传下载删除修改等操作

2.3、工具类

import org.csource.common.MyException;
import org.csource.fastdfs.ClientGlobal;
import org.csource.fastdfs.StorageClient;
import org.csource.fastdfs.TrackerClient;
import org.csource.fastdfs.TrackerServer;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;
import java.io.IOException;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Configuration
@ConfigurationProperties("fastdfs")
public class FastdfsUtil {
    private String trackerServer;
    private String networkTimeoutInSeconds;
    private String connectTimeoutInSeconds;
    private static String accessPath;

    public void setAccessPath(String accessPath) {
        FastdfsUtil.accessPath = accessPath;
    }

    public void setTrackerServer(String trackerServer) {
        this.trackerServer = trackerServer;
    }

    public void setNetworkTimeoutInSeconds(String networkTimeoutInSeconds) {
        this.networkTimeoutInSeconds = networkTimeoutInSeconds;
    }

    public void setConnectTimeoutInSeconds(String connectTimeoutInSeconds) {
        this.connectTimeoutInSeconds = connectTimeoutInSeconds;
    }

    private static StorageClient client;
    @PostConstruct
    private void init() throws IOException, MyException {
        Properties properties = new Properties();
        properties.setProperty("fastdfs.connect_timeout_in_seconds", connectTimeoutInSeconds);
        properties.setProperty("fastdfs.network_timeout_in_seconds", networkTimeoutInSeconds);
        properties.setProperty("fastdfs.tracker_servers", trackerServer);
        ClientGlobal.initByProperties(properties);
        TrackerClient trackerClient = new TrackerClient();
        // 获得一个trackerServer的实例
        TrackerServer trackerServer = trackerClient.getConnection();
        client = new StorageClient(trackerServer, null);  //并发量小的时候重用StorageClient对象没啥问题,但他不是线程安全的!!
                                                        //解决办法是使用ThreadLocal懒加载赋值,或者每次使用都new一个
    }
// 以上为初始化
    //获取可操作的客户端
    public static StorageClient getClient() {
        return client;
    }
    //文件信息转地址工具
    public static String getUrl(String[] uploadInfos){
        StringBuilder url = new StringBuilder(accessPath);
        for (String uploadInfo : uploadInfos) {
            url.append("/").append(uploadInfo);
        }
        return url.toString();
    }
    //根据文件地址删除文件
    public static void delete(String url) throws IOException, MyException {
        final StorageClient client = FastdfsUtil.getClient();
        final Pattern compile = Pattern.compile(".*(group\\d+)/(.*)");
        final Matcher matcher = compile.matcher(url);
        if (matcher.find()) {
            System.out.println(client.delete_file(matcher.group(1), matcher.group(2)));
        }else{
            System.out.println("未找到: " + url);
        }
    }
}

配置文件application.yml:

fastdfs:
  tracker_server: 192.168.18.188:22122
  connect_timeout_in_seconds: 30
  network_timeout_in_seconds: 60
  accessPath: http://192.168.18.188:8888