FastDFS蛋疼的集群和负载均衡(十)之编写FastDFSUtil

diary_report.jpg

Interesting things

因为元旦,公司放3天假。但是这3天里面也不敢松懈,继续撸FastDFS了。

What did you do today

  • 我们查看fastdfs-client-java的源码,根据自己的需求封装FastDFSUtil,核心方法为upload()、download()、delete()、getFileInfo()、getFileMetaData()。代码没什么好说的,用到的设计模式是单例模式,是用静态内部类实现的单例。
public class FastDFSUtil {

    private static String classPath = FastDFSUtil.class.getProtectionDomain().getCodeSource().getLocation().getPath();

    private static String FASTDFS_CLIENT_CONF = classPath + "fdfs_client.conf";

    private static TrackerClient trackerClient = null;

    private static TrackerServer trackerServer = null;

    private static StorageClient storageClient = null;

    private static StorageServer storageServer = null;

    private FastDFSUtil () {
        FastDFSUtil.init();
    }

    private static void init() {
        try {
            ClientGlobal.init(FASTDFS_CLIENT_CONF);
            trackerClient = new TrackerClient();
            trackerServer = trackerClient.getConnection();
            storageServer = trackerClient.getStoreStorage(trackerServer);
            storageClient = new StorageClient(trackerServer, storageServer);

        } catch (Exception e) {
            throw new RuntimeException("init exception");
        }
    }

    public static FastDFSUtil getInstance() {
        return FastDFSUtilHolder.INSTANCE;
    }

    private static class FastDFSUtilHolder {
        private static final FastDFSUtil INSTANCE = new FastDFSUtil();
    }

    public static String upload(String maydayImg, String fileExtName, NameValuePair[] metaDataList) {
        try {
            String fileIds[] = storageClient.upload_file(maydayImg, fileExtName, metaDataList);

            System.out.println(fileIds.length);
            System.out.println("group:" + fileIds[0]);
            System.out.println("path:" + fileIds[1]);

            return fileIds[0] + fileIds[1];
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("upload exception");
        }
    }

    public static void download(String group, String file, String storePath) {
        try {
            byte[] data = storageClient.download_file(group, file);
            IOUtils.write(data, new FileOutputStream(new File(storePath
                    + "/" + file.substring(file.lastIndexOf("/") + 1))));
            System.out.println("download success");
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("download exception");
        }
    }

    public static FileInfo getFileInfo(String group, String file) {
        try {
            FileInfo fileInfo = storageClient.get_file_info(group, file);
            System.out.println("source ip=" + fileInfo.getSourceIpAddr());
            System.out.println("file sizes=" + fileInfo.getFileSize());
            System.out.println("timestamp=" + fileInfo.getCreateTimestamp());
            System.out.println("crc32=" + fileInfo.getCrc32());

            return fileInfo;
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("gets file info exception");
        }
    }

    public static NameValuePair[] getFileMataData(String group, String file) {
        try {
            NameValuePair[] metas = storageClient.get_metadata(group, file);
            return metas;
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("gets file metadata exception");
        }
    }

    public static void delete(String group, String file) {
        try {
            storageClient.delete_file(group, file);
            System.out.println("detele file success");
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("deletes file exception");
        }
    }
}
  • 整个项目结构是这样子的。
    FastDFS蛋疼的集群和负载均衡(十)之编写FastDFSUtil_第1张图片

  • resources目录下,还需要添加fdfs_client.conf。具体如下:

connect_timeout = 2
network_timeout = 30
charset = UTF-8
http.tracker_http_port = 8000
http.anti_steal_token = no
http.secret_key = FastDFS1234567890

tracker_server = 192.168.12.11:22122
tracker_server = 192.168.12.22:22122
  • 测试upload()方法,我们上传resources/upload/目录下的mayday_life.png。
    public static String BASE_UPLOAD_PATH = TestFastDFS.class.getProtectionDomain().getCodeSource()
            .getLocation().getPath();

    public static String MAYDAY_IMG = BASE_UPLOAD_PATH + "upload/mayday_life.jpg";

        NameValuePair[] metaDataList = new NameValuePair[] {
                new NameValuePair("bound_name", "mayday"),
                new NameValuePair("masterpiece", "juejiang")
        };

        //tests upload mayday_life.jpg
        FastDFSUtil.getInstance().upload(MAYDAY_IMG, "jpg", metaDataList);
  • 上传成功!我们可以去访问http://192.168.12.111/fastdfs/group1/M00/00/00/wKgMLFpHPe-AOvAYAAd8hCbLY3Y881.jpg
    FastDFS蛋疼的集群和负载均衡(十)之编写FastDFSUtil_第2张图片

  • 测试download()方法

        public static String BASE_DOWNLOAD_PATH =
            "D:/myeclipse_workspace/fastdfs_demo/src/main/resources/download";

        //download group1/M00/00/00/wKgMLFpHPe-AOvAYAAd8hCbLY3Y881.jpg
        FastDFSUtil.getInstance().download("group1", "M00/00/00/wKgMLFpHPe-AOvAYAAd8hCbLY3Y881.jpg"
                , BASE_DOWNLOAD_PATH);
  • 下载成功!我们可以看到download文件夹下多了wKgMLFpHPe-AOvAYAAd8hCbLY3Y881.jpg。
    FastDFS蛋疼的集群和负载均衡(十)之编写FastDFSUtil_第3张图片

