雲伺服器搭建分散式伺服器fastdfs
阿新 • • 發佈:2022-05-11
阿里雲伺服器搭建分散式伺服器fastdfs
一、準備環境
#1.使用wget命令來下載壓縮包,先安裝wget命令 yum install wget #下載fastdfs wget -c "https://github.com/happyfish100/fastdfs/archive/V6.06.tar.gz" \ -O fastdfs-6.06.tar.gz #下載libfastcommon wget -c "https://github.com/happyfish100/libfastcommon/archive/V1.0.43.tar.gz" \ -O libfastcommon-1.0.43.tar.gz #下載fastdfs-nginx-module wget -c "https://github.com/happyfish100/fastdfs-nginx-module/archive/V1.22.tar.gz" \ -O fastdfs-nginx-module-1.22.tar.gz #下載nginx wget -c http://nginx.org/download/nginx-1.17.7.tar.gz #安裝編譯工具及庫檔案 yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel #關閉防火牆 systemctl stop firewalld.service systemctl disable firewalld.service #檢視防火牆狀態 firewall-cmd --state
二、解壓壓縮包並安裝
1.找到上一步下載到的安裝包,我的是在root資料夾中
tar -zxvf fastdfs-6.06.tar.gz
tar -zxvf fastdfs-nginx-module-1.22.tar.gz
tar -zxvf libfastcommon-1.0.43.tar.gz
tar -zxvf nginx-1.17.7.tar.gz
2.編譯安裝
#安裝libfastcommon cd /root/libfastcommon-1.0.43 ./make.sh && ./make.sh install #安裝fastdfs cd /root/fastdfs-6.06 ./make.sh && ./make.sh install #啟用配置檔案 cd /etc/fdfs/ cp storage.conf.sample storage.conf cp client.conf.sample client.conf cp tracker.conf.sample tracker.conf mkdir -p /fastdfs/tracker #修改 tracker.conf 檔案 vi /etc/fdfs/tracker.conf #將tracker.conf檔案中的base_path的值設定位 /fastdfs/tracker #也就是base_path = /fastdfs/tracker #vim編輯器 / 進入搜尋模式 i 進入編輯模式 esc退出編輯模式 :wq 儲存並退出編輯器 #啟動tracker服務 /etc/init.d/fdfs_trackerd start #修改 storage.conf 檔案 #base_path = /fastdfs/storage #store_path0 = /fastdfs/storage #tracker_server = 公網ip:22122 #http.server_port = 80 #啟動 storage 服務 /etc/init.d/fdfs_storaged start #設定開機啟動 vi /etc/rc.d/rc.local #加入配置 /etc/init.d/fdfs_trackerd start #編輯 client.conf 檔案 vi /etc/fdfs/client.conf #base_path=/fastdfs/tracker #tracker_server=192.168.0.154:22122 #安裝nginx #先安裝pcre yum -y install pcre pcre-devel #進入 /root/fastdfs-nginx-module-1.22/src/ cd /root/fastdfs-nginx-module-1.22/src/ #編輯配置檔案 vi config #將config檔案中的/usr/local替換成/usr,直接在命令模式中輸入:%s+/usr/local+/usr 回車即可,然後:wq儲存退出 #進入 nginx 解壓目錄 cd /root/nginx-1.17.7 ./configure --prefix=/usr/local/nginx --with-http_stub_status_module ./configure --add-module=/root/download/fastdfs-nginx-module-1.22/src/ make && make install cp /root/download/fastdfs-nginx-module-1.22/src/mod_fastdfs.conf /etc/fdfs/ #修改配置 vi /etc/fdfs/mod_fastdfs.conf #connect_timeout=10 #tracker_server=公網ip:22122 #url_have_group_name = true #store_path0=/fastdfs/storage #進入fastdfd原始碼conf目錄 cd /root/download/fastdfs-6.06/conf/ cp http.conf mime.types /etc/fdfs/ #建立軟連線,如果建立失敗,看看資料夾是否存在 ln -s /fastdfs/storage/data/ /fastdfs/storage/data/M00 #修改nginx配置 vi /usr/local/nginx/conf/nginx.conf server { listen 80; server_name 公網ip; location ~/group([0-9])/M00 { root /fastdfs/storage/data; ngx_fastdfs_module; } }
三、測試
1.建立maven專案,pom.xml檔案
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.3.4.RELEASE</version> </parent> <dependencies> <dependency> <groupId>com.github.tobato</groupId> <artifactId>fastdfs-client</artifactId> <version>1.26.2</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> </dependency> </dependencies>
2.建立springboot應用程式類
@Configuration
@SpringBootApplication()
@Import(FdfsClientConfig.class)
@EnableMBeanExport(registration = RegistrationPolicy.IGNORE_EXISTING)
public class FileClientApplication {
public static void main(String[] args) {
SpringApplication.run(FileClientApplication.class,args);
}
}
3.建立FastDFSClient類
@Component
public class FastDFSClient {
private static Logger log =LoggerFactory.getLogger(FastDFSClient.class);
private static FastFileStorageClient fastFileStorageClient;
private static FdfsWebServer fdfsWebServer;
@Autowired
public void setFastDFSClient(FastFileStorageClient fastFileStorageClient, FdfsWebServer fdfsWebServer) {
FastDFSClient.fastFileStorageClient = fastFileStorageClient;
FastDFSClient.fdfsWebServer = fdfsWebServer;
}
/**
* @param multipartFile 檔案物件
* @return 返回檔案地址
* @author qbanxiaoli
* @description 上傳檔案
*/
public static String uploadFile(MultipartFile multipartFile) {
try {
StorePath storePath = fastFileStorageClient.uploadFile(multipartFile.getInputStream(), multipartFile.getSize(), FilenameUtils.getExtension(multipartFile.getOriginalFilename()), null);
return storePath.getFullPath();
} catch (IOException e) {
log.error(e.getMessage());
return null;
}
}
/**
* @param multipartFile 圖片物件
* @return 返回圖片地址
* @author qbanxiaoli
* @description 上傳縮圖
*/
public static String uploadImageAndCrtThumbImage(MultipartFile multipartFile) {
try {
StorePath storePath = fastFileStorageClient.uploadImageAndCrtThumbImage(multipartFile.getInputStream(), multipartFile.getSize(), FilenameUtils.getExtension(multipartFile.getOriginalFilename()), null);
return storePath.getFullPath();
} catch (Exception e) {
log.error(e.getMessage());
return null;
}
}
/**
* @param file 檔案物件
* @return 返回檔案地址
* @author qbanxiaoli
* @description 上傳檔案
*/
public static String uploadFile(File file) {
try {
FileInputStream inputStream = new FileInputStream(file);
StorePath storePath = fastFileStorageClient.uploadFile(inputStream, file.length(), FilenameUtils.getExtension(file.getName()), null);
return storePath.getFullPath();
} catch (Exception e) {
log.error(e.getMessage());
return null;
}
}
/**
* @param file 圖片物件
* @return 返回圖片地址
* @author qbanxiaoli
* @description 上傳縮圖
*/
public static String uploadImageAndCrtThumbImage(File file) throws IOException {
FileInputStream inputStream = new FileInputStream(file);
try {
StorePath storePath = fastFileStorageClient.uploadImageAndCrtThumbImage(inputStream, file.length(), FilenameUtils.getExtension(file.getName()), null);
return storePath.getFullPath();
} catch (Exception e) {
log.error(e.getMessage());
return null;
}
finally {
inputStream.close();
}
}
/**
* @param bytes byte陣列
* @param fileExtension 副檔名
* @return 返回檔案地址
* @author qbanxiaoli
* @description 將byte陣列生成一個檔案上傳
*/
public static String uploadFile(byte[] bytes, String fileExtension) {
ByteArrayInputStream stream = new ByteArrayInputStream(bytes);
StorePath storePath = fastFileStorageClient.uploadFile(stream, bytes.length, fileExtension, null);
return storePath.getFullPath();
}
/**
* @param fileUrl 檔案訪問地址
* @param file 檔案儲存路徑
* @author qbanxiaoli
* @description 下載檔案
*/
public static boolean downloadFile(String fileUrl, File file) throws IOException {
FileOutputStream stream = new FileOutputStream(file);
try {
StorePath storePath = StorePath.praseFromUrl(fileUrl);
byte[] bytes = fastFileStorageClient.downloadFile(storePath.getGroup(), storePath.getPath(), new DownloadByteArray());
stream.write(bytes);
} catch (Exception e) {
log.error(e.getMessage());
return false;
}
finally {
stream.close();
}
return true;
}
/**
* @param fileUrl 檔案訪問地址
* @author qbanxiaoli
* @description 刪除檔案
*/
public static boolean deleteFile(String fileUrl) {
if (StringUtils.isEmpty(fileUrl)) {
return false;
}
try {
StorePath storePath = StorePath.praseFromUrl(fileUrl);
fastFileStorageClient.deleteFile(storePath.getGroup(), storePath.getPath());
} catch (Exception e) {
log.error(e.getMessage());
return false;
}
return true;
}
// 封裝檔案完整URL地址
public static String getResAccessUrl(String path) {
String url = fdfsWebServer.getWebServerUrl() + path;
log.info("上傳檔案地址為:\n" + url);
return url;
}
}
4.修改application.properties配置檔案
fdfs.connect-timeout=600
fdfs.pool.max-total=153
fdfs.pool.max-wait-millis=102
fdfs.so-timeout=1500
fdfs.thumb-image.height=150
fdfs.thumb-image.width=150
fdfs.tracker-list=公網ip:22122
fdfs.web-server-url=http://公網ip/
spring.servlet.multipart.max-file-size=100MB
spring.servlet.multipart.max-request-size=100MB
5.測試
@RunWith(SpringRunner.class)
@SpringBootTest
public class FileClientApplicationTests {
@Test
public void Upload() {
String fileUrl = "圖片路徑";
System.out.println(fileUrl);
File file = new File(fileUrl);
String str = FastDFSClient.uploadFile(file);
FastDFSClient.getResAccessUrl(str);
}
@Test
public void Delete() {
FastDFSClient.deleteFile("");
}
}
參考文章: