说在前面: 很重要 我就不介绍了Fdfs 是个啥了,需要安装两端, tracker 不需要处理nginx storage端才需要
参考这里也可以https://blog.51cto.com/7072753/2286604
1,跟踪器tracker 创建
docker run -d \
--network=host \
--name tracker \
-v /etc/localtime:/etc/localtime:ro \
-v /home/fdfs/tracker:/var/fdfs \
delron/fastdfs \
tracker
2,文件存储storage,ip地址就是tracker的ip
docker run -d \
--network=host \
--name storage \
-v /etc/localtime:/etc/localtime:ro \
-e TRACKER_SERVER=20.20.89.165:22122 \
-v /home/fdfs/storage:/var/fdfs \
-e GROUP_NAME=group1 \
delron/fastdfs \
storage
-v /etc/localtime:/etc/localtime:ro \ 是为了让容器内的时间和宿主机的时间同步
3,然后介绍几个配置文件
docker cp http.conf storage:/etc/fdfs/ (参考命令 考入)
anti-steal.jpg 是一个默认404图片,我自己网上找的
client.conf 是java端用的配置文件 (在容器的 /etc/fdfs/client.conf 中 需要保存出来放到java工程里)
核心参数:
tracker_server=192.168.0.197:22122 (改成你宿主机的ip)
http.conf 是storage 中的配置文件负责token验证用 (在容器的 /etc/fdfs/client.conf 中 改完了放回storage中)
核心参数:
http.anti_steal.check_token=true (token验证使用这个默认false)
http.anti_steal.token_ttl=60 (token超时时间)
http.anti_steal.secret_key=FastDFS1234567890 (token的key 和java 端一致)
http.anti_steal.token_check_fail=/home/yuqing/fastdfs/conf/anti-steal.jpg (就是上面那个图片地址)
tracker.conf 是追踪器tracker的配置文件 (基本不需要改,只是要改需要 往tracker容器里放)
port=22122
storage.conf 是storage的配置 (/etc/fdfs/storage.conf )
tracker_server=20.20.89.165:22122 (这个要设置成你宿主机的ip)
http.server_port=8888 (这个就是 以后的访问地址 接的端口)
mod_fastdfs.conf 这个是 nginx 的模块文件 (也是storage的文件 /etc/fdfs/mod_fastdfs.conf)
tracker_server=20.20.89.165:22122 (我的配置 这里面有 两个端口 挺坑的)
另外nginx.conf 这个文件的位置是/usr/local/nginx/conf/nginx.conf (建议打开日志,nginx 配置这里就不单独介绍了)
server {
listen 8888;
server_name localhost;
location ~/group[0-9]/ {
ngx_fastdfs_module;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
4,更改完所有配置文件 之后 重起2个容器
5,java端 需要先把那个clien.conf 拷贝到java项目中
文件只需要更改 改成 tracker 的ip就阔以
tracker_server=20.20.89.165:22122
6, 依赖
<dependency> <groupId>cn.bestwu</groupId> <artifactId>fastdfs-client-java</artifactId> <version>1.27</version> </dependency>
7,代码
需要注意的几点
- client.conf的路径不要出错
- 获取当前时间是秒,而且不能和服务器的时间超过分钟的同步
- 获取token不要把组名 带进去 切记 用的是M00/00/00/FBRZpV6Fq-WAWoMUAAITD_EyS8A387.jpg
@SpringBootApplication
@EnableAutoConfiguration
@RestController
public class FdfsApplication {
public static void main(String[] args) {
SpringApplication.run(FdfsApplication.class, args);
}
@RequestMapping(value = "upload")
public String upload() throws IOException, MyException, NoSuchAlgorithmException {
String conf = "src/main/resources/fdfs/client.conf";
//加载连接信息(即FastDFS中tracker服务器IP)
ClientGlobal.init(conf);
//创建TrackerClient对象\
TrackerClient trackerClient = new TrackerClient();
//获取TrackerServer对象
TrackerServer trackerServer = trackerClient.getConnection();
//创建StorageServer对象,引用为空
StorageServer storageServer = null;
//使用TrackerServer和StorageServer构造StorageClient对象
StorageClient storageClient = new StorageClient(trackerServer, storageServer);
//使用storageClient上传文件到服务器
String[] strings = storageClient.upload_file("E:/321.jpg", "jpg", null);
//上传成功会返回一个字符数组,分别为:文件所在组和文件在组中的位置及名称
String imageUrl = "";
for(String s:strings){
imageUrl = s;
// System.out.println(s);
}
long ts = System.currentTimeMillis() / 1000;
int a = (int)ts;
System.out.println(imageUrl);
String token = ProtoCommon.getToken(imageUrl, a, "FastDFS1234567890");
String url = "http://20.20.89.165:8888/group1/";
url = url + imageUrl + "?token=" + token + "&ts=" + a;
System.out.println(url);
// http://20.20.89.165:8888/group1/M00/00/00/FBRZpV6EJKeAJDJSAAITD_EyS8A526.jpg
return url;
}
8测试
请求
访问
过期
不带 token