说明和相关软件下载
操作系统:CentOS 6.5
FastDFS v5.0.4
fastdfs-nginx-module v1.16
libfastcommon V5.0.2
FastDFS java client SDK
Nginx相关软件
Nginx 根据自己需要选择需要下载的版本
以下为Nginx编译依赖的三个库
PCRE
zlib
OpenSSL
注意:因为安装过程中基本使用源码安装,所以在系统中必须有gcc编译环境,如果没有可以使用yum install gcc命令来进行安装
安装过程
tracker安装与配置
先安装libfastcommon V5.0.2,这里面有FastDFS编译依赖的so和头文件。
bash-4.1$ unzip libfastcommon-master.zip
bash-4.1$ cd libfastcommon-master
bash-4.1$ ./make.sh
bash-4.1$ ./make.sh install
安装完成,开始安装FastDFS
bash-4.1$ unzip fastdfs-master.zip
bash-4.1$ cd fastdfs-master
bash-4.1$ ./make.sh
bash-4.1$ ./make.sh install
安装完成后将conf目录下的配置文件拷贝到/etc/fdfs目录
bash-4.1$ cp conf/* /etc/fdfs
在本机保存数据的目录下建立三个目录用来保存数据和运行时日志
例如我在安装时在/home/fdfs目录建立了三个子目录
bash-4.1$ mkdir /home/fdfs/FastDFS_Tracker
bash-4.1$ mkdir /home/fdfs/FastDFS_Storage
bash-4.1$ mkdir /home/fdfs/FastDFS_Client
进入/etc/fdfs修改配置文件,修改以下几项
bash-4.1$ vim tracker.conf
disabled=false #启用配置文件
port=22122 #设置 tracker 的端口号
base_path=/home/fdfs/FastDFS_Tracker #设置 tracker 的数据文件和日志目录
http.server_port=8090 #设置 http 端口号
修改完成后,进入/usr/local/bin目录启动fdfs_tracker
[root@gpfp bin] ./fdfs_trackerd /etc/fdfs/tracker.conf
查看是否启动
[root@gpfp bin] netstat -antp | grep tracker
tcp 0 0 0.0.0.0:22122 0.0.0.0:* LISTEN 2792/./fdfs_tracker
至此tracker启动成功
storage安装与配置
因为我测试的是本机安装,所以程序在上面的步骤已安装成功,在这里直接进入/etc/fdfs目录对storage.conf进行修改配置即可。如果安装在其他机器上,storage的安装过程和tracker的一致,在此不再重复。
bash-4.1$ vim storage.conf
disabled=false #启用配置文件
group_name=group1 #组名,根据实际情况修改
port=23000 #设置 storage 的端口号
base_path=/home/fdfs/FastDFS_Storage #设置 storage 的日志目录
store_path_count=1 #存储路径个数,需要和 store_path 个数匹配
store_path0=/home/fdfs/FastDFS_Storage #存储路径
tracker_server=10.168.6.6:22122 #tracker 服务器的 IP 地址和端口号
http.server_port=8090 #设置storage上启动的http服务的端口号,如安装的nginx的端口号
修改完成后,进入/usr/local/bin启动storage
[root@gpfp bin] ./fdfs_storaged /etc/fdfs/storage.conf
查看是否启动
[root@gpfp bin] netstat -antp | grep storage
tcp 0 0 0.0.0.0:23000 0.0.0.0:* LISTEN 3481/fdfs_storaged
到此,fdfs_storaged启动成功。
重点注意:tracker_server不能用127.0.0.1必须指明IP。
client配置与运行
tracker与storage运行成功以后,可以通过fdfs_test来上传一个文件进行测试。
首先,修改配置文件,进入/etc/fdfs/修改client.conf
bash-4.1$ vim client.conf
base_path=/home/fdfs/FastDFS_Client #设置 client 的数据文件和日志目录
tracker_server=10.168.6.6:22122 #tracker 服务器的 IP 地址和端口号
设置完成后回到/usr/local/bin目录,运行fdfs_test进行文件上传
[root@gpfp bin] ./fdfs_test /etc/fdfs/client.conf upload 123.png
点击回车,上传成功以后,将文件保存的路径、访问的URL等信息都打印出来了,如下所示
tracker_query_storage_store_list_without_group:
server 1. group_name=, ip_addr=10.168.6.6, port=23000
group_name=group1, ip_addr=10.168.6.6, port=23000
storage_upload_by_filename
group_name=group1, remote_filename=M00/00/00/CqgGBlWkqwaAPqxTAAAtk8Xea8E413.png
source ip address: 10.168.6.6
file timestamp=2015-07-14 14:24:06
file size=11667
file crc32=3319688129
example file url: http://10.168.6.6:8090/group1/M00/00/00/CqgGBlWkqwaAPqxTAAAtk8Xea8E413.png
Nginx安装与配置
上传 fastdfs-nginx-module、nginx、 pcre、zlib、openssl 到storage服务器上指定目录下,并使用tar命令将各个文件解压。
在nginx的src/http/modules/目录下建立fastdfs-nginx-module目录
bash-4.1$ mkdir src/http/modules/fastdfs-nginx-module
将解压出来的fastdfs-nginx-module/src目录下的文件拷到src/http/modules/fastdfs-nginx-module中,并修改config。以便将fasstcommon的lib和include文件目录指定正确目录,否则编译会出错。
bash-4.1$ vim config
CORE_INCS="$CORE_INCS /usr/local/include/fastdfs /usr/local/include/fastcommon/
修改mod_fastdfs.conf文件并拷贝到/etc/fdfs/
bash-4.1$ vim mod_fastdfs.conf
base_path=/home/fdfs/FastDFS_Storage
tracker_server=10.168.6.6:22122
storage_server_port=23000
group_name=group1
url_have_group_name = true
store_path_count=1
store_path0=/home/fdfs/FastDFS_Storage
[group1]
group_name=group1
storage_server_port=23000
store_path_count=1
store_path0=/home/fdfs/FastDFS_Storage
bash-4.1$ cp mod_fastdfs.conf /etc/fdfs/
安装依赖环境(可选)
1.将zip、pcre、openssl分别解压
2.进入目录执行./configure && make && make install进行安装
将fastdfs-nginx-module模块安装到nginx
如果没有进行上一步将nginx依赖环境安装到系统则通过以下命令进行nginx编译
bash-4.1$ ./configure --prefix=/usr/local/nginx --add-module=./src/http/modules/fastdfs-nginx-module --with-pcre=../pcre-8.36 --with-zlib=../zlib-1.2.8 --with-openssl=../openssl-1.0.1e
bash-4.1$ make && make install
如果将依赖库安装到系统,则使用以下命令编译即可
bash-4.1$ ./configure --prefix=/usr/local/nginx --add-module=./src/http/modules/fastdfs-nginx-module
bash-4.1$ make && make install
编译完成后,进行nginx配置
bash-4.1$ cd /usr/local/nginx/conf
bash-4.1$ vim nginx.conf
listen 8090; #端口可以自定义
location ~ /group1/M00 {
root /home/fdfs/FastDFS_Storage;
ngx_fastdfs_module;
}
建立M00至存储目录软链接
bash-4.1$ cd /home/fdfs/FastDFS_Storage;
bash-4.1$ ln -s data/ M00
bash-4.1$ ls -ald M00/
drwxrwxrwx. 259 root root 4096 Jul 14 09:25 M00/
启动nginx
[root@gpfp nginx] ./nginx -c conf/nginx.conf
可以通过浏览器访问刚才上传的文件,就可以访问到上传的图片
Java client SDK的调用
新建工程,将下载的fastdfs_client_java_v1.24.jar加入到工程中。
在bin目录下添加fdfs_client.conf配置文件
connect_timeout = 2
network_timeout = 30
charset = UTF-8
http.tracker_http_port = 8090
http.anti_steal_token = no
tracker_server = 10.168.6.6:22122
将以下代码添加到工程中测试文件上传、获取、删除
文件上传
package com.gary.test;
import java.io.File;
import java.io.FileInputStream;
import org.csource.common.NameValuePair;
import org.csource.fastdfs.ClientGlobal;
import org.csource.fastdfs.ServerInfo;
import org.csource.fastdfs.StorageClient;
import org.csource.fastdfs.StorageServer;
import org.csource.fastdfs.TrackerClient;
import org.csource.fastdfs.TrackerServer;
public class TestUpload {
public static void main(String[] args) throws Exception{
String classPath = new File(TestUpload.class.getResource("/").getFile()).getCanonicalPath();
String configFilePath = classPath + File.separator + "fdfs_client.conf";
System.out.println("配置文件:" + configFilePath);
ClientGlobal.init(configFilePath);
TrackerClient trackerClient = new TrackerClient();
TrackerServer trackerServer = trackerClient.getConnection();
StorageServer storageServer = null;
StorageClient storageClient = new StorageClient(trackerServer, storageServer);
NameValuePair[] meta_list = new NameValuePair[3];
meta_list[0] = new NameValuePair("width", "120");
meta_list[1] = new NameValuePair("heigth", "120");
meta_list[2] = new NameValuePair("author", "gary");
File file = new File("D:\123.png");
FileInputStream fis = new FileInputStream(file);
byte[] file_buff = null;
if(fis != null){
int len = fis.available();
file_buff = new byte[len];
fis.read(file_buff);
}
System.out.println("file length: " + file_buff.length);
String group_name = null;
StorageServer[] storageServers = trackerClient.getStoreStorages(trackerServer, group_name);
if (storageServers == null) {
System.err.println("get store storage servers fail, error code: " + storageClient.getErrorCode());
}else{
System.err.println("store storage servers count: " + storageServers.length);
for (int k = 0; k < storageServers.length; k++){
System.err.println(k + 1 + ". " + storageServers[k].getInetSocketAddress().getAddress().getHostAddress() + ":" + storageServers[k].getInetSocketAddress().getPort());
}
System.err.println("");
}
long startTime = System.currentTimeMillis();
String[] results = storageClient.upload_file(file_buff, "jpg", meta_list);
System.out.println("upload_file time used: " + (System.currentTimeMillis() - startTime) + " ms");
if (results == null){
System.err.println("upload file fail, error code: " + storageClient.getErrorCode());
return;
}
group_name = results[0];
String remote_filename = results[1];
System.err.println("group_name: " + group_name + ", remote_filename: " + remote_filename);
System.err.println(storageClient.get_file_info(group_name, remote_filename));
ServerInfo[] servers = trackerClient.getFetchStorages(trackerServer, group_name, remote_filename);
if (servers == null){
System.err.println("get storage servers fail, error code: " + trackerClient.getErrorCode());
} else {
System.err.println("storage servers count: " + servers.length);
for (int k = 0; k < servers.length; k++){
System.err.println(k + 1 + ". " + servers[k].getIpAddr() + ":" + servers[k].getPort());
}
System.err.println("");
}
}
}
文件获取
package com.gary.test;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import org.csource.fastdfs.ClientGlobal;
import org.csource.fastdfs.FileInfo;
import org.csource.fastdfs.StorageClient;
import org.csource.fastdfs.StorageServer;
import org.csource.fastdfs.TrackerClient;
import org.csource.fastdfs.TrackerServer;
public class TestGet {
public static void main(String[] args) throws Exception {
String classPath = new File(TestGet.class.getResource("/").getFile()).getCanonicalPath();
String configFilePath = classPath + File.separator + "fdfs_client.conf";
ClientGlobal.init(configFilePath);
TrackerClient trackerClient = new TrackerClient();
TrackerServer trackerServer = trackerClient.getConnection();
StorageServer storageServer = null;
StorageClient storageClient = new StorageClient(trackerServer, storageServer);
String group_name = "group1";
String remote_filename = "M00/00/00/CqgGBlWjgWWAOE6FAAAtk8Xea8E724.jpg";
FileInfo fi = storageClient.get_file_info(group_name, remote_filename);
String sourceIpAddr = fi.getSourceIpAddr();
long size = fi.getFileSize();
System.out.println("ip:" + sourceIpAddr + ",size:" + size);
byte[] img = storageClient.download_file(group_name, remote_filename);
if (null != img) {
OutputStream out = new BufferedOutputStream(new FileOutputStream("E:\1.jpg"));
out.write(img);
out.close();
}
}
}
文件删除
package com.gary.test;
import java.io.File;
import org.csource.fastdfs.ClientGlobal;
import org.csource.fastdfs.StorageClient;
import org.csource.fastdfs.StorageServer;
import org.csource.fastdfs.TrackerClient;
import org.csource.fastdfs.TrackerServer;
public class TestDelete {
public static void main(String[] args) throws Exception {
String classPath = new File(TestDelete.class.getResource("/").getFile()).getCanonicalPath();
String configFilePath = classPath + File.separator + "fdfs_client.conf";
ClientGlobal.init(configFilePath);
TrackerClient trackerClient = new TrackerClient();
TrackerServer trackerServer = trackerClient.getConnection();
StorageServer storageServer = null;
StorageClient storageClient = new StorageClient(trackerServer, storageServer);
String group_name = "group1";
String remote_filename = "M00/00/00/CqgGBlWjgWWAOE6FAAAtk8Xea8E724.jpg";
int ret = storageClient.delete_file(group_name, remote_filename);
System.out.println("删除成功 " + ret);
}
}
到此,FastDFS的安装部署完成。