1. 程式人生 > >fastdfs操作,基礎知識, 分散式檔案系統fastdfs

fastdfs操作,基礎知識, 分散式檔案系統fastdfs

fastdfs概念圖

在這裡插入圖片描述

fastdfs操作順序

    fastdfs 安裝
    
    //查詢:
    docker search fastdfs
    
    //下載:
    docker pull season/fastdfs
    
   // 本機方式啟動註冊中心tracker:
   docker run -itd --name trakcer -v ~/tracker_data:/fastdfs/tracker/data --net=host season/fastdfs tracker
     預設啟動22122//  禁用防火牆:
    vi /
etc/sysconfig/selinux //重啟虛擬機器: reboot

防火牆禁用方法

在這裡插入圖片描述

啟動倉庫storage

   docker run -itd --name storage -v ~/storage_data:/fastdfs/storage/data -v ~/store_path:
   /fastdfs/store_path --net=host -e TRACKER_SERVER:192.168.1.2:22122 season/fastdfs storage

進入容器


	//進入容器:
	docker exec -it storage bash
	
	//檢視配置:
more storage.conf //複製出來,修改配置再考回去 docker cp storage.conf:/fdfs_conf ~/ //考回去 docker cp ./storage.conf storage:/fdfs_conf/ //因為docker是精簡版的系統,所以修改配置需要拷貝出來改好再複製回去

在這裡插入圖片描述
將ip修改為Linux系統Ip
重啟storage:docker stop storage → docker start storage
再次進入storage容器 docker exec -it storage bash

確認配置是否修改成功

下圖箭頭為組名,預設group1,依次往上加,有組號說明tracker已啟動

    fdfs_monitor fdfs_conf/storage.conf

在這裡插入圖片描述
》》測試檔案上傳
檢視/usr/bin下的所有fdfs工具

[root@bogon fdfs]# ls /usr/bin | grep fdfs
fdfs_appender_test
fdfs_appender_test1
fdfs_append_file
fdfs_crc32
fdfs_delete_file
fdfs_download_file
fdfs_file_info
fdfs_monitor
fdfs_storaged
fdfs_test
fdfs_test1
fdfs_trackerd
fdfs_upload_appender
fdfs_upload_file  //上傳檔案

啟動客戶端

//啟動容器
docker run -itd --name fdfs_sh --net=host season/fastdfs sh

//進入容器
docker exec -it sh bash

//建立檔案賦值
a.txt: echo  dajiahao>a.txt

//進入目錄
root@localhost:/#  cd fdfs_conf

//上傳檔案
root@localhost:/fdfs_conf#  fdfs_upload_file storage.conf /a.txt

//上傳成功 返回的地址
group1/M00/00/00/wKiuglwGoJWARxYvAAAACZLaVGU072.txt  
//組名/地址/保證安全的隨機名字.字尾名

命令下載

	下載檔案到本地(fdfs_download_file <config_file> <file_id> [local_filename][root@bogon 00]# fdfs_download_file /etc/fdfs/client.conf group1/M00/00/00/wKiuglwGoJWARxYvAAAACZLaVGU072.txt ~/a.txt
	
	[root@bogon 00]# ll ~
	total 168836
	-rw-------. 1 root root      2913 Apr  5  2016 anaconda-ks.cfg
	-rw-r--r--  1 root root        13 Sep  7 20:35 a.txt
	-rw-r--r--  1 root root        13 Sep  7 20:46 bb.tx

刪除檔案(fdfs_delete_file <config_file> <file_id>)

   [root@bogon 00]# fdfs_delete_file /etc/fdfs/client.conf group1/M00/00/00/wKiuglwGoJWARxYvAAAACZLaVGU072.txt
    #刪除了該檔案 還可以檢視到它的資訊
    [root@bogon 00]# fdfs_file_info /etc/fdfs/client.conf group1/M00/00/00/wKiuglwGoJWARxYvAAAACZLaVGU072.txt
   
    source storage id: 0
    source ip address: 192.168.58.146
    file create timestamp: 2017-09-07 20:37:19
    file size: 13
    file crc32: 2430562586 (0x90DF711A)
    
    #再次嘗試刪除 發現報錯檔案不存在
    [root@bogon 00]# fdfs_delete_file /etc/fdfs/client.conf group1/M00/00/00/wKiuglwGoJWARxYvAAAACZLaVGU072.txt
    [2017-09-07 20:48:23] ERROR - file: tracker_proto.c, line: 48, server: 192.168.58.146:23000, response status 2 != 0
    delete file fail, error no: 2, error info: No such file or directory

java程式碼 上傳下載:

pom檔案

 <dependencies>
     <dependency>
         <groupId>com.luhuiguo</groupId>
         <artifactId>fastdfs-spring-boot-starter</artifactId>
         <version>0.2.0</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-jdbc</artifactId>
     </dependency>
     <dependency>
         <groupId>mysql</groupId>
         <artifactId>mysql-connector-java</artifactId>
     </dependency>
 </dependencies>

controller層

package cn.ps.controll;

@RestController
public class UploadControll {
    @Autowired
    JdbcTemplate jt;
    @Autowired
    FastFileStorageClient ffsc;

    @PostMapping("/up") //上傳檔案
    public String upload(@RequestParam MultipartFile myfile) throws IOException {
        String extname = FilenameUtils.getExtension(myfile.getOriginalFilename());
        StorePath group1 = ffsc.uploadFile("group1",myfile.getInputStream(), myfile.getSize(),extname);
        String sql="insert into filenametable (filename,groupname,filepath) values (?,?,?)";
        jt.update(sql,myfile.getOriginalFilename(),group1.getGroup(),group1.getPath());
        return group1.getFullPath();
    }

    //下載檔案
    @Autowired
    FastFileStorageClient storageClient;
    @GetMapping("/down/{fileid}")
    public void download(@PathVariable String fileid, HttpServletResponse resp) throws IOException {
        List query = jt.query("select * from filenametable where fileid="+fileid ,new ColumnMapRowMapper() );
        Map map=(Map)query.get(0);
        String filename = URLEncoder.encode(map.get("filename").toString(),"UTF-8");
        String groupname =  map.get("groupname").toString();
        String filepath =map.get("filepath").toString();
        resp.setHeader("Content-Disposition","attachment;filename="+filename);
        byte [] byt=storageClient.downloadFile(groupname,filepath);
        resp.getOutputStream().write(byt);
    }
}

html頁面

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>upload</title>
</head>
<body>
<form method="post" enctype="multipart/form-data" action="up">
    檔案:<input type="file" name="myfile" >
    <input type="submit" value="上傳">
</form>

   <a href="down/1">下載</a>

</body>
</html>

配置檔案application.yml

fdfs:
  connect-timeout: 10000 # 連線超時時間
  so-timeout: 5000  # 響應超時時間
  tracker-list:
  - 192.168.174.130:22122 #linux系統 docker執行地址
server:
  port: 8899 # window 埠
spring:
  datasource: # 資料庫連線
    url: jdbc:mysql://192.168.0.250/school
    password: ps123456
    username: root
    driver-class-name: com.mysql.jdbc.Driver
  http:
    multipart:
      max-file-size: 10485760 # 記憶體大小,單位byte

返回陣列 返回兩個引數 第一個是儲存的組名 第二是 圖片的路徑

組名/地址/隨機名/字尾名

group1/M00/00/00/wKiuglwGsX-Aa62AAACf_PEcWVI803.png