FastDFS蛋疼的集群和负载均衡(十)之编写FastDFSUtil_第4张图片

  • 测试getFileInfo()方法
        //gets "M00/00/00/wKgMLFpHPe-AOvAYAAd8hCbLY3Y881.jpg" file info
        FastDFSUtil.getInstance().getFileInfo("group1", "M00/00/00/wKgMLFpHPe-AOvAYAAd8hCbLY3Y881.jpg");
  • 获取文件信息成功!
    FastDFS蛋疼的集群和负载均衡(十)之编写FastDFSUtil_第5张图片

  • 测试getFileMetaData()方法

        //gets "M00/00/00/wKgMLFpHPe-AOvAYAAd8hCbLY3Y881.jpg" file metadata
        NameValuePair[] metaLists = FastDFSUtil.getInstance().getFileMataData("group1", "M00/00/00/wKgMLFpHPe-AOvAYAAd8hCbLY3Y881.jpg");

        for (NameValuePair nvp : metaDataList) {
            System.out.println(nvp.getName() + ":" + nvp.getValue());
        }
  • 获取文件元信息成功!
    FastDFS蛋疼的集群和负载均衡(十)之编写FastDFSUtil_第6张图片

  • 测试detele()方法

        //detele "M00/00/00/wKgMLFpHPe-AOvAYAAd8hCbLY3Y881.jpg" file
        FastDFSUtil.getInstance().delete("group1", "M00/00/00/wKgMLFpHPe-AOvAYAAd8hCbLY3Y881.jpg");

FastDFS蛋疼的集群和负载均衡(十)之编写FastDFSUtil_第7张图片

  • 删除成功!我们进入192.168.12.33和192.168.12.44的cd /fastdfs/storage/data/M00/00/00/目录下面已经找不到wKgMLFpHPe-AOvAYAAd8hCbLY3Y881.jpg了。
    image.png
    image.png

  • 最后附上完整测试用例。

public class TestFastDFS {

    public static String BASE_UPLOAD_PATH = TestFastDFS.class.getProtectionDomain().getCodeSource()
            .getLocation().getPath();

    public static String MAYDAY_IMG = BASE_UPLOAD_PATH + "upload/mayday_life.jpg";

    public static String BASE_DOWNLOAD_PATH =
            "D:/myeclipse_workspace/fastdfs_demo/src/main/resources/download";

    public static void main(String[] args) {
        NameValuePair[] metaDataList = new NameValuePair[] {
                new NameValuePair("bound_name", "mayday"),
                new NameValuePair("masterpiece", "juejiang")
        };

        //tests upload mayday_life.jpg
        //FastDFSUtil.getInstance().upload(MAYDAY_IMG, "jpg", metaDataList);

        //upload success
        //group:group1
        //path:M00/00/00/wKgMLFpHPe-AOvAYAAd8hCbLY3Y881.jpg

        //download group1/M00/00/00/wKgMLFpHPe-AOvAYAAd8hCbLY3Y881.jpg
        //FastDFSUtil.getInstance().download("group1", "M00/00/00/wKgMLFpHPe-AOvAYAAd8hCbLY3Y881.jpg"
                //, BASE_DOWNLOAD_PATH);

        //gets "M00/00/00/wKgMLFpHPe-AOvAYAAd8hCbLY3Y881.jpg" file info
        //FastDFSUtil.getInstance().getFileInfo("group1", "M00/00/00/wKgMLFpHPe-AOvAYAAd8hCbLY3Y881.jpg");

        //gets "M00/00/00/wKgMLFpHPe-AOvAYAAd8hCbLY3Y881.jpg" file metadata
        //NameValuePair[] metaLists = FastDFSUtil.getInstance().getFileMataData("group1", "M00/00/00/wKgMLFpHPe-AOvAYAAd8hCbLY3Y881.jpg");

        //for (NameValuePair nvp : metaDataList) {
            //System.out.println(nvp.getName() + ":" + nvp.getValue());
        //}

        //detele "M00/00/00/wKgMLFpHPe-AOvAYAAd8hCbLY3Y881.jpg" file
        FastDFSUtil.getInstance().delete("group1", "M00/00/00/wKgMLFpHPe-AOvAYAAd8hCbLY3Y881.jpg");

    }
}

Summary

写完了,就要陪女朋友吃螺蛳粉,逛万达广场,看电影咯。

你可能感兴趣的:(架构师成长之路)