0.docker宿主机要安装在Linux操作系统上,macbook会有问题,macbook上使用--net=host模式不起作用,可以安装CentOS虚拟机在虚拟机中安装docker
1.创建容器
docker pull morunchang/fastdfs
docker run -d --name tracker --net=host morunchang/fastdfs sh tracker.sh
docker run -d --name storage --net=host -e TRACKER_IP=172.16.200.153:22122 -e GROUP_NAME=group1 morunchang/fastdfs sh storage.sh
172.16.200.153是docker宿主机的IP,因为使用--net=host模式,docker tracker的IP和宿主机的是同一个,不单独分配IP
2.修改storage配置
docker exec -it storage /bin/bash
vi /data/nginx/conf/nginx.conf
location /group1/M00 {
proxy_next_upstream http_502 http_504 error timeout invalid_header;
proxy_cache http-cache;
proxy_cache_valid 200 304 12h;
proxy_cache_key $uri$is_args$args;
proxy_pass http://fdfs_group1;
expires 30d;
}
docker restart storage
3.测试
使用SpringBoot的测试功能
A.依赖:
<dependency>
<groupId>org.csource</groupId>
<artifactId>fastdfs-client-java</artifactId>
<version>1.27-SNAPSHOT</version>
</dependency>
B.fastdfs配置文件fdfs_client.conf
connect_timeout = 600
network_timeout = 600
tracker_server = 172.16.200.153:22122
C.application.properties
spring.servlet.http.multipart.max-file-size=10MB
spring.servlet.http.multipart.max-request-size=10MB
D.测试代码
@Test
public void testdfs() throws IOException, MyException{
ClientGlobal.init("/D/workspace/springcloudws/ws02/EpcData/fastdfsdemo/src/main/resources/fdfs_client.conf");
TrackerClient trackerClient = new TrackerClient();
TrackerServer trackerServer = trackerClient.getConnection();
StorageServer storageServer = null;
StorageClient storageClient = new StorageClient(trackerServer, storageServer);
String[] strings = storageClient.upload_file("/D/car.jpg", "jpg", null);
for (String str : strings) {
System.out.println(str);
}
}
返回结果:
group1
/M00/00/00/rBDImVyixpuAX1cDABcgY2VaiuE832.jpg
在浏览器中输入:
http://172.16.200.153:8080/group1/M00/00/00/rBDImVyixpuAX1cDABcgY2VaiuE832.jpg
访问到/D/car.jpg图片