FastDFS、Nginx安装、配置、测试(java client)

news/2024/7/7 15:12:42

生产环境结果应该如下:

 

本文tracker、storage都在一台机器上!本机既是tracker,又是storage,仅用于测试!

 

一、需要下载的

fastdfs-nginx-module_v1.15.tar.gz

nginx-1.4.1.tar.gz

libevent-2.0.21-stable.tar.gz

FastDFS_v4.06.tar.gz

二、安装

1、安装libevent,nginx都是一个套路

tar –xvf xxxxxx.tar.gz

./configure --prefix=xxxxxxx

make && make install

不多说。

2、安装FastDFS

指定下刚才安装的libevent的path

/make.sh C_INCLUDE_PATH=/usr/local/libevent/include LIBRARY_PATH=/usr/local/libevent/lib
/make.shinstall

3、配置FastDFS

PS,FastDFS分为三个Server

Tracker:接受请求,调度请求,路由请求

Storage:文件存放的地方

Client:客户端,发起请求

默认配置文件在/etc/fdfs/,有需要可以修改。

配置项都有注释,仅列一下我修改了的

  • tracker.conf :

base_path=/fdfs

# base_path 附目录说明:

tracker server目录及文件结构:
${base_path}
|__data
|     |__storage_groups.dat:存储分组信息
|     |__storage_servers.dat:存储服务器列表
|__logs
|__trackerd.log:tracker server日志文件

  • storage.conf

base_path=/fdfs

store_path0=/fdfs

tracker_server=192.168.20.160:22122

  • client.conf

base_path=/fdfs

tracker_server=192.168.20.160:22122

http.tracker_server_port=8888(改成nginx的端口)

改完上面的配置文件,已经基本OK了

启动tracker

fdfs_trackerd /etc/fdfs/tracker.conf

启动storage

fdfs_storaged /etc/fdfs/storage.conf

上传文件

fdfs_upload_file /etc/fdfs/http.conf /tmp/f.png

--返回group1/M00/00/00/wKgUoFGwT1WAFqIfAAA-xlb-210799.png

 

下面配置下nginx,让你上传文件可以通过url访问到

1.修改nginx配置文件

location /M00 {
            root /fdfs/data;
            ngx_fastdfs_module;
}

2.fastdfs的nginx的module中的mod_fastdfs.conf,copy到/etc/fdfs下,修改配置

base_path=/tmp

tracker_server=192.168.20.160:22122

store_path0=/fdfs

3.启动nginx,

http://192.168.20.160:8888/M00/00/00/wKgUoFGwT1WAFqIfAAA-xlb-210799.png

可以访问到刚才上传的图片!

这里不需要/group1,如果想在url中加入group1,需要修改fdfs的配置文件和nginx的配置文件!

