docker安装nginx+fastdfs(含java demo)
1、查看 fastdfs 镜像列表
命令:
docker search fastdfs
2、拉取镜像
命令:
docker pull delron/fastdfs
这个版本的镜像是比较全的,含有fastdfs以及nginx以及其相关的配置。
3、准备存储路径和配置文件路径
3.1、主机创建目录
主机创建 上传文件存储目录
命令:
mkdir -p /home/fastdfs/storage
主机创建 主要配置文件的存放目录
命令:
mkdir -p /home/fastdfs/conf
3.2、 从容器中复制出配置文件
1、启动fastdfs容器 tracker
命令:
docker run -d --network=host --name tracker -v /home/fastdfs/storage:/var/fdfs delron/fastdfs tracker
2、从容器中复制出 storage.conf 和 nginx.conf 文件
命令:
docker cp storage:/etc/fdfs/storage .conf /home/fastdfs/conf/
docker cp storage:/usr/local/nginx/conf/nginx.conf /home/fastdfs/conf/3、修改配置文件
命令:
vi /home/fastdfs/conf/storage.conf
文件最后这个http.server_port 这个就是你要让fastdfs 里nginx代理文件的对应端口(默认是8888),记住这个端口
命令:
vi /home/fastdfs/conf/nginx.conf
此处端口就要和上一步 storage.conf中配置的端口保持一致
4、正式启动fastdfs
4.1、先停止先前临时启动的tracker
命令:
docker ps -a
命令:
docker kill tracker
docker rm tracker
4.2、正式启动 tracker
命令:
docker run -d --network=host --name tracker
-v /home/fastdfs/storage:/var/fdfs
-v /home/fastdfs/conf/storage.conf:/etc/fdfs/storage.conf:ro
-v /home/fastdfs/conf/nginx.conf:/usr/local/nginx/conf/nginx.conf:ro
delron/fastdfs tracker说明:
– name tracker 表示给当前容器命名
第一个 -v 将主机的 /home/fastdfs/storage 目录映射到 容器的 /var/fdfs目录中,此目录用于存储文件
第二个 -v 将主机的 storage.conf 配置文件映射到容器中的 storage.conf,这样启动时就会读主机配置
第三个 -v 将主机nginx.conf 映射到容器中,启动nginx时会读主机配置文件
delron/fastdfs tracker 指定启动的镜像
4.3、正式启动storage
命令:
docker run -d --network=host --name storage
-v /home/fastdfs/storage:/var/fdfs
-v /home/fastdfs/conf/storage.conf:/etc/fdfs/storage.conf:ro
-v /home/fastdfs/conf/nginx.conf:/usr/local/nginx/conf/nginx.conf:ro
-e TRACKER_SERVER=192.168.102.10:22122 -e GROUP_NAME=group1 delron/fastdfs storage说明:
–name storage 和 前 3个 -v 和上面一样的意思
-e TRACKER_SERVER=192.168.102.10:22122 表示fastdfs 用的端口和主机映射ip,此处把ip改成自己主机ip即可
4.4、服务停止
命令:
docker kill tracker
docker kill storage
docker rm tracker
docker rm storage
5、服务验证
5.1、命令验证
1、查看fastdfs 容器是否启动正常
命令:
docker ps
2、上传文件
1、上传一张文件到主机 /home/fastdfs/storage 目录下
2、进入storage 容器
命令:
docker exec -it storage bash
由于主机 /home/fastdfs/storage 目录映射到了 容器中的 /var/fdfs ,所以此处可以看到刚在主机中上传的test.jpg图片
3、上传此图片到fastdfs
命令:
/usr/bin/fdfs_upload_file /etc/fdfs/client.conf test.jpg
出现 group1/M00/00/00/wKhmCmBFo7GAH1JhAAB7MT8bJfU514.jpg 就表示正常上传了
浏览器访问一下试试:
http://192.168.102.10:9999/group1/M00/00/00/wKhmCmBFo7GAH1JhAAB7MT8bJfU514.jpg
图片可以访问,表示很正常。
说明:
此处的ip就是你的主机ip,端口就是上面nginx中配置的那个端口
7、java demo实现文件上传和下载
7.1、pom依赖
<dependency> <groupId>org.csource</groupId> <artifactId>fastdfs-client-java</artifactId> <version>1.27</version> </dependency>
7,2、配置文件
在resource目录下放置 fdfs_client.conf 文件
内容如下
connect_timeout = 2 network_timeout = 30 charset = UTF-8 http.tracker_http_port = 9999 http.anti_steal_token = no #http.secret_key = FastDFS1234567890 tracker_server =192.168.102.10:22122
说明:
其中最主要的是
tracker_server =192.168.102.10:22122
配置成 fastdfs 服务器所在ip和端口即可
7.3、代码
package com.hhf.www; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.util.UUID; import org.apache.commons.io.IOUtils; import org.csource.common.MyException; import org.csource.common.NameValuePair; import org.csource.fastdfs.ClientGlobal; import org.csource.fastdfs.FileInfo; import org.csource.fastdfs.StorageClient; import org.csource.fastdfs.StorageServer; import org.csource.fastdfs.TrackerClient; import org.csource.fastdfs.TrackerServer; public class TestFastDfs { public static String conf_filename = "C:\\develop\\project\\Gitee-Project\\spring-cloud\\forum\\src\\main\\resources\\fdfs_client.conf"; //public String local_filename = "D:\\stsworkspace\\fastdfs-demo\\src\\main\\resources\\fdfs_client.conf"; public static String local_filename = "C:\\develop\\project\\Gitee-Project\\spring-cloud\\forum\\src\\main\\resources\\static\\img\\1.JPG"; public static String path = "C:\\develop\\project\\Gitee-Project\\spring-cloud\\forum\\src\\main\\resources\\static\\img\\"; public static void main(String[] args) { testUpload(); // testDownload(); } public static void testUpload() { try { ClientGlobal.init(conf_filename); TrackerClient tracker = new TrackerClient(); TrackerServer trackerServer = tracker.getConnection(); StorageServer storageServer = null; StorageClient storageClient = new StorageClient(trackerServer, storageServer); // NameValuePair nvp = new NameValuePair("age", "18"); NameValuePair nvp [] = new NameValuePair[]{ new NameValuePair("age", "18"), new NameValuePair("sex", "male") }; String fileIds[] = storageClient.upload_file(local_filename, "png", nvp); System.out.println(fileIds.length); System.out.println("组名:" + fileIds[0]); System.out.println("路径: " + fileIds[1]); String url = "http://192.168.102.10:9999/"+fileIds[0]+"/"+fileIds[1]; System.out.println(url); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (MyException e) { e.printStackTrace(); } } public static void testDownload() { try { ClientGlobal.init(conf_filename); TrackerClient tracker = new TrackerClient(); TrackerServer trackerServer = tracker.getConnection(); StorageServer storageServer = null; StorageClient storageClient = new StorageClient(trackerServer, storageServer); byte[] b = storageClient.download_file("group1", "M00/00/00/wKhmCmBFh_iAFRiSAEN7SXQ-0OI536.png"); System.out.println(b); IOUtils.write(b, new FileOutputStream(path+"\\"+UUID.randomUUID().toString()+".png")); } catch (Exception e) { e.printStackTrace(); } } public void testGetFileInfo(){ try { ClientGlobal.init(conf_filename); TrackerClient tracker = new TrackerClient(); TrackerServer trackerServer = tracker.getConnection(); StorageServer storageServer = null; StorageClient storageClient = new StorageClient(trackerServer, storageServer); FileInfo fi = storageClient.get_file_info("group1", "M00/00/00/wKgRcFV_08OAK_KCAAAA5fm_sy874.conf"); System.out.println(fi.getSourceIpAddr()); System.out.println(fi.getFileSize()); System.out.println(fi.getCreateTimestamp()); System.out.println(fi.getCrc32()); } catch (Exception e) { e.printStackTrace(); } } public void testGetFileMate(){ try { ClientGlobal.init(conf_filename); TrackerClient tracker = new TrackerClient(); TrackerServer trackerServer = tracker.getConnection(); StorageServer storageServer = null; StorageClient storageClient = new StorageClient(trackerServer, storageServer); NameValuePair nvps [] = storageClient.get_metadata("group1", "M00/00/00/wKgRcFV_08OAK_KCAAAA5fm_sy874.conf"); for(NameValuePair nvp : nvps){ System.out.println(nvp.getName() + ":" + nvp.getValue()); } } catch (Exception e) { e.printStackTrace(); } } public void testDelete(){ try { ClientGlobal.init(conf_filename); TrackerClient tracker = new TrackerClient(); TrackerServer trackerServer = tracker.getConnection(); StorageServer storageServer = null; StorageClient storageClient = new StorageClient(trackerServer, storageServer); int i = storageClient.delete_file("group1", "M00/00/00/wKgRcFV_08OAK_KCAAAA5fm_sy874.conf"); System.out.println( i==0 ? "删除成功" : "删除失败:"+i); } catch (Exception e) { e.printStackTrace(); } } }
控制台执行,打印如下
至此完毕!
注:此文档里没写 关于防火墙之类的配置,如果你自己比较玩的比较溜就自己配置防火墙,如果不会且只是安装测试,可以先关了防火墙调通了再开。