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
| docker exec -it storage /bin/bash
|
进入到容器内部后
1 2 3 4 5 6 7 8
| root@iZ8vb6w2xyjemtqcxtmaj4Z:/ nginx: /etc/nginx
root@iZ8vb6w2xyjemtqcxtmaj4Z:/ 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
| vi /etc/nginx/conf/nginx.conf
server { listen 80; server_name localhost; location ~ /M00 { root /data/fast_data/data; ngx_fastdfs_module; } }
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
| <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 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
|
@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
|
@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); } }
public byte[] download(String groupName, String path) throws IOException { InputStream ins = storageClient.downloadFile(groupName, path, ins1 -> { 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(); } }
|
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); } }
|