目錄
- 1minio簡(jiǎn)潔
- 2 docker搭建minio
- 2.1 單節(jié)點(diǎn)
- 2.2 多節(jié)點(diǎn)部署
- 3 java sdk使用minio
1minio簡(jiǎn)潔
MinIO 是一款高性能、分布式的對(duì)象存儲(chǔ)系統(tǒng). 它是一款軟件產(chǎn)品, 可以100%的運(yùn)行在標(biāo)準(zhǔn)硬件。即X86等低成本機(jī)器也能夠很好的運(yùn)行MinIO。
MinIO與傳統(tǒng)的存儲(chǔ)和其他的對(duì)象存儲(chǔ)不同的是:它一開(kāi)始就針對(duì)性能要求更高的私有云標(biāo)準(zhǔn)進(jìn)行軟件架構(gòu)設(shè)計(jì)。因?yàn)镸inIO一開(kāi)始就只為對(duì)象存儲(chǔ)而設(shè)計(jì)。所以他采用了更易用的方式進(jìn)行設(shè)計(jì),它能實(shí)現(xiàn)對(duì)象存儲(chǔ)所需要的全部功能,在性能上也更加強(qiáng)勁,它不會(huì)為了更多的業(yè)務(wù)功能而妥協(xié),失去MinIO的易用性、高效性。 這樣的結(jié)果所帶來(lái)的好處是:它能夠更簡(jiǎn)單的實(shí)現(xiàn)局有彈性伸縮能力的原生對(duì)象存儲(chǔ)服務(wù)。
MinIO在傳統(tǒng)對(duì)象存儲(chǔ)用例(例如輔助存儲(chǔ),災(zāi)難恢復(fù)和歸檔)方面表現(xiàn)出色。同時(shí),它在機(jī)器學(xué)習(xí)、大數(shù)據(jù)、私有云、混合云等方面的存儲(chǔ)技術(shù)上也獨(dú)樹(shù)一幟。當(dāng)然,也不排除數(shù)據(jù)分析、高性能應(yīng)用負(fù)載、原生云的支持。
minio社區(qū)版本開(kāi)源免費(fèi),在沒(méi)有預(yù)算使用oss的時(shí)候可以考慮使用。
2 docker搭建minio
minio是支持云原生的,所以直接講使用docker來(lái)搭建,當(dāng)然也可以使用k8s,直接下載官方的chart使用即可。
2.1 單節(jié)點(diǎn)
單節(jié)點(diǎn)可以直接使用docker run啟動(dòng)即可
docker run \ -p 9000:9000 \ -p 9001:9001 \ minio/minio server /data --console-address ":9001"
也可以使用docker-compose來(lái)運(yùn)行。
編寫(xiě)docker-compose.yaml
version: '3' services: minio: image: minio/minio hostname: "minio" ports: - 9000:9000 - 9001:9001 environment: MINIO_ACCESS_KEY: admin #控制臺(tái)登錄賬號(hào) MINIO_SECRET_KEY: 12345678 #控制臺(tái)登錄密碼 volumes: - ./data:/data #存儲(chǔ)路徑 - ./config:/root/.minio/ #配置文件 command: server --console-address ':9001' /data privileged: true restart: always
創(chuàng)建掛載的文件目錄,運(yùn)行docker-compos啟動(dòng)。
docker-compser up -d
輸入ip:9001 輸入admin/12345678進(jìn)入控制臺(tái)
控制臺(tái):
創(chuàng)建bucket,就可以上傳文件了。
輸入名稱(chēng)保存。
可以配置,相關(guān)策略,這里就不說(shuō)明了。
可以上傳下載操對(duì)象文件。
2.2 多節(jié)點(diǎn)部署
多節(jié)點(diǎn)部署使用docker-compse來(lái)模擬。創(chuàng)建4個(gè)節(jié)點(diǎn),每個(gè)節(jié)點(diǎn)掛載兩份數(shù)據(jù)。
編寫(xiě)docker-compose.yaml
version: '3' # starts 4 docker containers running minio server instances. # using nginx reverse proxy, load balancing, you can access # it through port 9000. services: minio1: image: minio/minio hostname: minio1 volumes: - ./data1-1:/data1 - ./data1-2:/data2 expose: - "9000" - "9001" environment: MINIO_ROOT_USER: minio MINIO_ROOT_PASSWORD: minio123 command: server --console-address ":9001" http://minio{1...4}/data{1...2} healthcheck: test: ["CMD", "curl", "-f", "http://localhost:9000/minio/health/live"] interval: 30s timeout: 20s retries: 3 minio2: image: minio/minio hostname: minio2 volumes: - ./data2-1:/data1 - ./data2-2:/data2 expose: - "9000" - "9001" environment: MINIO_ROOT_USER: minio MINIO_ROOT_PASSWORD: minio123 command: server --console-address ":9001" http://minio{1...4}/data{1...2} healthcheck: test: ["CMD", "curl", "-f", "http://localhost:9000/minio/health/live"] interval: 30s timeout: 20s retries: 3 minio3: image: minio/minio hostname: minio3 volumes: - ./data3-1:/data1 - ./data3-2:/data2 expose: - "9000" - "9001" environment: MINIO_ROOT_USER: minio MINIO_ROOT_PASSWORD: minio123 command: server --console-address ":9001" http://minio{1...4}/data{1...2} healthcheck: test: ["CMD", "curl", "-f", "http://localhost:9000/minio/health/live"] interval: 30s timeout: 20s retries: 3 minio4: image: minio/minio hostname: minio4 volumes: - ./data4-1:/data1 - ./data4-2:/data2 expose: - "9000" - "9001" environment: MINIO_ROOT_USER: minio MINIO_ROOT_PASSWORD: minio123 command: server --console-address ":9001" http://minio{1...4}/data{1...2} healthcheck: test: ["CMD", "curl", "-f", "http://localhost:9000/minio/health/live"] interval: 30s timeout: 20s retries: 3 nginx: image: nginx:1.19.2-alpine hostname: nginx volumes: - ./nginx.conf:/etc/nginx/nginx.conf:ro ports: - "9000:9000" - "9001:9001" depends_on: - minio1 - minio2 - minio3 - minio4
創(chuàng)建掛載的對(duì)應(yīng)的data目錄和nginx目錄。
使用nginx負(fù)載均衡4個(gè)節(jié)點(diǎn),創(chuàng)建nginx.conf。
user nginx; worker_processes auto; error_log /var/log/nginx/error.log warn; pid /var/run/nginx.pid; events { worker_connections 4096; } http { include /etc/nginx/mime.types; default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log /var/log/nginx/access.log main; sendfile on; keepalive_timeout 65; # include /etc/nginx/conf.d/*.conf; upstream minio { server minio1:9000; server minio2:9000; server minio3:9000; server minio4:9000; } upstream console { ip_hash; server minio1:9001; server minio2:9001; server minio3:9001; server minio4:9001; } server { listen 9000; listen [::]:9000; server_name localhost; # To allow special characters in headers ignore_invalid_headers off; # Allow any size file to be uploaded. # Set to a value such as 1000m; to restrict file size to a specific value client_max_body_size 0; # To disable buffering proxy_buffering off; location / { proxy_set_header Host $http_host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; proxy_connect_timeout 300; # Default is HTTP/1, keepalive is only enabled in HTTP/1.1 proxy_http_version 1.1; proxy_set_header Connection ""; chunked_transfer_encoding off; proxy_pass http://minio; } } server { listen 9001; listen [::]:9001; server_name localhost; # To allow special characters in headers ignore_invalid_headers off; # Allow any size file to be uploaded. # Set to a value such as 1000m; to restrict file size to a specific value client_max_body_size 0; # To disable buffering proxy_buffering off; location / { proxy_set_header Host $http_host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; proxy_set_header X-NginX-Proxy true; # This is necessary to pass the correct IP to be hashed real_ip_header X-Real-IP; proxy_connect_timeout 300; # To support websocket proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; chunked_transfer_encoding off; proxy_pass http://console; } } }
運(yùn)行。
docker-compser up -d
然后進(jìn)入控制臺(tái),操作和單節(jié)點(diǎn)一樣。
3 java sdk使用minio
sdk使用minio要先獲取AccessKey和SecretKey。
在控制臺(tái)生成。
項(xiàng)目pom文件引入。
<dependency> <groupId>io.minio</groupId> <artifactId>minio</artifactId> <version>8.3.0</version> </dependency>
編寫(xiě)上傳、下載、刪除的接口。
package com.test.minio; import javax.servlet.http.HttpServletResponse; import java.io.InputStream; /** * 存儲(chǔ)文件 * * @author jiangyulu */ public interface FileService { /** * 上傳文件 * * @param inputStream inputStream * @param fdsFileName fdsFileName * @param img img * @return UUID */ String upload(InputStream inputStream, String fdsFileName, boolean img); /** * 下載文件 * * @param fdsFileName 文件在fds中的名稱(chēng) * @param fileName 重新指定的文件名 * @param response response */ void download(String fdsFileName, String fileName, HttpServletResponse response); /** * 刪除 * * @param fdsFileName fdsFileName */ void delete(String fdsFileName); }
寫(xiě)實(shí)現(xiàn)類(lèi)。
package com.test.minio.impl; import com.test.minio.FileService; import io.minio.*; import lombok.extern.slf4j.Slf4j; import org.apache.commons.io.IOUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.InputStream; import java.net.URLEncoder; import java.util.UUID; /** * @author jaingyulu */ @Slf4j @Service("minio") public class MinioFileServiceImpl implements FileService { @Value("{$minio.endpoint}") private String endpoint; @Value("{$minio.accessKeyId}") private String accessKeyId; @Value("{$minio.accessKeySecret}") private String accessKeySecret; @Value("{$minio.bucketName}") private String bucketName; @Override public String upload(InputStream inputStream, String fdsFileName, boolean img) { try { MinioClient minioClient = MinioClient.builder() .endpoint(endpoint) .credentials(accessKeyId, accessKeySecret) .build(); boolean found = minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build()); if (found) { log.info("Bucket already exists."); } else { minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build()); } if (!img) { minioClient.putObject( PutObjectArgs.builder() .bucket(bucketName) .object(fdsFileName) .stream(inputStream, inputStream.available(), -1) .build()); } else { minioClient.putObject( PutObjectArgs.builder() .bucket(bucketName) .object(fdsFileName) .stream(inputStream, inputStream.available(), -1) .contentType("image/jpg") .build()); } inputStream.close(); } catch (Exception e) { e.printStackTrace(); } return UUID.randomUUID().toString(); } @Override public void download(String fdsFileName, String fileName, HttpServletResponse response) { InputStream in = null; try { MinioClient minioClient = MinioClient.builder() .endpoint(endpoint) .credentials(accessKeyId, accessKeySecret) .build(); StatObjectResponse objectStat = minioClient.statObject(StatObjectArgs.builder().bucket(bucketName).object(fdsFileName).build()); response.setContentType(objectStat.contentType()); //response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE); response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8")); in = minioClient.getObject(GetObjectArgs.builder().bucket(bucketName).object(fdsFileName).build()); IOUtils.copy(in, response.getOutputStream()); } catch (Exception e) { log.error(e.getMessage()); } finally { if (in != null) { try { in.close(); } catch (IOException e) { log.error(e.getMessage()); } } } } @Override public void delete(String fdsFileName) { try { MinioClient minioClient = MinioClient.builder() .endpoint(endpoint) .credentials(accessKeyId, accessKeySecret) .build(); minioClient.removeObject(RemoveObjectArgs.builder().bucket(bucketName).object(fdsFileName).build()); } catch (Exception e) { e.printStackTrace(); } } }
以上完成了minio文件操作的基本功能,其他功能可以查看官方的文檔。8.3.0版本sdk比起7.x的變化還是比較大的。