使用docker安裝fastDFS
阿新 • • 發佈:2020-10-26
1.安裝fastdfs必要元件 tracker 什麼都不用改
1 docker run -d --network=host --name tracker -v /var/fdfs/tracker:/var/fdfs delron/fastdfs tracker
2.安裝fastdfs儲存地址 storage 除了ip其他的都不用改
1 docker run -d --network=host --name storage -e TRACKER_SERVER=你伺服器自己的ip:22122 -v /var/fdfs/storage:/var/fdfs -e GROUP_NAME=group1 delron/fastdfs storage
3.進入容器修改配置 一定要,要不然連結不上
1 docker exec -it tracker bash
4.修改配置
1 vi /etc/fdfs/client.conf
將配置 tracker_server=你自己的ip:22122
到這其實fastDFS就配好了
建立專案測試
我這裡用的是**springBoot**進行整合。**swagger-ui**進行圖片上傳
1.pom依賴
1 <!-- FastDFS依賴 --> 2 <dependency> 3 <groupId>com.github.tobato</groupId> 4<artifactId>fastdfs-client</artifactId> 5 <version>1.26.5</version> 6 </dependency> 7 <!-- Swagger2 核心依賴 --> 8 <dependency> 9 <groupId>io.springfox</groupId> 10 <artifactId>springfox-swagger2</artifactId> 11<version>2.6.1</version> 12 </dependency> 13 <dependency> 14 <groupId>io.springfox</groupId> 15 <artifactId>springfox-swagger-ui</artifactId> 16 <version>2.6.1</version> 17 </dependency>
2.配置 yml **需要修改ip**
1 spring: 2 servlet: 3 multipart: 4 max-file-size: 100MB # 最大支援檔案大小 5 max-request-size: 100MB # 最大支援請求大小 6 # 分散式檔案系統FDFS配置 7 fdfs: 8 # 連結超時 9 connect-timeout: 600 10 # 讀取時間 11 so-timeout: 600 12 # 生成縮圖引數 13 thumb-image: 14 width: 150 15 height: 150 16 tracker-list: 你自己的ip:22122
3.配置檔案(兩個)
SwaggerConfig.java **一定要改成你自己專案的controller包路徑,這裡會掃描你的介面**
1 @Configuration 2 public class SwaggerConfig { 3 @Bean 4 public Docket createRestApi() { 5 return new Docket(DocumentationType.SWAGGER_2) 6 .apiInfo(apiInfo()) 7 .select() 8 .apis(RequestHandlerSelectors.basePackage("top.mail.email.controller")) 9 .paths(PathSelectors.any()) 10 .build(); 11 } 12 private ApiInfo apiInfo() { 13 return new ApiInfoBuilder() 14 .title("SpringBoot利用Swagger構建API文件") 15 .description("使用RestFul風格, 建立人:知了一笑") 16 .termsOfServiceUrl("https://github.com/cicadasmile") 17 .version("version 1.0") 18 .build(); 19 } 20 }
DfsConfig.java
1 @Configuration 2 @Import(FdfsClientConfig.class) 3 // Jmx重複註冊bean的問題 4 @EnableMBeanExport(registration = RegistrationPolicy.IGNORE_EXISTING) 5 public class DfsConfig { 6 }
4.工具類
FileDfsUtil.java
1 @Component 2 public class FileDfsUtil { 3 private static final Logger LOGGER = LoggerFactory.getLogger(FileDfsUtil.class); 4 @Resource 5 private FastFileStorageClient storageClient ; 6 /** 7 * 上傳檔案 8 */ 9 public String upload(MultipartFile file) throws Exception{ 10 StorePath storePath = storageClient.uploadFile(file.getInputStream(),file.getSize(), FilenameUtils.getExtension(file.getOriginalFilename()),null); 11 return storePath.getFullPath() ; 12 } 13 /** 14 * 刪除檔案 15 */ 16 public void deleteFile(String fileUrl) { 17 if (StringUtils.isEmpty(fileUrl)) { 18 LOGGER.info("fileUrl == >>檔案路徑為空..."); 19 return; 20 } 21 try { 22 StorePath storePath = StorePath.parseFromUrl(fileUrl); 23 storageClient.deleteFile(storePath.getGroup(), storePath.getPath()); 24 } catch (Exception e) { 25 LOGGER.info(e.getMessage()); 26 } 27 } 28 }
5.controller介面
FileController.java
1 @RestController 2 public class FileController { 3 @Resource 4 private FileDfsUtil fileDfsUtil ; 5 /** 6 * 檔案上傳 7 */ 8 @ApiOperation(value="上傳檔案", notes="測試FastDFS檔案上傳") 9 @RequestMapping(value = "/uploadFile",headers="content-type=multipart/form-data", method = RequestMethod.POST) 10 public ResponseEntity<String> uploadFile (@RequestParam("file") MultipartFile file){ 11 String result ; 12 try{ 13 String path = fileDfsUtil.upload(file) ; 14 if (!StringUtils.isEmpty(path)){ 15 result = path ; 16 } else { 17 result = "上傳失敗" ; 18 } 19 } catch (Exception e){ 20 e.printStackTrace() ; 21 result = "服務異常" ; 22 } 23 return ResponseEntity.ok(result); 24 } 25 /** 26 * 檔案刪除 27 */ 28 @RequestMapping(value = "/deleteByPath", method = RequestMethod.GET) 29 public ResponseEntity<String> deleteByPath (){ 30 String filePathName = "group1/M00/00/00/wKhIgl0n4AKABxQEABhlMYw_3Lo825.png" ; 31 fileDfsUtil.deleteFile(filePathName); 32 return ResponseEntity.ok("SUCCESS") ; 33 } 34 }
6.springBoot啟動類
1 @SpringBootApplication 2 @EnableSwagger2 3 public class EmailApplication { 4 5 public static void main(String[] args) { 6 SpringApplication.run(EmailApplication.class, args); 7 } 8 9 }
啟動專案
訪問::::
http://localhost:8080/swagger-ui.html
返回地址表示上傳成功。