FastDfs集群搭建&java客户端

安装包下载:https://github.com/happyfish100/fastdfs/releases

1.安装libfastcommon

tar -zxvf libfastcommon-1.0.7.tar.gz

cd libfastcommon-1.0.7

./make.sh

./make.sh install


2.安装FastDfs

tar -zxvf FastDFS_v5.05.tar.gz

cd FastDFS

./make.sh

./make.sh install

拷贝配置

cp  /usr/local/FastDFS/conf/* /etc/fdfs/


3.编辑tracker配置

vim /etc/fdfs/tracker.conf

修改文件存放位置

base_path=/home/yuqing/fastdfs


4.启动tracker

启动:/usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf
重启:/usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf restart

设置开机自动启动

编辑配置:vim /etc/rc.d/rc.local
添加命令:/usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf restart


5.编辑storage配置

vim /etc/fdfs/storage.conf

base_path=/home/yuqing/fastdfs:storage服务日志存放路径,该路径必须存在

store_path0=/home/yuqing/fastdfs:图片的保存路径,该路径必须存在

tracker_server=192.168.25.133:22122:指定tracker服务器的ip和端口


6.启动storageServer

启动:/usr/bin/fdfs_storaged /etc/fdfs/storage.conf

重启:/usr/bin/fdfs_storaged /etc/fdfs/storage.conf restart



设置开机自启动

编辑 vim /etc/rc.d/rc.local

添加 /usr/bin/fdfs_storaged /etc/fdfs/storage.conf restart


7.安装nginx及外网访问插件

tar -zxvf  fastdfs-nginx-module_v1.16.tar.gz

tar -zxvf nginx-1.7.0.tar.gz

cd nginx-1.7.0
//编译安装 nginx附带 fastdfs-nginx-module模块
./configure --user=nginx --group=nginx--prefix=/usr/local/nginx --add-module=../fastdfs-nginx-module/src
./make
./make install

编辑nginx配置

server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location /group1/M00 {
             root /home/yuqing/fastdfs/data;
             ngx_fastdfs_module;
        }

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



JAVA客户端:

public class FastDfsUtil {

    private static FastDfsManager fastDfsManager;

    private static FastDfsProperties fastDfsProperties;

    private final static Logger LOGGER = LoggerFactory.getLogger(FastDfsUtil.class);

    static {
        if (fastDfsManager == null) {
            fastDfsManager = SpringUtil.getBean("fastDfsManager");
        }
        if (fastDfsProperties == null) {
            fastDfsProperties = SpringUtil.getBean("fastDfsProperties");
        }
    }

    /**
     * 上传文件
     * @param filePath 本地文件路径
     * @return 返回文件存储位置
     */
    public static String uploadFile(String filePath) throws Exception {
        if(StringUtils.isBlank(filePath)){
            throw new Exception("param error,filePath:" + filePath);
        }
        File file = new File(filePath);
        // 获取文件后缀名
        String prefix=filePath.substring(filePath.lastIndexOf(".")+1);
        NameValuePair[] metaList = new NameValuePair[]{
                new NameValuePair(MetaKey.FILE_NAME, file.getName()),
                new NameValuePair(MetaKey.FILE_LENGTH, String.valueOf(file.length())),
                new NameValuePair(MetaKey.FILE_PREFIX, prefix)
        };
        return fastDfsManager.uploadFile(filePath,
                prefix, metaList);
    }

    /**
     * 获取文件下载地址
     * @param fileId 上传文件时返回的存储位置
     * @return 返回文件下载地址
     */
    public static String getFileDownUrl(String fileId) throws Exception{
        if(StringUtils.isBlank(fileId)){
            throw new Exception("param error,fileId:" + fileId);
        }
        int ts = (int) Instant.now().getEpochSecond();
        // 生成token需要去除路径的group
        String fileId2 = fileId.substring(fileId.indexOf("/")+1,fileId.length());
        String token = fastDfsManager.getToken(fileId2, ts);
        Map<String,String> metaMap = getFileMetaData(fileId);
        String fileName = "";
        if(metaMap != null ){
            fileName = StringUtils.isBlank(metaMap.get(MetaKey.FILE_NAME)) ? "":metaMap.get(MetaKey.FILE_NAME);
        }
        return fastDfsProperties.getHost() + fileId + "?token=" + token + "&ts=" + ts + "&filename=" + fileName;
    }

    
    /**
     * 下载文件
     * @param fileId 上传文件时返回的存储位置
     * @param localFileName 目标文件名
     * @return 是否下载成功
     */
    public static boolean downLoadFile(String fileId,String localFileName) throws Exception{
        if(StringUtils.isAnyBlank(fileId,localFileName)){
            throw new Exception("param error,fileId:" + fileId + ",localFileName:" + localFileName);
        }
        return fastDfsManager.downLoadFile(fileId, localFileName) == 0;
    }

    /**
     * 删除文件
     * @param fileId 上传文件时返回的存储位置
     * @return 是否删除成功
     */
    public static boolean deleteFile(String fileId) throws Exception {
        if(StringUtils.isBlank(fileId)){
            throw new Exception("param error,fileId:" + fileId);
        }
        return fastDfsManager.deleteFile(fileId);
    }

    /**
     * 获取文件元数据map
     * @param fileId 上传文件时返回的存储位置
     * @return 元数据map
     */
    public static Map<String,String> getFileMetaData(String fileId) throws Exception {
        if(StringUtils.isBlank(fileId)){
            throw new Exception("param error,fileId:" + fileId);
        }
        NameValuePair[] metaList = fastDfsManager.getFileMetaData(fileId);
        Map<String,String> map = null;
        if( metaList!=null ){
            map = Maps.newHashMap();
            for(NameValuePair meta : metaList) {
                map.put(meta.getName(),meta.getValue());
            }
        }
        return map;
    }

}
@Lazy
@Component
public class FastDfsManager {

