以前大学时搭建过一个FastDFS的图片服务器,当时只是抱着好奇的态度搭着玩一下,当时搭建采用了一台虚拟机,tracker和storage服务在一台机器上放着,最近翻以前的博客忽然想着在两台机器上搭建试一下,顺便整合了SpringBoot实现了一下图片的上传服务。
新的阅读体验地址:http://www.zhouhong.icu/post/140
使用旧版本的在一台机器上搭建能够参考以前的那篇文章:https://www.cnblogs.com/Tom-shushu/p/10603723.html
连接:https://pan.baidu.com/s/1Lic4JfUT4a8YdYmqJ5_vcQ 提取码:bm0a 复制这段内容后打开百度网盘手机App,操做更方便哦
两台服务器html
yum install -y gcc gcc-c++
yum -y install libevent
# 1.解压 tar -zxvf libfastcommon-1.0.42.tar.gz
# 2.进入解压后的文件夹编译并安装 cd libfastcommon-1.0.42/ ./make.sh ./make.sh install
# 1.解压 tar -zxvf fastdfs-6.04.tar.gz
# 2.进入到fastdfs目录,查看fastdfs安装配置 cd fastdfs-6.04/ vim make.sh
# 3.安装fastdfs ./make.sh ./make.sh install
# 4.将FastDFS中conf中的文件拷贝到 /etc/fdfs下 cp /software/FastDFS/fastdfs-6.04/conf/* /etc/fdfs/
# 1.修改文件路径 base_path=/usr/local/fastdfs/tracker
# 2.建立文件路径 mkdir /usr/local/fastdfs/tracker -p
# 3.启动 /usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf
# 4.查看服务 ps -ef|grep tracker
# 5.中止tracker /usr/bin/stop.sh /etc/fdfs/tracker.conf
# 1.修改配置文件 storage.conf # 修改组名 group_name=zhouhong # 修改storage的工做空间 base_path=/usr/local/fastdfs/storage # 修改storage的存储空间 store_path0=/usr/local/fastdfs/storage # 修改tracker的地址和端口号,用于心跳 tracker_server=192.168.2.120:22122 # 后续结合nginx的一个对外服务端口号 http.server_port=8888
# 2.建立目录 mkdir /usr/local/fastdfs/storage -p
# 3.启动 /usr/bin/fdfs_storaged /etc/fdfs/storage.conf
4.查看 ps -ef|grep storage
# 1.修改client.conf文件 base_path=/usr/local/fastdfs/client tracker_server=192.168.2.120:22122
# 2.建立目录 mkdir /usr/local/fastdfs/client
# 3.准备一张图片测试 /usr/bin/fdfs_test /etc/fdfs/client.conf upload zhouhong.jpg
fastdfs安装好之后是没法经过http访问的,这个时候就须要借助nginx了,因此须要安装fastdfs的第三方模块到nginx中,就能使用了。
# 1.解压 tar -zxvf fastdfs-nginx-module-1.22.tar.gz
# 2.复制配置文件 cp /software/FastDFS/fastdfs-nginx-module-1.22/src/mod_fastdfs.conf /etc/fdfs/
# 3.修改/fastdfs-nginx-module/src/config文件 修改/fastdfs-nginx-module/src/config文件,主要是修改路径,把local删除, 由于fastdfs安装的时候咱们没有修改路径,原路径是/usr
如图所示,将local删除便可nginx
# 1.环境安装 yum install -y pcre pcre-devel yum install -y zlib zlib-devel yum install -y gcc-c++ yum install -y openssl openssl-decel
# 2.解压 tar -zxvf nginx-1.16.1.tar.gz
# 3.建立目录 mkdir /var/temp/nginx -p
# 4.进入解压目录、最后一个为fastdfs-nginx-module-1.22解压目录 ./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=/software/FastDFS/fastdfs-nginx-module-1.22/src
# 5.安装 make make install
# 6.修改 /etc/fdfs/mod_fastdfs.conf base_path=/usr/local/fastdfs/tmp tracker_server=192.168.2.120:22122 group_name=zhouhong url_have_group_name = true store_path0=/usr/local/fastdfs/storage
# 7.建立目录 mkdir /usr/local/fastdfs/tmp
# 8.修改/usr/local/nginx/conf/nginx.conf server { listen 8888; server_name localhost; location /zhouhong/M00 { ngx_fastdfs_module; } }
cd /usr/local/fastdfs/storage/data/00/00 ls
<dependency> <groupId>com.github.tobato</groupId> <artifactId>fastdfs-client</artifactId> <version>1.26.7</version> </dependency>
############################################################ # # fdfs 配置 # ############################################################ fdfs: connect-timeout: 30 # 链接的超时时间 so-timeout: 30 # 读取的超时时间 tracker-list: 192.168.2.120:22122 # tracker服务所在的ip地址和端口号
@Autowired private FastFileStorageClient fastFileStorageClient; @Override public String upload(MultipartFile file, String fileExtName) throws Exception { StorePath storePath = fastFileStorageClient.uploadFile(file.getInputStream(), file.getSize(), fileExtName, null); String path = storePath.getFullPath(); return path; }
@RestController @RequestMapping("fdfs") public class CenterUserController { @Autowired private FileResource fileResource; @Autowired private CenterUserService centerUserService; @Autowired private FileService fdfsService; @PostMapping("uploadFace") public JSONResult uploadFace( String userId, MultipartFile file, HttpServletRequest request, HttpServletResponse response) throws Exception { String path = ""; // 开始文件上传 if (file != null) { // 得到文件上传的文件名称 String fileName = file.getOriginalFilename(); if (StringUtils.isNotBlank(fileName)) { // 文件重命名 String fileNameArr[] = fileName.split("\\."); // 获取文件的后缀名 String suffix = fileNameArr[fileNameArr.length - 1]; if (!suffix.equalsIgnoreCase("png") && !suffix.equalsIgnoreCase("jpg") && !suffix.equalsIgnoreCase("jpeg") ) { return JSONResult.errorMsg("图片格式不正确!"); } path = fdfsService.upload(file, suffix); System.out.println(path); } } else { return JSONResult.errorMsg("文件不能为空!"); } if (StringUtils.isNotBlank(path)) { String finalUserFaceUrl = fileResource.getHost() + path; //更新图片地址到数据库 Users userResult = centerUserService.updateUserFace(userId, finalUserFaceUrl); UsersVO usersVO = conventUsersVO(userResult); CookieUtils.setCookie(request, response, "user", JsonUtils.objectToJson(usersVO), true); } else { return JSONResult.errorMsg("上传头像失败"); } return JSONResult.ok(); } }
@Component @PropertySource("classpath:file.properties") @ConfigurationProperties(prefix = "file") public class FileResource { private String host; public String getHost() { return host; } public void setHost(String host) { this.host = host; } }