普通的存储方式,图片存储过于分散
安装C依赖
yum install -y make cmake gcc gcc-c++
安装C的一个库,我这边直接GitHub下载
cd /usr/local/tmp git clone https://github.com/happyfish100/libfastcommon.git
编译并安装
cd libfastcommon-master ./make.sh ./make.sh install
默认的固定安装位置
创建软链接
ln -s /usr/lib64/libfastcommon.so /usr/local/lib/libfastcommon.so ln -s /usr/local/lib64/libfdfsclient.so /usr/local/lib/libfdfsclient.so
上传并解压FastDFS主程序
git clone https://github.com/happyfish100/fastdfs.git
编译并安装
cd fastdfs ./make.sh ./make.sh install
复制配置文件
cd /etc/fdfs cp tracker.conf.sample tracker.conf
创建数据目录
创建防止tracker数据的目录
mkdir -p /usr/local/fastdfs/tracker
修改配置文件,修改tracker.conf设置tracker内容存储目录
base_path=/usr/local/fastdfs/tracker
vim tracker.conf
默认端口 22122 不需要修改
启动服务
service fdfs_trackerd start
启动成功后,配置文件base_path指向的目录出现FastDFS服务相关数据目录(data目录,logs目录)
service fdfs_trackerd status
如果显示 is running 表示正常运行
service iptables stop
chkconfig iptables off
storage可以和tracker不在同一台服务器中,示例中把storage和tracker安装在同一台服务器
进入到/etc/fdfs,把storage配置文件复制一份
cd /etc/fdfs cp storage.conf.sample storage.conf
把base用于存储基础数据和日志,store用于存储上传数据
mkdir -p /usr/local/fastdfs/storage/base mkdir -p /usr/local/fastdfs/storage/store
storage.conf配置文件用于描述存储服务的行为,需要进行下述修改
vim /etc/fdsf/storage.conf
配置内容如下
base_path=/usr/local/fastdfs/storage/base store_path0=/usr/local/fastdfs/storage/store tracker_server=tracker 服务IP:22122
service fdfs_storaged start
启动成功后,配置文件base_path指向的目录中出现FastDFS服务相关数据目录(data目录,logs目录库)
配置文件中的store_path0指向的目录同样出现了FastDFS存储相关数据目录(data目录)
其中$store_path0/data 目录中默认创建若干子孙目录(两层一共256*256),用于存储具体文件数据
Storage服务器启动比较慢,第一次启动的时候,需要创建256*256个目录
service fdfs_storaged status
时序图
添加依赖,官方地址:https://mvnrepository.com/artifact/cn.bestwu/fastdfs-client-java
<!-- https://mvnrepository.com/artifact/cn.bestwu/fastdfs-client-java --> <dependency> <groupId>cn.bestwu</groupId> <artifactId>fastdfs-client-java</artifactId> <version>1.27</version> </dependency>
fdfs_client.conf,修改成自己的tracker服务器ip
connect_timeout = 10 network_timeout = 30 charset = UTF-8 http.tracker_http_port = 8080 tracker_server = 192.168.93.10:22122
在com.utils.FastDFSClient下粘贴配置工具类
/** * FastDFS分布式文件系统操作客户端. */ public class FastDFSClient { private static final String CONF_FILENAME = Thread.currentThread().getContextClassLoader().getResource("").getPath() + "fdfs_client.conf"; private static StorageClient storageClient = null; /** * 只加载一次. */ static { try { ClientGlobal.init(CONF_FILENAME); TrackerClient trackerClient = new TrackerClient(ClientGlobal.g_tracker_group); TrackerServer trackerServer = trackerClient.getConnection(); StorageServer storageServer = trackerClient.getStoreStorage(trackerServer); storageClient = new StorageClient(trackerServer, storageServer); } catch (Exception e) { e.printStackTrace(); } } /** * * @param inputStream * 上传的文件输入流 * @param fileName * 上传的文件原始名 * @return */ public static String[] uploadFile(InputStream inputStream, String fileName) { try { // 文件的元数据 NameValuePair[] meta_list = new NameValuePair[2]; // 第一组元数据,文件的原始名称 meta_list[0] = new NameValuePair("file name", fileName); // 第二组元数据 meta_list[1] = new NameValuePair("file length", inputStream.available()+""); // 准备字节数组 byte[] file_buff = null; if (inputStream != null) { // 查看文件的长度 int len = inputStream.available(); // 创建对应长度的字节数组 file_buff = new byte[len]; // 将输入流中的字节内容,读到字节数组中。 inputStream.read(file_buff); } // 上传文件。参数含义:要上传的文件的内容(使用字节数组传递),上传的文件的类型(扩展名),元数据 String[] fileids = storageClient.upload_file(file_buff, getFileExt(fileName), meta_list); return fileids; } catch (Exception ex) { ex.printStackTrace(); return null; } } /** * * @param file * 文件 * @param fileName * 文件名 * @return 返回Null则为失败 */ public static String[] uploadFile(File file, String fileName) { FileInputStream fis = null; try { NameValuePair[] meta_list = null; // new NameValuePair[0]; fis = new FileInputStream(file); byte[] file_buff = null; if (fis != null) { int len = fis.available(); file_buff = new byte[len]; fis.read(file_buff); } String[] fileids = storageClient.upload_file(file_buff, getFileExt(fileName), meta_list); return fileids; } catch (Exception ex) { return null; }finally{ if (fis != null){ try { fis.close(); } catch (IOException e) { e.printStackTrace(); } } } } /** * 根据组名和远程文件名来删除一个文件 * * @param groupName * 例如 "group1" 如果不指定该值,默认为group1 * @param remoteFileName * 例如"M00/00/00/wKgxgk5HbLvfP86RAAAAChd9X1Y736.jpg" * @return 0为成功,非0为失败,具体为错误代码 */ public static int deleteFile(String groupName, String remoteFileName) { try { int result = storageClient.delete_file(groupName == null ? "group1" : groupName, remoteFileName); return result; } catch (Exception ex) { return 0; } } /** * 修改一个已经存在的文件 * * @param oldGroupName * 旧的组名 * @param oldFileName * 旧的文件名 * @param file * 新文件 * @param fileName * 新文件名 * @return 返回空则为失败 */ public static String[] modifyFile(String oldGroupName, String oldFileName, File file, String fileName) { String[] fileids = null; try { // 先上传 fileids = uploadFile(file, fileName); if (fileids == null) { return null; } // 再删除 int delResult = deleteFile(oldGroupName, oldFileName); if (delResult != 0) { return null; } } catch (Exception ex) { return null; } return fileids; } /** * 文件下载 * * @param groupName 卷名 * @param remoteFileName 文件名 * @return 返回一个流 */ public static InputStream downloadFile(String groupName, String remoteFileName) { try { byte[] bytes = storageClient.download_file(groupName, remoteFileName); InputStream inputStream = new ByteArrayInputStream(bytes); return inputStream; } catch (Exception ex) { return null; } } public static NameValuePair[] getMetaDate(String groupName, String remoteFileName){ try{ NameValuePair[] nvp = storageClient.get_metadata(groupName, remoteFileName); return nvp; }catch(Exception ex){ ex.printStackTrace(); return null; } } /** * 获取文件后缀名(不带点). * * @return 如:"jpg" or "". */ private static String getFileExt(String fileName) { if (StringUtils.isBlank(fileName) || !fileName.contains(".")) { return ""; } else { return fileName.substring(fileName.lastIndexOf(".") + 1); // 不带最后的点 } } }
下载fastdfs-nginx-module到/usr/local/tmp中
cd /usr/local/tmp git clone https://github.com/happyfish100/fastdfs-nginx-module
cd fastdfs-nginx-module/src
vim config
修改第四行路径,参数用于配置安装nginx中的FastDFS组件的时候,在什么位置查找FastDFS核心代码
git clone https://github.com/nginx/nginx.git
yum install -y gcc gcc-c++ make automake autoconf libtool pcre pcre-devel zlib zlib-devel openssl openssl-devel
mkdir -p /var/temp/nginx
./configure --prefix=/usr/local/nginx --pid-path=/var/run/nginx/nginx.pid --lock-path=/var/lock/nginx.lock --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --with-http_gzip_static_module --http-client-body-temp-path=/var/temp/nginx/client --http-proxy-temp-path=/var/temp/nginx/proxy --http-fastcgi-temp-path=/var/temp/nginx/fastcgi --http-uwsgi-temp-path=/var/temp/nginx/uwsgi --http-scgi-temp-path=/var/temp/nginx/scgi --add-module=/usr/local/tmp/fastdfs-nginx-module/src
chmod 777 nginx -R
cd nginx make make install
复制 fastdfs-nginx-module/src/mod_fastdfs.conf 配置文件到/etc/fdfs目录中
cp /usr/local/tmp/fastdfs-nginx-module/src/mod_fastdfs.conf /etc/fdfs/
cd /etc/fdfs
vim mod_fastdfs.conf
connect_timeout=10 tracker_server=192.168.93.10:22122 // 自己的ip和port url_have_group_name=true store_path0=/usr/local/fastdfs/storage/store // 自己的目录
cp /usr/local/tmp/FastDFS/conf/http.conf /etc/fdfs/ cp /usr/local/tmp/FastDFS/conf/mime.types /etc/fdfs/
ln -s /usr/local/fastdfs/storage/store/data/ /usr/local/fastdfs/storage/store/data/M00
cd/ usr/local/nginx/conf
vim nginx.conf
用户
user root;
location /M00 {
ngx_fastdfs_module;
}
在server_name下面修改
location ~/group([0-9]/M00){ ngx_fastdfs_module; }
进入目录,然后
cd /usr/local/nginx/sbin
./nginx
./nginx -s quit
原文:https://www.cnblogs.com/YC-L/p/14367647.html