Linux

Centos7.x 搭建FastDFS并通过Nginx配置htt

2021-03-19  本文已影响0人  dev_winner

FastDFS及其架构的简介:

Centos7.x 安装FastDFS 的步骤如下:

cd /usr/local
tar -zxvf libfastcommon-1.0.36.tar.gz
cd libfastcommon-1.0.36
./make.sh
./make.sh install
cd /usr/local
tar -zxvf fastdfs-5.11.tar.gz
cd fastdfs-5.11
./make.sh
./make.sh install
cd conf/
cp ./* /etc/fdfs/
port=22122
base_path=/opt/fastdfs #这里我就不区分tracker和storage,都将其存在同一目录下
http.server_port=443 #若不用https来访问,则忽略设置此参数
base_path=/opt/fastdfs #这里我就不区分tracker和storage,都将其存在同一目录下
store_path0=/opt/fastdfs #存储文件的绝对路径为:$store_path0/data/
tracker_server=服务器主机外网ip:22122 # 这里只用了1个tracker服务器
http.server_port=443 #若不用https来访问,则忽略设置此参数
/usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf start
/usr/bin/fdfs_storaged /etc/fdfs/storage.conf start
启动成功
tar -zxvf nginx-1.17.0.tar.gz
cd nginx-1.17.0
yum -y install pcre-devel
yum -y install openssl openssl-devel
./configure
make
make install
cd /usr/local
unzip fastdfs-nginx-module-master.zip
mv fastdfs-nginx-module-master fastdfs-nginx-module # 更改文件夹名称
cp /usr/local/fastdfs-nginx-module/src/mod_fastdfs.conf  /etc/fdfs/ # 将mod_fastdfs.conf配置文件拷贝到 /etc/fdfs/ 目录下
tracker_server=服务器外网ip:22122
url_have_group_name = true # url是否包含group名,默认要开启
store_path0=/opt/fastdfs #文件存储目录,默认和前面一样
./configure --add-module=/usr/local/fastdfs-nginx-module/src
make
make install
./configure --prefix=/usr/local/nginx/ --conf-path=/usr/local/nginx/conf/nginx.conf --with-http_ssl_module --add-module=/usr/local/fastdfs-nginx-module/src
make
# 若是从http版本过来的,不需要执行下面这一条安装命令,只需要生存新的nginx启动项即可
make install 
# 若是从http版本过来的,还需将新生存的nginx 拷贝到运行目录,否则不执行。
cp objs/nginx   /usr/local/nginx/sbin/nginx
校验成功 配置fastdfs文件请求转发 强制将http转发为https 简单配置ssl证书
<dependency>
  <groupId>net.oschina.zcx7878</groupId>
  <artifactId>fastdfs-client-java</artifactId>
  <version>1.27.0.0</version>
</dependency>
fastdfs.connect_timeout_in_seconds=5
fastdfs.network_timeout_in_seconds=30
fastdfs.charset=UTF-8
fastdfs.http_anti_steal_token=false #禁用防盗链,相关配置在 /etc/fdfs/http.conf 中
fastdfs.http_secret_key=FastDFS1234567890 # 默认和配置文件一样,若开启防盗链,则必须不外露这个密钥,最好设置成长度长且复杂一点
fastdfs.http_tracker_http_port=80
fastdfs.tracker_servers=服务器外网ip:22122
fastdfs.connection_pool.enabled=true
fastdfs.connection_pool.max_count_per_entry=500
fastdfs.connection_pool.max_idle_time=3600
fastdfs.connection_pool.max_wait_time_in_ms=1000
import org.csource.common.MyException;
import org.csource.fastdfs.*;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.time.Instant;
public class FastDFSUtil {
    private static StorageClient1 client1;
    static {
        try {
            ClientGlobal.initByProperties("fastdfs-client.properties");
            TrackerClient trackerClient = new TrackerClient();
            TrackerServer trackerServer = trackerClient.getConnection();
            client1 = new StorageClient1(trackerServer, null);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    /**
     * 上传文件
     */
    public static String uploadFile(MultipartFile file) throws IOException, MyException {
        String fileName = file.getOriginalFilename();
        //返回上传到服务器的路径
        return client1.upload_file1(file.getBytes(), fileName.substring(fileName.lastIndexOf(".") + 1), null);
    }

    /**
     * 下载文件
     */
    public static byte[] downloadFile(String fileId) throws IOException, MyException {
        return client1.download_file1(fileId);
    }
    //测试
    public static String uploadFile(String localFilePath) throws IOException, MyException {
        return client1.upload_file1(localFilePath, localFilePath.substring(localFilePath.lastIndexOf(".") + 1), null);
    }
    /**
     * 获取访问文件的令牌,有全局异常处理(开启防盗链的情况下)
     */
    public static String getToken(String fileId) throws UnsupportedEncodingException, NoSuchAlgorithmException, MyException {
        int ts = (int) Instant.now().getEpochSecond();
        String subStr = fileId.substring(7); // 注意,这个地址里边不包含 group,千万别搞错了
        String token = ProtoCommon.getToken(subStr, ts, "FastDFS1234567890"); // FastDFS1234567890 是前面配置的参数 fastdfs.http_secret_key 的值
        StringBuilder sb = new StringBuilder();
        String IP = "http(s)://xx.xx.xx.x/"; //服务器外网ip,括号中的 s 表示按照前面自己的配置来决定是http还是https访问
        sb.append(IP);
        sb.append(fileId);
        sb.append("?token=").append(token);
        sb.append("&ts=").append(ts);
        return sb.toString();
    }
}
//上传文件
@PostMapping("/uploadFile")
@ResponseBody
public R uploadFile(MultipartFile file) throws IOException, MyException {
    //根据扩展名来设置消息类型:emoji/text/img/file/sys/whiteboard/video/audio
    String filePartName = FastDFSUtil.uploadFile(file);
    String filePath = nginxHost + filePartName; // nginxHost 为服务器域名,注意跟前面设置http还是https访问保持一致
    System.out.println("在服务器的文件名为:" + filePartName);
    return R.ok().data("filePath", filePath);
}
//提供文件下载
@GetMapping("/downloadFile")
public void downloadFile(@RequestParam("fileId") String fileId,
                         @RequestParam("fileName") String fileName,
                         HttpServletResponse resp) {
    try {
        byte[] bytes = FastDFSUtil.downloadFile(fileId);
        resp.setCharacterEncoding("UTF-8");
        resp.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
        ServletOutputStream outputStream = resp.getOutputStream();
        IOUtils.write(bytes, outputStream);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
@Test
void testGetFileToken() throws UnsupportedEncodingException, NoSuchAlgorithmException, MyException {
   String fileId = "group1/M00/00/00/wKgxxxAgGBxxxxxsMM090.jpg";
   String fileUrl = FastDFSUtil.getToken(fileId);
   System.out.println(fileUrl);
}
上一篇下一篇

猜你喜欢

热点阅读