前言
环境centOS6.5,学习的时候请关闭防火墙(生产环境命令开放端口)
关闭命令:chkconfig iptables off 然后重启:reboot(也可以在控制台操作)
软件准备
nginx
fastdfs-nginx-module.tar.gz
FastDFS.tar.gz
libfastcommon-master.zip
fastdfs-client-java-1.27-SNAPSHOT.jar
安装gcc
命令:yum install make cmake gcc gcc-c++
部署fastdfs
安装文件路径在 /usr/local
安装libfastcommon
解压
命令:unzip libfastcommon-master.zip -d /usr/local/fast/
cd /usr/local/fast/libfastcommon-master
./make.sh
./make.sh install
ps:libfastcommon默认安装到了/usr/lib64/这个位置
软连接创建
ln -s /usr/lib64/libfastcommon.so /usr/lib/libfastcommon.so
ln -s /usr/lib64/libfdfsclient.so /usr/local/lib/libfdfsclient.so
ln -s /usr/lib64/libfdfsclient.so /usr/lib/libfdfsclient.so
安装FastDFS
解压
命令:tar -zxvf FastDFS_v5.0.5.tar.gz -C /usr/local/fast/
cd /usr/local/fast/FastDFS/
./make.sh
./make.sh install
因为FastDFS脚本服务脚本设置的bin目录为/usr/local/bin/下,但是实际我们安装在了/usr/bin/下面。所以我们需要修改fastdfs配置文件中的路径,
也就是修改这两个文件:
命令:vim /etc/init.d/fdfs_storaged
进行全局替换命令: %s+/usr/local/bin+/usr/bin
命令:vim /etc/init.d/fdfs_trackerd
进行全局替换命令: %s+/usr/local/bin+/usr/bin
配置跟踪器(trackerd)
修改配置文件
cd /etc/fdfs/
cp tracker.conf.sample tracker.conf
vim tracker.conf
修改内容:
# the base path to store data and log files
base_path=/fastdfs/tracker
创建目录:
mkdir -p /fastdfs/tracker
启动trackerd
启动命令:/etc/init.d/fdfs_trackerd start
停止命令:/etc/init.d/fdfs_trackerd stop
配置FastDFS存储(storaged)
修改配置文件
cd /etc/fdfs/
cp storage.conf.sample storage.conf
vim storage.conf
修改内容:
# the base path to store data and log files
base_path=/fastdfs/storage
# store_path#, based 0, if store_path0 not exists, it's value is base_path
# the paths must be exist
store_path0=/fastdfs/storage
# tracker_server can ocur more than once, and tracker_server format is
# "host:port", host can be hostname or ip address
tracker_server=192.168.1.9:22122
ps:ip地址为tracker地址
# the port of the web server on this storage server
http.server_port=80
创建存储目录:
mkdir -p /fastdfs/storage
启动存储
/etc/init.d/fdfs_storaged start
设置开机启动追踪器和存储器:(一半生产环境需要开机启动一些服务)
vim /etc/rc.d/rc.local
加入配置 :/etc/init.d/fdfs_trackerd start
/etc/init.d/fdfs_storaged start
测试
cd /etc/fdfs/
cp client.conf.sample client.conf
修改client.conf
修改内容:
# the base path to store log files
base_path=/fastdfs/tracker
# tracker_server can ocur more than once, and tracker_server format is
# "host:port", host can be hostname or ip address
tracker_server=192.168.1.9:22122
测试上传
/usr/bin/fdfs_upload_file /etc/fdfs/client.conf /usr/local/libfastcommon-master.zip
打印:
group1/M00/00/00/wKgBCVrWd0-APnYuAAGP6hUWM6I231.zip
FastDFS与nginx整合
安装fastdfs-nginx-module
tar -zxvf fastdfs-nginx-module_v1.16.tar.gz -C /usr/local/fast/
cd fast/fastdfs-nginx-module/
修改配置文件config
vim src/config
修改前:
CORE_INCS="$CORE_INCS /usr/local/include/fastdfs /usr/local/include/fastcommon/"
修改后:
CORE_INCS="$CORE_INCS /usr/include/fastdfs /usr/include/fastcommon/"
解压nginx
下载nginx wget http://nginx.org/download/nginx-1.10.3.tar.gz
tar -zxvf nginx-1.10.3.tar.gz
fastDFS与nginx集成
cd nginx-1.10.3
./configure --add-module=/usr/local/fast/fastdfs-nginx-module/src/
如有报错请参照 nginx安装与部署,安装相关依赖
./configure --add-module=/usr/local/fast/fastdfs-nginx-module/src/
make &&make install
修改配置文件
cp /usr/local/fast/fastdfs-nginx-module/src/mod_fastdfs.conf /etc/fdfs/
vim /etc/fdfs/mod_fastdfs.conf
修改内容:
# default value is 30s
connect_timeout=10
# valid only when load_fdfs_parameters_from_tracker is true
tracker_server=192.168.1.9:22122
# set to true when uri like ${group_name}/M00/00/00/xxx, such as group1/M00/xxx
# default value is false
url_have_group_name = true
# the paths must be exist
# must same as storage.conf
store_path0=/fastdfs/storage
复制FastDFS里的2个文件
cd /usr/local/fast/FastDFS/conf/
cp http.conf mime.types /etc/fdfs/
建立软连接
ln -s /fastdfs/storage/data/ /fastdfs/storage/data/M00
修改nginx配置
vim /usr/local/nginx/conf/nginx.conf
listen 80;
server_name localhost;
location ~/group([0-9])/M00{
ngx_fastdfs_module;
}
启动nginx
/usr/local/nginx/sbin/nginx
测试
http://192.168.1.9/group1/M00/00/00/wKgBCVrWd0-APnYuAAGP6hUWM6I231.zip
java客户端调用示例
本示例基于springboot:
在pom.xml加入
csource
1.27-SNAPSHOT
这个包需要使用mvn命令手动打入m2仓库中,命令如下:
mvn install:install-file -Dfile=D:\baiduDown\fastdfs-client-java-1.27-SNAPSHOT.jar -DgroupId=csource -DartifactId=fastdfs-client-java -Dversion=1.27-SNAPSHOT -Dpackaging=jar
(文件地址请根据自己的jar地址更改)
在resources下创建 fdfs/fdfs_client.properties
# 连接tracker服务器超时时长
connect_timeout =10
# socket连接超时时长
network_timeout =30
# 文件内容编码
charset = UTF-8
# tracker服务器端口
http.tracker_http_port =80
http.anti_steal_token =no
http.secret_key =FastDFS1234567890
# tracker服务器IP和端口(可以写多个)
tracker_server =192.168.1.9:22122
FastDFS工具类
package com.example.fastdfs.utils;
import lombok.Cleanup;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.csource.fastdfs.*;
import org.springframework.web.multipart.MultipartFile;
import java.io.*;
/**
* @author feng
* FastDFS工具类
*/
public class FastDFS {
private static final String CONF_FILENAME = "src/main/resources/fdfs/fdfs_client.properties";
private static Logger logger = Logger.getLogger(FastDFS.class);
private static StorageClient1 storageClient1 ;
//初始化
static {
try {
ClientGlobal.init(CONF_FILENAME);
TrackerGroup trackerGroup = ClientGlobal.g_tracker_group;
TrackerClient trackerClient = new TrackerClient(trackerGroup);
@Cleanup
TrackerServer trackerServer = trackerClient.getConnection();
if (trackerServer == null) {
logger.error("getConnection return null");
}
@Cleanup
StorageServer storageServer = trackerClient.getStoreStorage(trackerServer);
storageClient1 = new StorageClient1(trackerServer, storageServer);
} catch (Exception e) {
logger.error(e);
}
}
/**
* 上传
* @param file 文件
* @param path 路径
* @return
* 上传成功返回id,失败返回null
*/
public static String upload(MultipartFile file, String path) {
try {
String fileId = storageClient1.upload_file1(file.getBytes(), getFileExt(path), null);
return fileId;
} catch (Exception ex) {
logger.error(ex);
return null;
}
}
/**
* 下载
* @param fileId 文件id
* @return
* 返回InputStream
*/
public static InputStream download( String fileId) {
try {
byte[] bytes = storageClient1.download_file1(fileId);
InputStream inputStream = new ByteArrayInputStream(bytes);
return inputStream;
} catch (Exception ex) {
logger.error(ex);
return null;
}
}
/**
* 删除
* @param fileId 文件id
* @return
* 删除成功返回0,非0则操作失败,返回错误代码
*/
public static int delete( String fileId) {
try {
int result = storageClient1.delete_file1(fileId);
return result;
} catch (Exception ex) {
logger.error(ex);
return 0;
}
}
/**
* 获取文件后缀名
* @param fileName
* @return
* 如:"jpg"、"txt"、"zip" 等
*/
private static String getFileExt(String fileName) {
if (StringUtils.isBlank(fileName) || !fileName.contains(".")) {
return "";
} else {
return fileName.substring(fileName.lastIndexOf(".") + 1);
}
}
}
测试
package com.example.fastdfs.controller;
import com.example.fastdfs.entity.DataReturn;
import com.example.fastdfs.entity.FileVo;
import com.example.fastdfs.utils.FastDFS;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
/**
* 文件上传
* @author feng
*/
@RestController
@RequestMapping("/fastDFS/")
public class FastController {
/**
* 文件上传
* @param file
* @return
*/
@RequestMapping(value = "upload",method = RequestMethod.POST)
public DataReturn> upload( MultipartFile file) {
try {
if(file==null){
return new DataReturn(10001,"参数错误",null);
}
String fileId=FastDFS.upload(file,file.getOriginalFilename());
if(StringUtils.isNotBlank(fileId)){
FileVo fileVo=new FileVo(fileId);
return new DataReturn<>(0,"成功",fileVo);
}
}catch ( Exception e){
e.printStackTrace();
}
return new DataReturn<>(1,"失败",null);
}
/**
* 文件删除
* @param fileId
* @return
*/
@RequestMapping(value = "delete")
public DataReturn> delete(String fileId) {
if(StringUtils.isBlank(fileId)){
return new DataReturn(10001,"参数错误",null);
}
int result=FastDFS.delete(fileId);
if(result>0){
return new DataReturn<>(0,"成功",null);
}
return new DataReturn<>(1,"失败",null);
}
}