Spring-Boot项目

lyshop学习笔记四-FastDFS学习

2019-04-14  本文已影响7人  smallmartial

title: 乐优商城学习笔记四
date: 2019-04-14 13:29:48
tags:
- 乐优商城
- java
- springboot
- FastDFS
categories:
- 乐优商城


FastDFS学习

1.什么是分布式文件系统

分布式文件系统(Distributed File System)是指文件系统管理的物理存储资源不一定直接连接在本地节点上,而是通过计算机网络与节点相连。

通俗来讲:

2.什么是FastDFS

FastDFS是由淘宝的余庆先生所开发的一个轻量级、高性能的开源分布式文件系统。用纯C语言开发,功能丰富:

适合有大容量存储需求的应用或系统。同类的分布式文件系统有谷歌的GFS、HDFS(Hadoop)、TFS(淘宝)等。

2.1. Ubuntu下安装FastDFS

2.1.1上传

[站外图片上传中...(image-3ae886-1555222485663)]

2.1.2安装依赖

安装GCC依赖

GCC用来对C语言代码进行编译运行,使用yum命令安装:

sudo apt-get install gcc

安装unzip工具

unzip工具可以帮我们对压缩包进行解压

sudo apt-get install unzip zip

安装libevent

sudo apt-get install libevent

安装Nginx所需依赖

sudo apt-get install pcre pcre-devel zlib zlib-devel openssl openssl-devel

安装libfastcommon-master

这个没有包,只能通过编译安装:

2.1.3 安装FastDFS

这里我们也采用编译安装,步骤与刚才的编译安装方式一样:

1)安装完成,我们应该能在/etc/init.d/目录,通过命令ll /etc/init.d/ | grep fdfs看到FastDFS提供的启动脚本:
其中:

[站外图片上传中...(image-31df5e-1555222485663)]

其中:

2.2 启动tracker

FastDFS的tracker和storage在刚刚的安装过程中,都已经被安装了,因此我们安装这两种角色的方式是一样的。不同的是,两种需要不同的配置文件。

我们要启动tracker,就修改刚刚看到的tarcker.conf,并且启动fdfs_trackerd脚本即可。

首先我们将模板文件进行赋值和重命名:

sudo cp tracker.conf.sample tracker.conf
sudo vim tracker.conf

打开tracker.conf,修改base_path配置:

base_path=/leyou/fdfs/tracker # tracker的数据和日志存放目录

刚刚配置的目录可能不存在,我们创建出来

sudo mkdir -p /leyou/fdfs/tracker
sudo service fdfs_trackerd start # 启动fdfs_trackerd服务,停止用stop

2.3 启动storage

我们要启动tracker,就修改刚刚看到的tarcker.conf,并且启动fdfs_trackerd脚本即可。

首先我们将模板文件进行赋值和重命名:

sudo cp storage.conf.sample storage.conf
sudo vim storage.conf

打开storage.conf,修改base_path配置:

base_path=/leyou/fdfs/storage # storage的数据和日志存放目录
store_path0=/leyou/fdfs/storage # storage的上传文件存放路径
tracker_server=192.168.56.101:22122 # tracker的地址

刚刚配置的目录可能不存在,我们创建出来

sudo mkdir -p /leyou/fdfs/storage
sudo service fdfs_storaged start  # 启动fdfs_storaged服务,停止用stop

最后,通过ps -ef | grep fdfs 查看进程:

[站外图片上传中...(image-e3b0a9-1555222485663)]

2.4 安装Nginx及FastDFS模块

2.4.1 FastDFS的Nginx模块

1.4.2 安装Nginx

3.引入依赖

在父工程中,我们已经管理了依赖,版本为:

<fastDFS.client.version>1.26.2</fastDFS.client.version>

因此,这里我们直接引入坐标即可:

<dependency>
    <groupId>com.github.tobato</groupId>
    <artifactId>fastdfs-client</artifactId>
</dependency>

3.2.引入配置类

纯java配置:

@Configuration
@Import(FdfsClientConfig.class)
// 解决jmx重复注册bean的问题
@EnableMBeanExport(registration = RegistrationPolicy.IGNORE_EXISTING)
public class FastClientImporter {
}

3.3.编写FastDFS属性

fdfs:
  so-timeout: 1501
  connect-timeout: 601
  thumb-image: # 缩略图
    width: 60
    height: 60
  tracker-list: # tracker地址
    - 192.168.56.101:22122

3.4.测试

@RunWith(SpringRunner.class)
@SpringBootTest(classes = LyUploadService.class)
public class FdfsTest {

    @Autowired
    private FastFileStorageClient storageClient;

    @Autowired
    private ThumbImageConfig thumbImageConfig;

    @Test
    public void testUpload() throws FileNotFoundException {
        File file = new File("D:\\test\\baby.png");
        // 上传并且生成缩略图
        StorePath storePath = this.storageClient.uploadFile(
                new FileInputStream(file), file.length(), "png", null);
        // 带分组的路径
        System.out.println(storePath.getFullPath());
        // 不带分组的路径
        System.out.println(storePath.getPath());
    }

    @Test
    public void testUploadAndCreateThumb() throws FileNotFoundException {
        File file = new File("D:\\test\\baby.png");
        // 上传并且生成缩略图
        StorePath storePath = this.storageClient.uploadImageAndCrtThumbImage(
                new FileInputStream(file), file.length(), "png", null);
        // 带分组的路径
        System.out.println(storePath.getFullPath());
        // 不带分组的路径
        System.out.println(storePath.getPath());
        // 获取缩略图路径
        String path = thumbImageConfig.getThumbImagePath(storePath.getPath());
        System.out.println(path);
    }
}

结果:

group1/M00/00/00/CoeGQ1yyzaqAE_U5AAAgsUsN-Oo524.jpg
M00/00/00/CoeGQ1yyzaqAE_U5AAAgsUsN-Oo524.jpg
M00/00/00/CoeGQ1yyzaqAE_U5AAAgsUsN-Oo524_60x60.jpg

[站外图片上传中...(image-fd25c6-1555222485663)]

上一篇 下一篇

猜你喜欢

热点阅读