Skip to content

Commit

Permalink
perf($auth-center): create fallback for remote API
Browse files Browse the repository at this point in the history
  • Loading branch information
johnnymillergh committed Feb 11, 2022
1 parent 984fff2 commit c445d6f
Show file tree
Hide file tree
Showing 12 changed files with 88 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright By ZATI
* Copyright By 3a3c88295d37870dfd3b25056092d1a9209824b256c341f2cdc296437f671617
* All rights reserved.
*
* If you are not the intended user, you are hereby notified that any use, disclosure, copying, printing, forwarding or
* dissemination of this property is strictly prohibited. If you have got this file in error, delete it from your
* system.
*/
package com.jmsoftware.maf.authcenter.remote;

import com.jmsoftware.maf.common.domain.osscenter.write.ObjectResponse;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.constraints.NotNull;

/**
* Description: OssCenterFeignService, change description here.
*
* @author 钟俊 (za-zhongjun), email: jun.zhong@zatech.com, date: 2/5/2022 7:46 PM
**/
@Validated
public interface OssCenterFeignService {
/**
* Upload single resource object response.
*
* @param multipartFile the multipart file
* @return the object response
*/
ObjectResponse uploadSingleResource(@NotNull MultipartFile multipartFile);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright By ZATI
* Copyright By 3a3c88295d37870dfd3b25056092d1a9209824b256c341f2cdc296437f671617
* All rights reserved.
*
* If you are not the intended user, you are hereby notified that any use, disclosure, copying, printing, forwarding or
* dissemination of this property is strictly prohibited. If you have got this file in error, delete it from your
* system.
*/
package com.jmsoftware.maf.authcenter.remote.impl;

import com.jmsoftware.maf.authcenter.remote.OssCenterFeignClient;
import com.jmsoftware.maf.authcenter.remote.OssCenterFeignService;
import com.jmsoftware.maf.common.bean.ResponseBodyBean;
import com.jmsoftware.maf.common.domain.osscenter.write.ObjectResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.constraints.NotNull;
import java.util.Optional;

/**
* Description: OssCenterFeignServiceImpl, change description here.
*
* @author 钟俊 (za-zhongjun), email: jun.zhong@zatech.com, date: 2/5/2022 7:47 PM
**/
@Slf4j
@Service
@RequiredArgsConstructor
public class OssCenterFeignServiceImpl implements OssCenterFeignService {
private final OssCenterFeignClient ossCenterFeignClient;

@Override
public ObjectResponse uploadSingleResource(@NotNull MultipartFile multipartFile) {
log.info("Uploading single resource to oss center. multipartFile: {}", multipartFile);
return Optional.ofNullable(ossCenterFeignClient.uploadSingleResource(multipartFile))
.map(ResponseBodyBean::getData)
.orElseThrow(() -> {
log.error("Failed to upload single resource to oss center. multipartFile: {}", multipartFile);
return new RuntimeException("Upload single resource to oss center failed.");
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package com.jmsoftware.maf.authcenter.user;
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.jmsoftware.maf.authcenter.user.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.text.CharSequenceUtil;
import cn.hutool.core.util.BooleanUtil;
Expand Down Expand Up @@ -42,7 +41,6 @@
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.concurrent.TimeUnit;

import static java.util.concurrent.TimeUnit.DAYS;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@
import javax.validation.constraints.NotNull;

/**
* Description: OssCenterRemoteApi, change description here.
* Description: OssCenterFeignClient, change description here.
*
* @author Johnny Miller (锺俊), email: johnnysviva@outlook.com, date: 9/15/2021 11:10 AM
* @see <a href='https://docs.spring.io/spring-cloud-openfeign/docs/current/reference/html/'>Spring Cloud OpenFeign</a>
**/
@Validated
@FeignClient(value = OssCenterRemoteApi.SERVICE_NAME, fallback = OssCenterRemoteApi.OssCenterRemoteApiFallback.class)
public interface OssCenterRemoteApi {
@FeignClient(value = OssCenterFeignClient.SERVICE_NAME, fallback = OssCenterFeignClient.OssCenterFeignClientFallback.class)
public interface OssCenterFeignClient {
String SERVICE_NAME = "oss-center";

/**
Expand All @@ -35,10 +35,10 @@ public interface OssCenterRemoteApi {

@Slf4j
@Component
class OssCenterRemoteApiFallback implements OssCenterRemoteApi {
class OssCenterFeignClientFallback implements OssCenterFeignClient {
@Override
public ResponseBodyBean<ObjectResponse> uploadSingleResource(@NotNull MultipartFile multipartFile) {
log.error("Fallback -> OssCenterRemoteApi#uploadSingleResource()");
log.error("Fallback -> OssCenterFeignClient#uploadSingleResource()");
return null;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.jmsoftware.maf.authcenter.configuration;

import com.jmsoftware.maf.authcenter.remote.OssCenterRemoteApi;
import com.jmsoftware.maf.authcenter.remote.OssCenterFeignService;
import com.jmsoftware.maf.springcloudstarter.poi.OssUploader;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -18,15 +18,15 @@
@Configuration
@RequiredArgsConstructor
public class OssConfiguration {
private final OssCenterRemoteApi ossCenterRemoteApi;
private final OssCenterFeignService ossCenterFeignService;

@Bean
public OssUploader ossUploader() {
return (name, inputStream) -> {
val multipartFile = new MockMultipartFile(name, name, null, inputStream);
val response = this.ossCenterRemoteApi.uploadSingleResource(multipartFile);
log.info("Called {} to upload multipartFile. {}", OssCenterRemoteApi.SERVICE_NAME, response);
return String.format("%s/%s", response.getData().getBucket(), response.getData().getObject());
val objectResponse = this.ossCenterFeignService.uploadSingleResource(multipartFile);
log.info("Uploaded multipartFile. objectResponse: {}", objectResponse);
return String.format("%s/%s", objectResponse.getBucket(), objectResponse.getObject());
};
}
}

0 comments on commit c445d6f

Please sign in to comment.