minio分布式存储部署和基于SpringBoot的使用

390 阅读3分钟

摘要

本文主要介绍了minio的部署方式和java客户端的基本使用

minio docker部署

docker安装教程参考该文章https://juejin.cn/post/6968271644372500493

单节点单数据集副本部署方式

  • 创建docker-compose.yml

networks需要缓存自己创建的网段,9000端口是默认的服务端端口,后面的9001是web管理端口

version: '3'
services:
  minio:
    image: minio/minio:latest
    container_name: minio
    networks:
      - default
    privileged: true
    volumes:
      - ./data:/data
    environment:
      - "MINIO_ROOT_USER=minio"
      - "MINIO_ROOT_PASSWORD=minio@123"
    command: server /data --console-address ":9001"
    ports:
      - 9000:9000
      - 9001:9001
networks:
  default:
    external:
      name: huzhihui
  • 运行 docker-compose up -d
  • 查看运行情况 image.png
  • 使用

我本机的ip是http://192.168.137.180:9001/login 你们换成你们自己的ip和端口,用户名和密码在环境变量中配置的

image.png

  • 创建桶

我先创建了一个home的桶 image.png

  • 设置桶的访问权限

如果想全部公开,直接在Summary页面设置Access Policy:pulic,否则则可以对指定前缀设置权限,分级拦截 image.png

  • 文件的访问路径

文件的访问路径是http://ip:9000/桶名称/文件路径名 比如 http://192.168.137.180:9000/home/head.jpg

  • 该方式的一个优点

如果已经有了很多文件,则不用手动上传,自己把文件复制到该挂载的文件夹里面去就可以,它会自动解析出来,文件的访问地址实际上和层级结构完全一致

image.png

单节点多数据集副本部署方式

  • 创建docker-compose.yml

networks需要缓存自己创建的网段,这里需要注意的是,如果是多数据集则至少需要4个副本才行,推荐挂载在不同的硬盘上,这样其中的一个盘坏掉了,不会丢失数据,如果公司做了raid5磁盘阵列,则单副本的也可以,本文就是在一个磁盘模拟了多副本情况

version: '3'
services:
  minio2:
    image: minio/minio:latest
    container_name: minio2
    networks:
      - default
    privileged: true
    volumes:
      - ./data1:/data1
      - ./data2:/data2
      - ./data3:/data3
      - ./data4:/data4
    environment:
      - "MINIO_ROOT_USER=minio"
      - "MINIO_ROOT_PASSWORD=minio@minio"
    command: server /data{1...4} --console-address ":9001"
    ports:
      - 9000:9000
      - 9001:9001
networks:
  default:
    external:
      name: huzhihui
  • 运行 docker-compose up -d
  • 查看运行情况

image.png

  • 使用

使用方式和单节点单数据集的完全一致,只是它文件存储不一样,他是以数据校验码存储的,客户端调用需要创建AK SK

image.png

image.png

SpringBoot使用案例

  • 创建配置类
@Data
@ConfigurationProperties(value = "spring.minio")
public class MinIoProperties {

    /** 服务地址 */
    private String endpoint;
    /** 存放桶地址 */
    private String bucketName;
    /** 访问key */
    private String accessKey;
    /** 访问密码 */
    private String secretKey;
}
  • 配置Client
@Configuration
@EnableConfigurationProperties(value = {MinIoProperties.class})
public class MinIoConfig {

    @Bean
    public MinioClient minIoClient(MinIoProperties minIoProperties){
        return MinioClient.builder()
                .endpoint(minIoProperties.getEndpoint())
                .credentials(minIoProperties.getAccessKey(), minIoProperties.getSecretKey())
                .build();
    }


}
  • 构建一个简易Service
@Service
public class MinIoService {

    @Autowired
    private MinIoProperties minIoProperties;
    @Autowired
    private MinioClient minioClient;

    /**
     * 上传文件
     * @param bucketName
     * @param file
     * @param objectName
     * @param contentType
     * @return
     * @throws Exception
     */
    public String upload(String bucketName, MultipartFile file, String objectName, String contentType) throws Exception {
        InputStream inputStream = file.getInputStream();
        minioClient.putObject(
                PutObjectArgs.builder()
                        .bucket(bucketName)
                        .object(objectName)
                        .contentType(contentType)
                        .stream(inputStream, inputStream.available(), -1)
                        .build());

        return minIoProperties.getEndpoint() + "/" + bucketName + "/" + objectName;
    }

    /**
     * 下载文件
     * @param bucketName
     * @param objectName
     * @return
     * @throws Exception
     */
    public InputStream download(String bucketName,String objectName) throws Exception{
        return minioClient.getObject(GetObjectArgs.builder().bucket(bucketName).object(objectName).build());
    }

}
  • Controller调用案例
@RestController
@RequestMapping(value = "file")
public class FileController {

    @Autowired
    private MinIoService minIoService;

    @CrossOrigin
    @RequestMapping(value = "upload")
    public ResponseMessage<String> upload(@RequestParam("file") MultipartFile file) throws Exception{
        String result = minIoService.upload("home",file, "a/" + UUID.randomUUID().toString()+file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".")),file.getContentType());
        return ResponseMessage.success(result);
    }

    @CrossOrigin
    @GetMapping(value = "download")
    public void download(HttpServletResponse response,String fileName) throws Exception{
        response.setCharacterEncoding("utf-8");
        response.setContentType("multipart/form-data");
        response.setHeader("Content-Disposition", "attachment;fileName="+fileName);
        ServletOutputStream out = response.getOutputStream();
        InputStream input = minIoService.download("home",fileName);
        IOUtils.copy(input, out);
    }
}
  • application.yml
spring:

  minio:
    endpoint: http://192.168.137.180:9000
    accessKey: xxxx
    secretKey: xxxx
    bucketName: home