1. 程式人生 > >FastDFS--圖片伺服器

FastDFS--圖片伺服器

1、什麼是FastDFS

             FastDFS採用C語言編寫,是一款開源的分散式檔案系統。它充分考慮了冗餘備份、負載均衡、線性擴容等機制,主要是用於搭建相對高效能的檔案服務系統,提供檔案上傳、下載等服務

 

2、FastDFS的基本架構

               該架構主要由Tracker server和 Storage server組成。客戶端發出請求,Tracker Server接受請求進行檔案的上傳下載的排程,最後由Storage server完成檔案的上傳與下載。

              Tracker Server作用是負載均衡和排程,將其稱為追蹤伺服器和排程伺服器

              Storage Server的作用是檔案儲存,客戶端所上傳的檔案最終是儲存在Storage伺服器之上,Storage server沒有實現自己的檔案系統,而是利用作業系統的檔案系統來進行檔案的管理,可以將其理解為儲存伺服器

             如下圖所示,一般的的FastDFS分為兩種叢集:Tracker叢集以及Storager叢集。在Tracker叢集之中,Tracker server可以有多臺,Tracker server之間是相互平等關係同時提供服務,Tracker server不存在單點故障。客戶端請求Tracker server採用輪詢方式,如果請求的tracker無法提供服務則換另一個tracker;在Storage叢集中,採用分組的儲存方式,每一組儲存的內容一致,不同組之間不會進行通訊。

             注意:圖片伺服器中,Tracker叢集會不斷接受Storage Server的狀態報告,包括磁碟剩餘空間、檔案同步狀況、檔案上傳下載次數等統計資訊。

            

3、檔案上傳的基本流程

     

4、檔案下載的基本流程

5、封裝基本程式碼

public class FastDFSClient {

    private TrackerClient trackerClient = null;
    private TrackerServer trackerServer = null;
    private StorageServer storageServer = null;
    private StorageClient1 storageClient = null;
    
    public FastDFSClient(String conf) throws Exception {
        if (conf.contains("classpath:")) {
            conf = conf.replace("classpath:", this.getClass().getResource("/").getPath());
        }
        ClientGlobal.init(conf);
        trackerClient = new TrackerClient();
        trackerServer = trackerClient.getConnection();
        storageServer = null;
        storageClient = new StorageClient1(trackerServer, storageServer);
    }
    
    /**
     * 上傳檔案方法
     * <p>Title: uploadFile</p>
     * <p>Description: </p>
     * @param fileName 檔案全路徑
     * @param extName 副檔名,不包含(.)
     * @param metas 檔案擴充套件資訊
     * @return
     * @throws Exception
     */
    public String uploadFile(String fileName, String extName, NameValuePair[] metas) throws Exception {
        String result = storageClient.upload_file1(fileName, extName, metas);
        return result;
    }
    
    public String uploadFile(String fileName) throws Exception {
        return uploadFile(fileName, null, null);
    }
    
    public String uploadFile(String fileName, String extName) throws Exception {
        return uploadFile(fileName, extName, null);
    }
    
    /**
     * 上傳檔案方法
     * <p>Title: uploadFile</p>
     * <p>Description: </p>
     * @param fileContent 檔案的內容,位元組陣列
     * @param extName 副檔名
     * @param metas 檔案擴充套件資訊
     * @return
     * @throws Exception
     */
    public String uploadFile(byte[] fileContent, String extName, NameValuePair[] metas) throws Exception {
        
        String result = storageClient.upload_file1(fileContent, extName, metas);
        return result;
    }
    
    public String uploadFile(byte[] fileContent) throws Exception {
        return uploadFile(fileContent, null, null);
    }
    
    public String uploadFile(byte[] fileContent, String extName) throws Exception {
        return uploadFile(fileContent, extName, null);
    }
}


6、呼叫的基本程式碼

@Controller
public class PictrueController {

    //發起請求的ip地址:IMAGE_SERVER_URL=http://192.168.25.133/
    @Value("${IMAGE_SERVER_URL}")
    private String IMAGE_SERVER_URL;
    
    @RequestMapping(value="/pic/upload", produces=MediaType.TEXT_PLAIN_VALUE+";charset=utf-8")
    @ResponseBody
    public String fileUpload(MultipartFile uploadFile) {
        
        try { 
            //1、獲取檔案的副檔名
            String orignalFilename = uploadFile.getOriginalFilename();
            String extName = orignalFilename.substring(orignalFilename.lastIndexOf(".")+1);
            
            //2、建立FastDFSClient的客戶端(配置檔案中包含了圖片伺服器的地址:tracker_server=192.168.25.133:22122)
            FastDFSClient fastDFSClient = new FastDFSClient("classpath:conf/client.conf");
            
            //3、執行上傳處理
            String url = fastDFSClient.uploadFile(uploadFile.getBytes(), extName);
            
            //4、拼接返回的url和ip地址,拼裝成完整的url
            url = IMAGE_SERVER_URL + url;

            //封裝到map中(要求返回json資訊)
            Map<String,Object> result = new HashMap<>();
            result.put("error", 0);
            result.put("url", url);
            return JsonUtils.objectToJson(result);
        } catch (Exception e) {
            e.printStackTrace();
            Map<String,Object> result = new HashMap<>();
            result.put("error", 1);
            result.put("message", "圖片上傳失敗");
            return JsonUtils.objectToJson(result);
        }
    }
}