搭建自用单点OOS服务

Posted dingwen_blog

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了搭建自用单点OOS服务相关的知识,希望对你有一定的参考价值。

前言

本篇文章记录了搭建自用单点OOS服务的过程,避坑记录等。采用SpingBoot自定义starter形式,构建自己的文件存储服务。主要涉及到的技术有:SpringBoot、Docker 、MinIo。

一、安装MinIo

详细见我的另外一片文章。https://blog.csdn.net/qq_38020915/article/details/117386512

二、自定义starter

2.1 starter命名规范

  • Spring 官方命名规则
    • 前缀:spring-boot-starter-{name}
    • 模式:spring-boot-starter-模块名
      • 举例:spring-boot-starter-web、spring-boot-starter-thymeleaf
  • 自定义命名规则
  • 后缀:{name}-spring-boot-starter
  • 模式:模块-spring-boot-starter
    • 举例:mybatis-spring-boot-starter

2.2 创建maven工程

2.1.1 项目目录结构

在这里插入图片描述

2.1.2 依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.0</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.dingwen</groupId>
    <artifactId>dingwen-minio-starter</artifactId>
    <version>1.0-SNAPSHOT</version>
    <name>dingwen-minio-starter</name>
    <description>dingwen-minio-starter</description>
    <properties>
        <java.version>1.8</java.version>
        <minio.version>7.0.2</minio.version>
        <commons.io.version>2.6</commons.io.version>
        <spring.boot.version>2.4.1</spring.boot.version>
    </properties>
    <dependencies>

        <!--spring boot-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
           <version>${spring.boot.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>

        <!-- minio client -->
        <dependency>
            <groupId>io.minio</groupId>
            <artifactId>minio</artifactId>
            <version>${minio.version}</version>
        </dependency>

        <!--xml、properties配置加载-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>

        <!--autoconfigure-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-autoconfigure</artifactId>
        </dependency>

        <!--commons-io-->
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>${commons.io.version}</version>
        </dependency>
        <!--lombok-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-javadoc-plugin</artifactId>
                <version>3.0.1</version>
                <configuration>
                    <encoding>UTF-8</encoding>
                    <charset>UTF-8</charset>
                    <docencoding>UTF-8</docencoding>
                    <doclint>none</doclint>
                </configuration>
                <executions>
                    <execution>
                        <id>attach-javadocs</id>
                        <phase>package</phase>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-source-plugin</artifactId>
                <version>3.0.0</version>
                <configuration>
                    <attach>true</attach>
                </configuration>
                <executions>
                    <execution>
                        <phase>compile</phase>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

注意:引入spring-boot-autoconfigure与spring-boot-autoconfigure-processor(xml、properties)来加载配置

2.3 编写配置文件

2.2.1 启动配置文件映射实体

package com.dingwen.minsta.config;

import lombok.Getter;
import lombok.Setter;
import org.springframework.boot.context.properties.ConfigurationProperties;

import java.io.File;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * minio 配置类
 *
 * @author dingwen
 * 2021.05.25 17:05
 */
@Getter
@Setter
@ConfigurationProperties(prefix = "dingwen.minio")
public class DingwenMiniostarterProperties {
    /**
     * 一天的毫秒数
     */
    private static final Long DAY_MILLISECOND = 1000L * 60 * 60 * 24;

    /**
     * 临时缓存目录
     */
    private static final String TEMP_DIR = "." + File.separator + "temp" + File.separator;

    /**
     * 访问minio服务URL
     */
    private String endPoint;
    /**
     * 存储桶
     */
    private String bucket;

    /**
     * 访问密钥
     */
    private String accessKey;

    /**
     * 秘密密钥
     */
    private String secretKey;

    /**
     * 临时dir
     */
    private String tempDir = TEMP_DIR;


    /**
     * 是否进行缓存清理
     */
    private boolean tempCleanFlag = true;


    /**
     * 第一次临时清理时间
     */
    private Date tempCleanFirstTime;


    /**
     * 临时清洁周期(延时时间)
     */
    private Long tempCleanPeriod;

    /**
     * 临时清洁时间(间隔时间)
     */
    private Long tempCleanDuration;

    public void setTempCleanFirstTime(String tempCleanFirstTime) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        try {
            this.tempCleanFirstTime = format.parse(tempCleanFirstTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }
}

2.2.1 配置类

package com.dingwen.minsta.config;


import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;

/**
 * 自动配置类
 *
 * @author dingwen
 * 2021.05.25 17:03
 */
@Configuration
@EnableConfigurationProperties(DingwenMinioStarterProperties.class)
public class DingwenMinioStarterAutoConfiguration {

}

@Configuration: 告诉Spring该类是配置类 @EnableConfigurationProperties(DingwenMinioStarterProperties.class)
:配置映射

2.4 编写 spring.factories

由于starter工程和service工程包路径不一致了,是两个工程@Configuration注解标注的类扫描不到无法加入IOC容器,需要在此处配置。
SpringBoot 启动类默认的@SpringBootApplication注解中默认包含了@EnableAutoConfiguration注解,启动时就能扫描本项目下被@Configuration注解修饰的类并将其注入IOC容器。而对于引入的starter项目来说,@Configuration不能在本项目中扫描到,是无法加载的。所以需要启动时读取/META-INF/spring.factories文件,然后读取org.springframework.boot.autoconfigure.EnableAutoConfiguration指向的由@Configuration注解修饰的类并将其注入IOC容器。

在这里插入图片描述

org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.dingwen.minsta.config.DingwenMinioStarterAutoConfiguration

2.5 完成业务逻辑工程

  • 根据配置定时清除上传临时目录
  • 创建bucket
  • 删除bucket
  • 上传文件
  • 下载文件

2.6 自定义banner

2.6.1 生成

到工具网站生成banner新建txt文件。http://patorjk.com/software/taag/#p=display&f=Graffiti&t=Type%20Something%20

在这里插入图片描述

2.6.2 配置

存放到该目录下即可,注意文件名称。
在这里插入图片描述

 ${AnsiColor.BRIGHT_CYAN} # 指定颜色
 ${application.version})  # 获取版本信息
 ${spring-boot.version} # 获取版本信息

2.7 打包部署到本地仓库

mvn clean install

2.8 完整代码地址

https://gitee.com/dingwen-gitee/dingwen-minio-starter

三、开发service

3.1 创建SpringBoot项目

3.1.1 项目结构概览

在这里插入图片描述

3.1.2 依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.0</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.dingwen</groupId>
    <artifactId>dingwen-minio-service</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <name>dingwen-minio-service</name>
    <description>dingwen-minio-service</description>
    <properties>
        <java.version>1.8</java.version>
        <fastjson.version>1.2.75</fastjson.version>
    </properties>
    <dependencies>
        <!--boot-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <!--dingwen-minio-starter-->
        <dependency>
            <groupId>com.dingwen</groupId>
            <artifactId>dingwen-minio-starter</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <!--web-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--test-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!--lombok-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>

        <!--fast json-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>${fastjson.version}</version>
        </dependency>

        <!--接口文档-->
        <dependency>
            <groupId>com.github.xiaoymin</groupId>
            <artifactId>knife4j-spring-boot-starter</artifactId>
            <version>2.0.7</version>
        </dependency>

    </dependencies>


    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

3.2配置包扫描

package com.dingwen.minser;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication(scanBasePackages = "com.dingwen")
public class DingwenMinioServiceApplication {

    public static void main(String[] args) {
        SpringApplicati

以上是关于搭建自用单点OOS服务的主要内容,如果未能解决你的问题,请参考以下文章

SpringBoot + Layui + Mysql + Flutter (PC + 移动端)实现大学课程平时成绩管理

Kafka 集群搭建 (自用)

CAS搭建单点登录Web端

opencv环境搭建自用

CAS5.2x单点登录——搭建cas服务器

cas单点登录服务端客户端搭建