分布式文件储存,使用FastDFS进行文件上传

一.环境:

  1. linux
  2. 确定安装 docker-compose容器
  3. 工具XTerm 或者 X_shell
第一步.

(1).准备docker-compose.yml配置文件
(2).我安装的目录是: /opt
(3).新建一个文件夹: compose-fasfdfs

image

(4).进入刚创建目录,创建文件 docker-compose.yml

image
(5).编辑创建的文件: vim docker-compose.yml

点击查看代码
version: '3.1' services: fastdfs-tracker: hostname: fastdfs-tracker container_name: fastdfs-tracker image: season/fastdfs:1.2 network_mode: "host" command: tracker volumes: - ./tracker_data:/fastdfs/tracker/data fastdfs-storage: hostname: fastdfs-storage container_name: fastdfs-storage image: season/fastdfs:1.2 network_mode: "host" volumes: - ./storage_data:/fastdfs/storage/data - ./store_path:/fastdfs/store_path environment: - TRACKER_SERVER=162.114.164.721:22122 command: storage depends_on: - fastdfs-tracker fastdfs-nginx: hostname: fastdfs-nginx container_name: fastdfs-nginx image: season/fastdfs:1.2 network_mode: "host" volumes: - /opt/docker_nginx/conf.d/:/etc/nginx/conf - ./store_path:/fastdfs/store_path environment: - TRACKER_SERVER=162.114.164.721:22122 command: nginx

代码完整图片:

image
(6).保存退出 : wq!
(7). 返回 /opt目录, 在目录 /opt/docker_nginx/conf.d下新建一个文件 nginx.conf
(8).配置 vim nginx.conf

点击查看代码
#user nobody; worker_processes 1; #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid; events { worker_connections 1024; }http {default_type application/octet-stream; #log_format main '$remote_addr - $remote_user [$time_local] "$request" ' # '$status $body_bytes_sent "$http_referer" ' # '"$http_user_agent" "$http_x_forwarded_for"'; #access_log logs/access.log main; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #gzip on;server { listen 7003; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location /group1/M00 { root /fastdfs/storage/data; ngx_fastdfs_module; }#error_page 404 /404.html; # redirect server error pages to the static page /50x.html #error_page 500 502 503 504 /50x.html; location = /50x.html { root html; }} }

(9).最后保存退出 回到 /opt/compose-fasfdfs目录
(10).执行命令: docker-comspoe up -d
(11).成功 退出

第二步 使用springboot测试文件上传

(1).导入依赖

点击查看依赖代码
<dependency> <groupId>net.oschina.zcx7878</groupId> <artifactId>fastdfs-client-java</artifactId> <version>1.27.0.0</version> </dependency>
(2).在resources资源文件目录创建 fastdfs.properties
点击资源查看代码
fastdfs.tracker_servers=162.114.169.151:22122
fastdfs.connect_timeout_in_seconds=5
fastdfs.network_timeout_in_seconds=30
fastdfs.charset=UTF-8
(3).编写测试类
点击查看代码
 ClientGlobal.initByProperties("fastdfs-client.properties");
        System.out.println("fastdfs.network_timeout_in_seconds=" + ClientGlobal.g_network_timeout + "ms");
        System.out.println("fastdfs.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\\Administrator\\Desktop\\xz.jpg";
        String fileid;
        fileid = sc1.upload_file1(item, "png", meta_list);
        System.out.println("Upload local file " + item + " ok, fileid=" + fileid);
(4).完结
新手上路,多多谅解;
如有疑问?联系作者

posted on 2021-12-08 23:10  |完美世界|  阅读(406)  评论(0)    收藏  举报

导航