fastdfs 单机多groupName部署和springboot区分环境上传

fastdfs 单机多groupName部署和springboot区分环境上传

fastdfs

fastdfs是轻量级小文件存储不错的选择。网上有大量的资料介绍,这里就不多说了。github地址是:https://github.com/happyfish100/fastdfs

部署

  1. 安装组件
./make.sh
./make.sh install
  • 下载fastdfs源码,然后进入目录。
./make.sh
./make.sh install

拷贝conf目录中的http.conf和mime.types到/etc/fdfs目录。这里一定要注意否则报错!!

  • 下载nginx
    nginx-1.15.5、openssl-1.1.1、pcre-8.42、zlib-1.2.11
  • 下载fastdfs和nginx的mod fastdfs-nginx-module
    拷贝 mod_fastdfs.conf到/etc/fdfs目录

2.配置文件和启动

  • tracker.conf
    base_path,改为自己的工作目录。
  • 启动tracker
    /usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf
  • storage-test.conf
    这里我为了区分不同的storage使用了不同的配置文件,分别代表测试环境和生产环境。
    port,监听端口号
    base_path,改为自己的工作目录
    store_path_count,如果多目录这里可以改
    store_path1,如果多目录这里数字可以往后排,注意这是测试环境的配置,所以这个目录的地址和生产环境的不同
    tracker_server,tracker服务器的地址和端口号
    group_name,这里是组名,用于在上传和下载的时候区分不同的组。这里取名test
  • storage-prod.conf
    配置跟-test基本相同,不同的是下面几个地方
    port、base_path、store_path1、group_name
  • storage 启动的时候区分配置文件
    /usr/bin/fdfs_storaged /etc/fdfs/storage-test.conf
    /usr/bin/fdfs_storaged /etc/fdfs/storage-prod.conf
  • mod_fastdfs.conf
    base_path,工作目录
    tracker_server,tracker服务器地址
    group_name,由于是多个groupName,所以用斜线分隔开,例如:test/prod
    group_count,两个组,所以这里是2
    然后配置区分两个组的地方
[group1]
group_name=test
storage_server_port=23000
store_path_count=1
store_path0=/data/fastdfs/storagetest/storefile

[group2]
group_name=prod
storage_server_port=24000
store_path_count=1
store_path0=/data/fastdfs/storageprod/storefile
  • 安装nginx
./configure --prefix=/opt/nginx --with-http_stub_status_module --with-http_ssl_module --with-pcre=/opt/pcre-8.42 --with-openssl=/opt/openssl-1.0.2q  --with-zlib=/opt/zlib-1.2.11 --with-http_realip_module  --add-module=/opt/fastdfs-nginx-module-master/src
make
make install

springboot区分环境上传

  1. 下载代码
    fastdfs-clint-java
  2. springboot区分环境。
    application.yml 放总的配置信息,如下:
fdfs:
  fastdfs.connect_timeout_in_seconds: 5
  fastdfs.network_timeout_in_seconds: 30
  fastdfs.charset: UTF-8
  fastdfs.http_anti_steal_token: false
  fastdfs.http_tracker_http_port: 22122
  fastdfs.http_secret_key: FastDFS1234567890
  fastdfs.tracker_servers: 127.0.0.1:22122

application-test.yml 放测试环境

fdfs:
  groupName: test

application-prod.yml 放生产环境

fdfs:
  groupName: prod

写一个properties来接收配置

@ConfigurationProperties("")
public class FdfsProperties {
    private final Map<String, String> fdfs = new HashMap<>();

    public Map<String, String> getFdfs() {
        return fdfs;
    }
}

文件上传的类

@Component
public class FileManager {

    private Logger log = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private FdfsProperties fdfsProperties;

    @PostConstruct
    public void init() throws IOException, MyException {
        Properties properties=new Properties();
        properties.putAll(fdfsProperties.getFdfs());
        //写入fdfs配置
        ClientGlobal.initByProperties(properties);
    }

    public String upload(FileItem file) {
        TrackerServer trackerServer=null;
        try {
            TrackerClient trackerClient = new TrackerClient();
            trackerServer= trackerClient.getConnection();
            StorageServer storageServer = null;
            StorageClient1 storageClient = new StorageClient1(trackerServer,storageServer);
            NameValuePair[] metas=new NameValuePair[]{new NameValuePair("fileName",file.getFileName())};
            String result = storageClient.upload_file1(fdfsProperties.getFdfs().get("groupName"),file.getContent(), file.getFileName().split("\\.")[1],metas);
           return result;
        } catch (Exception e) {
            log.error(Throwables.getStackTraceAsString(e));
            throw new BusinessException("上传错误!");
        } finally {
            if(trackerServer!=null){
                try {
                    trackerServer.close();
                } catch (IOException e) {
                    throw new BusinessException("上传错误!");
                }
            }
        }
    }
}

FileItem.java

public class FileItem implements Serializable{
    private String fileName;
    private byte[] content;

    public String getFileName() {
        return fileName;
    }

    public void setFileName(String fileName) {
        this.fileName = fileName;
    }

    public byte[] getContent() {
        return content;
    }

    public void setContent(byte[] content) {
        this.content = content;
    }
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值