/ 中存储网

FastDFS和Nginx的优化过程

2013-09-05 11:38:32 来源:IT技术网

生产环境结果应该如下:

本文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/,有需要可以修改。

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

base_path=/fdfs

# base_path 附目录说明:

tracker server目录及文件结构:

${base_path}

|__data

|     |__storage_groups.dat:存储分组信息

|     |__storage_servers.dat:存储服务器列表

|__logs

|__trackerd.log:tracker server日志文件

base_path=/fdfs

store_path0=/fdfs

tracker_server=192.168.20.160:22122

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:workspacefastdfssrcfdfs_client.conf";

public String local_filename =

"F:workspacefastdfssrcfdfs_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();

}

}