1.所需安装包
libfastcommon-master.zip
FastDFS_v5.05.tar.gz
fastdfs-nginx-module_v1.16.tar.gz
nginx-1.8.0.tar.gz
2.安装libfastcommon包
① 安装libfastcommon前需要安装以下依赖(系统如果有带有,就不需要安装):
yum install -y gcc
yum install -y gcc-c++
libfastcommon安装地址:
wget https://github.com/happyfish100/libfastcommon/archive/master.zip
安装解压工具
yum install -y unzip zip
unzip master.zip
cd libfastcommon-master/
./make.sh
./make.sh install
下载安装fastdfs
wget https://github.com/happyfish100/fastdfs/archive/V5.05.tar.gz
tar -zxvf V5.05.tar.gz
cd fastdfs-5.05
./make.sh
./make.sh install
修改配置文件名
mv client.conf.sample client.conf
mv storage.conf.sample storage.conf
mv tracker.conf.sample tracker.conf
打开tracker.conf,修改如下配置:
base_path=/home/yuqing/fastdfs
port是监控的端口,不建议修改.另外上面的路径为文件夹.需要自己建立
启动tracker
/usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf start
Tracker添加到开机自启动
vim /etc/rc.d/rc.local
将如下命令添加到文件中
/usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf start
配置Storage服务
base_path=/home/yuqing/fastdfs_storage_log 日志目录
store_path0=/home/yuqing/fastdfs_storage_data 储存目录
tracker_server=10.211.55.7:22122 Tracker服务器IP和端口 这个地址就是你配置的这个服务器的地址.记得端口要开放,否则无法执行 会报错链接超时.
启动Storage服务
/usr/bin/fdfs_storaged /etc/fdfs/storage.conf restart
查看服务启动是否成功
netstat -unltp|grep fdfs
启动成功后,通过fdfs_monitor查看集群情况,即storage是否已经注册到了racker服务器中
/usr/bin/fdfs_monitor /etc/fdfs/storage.conf
tracker添加到开机自启动
vim /etc/rc.d/rc.local
将如下命令添加到文件中
/usr/bin/fdfs_monitor /etc/fdfs/storage.conf restart
测试图片上传
/usr/bin/fdfs_upload_file /etc/fdfs/client.conf /root/fastdfs-5.05/conf/anti-steal.jpg
测试图片下载
/usr/bin/fdfs_download_file /etc/fdfs/client.conf group1/M00/00/00/rBBVoVsbP1qAAvyjAAPtbS7Vknk30..png /usr/03.jpg
以上没有问题.进行Nginx的配置
安装Nginx
wget http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
下载对应当前系统版本的nginx包(package)
rpm -ivh nginx-release-centos-7-0.el7.ngx.noarch.rpm建立nginx的yum仓库
yum install nginx 下载并安装nginx
systemctl start nginx 启动nginx服务
默认的配置文件在 /etc/nginx 路径下,使用该配置已经可以正确地运行nginx;如需要自定义,修改其下的 nginx.conf 等文件即可。
fast-nginx-module配置:
下载Nginx module模块
wget http://nchc.dl.sourceforge.net/project/fastdfs/FastDFS%20Nginx%20Module%20Source%20Code/fastdfs-nginx-module_v1.16.tar.gz
解压:
tar xf fastdfs-nginx-module_v1.16.tar.gz
替换配置文件内容:
cd fastdfs-nginx-module/src/
vim config
:%s+/usr/local/+/usr/+g
拷贝fastdfs-nginx-module模块中配置文件到/etc/fdfs目录中并编辑
vim /etc/fdfs/mod_fastdfs.conf
connect_timeout=10
base_path=/tmp(默认为/tmp)
tracker_server=172.18.10.232:22122
storage_server_port=23000(默认配置为23000)
url_have_group_name = true
store_path0=/data/fastdfs/storage
group_name=group1(默认配置为group1)
以上内容有的是被注释掉的,有的则是需要修改的.store_path0路径为storage server存储服务器的存储文件夹地址
安装Nginx依赖库
yum install -y pcre-devel zlib-devel
下载Nginx
wget http://nginx.org/download/nginx-1.8.1.tar.gz
tar xf nginx-1.8.1.tar.gz
cd nginx-1.8.1
./configure --prefix=/application/nginx/ --add-module=…/fastdfs-nginx-module/src/
make && make install
拷贝FastDFS中的部分配置文件到/etc/fdfs目录中
cp /home/oldcat/tools/FastDFS/conf/http.conf /etc/fdfs/
cp /home/oldcat/tools/FastDFS/conf/mime.types /etc/fdfs/
以上是根据自己的FastDFS的根目录进行修改
配置nginx,如下所示:
user root;
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 8888;
server_name localhost;
location ~/group[0-9]/ {
ngx_fastdfs_module;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
将安装好的Nginx的Nginx.conf文件直接复制进去运行,按照流程进行会直接运行.并且打印端口号.listen是你要监控的端口.端口要和storage.conf文件中的http.server_port相互对应
启动成功后访问端口.可以访问到图片文件
Java文件
新建fdfs_client.conf
connect_timeout = 2
network_timeout = 30
charset = UTF-8
http.tracker_http_port = 80
http.anti_steal_token = no
http.secret_key = FastDFS1234567890
tracker_server = 服务器ip:22122
/**
*作者:Mr.王
*时间:2018/6/11 0011 下午 14:14
*功能:文件上传
*参数:
*/
public static String uploadFastDFS(@RequestParam(“file”) MultipartFile file) {
FileUpload files = new FileUpload();
try {
// 获取文件的后缀名
String suffixName = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
//获取配置文件.
ClassPathResource resource = new ClassPathResource(“fdfs_client.conf”);
//获取配置文件内容
ClientGlobal.init(resource.getClassLoader().getResource(“fdfs_client.conf”).getPath());
//实例化跟踪器客户端
TrackerClient trackerClient = new TrackerClient();
//实例化跟踪器服务端
TrackerServer trackerServer = trackerClient.getConnection();
//实例化存储服务端
StorageServer storageServer = null;
//实例化存储客户端
StorageClient1 storageClient1 = new StorageClient1(trackerServer, storageServer);
//获取上传参数,以bytes存储.并且返回路径名称.
return storageClient1.upload_file1(file.getBytes(), suffixName, null);
} catch (Exception e) {
e.printStackTrace();
return “上传失败”;
}
}
/**
*作者:Mr.王
*时间:2018/6/11 0011 下午 14:12
*功能:文件下载
*参数:groupName:前缀(group1).filepath:图片路径(M00/00/00/rBBVoVsd8OyAKyUjAAASGf-0z_g90..txt).fileUrl:下载路径(D:/).imgName:文件名称(rBBVoVsd8OyAKyUjAAASGf-0z_g90..txt)
*/
public static void downloadFile(String groupName,String filepath,String fileUrl,String imgName) throws Exception{
//获取配置文件.
ClassPathResource resource = new ClassPathResource("fdfs_client.conf");
//获取配置文件内容
ClientGlobal.init(resource.getClassLoader().getResource("fdfs_client.conf").getPath());
TrackerClient tracker = new TrackerClient();
TrackerServer trackerServer = tracker.getConnection();
StorageServer storageServer = null;
StorageClient storageClient = new StorageClient(trackerServer, storageServer);
byte[] b = storageClient.download_file(groupName, filepath);
System.out.println("文件大小:"+b.length);
OutputStream out = new FileOutputStream(fileUrl+imgName);
out.write(b);
out.flush();
out.close();
}
public static void main(String[]args)throws Exception{
downloadFile("group1","M00/00/00/rBBVoVsd8OyAKyUjAAASGf-0z_g90..txt","D:/zhy/","rBBVoVsd8OyAKyUjAAASGf-0z_g90..txt");
}