Spring Boot 3 + MinIO集群 + Nginx 负载均衡 实现图片(头像)的上传 + 更新替换 + 下载 简单案例
以下是一个简化的解决方案,展示了如何在Spring Boot 3应用中使用MinIO集群和Nginx进行图片上传、更新和替换的基本代码示例:
Spring Boot 3 + MinIO集群配置:
@Configuration
public class MinioConfig {
@Bean
public MinioClient minioClient(MinioProperties minioProperties) throws InvalidPortException, InvalidEndpointException {
return new MinioClient(minioProperties.getUrl(), minioProperties.getAccessKey(), minioProperties.getSecretKey());
}
}
@ConfigurationProperties(prefix = "minio")
@Data
public class MinioProperties {
private String url;
private String accessKey;
private String secretKey;
}
Service层处理上传和替换逻辑:
@Service
public class ImageService {
@Autowired
private MinioClient minioClient;
public void uploadImage(String bucketName, String objectName, InputStream data) throws IOException, NoSuchAlgorithmException, InvalidKeyException, XmlPullParserException, ErrorResponseException, InsufficientDataException {
minioClient.putObject(bucketName, objectName, data, data.available(), "application/octet-stream");
}
public void replaceImage(String bucketName, String objectName, InputStream data) throws IOException, NoSuchAlgorithmException, InvalidKeyException, XmlPullParserException, ErrorResponseException, InsufficientDataException {
removeImage(bucketName, objectName);
uploadImage(bucketName, objectName, data);
}
public void removeImage(String bucketName, String objectName) throws IOException, NoSuchAlgorithmException, InvalidKeyException, XmlPullParserException, ErrorResponseException, InsufficientDataException {
minioClient.removeObject(bucketName, objectName);
}
}
Nginx 配置和使用:
Nginx 作为负载均衡器,配置一个upstream模块指向MinIO集群的节点。
http {
upstream minio_cluster {
server http://minio1:9000;
server http://minio2:9000;
server http://minio3:9000;
}
server {
listen 80;
location / {
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-
评论已关闭