三、Java client

 

    public String conf_filename = "F:\\workspace\\fastdfs\\src\\fdfs_client.conf";
    public String local_filename = "F:\\workspace\\fastdfs\\src\\fdfs_client.conf";

    @org.junit.Test
    public void testUpload() {

        try {
            ClientGlobal.init(conf_filename);

            TrackerClient tracker = new TrackerClient();
            TrackerServer trackerServer = tracker.getConnection();
            StorageServer storageServer = null;

            StorageClient storageClient = new StorageClient(trackerServer,
                    storageServer);
//            NameValuePair nvp = new NameValuePair("age", "18");
            NameValuePair nvp [] = new NameValuePair[]{
                    new NameValuePair("age", "18"),
                    new NameValuePair("sex", "male")
            };
            String fileIds[] = storageClient.upload_file(local_filename, null,
                    nvp);

            System.out.println(fileIds.length);
            System.out.println("组名:" + fileIds[0]);
            System.out.println("路径: " + fileIds[1]);

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (MyException e) {
            e.printStackTrace();
        }
    }

    @org.junit.Test
    public void testDownload() {

        try {

            ClientGlobal.init(conf_filename);

            TrackerClient tracker = new TrackerClient();
            TrackerServer trackerServer = tracker.getConnection();
            StorageServer storageServer = null;

            StorageClient storageClient = new StorageClient(trackerServer,
                    storageServer);
            byte[] b = storageClient.download_file("group1",
                    "M00/00/00/wKgUoFGwPuSAS-QZAAAA4y5hK3c33.conf");
            System.out.println(b);
            getFile(b, "d:\\", UUID.randomUUID().toString()+".conf");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    @org.junit.Test
    public void testGetFileInfo(){
        try {
            ClientGlobal.init(conf_filename);

            TrackerClient tracker = new TrackerClient();
            TrackerServer trackerServer = tracker.getConnection();
            StorageServer storageServer = null;

            StorageClient storageClient = new StorageClient(trackerServer,
                    storageServer);
            FileInfo fi = storageClient.get_file_info("group1", "M00/00/00/wKgUoFGwPuSAS-QZAAAA4y5hK3c33.conf");
            System.out.println(fi.getSourceIpAddr());
            System.out.println(fi.getFileSize());
            System.out.println(fi.getCreateTimestamp());
            System.out.println(fi.getCrc32());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    @org.junit.Test
    public void testGetFileMate(){
        try {
            ClientGlobal.init(conf_filename);

            TrackerClient tracker = new TrackerClient();
            TrackerServer trackerServer = tracker.getConnection();
            StorageServer storageServer = null;

            StorageClient storageClient = new StorageClient(trackerServer,
                    storageServer);
            NameValuePair nvps [] = storageClient.get_metadata("group1", "M00/00/00/wKgUoFGwRMmASXSNAAAA4y5hK3c11.conf");
            for(NameValuePair nvp : nvps){
                System.out.println(nvp.getName() + ":" + nvp.getValue());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    @org.junit.Test
    public void testDelete(){
        try {
            ClientGlobal.init(conf_filename);

            TrackerClient tracker = new TrackerClient();
            TrackerServer trackerServer = tracker.getConnection();
            StorageServer storageServer = null;

            StorageClient storageClient = new StorageClient(trackerServer,
                    storageServer);
            int i = storageClient.delete_file("group1", "M00/00/00/wKgUoFGwRMmASXSNAAAA4y5hK3c11.conf");
            System.out.println( i==0 ? "删除成功" : "删除失败:"+i);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

 

http://my.oschina.net/yasenagat/blog/136101


http://www.niftyadmin.cn/n/3297867.html

相关文章

Problem--71A--Codeforces--Way Too Long Words

Way Too Long Words time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard outputSometimes some words like “localization” or “internationalization” are so long that writing them many times in one text is quite…

C#中如何保存文本为Word文件或Excel文件

一, 如何保存文本为Word文件 要在.net中操作Word,就需要在项目中引用Word的对象库文件MSWORD.OLB,这可以在office安装目录下找到(C:"Program Files"Microsoft Office"OFFICE11"MSWORD.OLB),把它引入项目中就可…

ElasticSearch关联查找

ElasticSearch是一个基于Lucene的开源搜索引擎,支持全文检索,提供restful接口。在ES中,提供了类似于MongoDB 的面向文档存储服务,这种面向文档的存储非常灵活,但是文档与文档直接的关联却比较麻烦,MongoDB里…

Problem--158A--Codeforces--Next Round

Next Round time limit per test3 seconds memory limit per test256 megabytes inputstandard input outputstandard output“Contestant who earns a score equal to or greater than the k-th place finisher’s score will advance to the next round, as long as the cont…

福建

福建有很多东西,比如说~欠揍的小雨晴 还有味道怪怪的肉粽,我一说不好吃就用我是北方人来攻击我.....5555 鱼丸还是很好吃的,红色的是一种叫甜辣酱的感觉,味道还可以,有点像沙茶酱 很有味道的小旅馆,在鼓浪屿…

各种 Java 的序列化库的性能比较测试结果

本次测试专注于 cyclefree 数据结构的编码和解码。一些序列化程序支持循环检测/对象共享,一些序列化程序两种模型都支持,一些序列化程序包括了元数据,一些则没有,有的是跨平台的,有的只支持特定语言,有些是…

大学十年---林锐(一)

本人推荐的书书 没事看看~比看武侠有意思的多 玩游戏带劲的很 下载地址1 PDF下载地址2 林锐-我的大学十年 (本文摘自《高质量C/C程序设计指南》,由电子工业出版社于2002年5月底出版。) 作者:林锐 写此文使我很为难,一是担心读者误以为我轻浮…

基于Spark MLlib平台的协同过滤算法---电影推荐系统

又好一阵子没有写文章了,阿弥陀佛...最近项目中要做理财推荐,所以,回过头来回顾一下协同过滤算法在推荐系统中的应用。 说到推荐系统,大家可能立马会想到协同过滤算法。本文基于Spark MLlib平台实现一个向用户推荐电影的简单应用。…