Documentation is detailed, very detailed every step
The following link:
link: https: //pan.baidu.com/s/1TOqQJwLmcRvKKONTVN54hA
extraction code: 26p5
FastDfs installation documentation
Installation Order
- libfastcommon
- fdfs_tracker
dependence: gcc, libevent, perl - fdfs_storage
- FastDFS-nginx-module
- nginx
依赖:pcre-devel、zlib-devel
Function file directory
opt / fastdfs data storage directory
usr / local / fdfs startup file directory
etc / fdfs profile directory
usr / bin / fdfs_trackerd boot configuration
etc / init.d / fdfs_trackerd start the service script
FastDFS-tracker is mounted
tutorial Centos7.4 used as installation environments.
- Installation gcc environment-dependent yum install gcc -y
- yum -y install libevent
- If there is no perl library, use yum install perl * command to install it
- Installation libfastcommon
- Upload Package file libfastcommonV1.0.7.tar.gz to under / usr / local directory, and unzip it.
- tar -zxvf libfastcommonV1.0.7.tar.gz
- After extracting the files into a folder
- Compile ./make.sh
- If the compiler does not recognize the perl running this command appears below
- yum -y install zlib zlib-devel pcre pcre-devel gcc gcc-c++ openssl openssl-devel libevent libevent-devel perl unzip net-tools wget
- /make.sh install
- Note: After libfastcommon installed will automatically copy files to the library under / usr / lib64, due FastDFS program references usr / lib directory so you need to copy the library files in / usr / lib64 to under / usr / lib.
- cp /usr/lib64/libfastcommon.so /usr/lib/
Installation tracker
- Upload FastDFS_v5.05.tar.gz to under / usr / local directory
- Compile and install unzip
- tar -zxvf FastDFS_v5.05.tar.gz
- CD FastDFS
- ./make.sh
- ./make.sh install
- After successful installation, the installation files in the conf directory copied to / etc / fdfs / down.
- cd conf
- * cp / etc / FDF /
- Modify the configuration file
- vim /etc/fdfs/tracker.conf
- Fastdfs create folders
mkdir / opt / fastdfs
Set Startup Items
-
mkdir /usr/local/fdfs
-
And copy the install directory stop.sh restart.sh the / usr / local / FDFS /
CP restart.sh / usr / local / FDFS /
CP stop.sh / usr / local / FDFS / -
Modify the startup script
vim /etc/init.d/fdfs_trackerd
把启动脚本中的路径按照上图修改
修改完毕后
4. 注册服务
chkconfig --add fdfs_trackerd
然后可以用service fdfs_trackerd start 启动测试 如下图
FastDFS—storage配置
-
修改配置文件
vim /etc/fdfs/storage.conf
-
创建 fdfs_storage文件夹
mkdir /opt/fastdfs/fdfs_storage -
设置服务
vim /etc/init.d/fdfs_storaged
-
chkconfig - -add fdfs_storaged
-
启动服务
service fdfs_storaged start
上传图片测试
- 修改 /etc/fdfs/client.conf文件
vim /etc/fdfs/client.conf
base_path=/opt/fastdfs
tracker_server=192.168.67.163:22122
测试上传:
执行命令:/usr/bin/fdfs_test /etc/fdfs/client.conf upload 图片路径/文件名
FastDFS整合nginx
- 安装nginx整合插件fastdfs-nginx-module
上传fastdfs-nginx-module_v1.16.tar.gz上传到 /usr/local
tar -zxvf fastdfs-nginx-module_v1.16.tar.gz - 编辑配置文件:修改config文件将/usr/local/路径改为/usr/
(这里只修改两个路径,第二行不用改,图是错的)
vim fastdfs-nginx-module/src/config
- 将FastDFS-nginx-module/src下的mod_fastdfs.conf拷贝至/etc/fdfs/下
cp mod_fastdfs.conf /etc/fdfs/
并修改mod_fastdfs.conf的内容:
vim /etc/fdfs/mod_fastdfs.conf
- 将libfdfsclient.so拷贝至/usr/lib下
cp /usr/lib64/libfdfsclient.so /usr/lib/
安装nginx
- 创建nginx/client目录
mkdir -p /var/temp/nginx/client - 安装环境:
安装pcre库
yum -y install pcre-devel
安装zlib库
yum install -y zlib-devel - 上传nginx
上传nginx压缩包到/usr/local目录下,解压
tar -zxvf nginx-1.12.2.tar.gz - 添加fastdfs-nginx-module模块
cd nginx-1.12.2到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/fastdfs-nginx-module/src
注意:特别注意最后一行,请按照自己的实际目录填写
执行结果如下,就说明成功了
5. 编译
[root@localhost nginx-1.12.2]# make
6. 安装
[root@localhost nginx-1.12.2]# make install
7.编辑 nginx.conf
vim /usr/local/nginx/conf/nginx.conf
注意:是location / 不是/group1/M00/
图片内容有错误
启动nginx
-
/usr/local/nginx/sbin/nginx
-
设置开机启动
vim /etc/rc.d/rc.local -
需要关闭防火墙
systemctl disable firewalld -
测试
-
/usr/bin/fdfs_test /etc/fdfs/client.conf upload /root/ty.jpg
6. 这时候可以直接访问了
关于集群,就是在不同机器上配置多个storage,或者配置多个tracker
利用Java客户端调用FastDFS
服务器安装完毕后,咱们通过Java调用fastdfs
加载Maven依赖
fastdfs 没有在中心仓库中提供获取的依赖坐标。
只能自己通过源码方式编译,打好jar 包,安装到本地仓库。
官方仓库地址:
https://github.com/happyfish100/fastdfs-client-java
它其实就是一个maven工程,下载完成后
直接用idea 直接把这个源码作为模块导入工程
别的不用改,只把pom.xml中的版本改成1.27。
然后右边 执行install 就好了
安装好了 ,别的模块就可以直接使用这个坐标了
<groupId>org.csource</groupId>
<artifactId>fastdfs-client-java</artifactId>
<version>1.27</version>
而这个fastdfs-client-java模块可以从idea 中删除。
加入tracker.conf文件
tracker_server=172.16.31.67:22122
# 连接超时时间,针对socket套接字函数connect,默认为30秒
connect_timeout=30000
# 网络通讯超时时间,默认是60秒
network_timeout=60000
然后可以进行一下上传的测试
@RunWith(SpringRunner.class)
@SpringBootTest
@Test
public void textFileUpload() throws IOException, MyException {
String file = this.getClass().getResource("/tracker.conf").getFile();
ClientGlobal.init(file);
TrackerClient trackerClient=new TrackerClient();
TrackerServer trackerServer=trackerClient.getConnection();
StorageClient storageClient=new StorageClient(trackerServer,null);
String orginalFilename="e://victor.jpg";
String[] upload_file = storageClient.upload_file(orginalFilename, "jpg", null);
for (int i = 0; i < upload_file.length; i++) {
String s = upload_file[i];
System.out.println("s = " + s);
}
}
打印结果
这个打印结果实际上就是我们访问的路径,加上服务器地址我们可以拼接成一个字符串
可以在浏览器直接访问
http://172.16.31.67/group1/M00/00/00/wKhDo1qjU2qAWKQmAAATla901AQ534.jpg
上传成功!
对接到业务模块中
在修改Controller的方法
利用@Value 标签可以引用application.properties中的值
fileServer.url=http://172.16.31.67
@Value("${fileServer.url}")
String fileUrl;
@RequestMapping(value = "fileUpload",method = RequestMethod.POST)
public String fileUpload(@RequestParam("file") MultipartFile file) throws IOException, MyException {
String imgUrl=fileUrl;
if(file!=null){
System.out.println("multipartFile = " + file.getName()+"|"+file.getSize());
String configFile = this.getClass().getResource("/tracker.conf").getFile();
ClientGlobal.init(configFile);
TrackerClient trackerClient=new TrackerClient();
TrackerServer trackerServer=trackerClient.getConnection();
StorageClient storageClient=new StorageClient(trackerServer,null);
String filename= file.getOriginalFilename();
String extName = StringUtils.substringAfterLast(filename, ".");
String[] upload_file = storageClient.upload_file(file.getBytes(), extName, null);
imgUrl=fileUrl ;
for (int i = 0; i < upload_file.length; i++) {
String path = upload_file[i];
imgUrl+="/"+path;
}
}
return imgUrl;
}
至此我们解决了文件上传的功能。
建议:初级开发和高级开发的差别就是在环境搭建和功能设计和代码设计,架构设计的差别(个人认为),所以应该把上传文件的功能直接写成一个工具类,controller层直接进行注入,调用