    private static final Logger LOGGER = LoggerFactory.getLogger(FastDfsManager.class);

    private StorageClient1 storageClient;

    @Resource
    private FastDfsProperties fastDfsProperties;

    @PostConstruct
    public void init(){
        try {
            Properties props = new Properties();
            props.put(ClientGlobal.PROP_KEY_TRACKER_SERVERS,fastDfsProperties.getTrackerServers());
            props.put(ClientGlobal.PROP_KEY_HTTP_SECRET_KEY,fastDfsProperties.getHttpSecretKey());
            props.put(ClientGlobal.PROP_KEY_HTTP_ANTI_STEAL_TOKEN,fastDfsProperties.getHttpAntiStealToken());
            ClientGlobal.initByProperties(props);
            TrackerClient trackerClient = new TrackerClient();
            TrackerServer trackerServer = trackerClient.getConnection();
            StorageServer storageServer = trackerClient.getStoreStorage(trackerServer);
            storageClient = new StorageClient1(trackerServer, storageServer);
        } catch (Exception e) {
            LOGGER.error("FastDfs初始化失败:{}",e);
        }
    }

    /**
     * 上传文件
     * @param filePath 本地文件路径
     * @param prefix 文件后缀名
     * @param metaList 文件元信息
     * @return 返回文件存储位置
     */
    public String uploadFile(String filePath,String prefix,NameValuePair[] metaList) throws IOException, MyException {
        return storageClient.upload_file1(filePath,
                prefix, metaList);
    }

    /**
     * 获取token
     * @param fileId 文件ID (不含group)
     * @param ts 时间戳(秒)
     * @return token a5eb699aaaf3b8fd70a54f90b395cff7
     */
    public String getToken(String fileId,int ts) throws UnsupportedEncodingException, NoSuchAlgorithmException, MyException {
        return ProtoCommon.getToken(fileId, ts, fastDfsProperties.getHttpSecretKey());
    }

    /**
     * 删除文件
     * @param fileId 文件ID
     * @return 是否删除成功
     */
    public boolean deleteFile(String fileId) throws IOException, MyException {
        return storageClient.delete_file1(fileId) == 2;
    }

    /**
     * 获取文件元数据
     * @param fileId 文件ID
     * @return 元数据
     */
    public NameValuePair[] getFileMetaData(String fileId) throws IOException, MyException {
        return storageClient.get_metadata1(fileId);
    }

    /**
     * 下载文件
     * @param fileId 文件ID
     * @param localFileName 目标文件名
     * @return 是否下载成功
     */
    public int downLoadFile(String fileId, String localFileName) throws IOException, MyException {
        return storageClient.download_file1(fileId, localFileName);
    }
}

#fastdfs配置
#tracker连接地址
fastdfs.tracker-servers=192.168.10.139:22122
#http secretKey 用于http连接时生成token,key需要与服务器配置保持一致
fastdfs.http-secret-key=JucaicatFastDFS
#开启token验证
fastdfs.http-anti-steal-token=true
#外网下载host地址
fastdfs.host=http://192.168.10.139/





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值