以前大學時搭建過一個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; } }