javascript
SpringBoot2.0 整合 FastDFS 中间件,实现文件分布式管理
一、FastDFS簡介
1、FastDFS作用
FastDFS是一個開源的輕量級分布式文件系統(tǒng),它對文件進(jìn)行管理,功能包括:文件存儲、文件同步、文件上傳、文件下載等,解決了大容量存儲和負(fù)載均衡的問題。
安裝連接:
安裝流程詳解
2、核心角色
FastDFS是由跟蹤服務(wù)器(trackerserver)、存儲服務(wù)器(storageserver)和客戶端(client)三個部分組成。
1)跟蹤服務(wù)器
FastDFS的協(xié)調(diào)者,負(fù)責(zé)管理所有的storage server和group,每個storage在啟動后會連接Tracker,告知自己所屬的group等信息,并保持周期性的心跳,tracker根據(jù)storage的心跳信息,建立group到[storage server list]的映射表。
2)存儲服務(wù)器
以組(group)為單位,一個group內(nèi)包含多臺storage機器,數(shù)據(jù)互為備份,存儲空間以group內(nèi)容量最小的storage為準(zhǔn),所以建議group內(nèi)的多個storage盡量配置相同,以免造成存儲空間的浪費。
3)客戶端
業(yè)務(wù)請求的發(fā)起方,通過專有接口,使用TCP/IP協(xié)議與跟蹤器服務(wù)器或存儲節(jié)點進(jìn)行數(shù)據(jù)交互。
3、運轉(zhuǎn)流程
1、存儲服務(wù)定時向跟蹤服務(wù)上傳狀態(tài)信息; 2、客戶端發(fā)起請求; 3、跟蹤器同步存儲器狀態(tài),返回存儲服務(wù)端口和IP; 4、客戶端執(zhí)行文件操作(上傳,下載)等。二、與SpringBoot2整合
1、核心步驟
1)、配置FastDFS執(zhí)行環(huán)境 2)、文件上傳配置 3)、整合Swagger2測試接口2、核心依賴
<!-- FastDFS依賴 --> <dependency><groupId>com.github.tobato</groupId><artifactId>fastdfs-client</artifactId><version>1.26.5</version> </dependency> <!-- Swagger2 核心依賴 --> <dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger2</artifactId><version>2.6.1</version> </dependency> <dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger-ui</artifactId><version>2.6.1</version> </dependency>3、配置FastDFS
2)文件工具類
@Component public class FileDfsUtil {private static final Logger LOGGER = LoggerFactory.getLogger(FileDfsUtil.class);@Resourceprivate FastFileStorageClient storageClient ;/*** 上傳文件*/public String upload(MultipartFile multipartFile) throws Exception{String originalFilename = multipartFile.getOriginalFilename().substring(multipartFile.getOriginalFilename().lastIndexOf(".") + 1);StorePath storePath = this.storageClient.uploadImageAndCrtThumbImage(multipartFile.getInputStream(),multipartFile.getSize(),originalFilename , null);return storePath.getFullPath() ;}/*** 刪除文件*/public void deleteFile(String fileUrl) {if (StringUtils.isEmpty(fileUrl)) {LOGGER.info("fileUrl == >>文件路徑為空...");return;}try {StorePath storePath = StorePath.parseFromUrl(fileUrl);storageClient.deleteFile(storePath.getGroup(), storePath.getPath());} catch (Exception e) {LOGGER.info(e.getMessage());}} }4、文件上傳配置
spring:application:name: ware-fast-dfsservlet:multipart:enabled: truemax-file-size: 10MBmax-request-size: 20MB5、配置Swagger2
主要用來生成文件上傳的測試界面。
1)配置代碼類
@Configuration public class SwaggerConfig {@Beanpublic Docket createRestApi() {return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select().apis(RequestHandlerSelectors.basePackage("com.fast.dfs")).paths(PathSelectors.any()).build();}private ApiInfo apiInfo() {return new ApiInfoBuilder().title("SpringBoot利用Swagger構(gòu)建API文檔").description("使用RestFul風(fēng)格, 創(chuàng)建人:知了一笑").termsOfServiceUrl("https://github.com/cicadasmile").version("version 1.0").build();} }2)啟動類注解
@EnableSwagger2三、演示案例
1、接口代碼
@RestController public class FileController {@Resourceprivate FileDfsUtil fileDfsUtil ;/*** 文件上傳*/@ApiOperation(value="上傳文件", notes="測試FastDFS文件上傳")@RequestMapping(value = "/uploadFile",headers="content-type=multipart/form-data", method = RequestMethod.POST)public ResponseEntity<String> uploadFile (@RequestParam("file") MultipartFile file){String result ;try{String path = fileDfsUtil.upload(file) ;if (!StringUtils.isEmpty(path)){result = path ;} else {result = "上傳失敗" ;}} catch (Exception e){e.printStackTrace() ;result = "服務(wù)異常" ;}return ResponseEntity.ok(result);}/*** 文件刪除*/@RequestMapping(value = "/deleteByPath", method = RequestMethod.GET)public ResponseEntity<String> deleteByPath (){String filePathName = "group1/M00/00/00/wKhIgl0n4AKABxQEABhlMYw_3Lo825.png" ;fileDfsUtil.deleteFile(filePathName);return ResponseEntity.ok("SUCCESS") ;} }2、執(zhí)行流程
1、訪問http://localhost:7010/swagger-ui.html測試界面 2、調(diào)用文件上傳接口,拿到文件在FastDFS服務(wù)的路徑 3、瀏覽器訪問:http://192.168.72.130/group1/M00/00/00/wKhIgl0n4AKABxQEABhlMYw_3Lo825.png 4、調(diào)用刪除接口,刪除服務(wù)器上圖片 5、清空瀏覽器緩存,再次訪問圖片Url,回返回404四、源代碼地址
GitHub地址:知了一笑 https://github.com/cicadasmile/middle-ware-parent 碼云地址:知了一笑 https://gitee.com/cicadasmile/middle-ware-parent
總結(jié)
以上是生活随笔為你收集整理的SpringBoot2.0 整合 FastDFS 中间件,实现文件分布式管理的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Kmeans CUDA
- 下一篇: SpringBoot2.0 基础案例(1