FastDFS笔记

FastDFS搭建

安装FastDFS镜像

# 拉取镜像
docker pull morunchang/fastdfs

# 运行 tracker 
docker run -d --name tracker --net=host morunchang/fastdfs sh tracker.sh
# 运行 storage
# 使用的网络模式是–net=host, 192.168.22.132是宿主机的IP
# group1是组名,即storage的组
# 如果想要增加新的storage服务器,再次运行该命令,注意更换 新组名
docker run -d --name storage --net=host -e TRACKER_IP=192.168.211.132:22122 -e GROUP_NAME=group1 morunchang/fastdfs sh storage.sh

# 查看 tracker 和 storage 运行的端口号(开放端口号使用)
netstat -unltp | grep fdfs
# tcp        0      0 0.0.0.0:22122           0.0.0.0:*               LISTEN      95499/fdfs_trackerd 
# tcp        0      0 0.0.0.0:23000           0.0.0.0:*               LISTEN      95572/fdfs_storaged 

配置Nginx

Nginx在这里主要提供对FastDFS图片访问的支持,Docker容器中已经集成了Nginx,我们需要修改nginx的配置,进入storage的容器内部,修改nginx.conf

# 进入storage容器内部
docker exec -it storage  /bin/bash

vim /etc/nginx/conf/nginx.conf
# location ~ /M00 {
#	   add_header Cache-Control no-store; # 设置禁用缓存
#      root /data/fast_data/data;
#      ngx_fastdfs_module;
# }

# 重启storage
docker restart storage

# 开启自动启动
docker update --restart=always tracker
docker update --restart=always storage

配置防盗链

修改storage配置文件

# 进入storage容器内部
docker exec -it storage  /bin/bash
vim /etc/fdfs/http.conf
# 修改配置文件,配置文件如下
# HTTP default content type
http.default_content_type = application/octet-stream

# MIME types mapping filename
# MIME types file format: MIME_type  extensions
# such as:  image/jpeg  jpeg jpg jpe
# you can use apache's MIME file: mime.types
http.mime_types_filename=mime.types

# if use token to anti-steal
# default value is false (0)
# 开启token检验
http.anti_steal.check_token=true

# token TTL (time to live), seconds
# default value is 600
# 设置token有效期,单位秒
http.anti_steal.token_ttl=1800

# secret key to generate anti-steal token
# this parameter must be set when http.anti_steal.check_token set to true
# the length of the secret key should not exceed 128 bytes
# 设置密钥,java工程的配置文件中使用
http.anti_steal.secret_key=xbsheng

生成token拼在url后面

http://192.168.232.132:8080/group1/M00/00/00/wKjohF–L4SAb72oAAEjaW32kIg668.jpg?token=dad8c3974df8879fd7f88f1afe40f93b&ts=1606299819

java使用

配置

  • pom.xml依赖
<!--依赖包-->
<dependencies>
    <dependency>
        <groupId>net.oschina.zcx7878</groupId>
        <artifactId>fastdfs-client-java</artifactId>
        <version>1.27.0.0</version>
    </dependency>
</dependencies>
  • FastDFS配置
    在resources文件夹下创建fasfDFS的配置文件fdfs_client.conf
connect_timeout=60
network_timeout=60
charset=UTF-8
http.tracker_http_port=8080
tracker_server=192.168.211.132:22122

connect_timeout:连接超时时间,单位为秒。
network_timeout:通信超时时间,单位为秒。发送或接收数据时。假设在超时时间后还不能发送或接收数据,则本次网络通信失败
charset: 字符集
http.tracker_http_port :.tracker的http端口
tracker_server: tracker服务器IP和端口设置

  • application.yml配置
spring:
  servlet:
    multipart:
      max-file-size: 10MB
      max-request-size: 10MB
  application:
    name: file
server:
  port: 18082
eureka:
  client:
    service-url:
      defaultZone: http://127.0.0.1:7001/eureka
  instance:
    prefer-ip-address: true
feign:
  hystrix:
    enabled: true

max-file-size是单个文件大小,max-request-size是设置总上传的数据大小

  • 启动类
@SpringBootApplication(exclude={DataSourceAutoConfiguration.class}) // 排除掉数据库检查
@EnableEurekaClient
public class FileApplication {

    public static void main(String[] args) {
        SpringApplication.run(FileApplication.class);
    }
}

java代码

码云仓库

package com.changgou.util;

