GitHub:https://github.com/happyfish100
安装>>>>>>>>>>>>>>>
1.环境
yum install git gcc gcc-c++ make automake autoconf libtool pcre pcre-devel zlib zlib-devel openssl-devel wget vim -y
解压进入libfastcommon-1.0.43目录
./make.sh && ./make.sh install #编译安装
解压进入fastdfs-6.06目录
./make.sh && ./make.sh install #编译安装
4.配置文件准备
cp /文件存在路径/tracker.conf.sample /etc/fdfs/tracker.conf //tracker.conf.sample文件 /etc/fdfs目录可能存在
cp /文件存在路径/storage.conf.sample /etc/fdfs/storage.conf //storage.conf.sample文件 /etc/fdfs目录可能存在
cp /文件存在路径/client.conf.sample /etc/fdfs/client.conf #客户端文件,测试用 //client.conf.sample文件 /etc/fdfs目录可能存在
cp /文件存在路径/conf/http.conf /etc/fdfs/ #供nginx访问使用 //在fastdfs-6.06文件夹中,也就是解压后的
cp /文件存在路径/conf/mime.types /etc/fdfs/ #供nginx访问使用
5.安装fastdfs-nginx-module
解压,
cp /usr/local/src/fastdfs-nginx-module/src/mod_fastdfs.conf /etc/fdfs
6.安装nginx
解压进入目录
cd nginx-1.15.4/ #添加fastdfs-nginx-module模块 ./configure --add-module=/usr/local/src/fastdfs-nginx-module/src/ 注意路径 make && make install #编译安装
配置
1.tracker配置,都在 /etc/fdfs目录下修改
vim /etc/fdfs/tracker.conf #需要修改的内容如下 port=22122 # tracker服务器端口(默认22122,一般不修改) # the base path to store data and log files
base_path = /opt/fastdfsdata/tracker # 存储日志和数据的根目录
2.storage配置
vim /etc/fdfs/storage.conf #需要修改的内容如下 port=23000 # storage服务端口(默认23000,一般不修改) base_path=/home/dfs # 数据和日志文件存储根目录 改成相应目录就好,但目录要存在 store_path0=/home/dfs # 第一个存储目录 tracker_server=192.168.52.1:22122 # tracker服务器IP和端口 http.server_port=8888 # http访问文件的端口(默认8888,看情况修改,和nginx中保持一致)
3.client测试
vim /etc/fdfs/client.conf #需要修改的内容如下 base_path=/home/dfs tracker_server=192.168.52.1:22122 #tracker服务器IP和端口 #保存后测试,返回ID表示成功 如:group1/M00/00/00/xx.tar.gz fdfs_upload_file /etc/fdfs/client.conf /usr/local/src/nginx-1.15.4.tar.gz 测试方式1
fdfs_test /etc/fdfs/client.conf upload 文件路径 测试方式2
结果
group_name=group1, ip_addr=192.168.146.140, port=23000 storage_upload_by_filename group_name=group1, remote_filename=M00/00/00/wKiSjF853QWASZBKAAHsie1c8DY203.jpg source ip address: 192.168.146.140 file timestamp=2020-08-17 09:27:33 file size=126089 file crc32=3982290998 example file url: http://192.168.146.140/group1/M00/00/00/wKiSjF853QWASZBKAAHsie1c8DY203.jpg storage_upload_slave_by_filename group_name=group1, remote_filename=M00/00/00/wKiSjF853QWASZBKAAHsie1c8DY203_big.jpg source ip address: 192.168.146.140 file timestamp=2020-08-17 09:27:33 file size=126089 file crc32=3982290998 example file url: http://192.168.146.140/group1/M00/00/00/wKiSjF853QWASZBKAAHsie1c8DY203_big.jpg
4.配置nginx访问
vim /etc/fdfs/mod_fastdfs.conf #需要修改的内容如下 tracker_server=192.168.52.1:22122 #tracker服务器IP和端口 url_have_group_name=true store_path0=/home/dfs #配置nginx.config vim /usr/local/nginx/conf/nginx.conf #添加如下配置 server { listen 8888; ## 该端口为storage.conf中的http.server_port相同 server_name localhost; location ~/group[0-9]/ { ngx_fastdfs_module; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } #测试下载,用外部浏览器访问刚才已传过的nginx安装包,引用返回的ID http://192.168.52.1:8888/group1/M00/00/00/wKgAQ1pysxmAaqhAAA76tz-dVgg.tar.gz
集群>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
1.准备7台机器
192.168.146.137 作为tracker服务 ,fastdfs-6.06.tar.gz libfastcommon-1.0.43.tar.gz nginx-1.15.4.tar.gz
192.168.146.138 作为tracker服务 ,fastdfs-6.06.tar.gz libfastcommon-1.0.43.tar.gz nginx-1.15.4.tar.gz
192.168.146.139 作为storage服务 ,fastdfs-6.06.tar.gz libfastcommon-1.0.43.tar.gz nginx-1.15.4.tar.gz
192.168.146.140 作为storage服务 ,fastdfs-6.06.tar.gz libfastcommon-1.0.43.tar.gz nginx-1.15.4.tar.gz fastdfs-nginx-module-1.22.tar.gz
192.168.146.141作为storage服务 ,fastdfs-6.06.tar.gz libfastcommon-1.0.43.tar.gz nginx-1.15.4.tar.gz fastdfs-nginx-module-1.22.tar.gz
192.168.146.142作为storage服务 ,fastdfs-6.06.tar.gz libfastcommon-1.0.43.tar.gz nginx-1.15.4.tar.gz fastdfs-nginx-module-1.22.tar.gz
192.168.146.143 作为单独nginx负载均衡 , nginx-1.15.4.tar.gz
2.编译环境
yum install git gcc gcc-c++ make automake autoconf libtool pcre pcre-devel zlib zlib-devel openssl-devel wget vim -y
3.安装libfastcommon
4.安装FastDFS
5.修改配置文件
137和138:vim /etc/fdfs/tracker.conf
base_path = /opt/fastdfsdata/tracker # 存储日志和数据的根目录
6.启动137,138
fdfs_trackerd /etc/fdfs/tracker.conf
7.139,140 vim /etc/fdfs/storage.conf
#需要修改的内容如下
group_name = group1 port=23000 # storage服务端口(默认23000,一般不修改) base_path=/home/dfs # 数据和日志文件存储根目录 改成相应目录就好,但目录要存在 store_path0=/home/dfs # 第一个存储目录 tracker_server = 192.168.146.137:22122
tracker_server = 192.168.146.138:22122 # tracker服务器IP和端口 http.server_port=8888 # http访问文件的端口(默认8888,看情况修改,和nginx中保持一致)
141,142 vim /etc/fdfs/storage.conf
#需要修改的内容如下
group_name = group2 port=23000 # storage服务端口(默认23000,一般不修改) base_path=/home/dfs # 数据和日志文件存储根目录 改成相应目录就好,但目录要存在 store_path0=/home/dfs # 第一个存储目录 tracker_server = 192.168.146.137:22122 tracker_server = 192.168.146.138:22122 # tracker服务器IP和端口 http.server_port=8888 # http访问文件的端口(默认8888,看情况修改,和nginx中保持一致)
139,140,141,142启动storage服务
fdfs_storaged /etc/fdfs/storage.conf
8.139,140,141,142 安装fastdfs-nginx-module
解压进入 记住 /opt/fastdfs-nginx-module-1.22/src 这个路径
cp /opt/fastdfs-nginx-module-1.22/src/mod_fastdfs.conf /etc/fdfs/
9. 139,140,141,142 安装nginx
解压进入
cd nginx-1.15.4/ #添加fastdfs-nginx-module模块 ./configure --add-module=/opt/fastdfs-nginx-module-1.22/src/ make && make install #编译安装
10.修改配置文件
139,140
vim /etc/fdfs/mod_fastdfs.conf #需要修改的内容如下
base_path=/opt/data/nginx_mod tracker_server=192.168.146.137:22122
tracker_server=192.168.146.138:22122 #tracker服务器IP和端口
group_name=group1 url_have_group_name=true store_path0=/opt/data/storagefile
group_count = 2
[group1]
group_name=group1
storage_server_port=23000
store_path_count=1
store_path0=/opt/data/storagefile
[group2]
group_name=group2
storage_server_port=23000
store_path_count=1
store_path0=/opt/data/storagefile
141,142
vim /etc/fdfs/mod_fastdfs.conf #需要修改的内容如下 base_path=/opt/data/nginx_mod tracker_server=192.168.146.137:22122 tracker_server=192.168.146.138:22122 #tracker服务器IP和端口 group_name=group2 url_have_group_name=true store_path0=/opt/data/storagefile group_count = 2 [group1] group_name=group1 storage_server_port=23000 store_path_count=1 store_path0=/opt/data/storagefile [group2] group_name=group2 storage_server_port=23000 store_path_count=1 store_path0=/opt/data/storagefile
139,140,141,142配置nginx
#配置nginx.config vim /usr/local/nginx/conf/nginx.conf
location ~/group[0-9]/ {
ngx_fastdfs_module;
}
137,138配置nginx
upstream fastdfs_group_server{ server 192.168.146.139:80; server 192.168.146.140:80; server 192.168.146.141:80; server 192.168.146.142:80; } server { listen 80; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { root html; index index.html index.htm; } location ~/group[0-9]/ { proxy_pass http://fastdfs_group_server; }
143配置nginx
upstream fastdfs_group_server{ server 192.168.146.137:80; server 192.168.146.138:80; } server { listen 80; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { root html; index index.html index.htm; } location ~/group[0-9]/ { proxy_pass http://fastdfs_group_server; }
测试>>>>>>>>>>>>
依赖 maven仓库,没有的话,自己打包安装但仓库
<dependency> <groupId>org.csource</groupId> <artifactId>fastdfs-client-java</artifactId> <version>1.28-SNAPSHOT</version> </dependency>
fastdfs.conf
tracker_server=192.168.146.137:22122
tracker_server=192.168.146.138:22122
package com.lvym.fdfs.util; import org.csource.fastdfs.*; public class FastDFSClientUtil { public static void main(String[] args) { upload(); // downLoad(); //delete(); } private static void delete() { TrackerServer trackerServer =null; StorageServer storeStorage=null; try { ClientGlobal.init("fastdfs.conf"); // TrackerClient trackerClient = new TrackerClient(); trackerServer = trackerClient.getTrackerServer(); storeStorage = trackerClient.getStoreStorage(trackerServer); StorageClient storageClient = new StorageClient(trackerServer,storeStorage); /** * 组,文件长名 */ int deleteFile = storageClient.delete_file("group1","M00/00/00/wKiSjF85_3uAft4mAAO0-BD7IsE044.jpg"); System.out.println(deleteFile); } catch (Exception e) { } } private static void downLoad() { TrackerServer trackerServer =null; StorageServer storeStorage=null; try { ClientGlobal.init("fastdfs.conf"); // TrackerClient trackerClient = new TrackerClient(); trackerServer = trackerClient.getTrackerServer(); storeStorage = trackerClient.getStoreStorage(trackerServer); StorageClient storageClient = new StorageClient(trackerServer,storeStorage); /** * 文件路径,文件拓展名,文件的属性文件 */ int downloadFile = storageClient.download_file("group1", "M00/00/00/wKiSjF85_3uAft4mAAO0-BD7IsE044.jpg", "C:\\Users\\14829\\Desktop\\a.jpg"); System.out.println(downloadFile); } catch (Exception e) { } } private static void upload() { TrackerServer trackerServer =null; StorageServer storeStorage=null; try { ClientGlobal.init("fastdfs.conf"); // TrackerClient trackerClient = new TrackerClient(); trackerServer = trackerClient.getTrackerServer(); storeStorage = trackerClient.getStoreStorage(trackerServer); StorageClient storageClient = new StorageClient(trackerServer,storeStorage); /** * 文件路径,文件拓展名,文件的属性文件 */ String[] uploadFile = storageClient.upload_file("D:\\ym\\psb4ASJ96MH.jpg", "jpg", null); for (String s : uploadFile) { System.out.println(s); } } catch (Exception e) { } } }
boot
<groupId>org.csource</groupId> <artifactId>fastdfs-client-java</artifactId> <version>1.28-SNAPSHOT</version> </dependency> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.5</version> </dependency>
工具类
package com.lvym.fdfs.util; import org.apache.commons.io.IOUtils; import org.csource.common.MyException; import org.csource.common.NameValuePair; import org.csource.fastdfs.*; import java.io.FileOutputStream; import java.io.IOException; import java.util.Date; public class FastDFSClient { private TrackerClient trackerClient = null; private TrackerServer trackerServer = null; private StorageServer storageServer = null; private StorageClient1 storageClient = null; public FastDFSClient(String conf) throws Exception { if (conf.contains("classpath:")) { conf = conf.replace("classpath:", this.getClass().getResource("/").getPath()); } ClientGlobal.init(conf); trackerClient = new TrackerClient(); trackerServer = trackerClient.getTrackerServer(); storageServer = null; storageClient = new StorageClient1(trackerServer, storageServer); } /** * 上传文件方法 * <p>Title: uploadFile</p> * <p>Description: </p> * @param fileName 文件全路径 * @param extName 文件扩展名,不包含(.) * @param metas 文件扩展信息 * @return * @throws Exception */ public String uploadFile(String fileName, String extName, NameValuePair[] metas) throws Exception { String result = storageClient.upload_file1(fileName, extName, metas); return result; } public String uploadFile(String fileName) throws Exception { return uploadFile(fileName, null, null); } public String uploadFile(String fileName, String extName) throws Exception { return uploadFile(fileName, extName, null); } /** * 上传文件方法 * <p>Title: uploadFile</p> * <p>Description: </p> * @param fileContent 文件的内容,字节数组 * @param extName 文件扩展名 * @param metas 文件扩展信息 * @return * @throws Exception */ public String uploadFile(byte[] fileContent, String extName, NameValuePair[] metas) throws Exception { String result = storageClient.upload_file1(fileContent, extName, metas); return result; } public String uploadFile(byte[] fileContent) throws Exception { return uploadFile(fileContent, null, null); } public String uploadFile(byte[] fileContent, String extName) throws Exception { return uploadFile(fileContent, extName, null); } /** * 文件下载 * @param filePath 文件地址 * @param savePath 本地保存地址 */ public void download(String filePath,String savePath){ try { byte[] bytes = storageClient.download_file(filePath,savePath); IOUtils.write(bytes,new FileOutputStream(savePath)); } catch (Exception e) { e.printStackTrace(); } } /** * 文件下载 * @param groupName 文件地址 * @param remoteFilename 本地保存地址 */ public byte[] download2(String groupName,String remoteFilename) throws IOException, MyException { byte[] bytes = storageClient.download_file(groupName, remoteFilename); System.out.println(bytes); return bytes; } /** * 文件删除 * @param filePath 文件的地址 * @return */ public Boolean deleteFile(String filePath){ try { int i = storageClient.delete_file1(filePath); return i==0; } catch (Exception e) { e.printStackTrace(); return false; } } /** * 获取文件信息 * @param filePath 文件的地址 * @return */ public String getFileInfo(String filePath){ try { FileInfo fileInfo = storageClient.get_file_info1(filePath); String sourceIpAddr = fileInfo.getSourceIpAddr();//文件IP地址 long fileSize = fileInfo.getFileSize();//文件大小 Date createTimestamp = fileInfo.getCreateTimestamp();//文件创建时间 long crc32 = fileInfo.getCrc32();//错误码 return filePath; } catch (Exception e) { e.printStackTrace(); return null; } } }
正常调用就好
》》》》》》》》》》》。
将项目安装到本地Maven
1.mvn clean package
2.mvn install:install-file -DgroupId=org.csource -DartifactId=fastdfs-client-java -Dversion=1.28-SNAPSHOT -Dpackaging=jar -Dfile=jar包