Docker部署FastDFS

igxiaoshan Lv5

1 拉取镜像

1
docker pull morunchang/fastdfs

使用 docker images查看是否成功

2 运行tracker

1
docker run -d --name tracker --net=host morunchang/fastdfs sh tracker.sh

3 运行storage

1
2
3
4
docker run -d --name storage --net=host -e TRACKER_IP=<your tracker server address>:22122 -e GROUP_NAME=<group name> morunchang/fastdfs sh storage.sh

例子:
docker run -d --name storage --net=host -e TRACKER_IP=192.168.6.129:22122 -e GROUP_NAME=group1 morunchang/fastdfs sh storage.sh
  • 使用的网络模式是–net=host, 替换为你机器的Ip即可
  • 是组名,即storage的组
  • 如果想要增加新的storage服务器,再次运行该命令,注意更换 新组名

4 修改nginx的配置

进入storage的容器内部,修改nginx.conf

1
2
# 进入到storage容器内部
docker exec -it storage /bin/bash

进入到容器内部后

1
2
3
4
5
6
7
8
#1 通过命令来查询Nginx的安装位置
root@iZ8vb6w2xyjemtqcxtmaj4Z:/# whereis nginx
nginx: /etc/nginx
#2 查看当前Nginx的进程
root@iZ8vb6w2xyjemtqcxtmaj4Z:/# ps aux | grep nginx
root 16 0.0 0.0 32480 1480 ? Ss 13:18 0:00 nginx: master process /etc/nginx/sbin/nginx
nobody 100 0.0 0.0 33036 2116 ? S 14:15 0:00 nginx: worker process
root 118 0.0 0.0 11272 728 pts/1 S+ 14:54 0:00 grep --color=auto nginx

添加以下内容

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#3 修改Nginx的配置文件
vi /etc/nginx/conf/nginx.conf

#4 修改Nginx配置内容
server {
listen 80;
server_name localhost;

location ~ /M00 {
# storage 实际存储图片的位置
root /data/fast_data/data;
ngx_fastdfs_module;
}
}

#5 进入到Nginx sbin目录从新加载Nginx配置文件
cd /etc/nginx/sbin
# 重新加载配置文件
./nginx -s reload

storage存储的位置/data/fast_data/data

5 设置开机启动容器

1
2
docker update --restart=always  tracker
docker update --restart=always storage

如果更新不成功,查看是否是下面错误

IPv4 forwarding is disabled. Networking will not work

解决:https://www.cnblogs.com/python-wen/p/11224828.html

SpringBoot整合fastDFS

  • 引入依赖
1
2
3
4
5
6
7
<!-- https://mvnrepository.com/artifact/com.github.tobato/fastdfs-client -->
<dependency>
<groupId>com.github.tobato</groupId>
<artifactId>fastdfs-client</artifactId>
<version>1.27.2</version>
</dependency>

  • 配置地址
1
2
3
4
5
6
7
8
9
10
11
12
fdfs:
# 连接超时时间
connect-timeout: 1000
# 读取时间
so-timeout: 3000
# tracker服务配置地址列表
thumb-image:
width: 60
height: 60
tracker-list:
- 192.168.56.101:22122
url: http://192.168.56.101:8080/
  • 配置类
1
2
3
4
5
6
7
8
9
/**
* @author igsshan
* 导入FastDFS-Client组件
*/
@Configuration
@Import(FdfsClientConfig.class)
@EnableMBeanExport(registration = RegistrationPolicy.IGNORE_EXISTING)
public class FdfsConfiguration {
}
  • 构建工具类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/**
* @author igsshan
*/
@Component
public class FastDFSClient {

@Autowired
private FastFileStorageClient storageClient;

/**
* 上传
*
*/
public String uploadFile(MultipartFile file) throws IOException {
StorePath storePath =
storageClient.uploadFile((InputStream) file.getInputStream()
, file.getSize()
, FilenameUtils.getExtension(file.getOriginalFilename())
, null);
return storePath.getFullPath();
}

/**
* 删除(批量)
*
*/
public void delFile(String filePath) {
storageClient.deleteFile(filePath);

}


/**
* 删除(批量)
*
*/
public void delFiles(List<String> filePaths) {
for (String filePath : filePaths) {
storageClient.deleteFile(filePath);
}
}

/**
* 下载
*
* @param groupName groupName
* @param path path
* @return 返回
*/
public byte[] download(String groupName, String path) throws IOException {
InputStream ins = storageClient.downloadFile(groupName, path, ins1 -> {
// 将此ins返回给上面的ins
return ins1;
});

ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
byte[] buff = new byte[100];
int rc = 0;
while ((rc = ins.read(buff, 0, 100)) > 0) {
byteArrayOutputStream.write(buff, 0, rc);
}
return byteArrayOutputStream.toByteArray();
}
}
  • Demo
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
@Controller
@Slf4j
public class FileController {
@Autowired
private FastDFSClient fastDFSClient;

@Value("${fast.url}")
private String url;

@PostMapping("/upload")
@ResponseBody
public R fileUpload(MultipartFile file) {
log.info("----------上传图片开始------------");
if ((null == file || file.isEmpty() || file.getSize() == 0 ||
StringUtils.isBlank(file.getOriginalFilename()))) {
throw new BaseBusinessException("5001","文件信息不完整");
}
String uploadFile = null;
try {
uploadFile = fastDFSClient.uploadFile(file);
} catch (IOException e) {
e.printStackTrace();
}
String imageUrl = url + uploadFile;
log.info("----------上传图片完成;预览链接------------: {}",imageUrl);
return R.ok().put("data", imageUrl);
}
}