import com.changgou.file.FastDFSFile;
import org.csource.common.NameValuePair;
import org.csource.fastdfs.*;
import org.springframework.core.io.ClassPathResource;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.time.Instant;

public class FastDFSUtil {

    /**
     * @Description 加载Tracker连接信息
     * @author xbsheng
     * @Datetime 2020/11/24 17:23
     * @param null:
     * @return null
     */
    static {
        // 查找classpath下的文件路径
        String fileName = new ClassPathResource("fdfs_client.conf").getPath();
        try {
            ClientGlobal.init(fileName);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * @param fastDFSFile :
     * @return void
     * @Description 文件上传
     * @author xbsheng
     * @Datetime 2020/11/24 17:31
     */
    public static String[] upload(FastDFSFile fastDFSFile) throws Exception {
        // 附加信息
        NameValuePair[] meta_list = new NameValuePair[1];
        meta_list[0] = new NameValuePair("author", fastDFSFile.getAuthor());

        StorageClient storageClient = getStorageClient();

        return storageClient.upload_file(fastDFSFile.getContent(), fastDFSFile.getExt(), meta_list);
    }

    public static ByteArrayInputStream download(String groupName, String remoteFileName) {
        try {
            StorageClient storageClient = getStorageClient();
            byte[] bytes = storageClient.download_file(groupName, remoteFileName);
            return new ByteArrayInputStream(bytes);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    /**
     * @param groupName:
     * @param remoteFileName:
     * @return int
     * @Description 删除文件
     * @author xbsheng
     * @Datetime 2020/11/25 18:21
     */
    public static int delete(String groupName, String remoteFileName) {
        try {
            StorageClient storageClient = getStorageClient();
            return storageClient.delete_file(groupName, remoteFileName);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * @param groupName:组名
     * @param remoteFileName:文件名
     * @return java.lang.String
     * @Description 获取文件的url
     * @author xbsheng
     * @Datetime 2020/11/25 18:02
     */
    public static String getFileUrl(String groupName, String remoteFileName) throws Exception {
        // 获取ip、端口号
        ServerInfo[] serverInfo = getServerInfo(groupName, remoteFileName);
        String ipAddr = serverInfo[0].getIpAddr();

        // 获取token
        String token = getToken(remoteFileName);
        return "http://" + ipAddr + ":8080/" + groupName + "/" + remoteFileName + token;
    }

    /**
     * @param remote_filename: "M00/00/00/wKjohF-8-GOAR3RRAAAAZCcObRY538.txt"
     * @return java.lang.String "?token=zxcxcz&ts=165355322"
     * @Description 获取token
     * @author xbsheng
     * @Datetime 2020/11/24 22:38
     */
    private static String getToken(String remote_filename) throws Exception {
        int ts = (int) Instant.now().getEpochSecond();
        String secret_key = "xbsheng";
//        String remote_filename = "M00/00/00/wKjohF-8-GOAR3RRAAAAZCcObRY538.txt";
        String token = ProtoCommon.getToken(remote_filename, ts, secret_key);
        System.out.println(token);
        return "?token=" + token + "&ts=" + ts;
    }

    private static ServerInfo[] getServerInfo(String groupName, String remoteFileName) {
        TrackerClient trackerClient = new TrackerClient();
        try {
            TrackerServer trackerServer = trackerClient.getConnection();
            return trackerClient.getFetchStorages(trackerServer, groupName, remoteFileName);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * @return org.csource.fastdfs.TrackerServer
     * @Description 获取TrackerServer
     * @author xbsheng
     * @Datetime 2020/11/25 17:26
     */
    private static TrackerServer getTrackerServer() throws Exception {
        TrackerClient trackerClient = new TrackerClient();
        return trackerClient.getConnection();
    }

    /**
     * @return org.csource.fastdfs.StorageClient
     * @Description 获取StorageClient
     * @author xbsheng
     * @Datetime 2020/11/25 17:27
     */
    private static StorageClient getStorageClient() throws Exception {
        TrackerServer trackerServer = getTrackerServer();
        return new StorageClient(trackerServer, null);
    }

    public static void main(String[] args) throws Exception {
        String groupName = "group1";
        String remoteFileName = "M00/00/00/wKjohF--L4SAb72oAAEjaW32kIg668.jpg";
//        String fileUrl = getFileUrl(groupName, remoteFileName);
//        System.out.println(fileUrl);
        delete(groupName, remoteFileName);

//        int i = delete("group1", "M00/00/00/wKjohF--JbuAPOAvAACtSS2Mzng358.jpg");
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值