1. 程式人生 > >docker安裝fastdfs與java客戶端測試

docker安裝fastdfs與java客戶端測試

一、docker 安裝FastDFS

1、拉取映象

docker pull morunchang/fastdfs

2、建立並啟動tracker容器

docker run -d --name=tracker -v /home/fastdfs_docker/fdfs/tracker:/data/fast_data --privileged=true --net=host morunchang/fastdfs sh tracker.sh

 

3、建立並啟動storage容器、此處只做單機版測試

     注意:由於tracker容器使用host網路模式、與宿主公用network namespace, 因此tracker容器ip與宿主機ip一致

docker run -d --name=storage -v /home/fastdfs_docker/fdfs/storage_data:/data/fast_data --privileged=rue --net=host -e TRACKER_IP=[宿主機ip]:22122 -e GROUP_NAME=group1 morunchang/fastdfs sh storage.sh

 

 

二、java客戶端測試

1、建立maven測試工程

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<
project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>xc-framework-parent</artifactId> <groupId
>com.dehigher</groupId> <version>1.0-SNAPSHOT</version> <relativePath>../xc-framework-parent/pom.xml</relativePath> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>test-fastdfs</artifactId> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- https://mvnrepository.com/artifact/net.oschina.zcx7878/fastdfs-client-java --> <dependency> <groupId>net.oschina.zcx7878</groupId> <artifactId>fastdfs-client-java</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-io</artifactId> </dependency> </dependencies> </project>

 

 

2、在classpath:config下建立fastdfs-client.properties檔案

fastdfs-client.properties

fastdfs.connect_timeout_in_seconds = 5  #http連線超時時間
fastdfs.network_timeout_in_seconds = 60 #tracker 與 storage 通訊連線超時時間
fastdfs.charset = UTF-8 #字元編碼
fastdfs.tracker_servers = [tracker_server_ip]:22122 #tracker_server_ip

 

3、上傳檔案測試

    /**
     * 上傳檔案
     * @throws Exception
     */
    @Test
    public void testUpload() throws Exception{
        
        ClientGlobal.initByProperties("config/fastdfs-client.properties");
        System.out.println("network_timeout=" + ClientGlobal.g_network_timeout + "ms");
        System.out.println("charset=" + ClientGlobal.g_charset);
        //建立客戶端
        TrackerClient tc = new TrackerClient();
        //連線tracker Server
        TrackerServer ts = tc.getConnection();
        if (ts == null) {
        System.out.println("getConnection return null");
        return;
        } 
        //獲取一個storage server
        StorageServer ss = tc.getStoreStorage(ts);
        if (ss == null) {
        System.out.println("getStoreStorage return null");
        } 
        //建立一個storage儲存客戶端
        StorageClient1 sc1 = new StorageClient1(ts, ss);
        NameValuePair[] meta_list = null; //new NameValuePair[0];
        String item = "C:\\Users\\degao\\Pictures\\111.png";
        String fileid;
        fileid = sc1.upload_file1(item, "png", meta_list);
        System.out.println("Upload local file " + item + " ok, fileid=" + fileid);
    }

 

 

4、查詢檔案資訊測試

    /**
     * 查詢檔案
     * @throws Exception
     */
    @Test
    public void testQueryFile() throws Exception{
        ClientGlobal.initByProperties("config/fastdfs-client.properties");
        TrackerClient tracker = new TrackerClient();
        TrackerServer trackerServer = tracker.getConnection();
        StorageServer storageServer = null;
        StorageClient storageClient = new StorageClient(trackerServer,
        storageServer);
        FileInfo fileInfo = storageClient.query_file_info("group1",
                "M00/00/00/rBsAAlwM58eAcFPfAAFs5SjEXRM444.png");
        System.out.println(fileInfo);
    }

 

 

5、下載檔案測試

    /**
     * 下載檔案
     */
    @Test
    public void testDownloadFile() throws Exception {
    ClientGlobal.initByProperties("config/fastdfs-client.properties");
    TrackerClient tracker = new TrackerClient();
    TrackerServer trackerServer = tracker.getConnection();
    StorageServer storageServer = null;
    StorageClient1 storageClient1 = new StorageClient1(trackerServer,
    storageServer);
    byte[] result = storageClient1.download_file1("group1/M00/00/00/rBsAAlwM58eAcFPfAAFs5SjEXRM444.png");
    File file = new File("d:/1.png");
    FileOutputStream fileOutputStream = new FileOutputStream(file);
    fileOutputStream.write(result);
    fileOutputStream.close();
    }