1.编译环境
yum install git gcc gcc-c++ make automake autoconf libtool pcre pcre-devel zlib zlib-devel openssl-devel wget vim -y
2.创建磁盘目录
a. 创建数据存储目录
mkdir /home/dfs
b.切换到安装目录准备下载安装包
cd /usr/local/src
3.安装libfatscommon
a.下载git安装包
git clone https://github.com/happyfish100/libfastcommon.git --depth 1
b.编译安装
cd libfastcommon/
./make.sh && ./make.sh install
4.安装FastDFS
a.返回上一级目录
cd ../
b.下载git安装包
git clone https://github.com/happyfish100/fastdfs.git --depth 1
c.进入目录
cd fastdfs/
d.编译安装
./make.sh && ./make.sh install
e.配置文件准备
cp /etc/fdfs/tracker.conf.sample /etc/fdfs/tracker.conf
cp /etc/fdfs/storage.conf.sample /etc/fdfs/storage.conf
cp /etc/fdfs/client.conf.sample /etc/fdfs/client.conf
cp /usr/local/src/fastdfs/conf/http.conf /etc/fdfs/
cp /usr/local/src/fastdfs/conf/mime.types /etc/fdfs/
5.安装fastdfs-nginx-module
a.返回上一级目录
cd ../
b.下载git安装包
git clone https://github.com/happyfish100/fastdfs-nginx-module.git --depth 1
c.复制配置文件
cp /usr/local/src/fastdfs-nginx-module/src/mod_fastdfs.conf /etc/fdfs
6.安装nginx
a.下载nginx压缩包
wget http://nginx.org/download/nginx-1.15.4.tar.gz
b.解压
tar -zxvf nginx-1.15.4.tar.gz
c.进入nginx目录
cd nginx-1.15.4/
d.添加fastdfs-nginx-module模块到nginx
./configure --add-module=/usr/local/src/fastdfs-nginx-module/src/
e.编译安装
make && make install
7.修改配置
a.tracker配置
vim /etc/fdfs/tracker.conf
#需要修改的内容如下
port=22122 # tracker服务器端口(默认22122,一般不修改)
base_path=/home/dfs # 存储日志和数据的根目录
b.storage配置
vim /etc/fdfs/storage.conf
#需要修改的内容如下
port=23000 # storage服务端口(默认23000,一般不修改)
base_path=/home/dfs # 数据和日志文件存储根目录
store_path0=/home/dfs # 第一个存储目录
tracker_server=192.168.52.1:22122 # tracker服务器IP和端口
http.server_port=8888 # http访问文件的端口(默认8888,看情况修改,和nginx中保持一致)
c.client测试
vim /etc/fdfs/client.conf
#需要修改的内容如下
base_path=/home/dfs
tracker_server=192.168.52.1:22122 #tracker服务器IP和端口
#保存后测试,返回ID表示成功 如:group1/M00/00/00/xx.tar.gz
fdfs_upload_file /etc/fdfs/client.conf /usr/local/src/nginx-1.15.4.tar.gz
d.配置nginx访问
vim /etc/fdfs/mod_fastdfs.conf
#需要修改的内容如下
tracker_server=192.168.52.1:22122 #tracker服务器IP和端口
url_have_group_name=true
store_path0=/home/dfs
vim /usr/local/nginx/conf/nginx.conf
#添加如下配置
server {
listen 8888; ## 该端口为storage.conf中的http.server_port相同
server_name localhost;
location ~/group[0-9]/ {
ngx_fastdfs_module;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
8.启动命令
a.tracker
/etc/init.d/fdfs_trackerd start #启动tracker服务
/etc/init.d/fdfs_trackerd restart #重启动tracker服务
/etc/init.d/fdfs_trackerd stop #停止tracker服务
chkconfig fdfs_trackerd on #自启动tracker服务
b.storage
/etc/init.d/fdfs_storaged start #启动storage服务
/etc/init.d/fdfs_storaged restart #重动storage服务
/etc/init.d/fdfs_storaged stop #停止动storage服务
chkconfig fdfs_storaged on #自启动storage服务
c.nginx
/usr/local/nginx/sbin/nginx #启动nginx
/usr/local/nginx/sbin/nginx -s reload #重启nginx
/usr/local/nginx/sbin/nginx -s stop #停止nginx
检查集群状态
/usr/bin/fdfs_monitor /etc/fdfs/storage.conf
# 会显示会有几台服务器 有3台就会 显示 Storage 1-Storage 3的详细信息
9.建立Springboot项目访问测试
a.引入swagger和fdfs依赖
<!-- FastDFS依赖 -->
<dependency>
<groupId>com.github.tobato</groupId>
<artifactId>fastdfs-client</artifactId>
<version>1.26.5</version>
</dependency>
<!-- swagger用于定义API文档 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
<!--美化swagger-->
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>swagger-bootstrap-ui</artifactId>
<version>1.9.3</version>
</dependency>
b.编写swagger和fdfs配置类
package com.xiaokai.demo.springboot.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
/**
* SwaggerConfig
* Description: <br/>
* date: 2020/5/3 18:54<br/>
*
* @author lvxk<br />
* @since JDK 1.8
*/
@Configuration
public class SwaggerConfig {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.xiaokai.demo.springboot.controller")) //此次每次使用须换成自己的web接口的全限定类名
//.paths(AppUtility.isProd() ? PathSelectors.none() : PathSelectors.any())
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("FastDFS - 文件测试接口")
.description("FastDFS - 文件测试接口")
.termsOfServiceUrl("http://localhost:8071/swagger-ui.html")
.contact(new Contact("xiaokai.lv", "http://localhost:8071/swagger-ui.html", "lvxiaokai@aliyun.com"))
.version("1.0")
.build();
}
}
fdfs配置
package com.xiaokai.demo.springboot.config;
import com.github.tobato.fastdfs.FdfsClientConfig;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableMBeanExport;
import org.springframework.context.annotation.Import;
import org.springframework.jmx.support.RegistrationPolicy;
/**
* DfsConfig
* Description: <br/>
* date: 2020/5/3 18:51<br/>
*
* @author lvxk<br />
* @since JDK 1.8
*/
@Configuration
@Import(FdfsClientConfig.class)
// Jmx重复注册bean的问题
@EnableMBeanExport(registration = RegistrationPolicy.IGNORE_EXISTING)
public class DfsConfig {
}
建立测试Controller
package com.xiaokai.demo.springboot.controller;
import com.xiaokai.demo.springboot.util.FileDfsUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
/**
* 一些声明信息
* Description: <br/>
* date: 2020/5/3 18:56<br/>
*
* @author lvxk<br />
* @since JDK 1.8
*/
@RestController
@Api(tags = "FastDFS文件测试")
public class FileController {
@Resource
private FileDfsUtil fileDfsUtil ;
/**
* 文件上传
*/
@ApiOperation(value="上传文件", notes="测试FastDFS文件上传")
@RequestMapping(value = "/uploadFile",headers="content-type=multipart/form-data", method = RequestMethod.POST)
public ResponseEntity<String> uploadFile (@RequestParam("file") MultipartFile file){
String result ;
try{
String path = fileDfsUtil.upload(file) ;
if (!StringUtils.isEmpty(path)){
result = path ;
} else {
result = "上传失败" ;
}
} catch (Exception e){
e.printStackTrace() ;
result = "服务异常" ;
}
return ResponseEntity.ok(result);
}
/**
* 文件删除
*/
@RequestMapping(value = "/deleteByPath", method = RequestMethod.GET)
public ResponseEntity<String> deleteByPath (){
String filePathName = "group1/M00/00/00/wKhIgl0n4AKABxQEABhlMYw_3Lo825.png" ;
fileDfsUtil.deleteFile(filePathName);
return ResponseEntity.ok("SUCCESS") ;
}
}
c.打开swagger页面测试上传接口选择图片并上传,成功会返回url
image.png
d.浏览器输入服务器ip:8888/url地址即可
image.png
auto:
mapping:
url: '/hello #自定义路由规则'
nacos:
name: lvxiaokai6-27
server:
port: 8887
spring:
application:
name: demo-springboot-lvxiaokai
servlet:
multipart:
enabled: true
max-file-size: 10MB
max-request-size: 20MB
fdfs:
# 链接超时
connect-timeout: 600
# 读取时间
so-timeout: 600
# 生成缩略图参数
thumb-image:
width: 150
height: 150
tracker-list: 192.168.79.136:22122
网友评论