FastDFS
- 是由阿里巴巴架构师——余庆开源的个人项目。
fastDFS 是以C语言开发的一项开源轻量级分布式文件系统,他对文件进行管理,主要功能有:文件存储,文件同步,文件访问(文件上传/下载),特别适合以文件为载体的在线服务,如图片网站,视频网站等。
架构示意图
所需文件
- libfastcommon
fastDFS所依赖的包
- fastDFS
fastDFS
- Nginx
- fastDFS-Nginx-Module
Nginx-FastDFS模块
链接:https://pan.baidu.com/s/1FG4CZjXLTM9x5zIjPXaA_w 密码:4aje
Tracker
-
配置文件:
- 将解压缩后的文件夹中的
conf
目录下的所有文件,拷贝至/etc/fdfs
下 - 配置
base path
,数据存储的基本目录 - 配置
reserved_storage_space
(剩余空间小于这个数额将会储存失败)
- 将解压缩后的文件夹中的
-
启动命令:
/usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf start
Storage
-
配置文件:
- 将解压缩后的文件夹中的
conf
目录下的所有文件,拷贝至/etc/fdfs
下(若与Tracker是同一个机子,则不必) - 配置
base path
,数据存储的基本目录 - 配置
store_path
,store_path0
指代Group集群中的第一个 - 配置
tracker_server
,内容为Tracker的ip + port
- 将解压缩后的文件夹中的
-
启动命令:
/usr/bin/fdfs_storaged /etc/fdfs/storage.conf start
安装Nginx
-
下载Nginx包
wget http://nginx.org/download/nginx-1.17.0.tar.gz
-
解压缩
-
安装两个依赖
yum -y install pcre-devel yum -y install openssl openssl-devel
-
编译安装
./configure make make install
FastDFS-Nginx-Module
-
配置文件:
- 拷贝
src/mod_fastdfs.conf
至/etc/fdfs
- 配置
tracker_server
内容为Tracker的ip + port
- 配置
url_have_group_name
为true
含义为路径中包含group名 - 配置
store_path
,store_path0
指代Group集群中的第一个
- 拷贝
-
为Nginx添加新的模块
./configure --add-module=/opt/software/fastdfs-nginx-module-1.22/src make make install
-
编译安装后的Nginx默认在
/usr/local/nginx
目录下 -
编辑配置Nginx文件
/usr/local/nginx/conf/nginx.conf
location ~/group([0-9]) { ngx_fastdfs_module; }
-
启动Nginx
/usr/local/nginx/sbin/nginx
大功告成可以正常使用了,下面附上Code-Test
-
fastdfs-client.properties
配置文件,此处主要修改fastdfs.tracker_servers
成自己对应的tracker地址## fastdfs-client.properties fastdfs.connect_timeout_in_seconds = 5 fastdfs.network_timeout_in_seconds = 30 fastdfs.charset = UTF-8 fastdfs.http_anti_steal_token = false fastdfs.http_secret_key = FastDFS1234567890 fastdfs.http_tracker_http_port = 80 fastdfs.tracker_servers = 192.168.150.110:22122 ## Whether to open the connection pool, if not, create a new connection every time fastdfs.connection_pool.enabled = true ## max_count_per_entry: max connection count per host:port , 0 is not limit fastdfs.connection_pool.max_count_per_entry = 500 ## connections whose the idle time exceeds this time will be closed, unit: second, default value is 3600 fastdfs.connection_pool.max_idle_time = 3600 ## Maximum waiting time when the maximum number of connections is reached, unit: millisecond, default value is 1000 fastdfs.connection_pool.max_wait_time_in_ms = 1000
-
导入Maven依赖
<!-- https://mvnrepository.com/artifact/net.oschina.zcx7878/fastdfs-client-java --> <dependency> <groupId>net.oschina.zcx7878</groupId> <artifactId>fastdfs-client-java</artifactId> <version>1.27.0.0</version> </dependency>
-
上传文件、下载文件示例:
/** * FastDFS上传文件 * @throws IOException * @throws MyException */ @Test void upload() throws IOException, MyException { //加载配置文件 ClientGlobal.initByProperties("fastdfs-client.properties"); //tracker客户端 TrackerClient trackerClient = new TrackerClient(); //get tracker服务端 TrackerServer trackerServer = trackerClient.getConnection(); //storage服务端 StorageServer storageServer = null; //new storage客户端 StorageClient1 storageClient1 = new StorageClient1(trackerServer, storageServer); //用于添加附加信息的 NameValuePair[] pairs = null; //返回的fastDFS文件名(路径) String file1 = storageClient1.upload_appender_file1("/Users/eddie/Downloads/CPU三级缓存.png", "png", pairs); //输出结果:group1/M00/00/00/wKiWbl8EfJ-EQasCAAAAAI0jLrY786.png System.out.println(file1); } /** * FastDFS下载文件 * @throws IOException * @throws MyException */ @Test void download() throws IOException, MyException { //加载配置文件 ClientGlobal.initByProperties("fastdfs-client.properties"); //tracker客户端 TrackerClient trackerClient = new TrackerClient(); //get tracker服务端 TrackerServer trackerServer = trackerClient.getConnection(); //storage服务端 StorageServer storageServer = null; //new storage客户端 StorageClient1 storageClient1 = new StorageClient1(trackerServer, storageServer); //通过storage客户端获取数据 byte[] bytes = storageClient1.download_file1("group1/M00/00/00/wKiWbl8EfJ-EQasCAAAAAI0jLrY786.png"); //new 文件输出流 FileOutputStream fos = new FileOutputStream("/Users/eddie/Downloads/CPU三级缓存.jpg"); //将文件写出 fos.write(bytes); fos.close(); }