1.查看下载镜像(内部包含nginx):
docker search fastdfs
docker pull delron/fastdfs
2.启动tracker
docker run -d --network=host --name tracker -v /var/fdfs/tracker:/var/fdfs delron/fastdfs tracker
3.启动storage(47.111.242.221为linux机器地址)
docker run -d --network=host --name storage -e TRACKER_SERVER=47.111.242.221:22122 -v /var/fdfs/storage:/var/fdfs -e GROUP_NAME=group1 delron/fastdfs storage
4.查看是否启动成功
docker ps
==============================================================
如果不需要修改文件访问端口号无需修改,需要的话则进入容器修改两个配置文件(默认都是8888)
进入storage容器:docker exec -it storage bash (或者容器ID,下同)
修改storage内部端口号(http.server_port=8888):vi /etc/fdfs/storage.conf
(配置集群集群:tracker_server=47.111.242.221:22122,每台机器的storage通过tracker_server去访问tracker)
修改Nginx端口与上面保持一致(listen 8888):vi /usr/local/nginx/conf/nginx.conf
修改完后退出容器(exit)再重启:docker restart 容器名或容器id(tracker容器与storage容器)
列出本机所有容器,包括已经终止运行的
docker container ls --all
停止一个已经在运行的容器
docker container stop 容器名或容器id
启动一个已经停止的容器
docker container start 容器名或容器id
**注意:如果无法重新运行,可以删除/var/fdfs/storage/data目录下的fdfs_storaged.pid 文件,然后重新运行storage。
==============================================================
5.测试存储图片
5.1.拷贝一张图片(test.png)到目录/var/fdfs/storage
5.2.进入storage容器:docker exec -it storage bash
进入fdfs目录:cd /var/fdfs,运行命令:/usr/bin/fdfs_upload_file /etc/fdfs/client.conf test.jpg,运行成功后会返回地址:group1/M00/00/00/rBDXUl68wwOAEkzXAACHBZl3G4c646.jpg
5.3.在浏览器中访问
http://127.0.0.1:8888/group1/M00/00/00/rBDXUl68wwOAEkzXAACHBZl3G4c646.jpg
6.退出容器:
执行:exit。
==============================================================
7.代码测试
7.1配置文件:
fastdfs.connect_timeout_in_seconds = 5
fastdfs.network_timeout_in_seconds = 30
fastdfs.charset = UTF-8
fastdfs.tracker_servers = 47.111.242.221:22122(此处注意非8888)
===
//加载fastdfs-client.properties配置文件
ClientGlobal.initByProperties("config/fastdfs-client.properties");
//定义TrackerClient,用于请求TrackerServer
TrackerClient trackerClient = new TrackerClient();
//连接tracker
TrackerServer trackerServer = trackerClient.getConnection();
//获取Stroage
StorageServer storeStorage = trackerClient.getStoreStorage(trackerServer);
//创建stroageClient
StorageClient1 storageClient1 = new StorageClient1(trackerServer,storeStorage);
//向stroage服务器上传文件
//本地文件的路径
String filePath = "d:/img/x.jpg";
//上传成功后拿到文件Id
String fileId = storageClient1.upload_file1(filePath, "jpg", null);
System.out.println(fileId);