Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package kr.magicbox.creator.adapter.out.communication.grpc;

import com.google.common.util.concurrent.ListenableFuture;
import io.github.resilience4j.circuitbreaker.annotation.CircuitBreaker;
import io.github.resilience4j.timelimiter.annotation.TimeLimiter;
import io.grpc.ManagedChannel;
import kr.magicbox.creator.adapter.out.communication.ServiceHost;
import kr.magicbox.creator.adapter.out.communication.grpc.exception.ReleaseServiceUnavailableException;
Expand All @@ -19,7 +21,8 @@
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

@Component
@RequiredArgsConstructor
Expand All @@ -29,59 +32,68 @@ public class ReleaseQueryGrpcAdapter implements ReleaseQueryPort {

@Override
@CircuitBreaker(name = "releaseService", fallbackMethod = "getReleaseCountFallback")
public long getReleaseCount(Long creatorId) {
@TimeLimiter(name = "releaseService", fallbackMethod = "getReleaseCountFallback")
public CompletableFuture<Long> getReleaseCount(Long creatorId) {
GetReleaseCountRequest request = GetReleaseCountRequest.newBuilder()
.setCreatorId(creatorId)
.build();

ManagedChannel channel = grpcChannelFactory.createChannel(ServiceHost.RELEASE.getHostName());
ReleaseServiceGrpc.ReleaseServiceBlockingStub stub = ReleaseServiceGrpc
.newBlockingStub(channel)
.withDeadlineAfter(2, TimeUnit.SECONDS);
GetReleaseCountResponse response = stub.getReleaseCount(request);

return response.getReleaseCount();
ReleaseServiceGrpc.ReleaseServiceFutureStub stub = ReleaseServiceGrpc.newFutureStub(channel);
ListenableFuture<GetReleaseCountResponse> future = stub.getReleaseCount(request);
try {
GetReleaseCountResponse response = future.get();
return CompletableFuture.completedFuture(response.getReleaseCount());
} catch (ExecutionException e) {
return CompletableFuture.failedFuture(e.getCause());
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
return CompletableFuture.failedFuture(e);
}
}

@Override
@CircuitBreaker(name = "releaseService", fallbackMethod = "getReleasesFallback")
public List<ReleaseResult> getReleases(Long creatorId) {
@TimeLimiter(name = "releaseService", fallbackMethod = "getReleasesFallback")
public CompletableFuture<List<ReleaseResult>> getReleases(Long creatorId) {
GetReleasesByCreatorIdRequest request = GetReleasesByCreatorIdRequest.newBuilder()
.setCreatorId(creatorId)
.build();

ManagedChannel channel = grpcChannelFactory.createChannel(ServiceHost.RELEASE.getHostName());
ReleaseServiceGrpc.ReleaseServiceBlockingStub stub = ReleaseServiceGrpc
.newBlockingStub(channel)
.withDeadlineAfter(2, TimeUnit.SECONDS);
GetReleasesByCreatorIdResponse response = stub.getReleasesByCreatorId(request);

return response.getReleasesList().stream()
.map(release -> ReleaseResult.builder()
.releaseId(ReleaseId.of(release.getReleaseId()))
.title(release.getTitle())
.thumbnailUrl(release.getThumbnailUrl())
.level(ReleaseLevel.valueOf(release.getLevel().name()))
.creatorNickname(release.getCreatorNickname())
.price(release.getPrice())
.limitedQuantity(release.getLimitedQuantity())
.build())
.toList();
ReleaseServiceGrpc.ReleaseServiceFutureStub stub = ReleaseServiceGrpc.newFutureStub(channel);
ListenableFuture<GetReleasesByCreatorIdResponse> future = stub.getReleasesByCreatorId(request);
try {
GetReleasesByCreatorIdResponse response = future.get();
List<ReleaseResult> releases = response.getReleasesList().stream()
.map(release -> ReleaseResult.builder()
.releaseId(ReleaseId.of(release.getReleaseId()))
.title(release.getTitle())
.thumbnailUrl(release.getThumbnailUrl())
.level(ReleaseLevel.valueOf(release.getLevel().name()))
.creatorNickname(release.getCreatorNickname())
.price(release.getPrice())
.limitedQuantity(release.getLimitedQuantity())
.build())
.toList();
return CompletableFuture.completedFuture(releases);
} catch (ExecutionException e) {
return CompletableFuture.failedFuture(e.getCause());
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
return CompletableFuture.failedFuture(e);
}
}

@SuppressWarnings("unused")
private long getReleaseCountFallback(Long creatorId, Throwable throwable) {
private CompletableFuture<Long> getReleaseCountFallback(Long creatorId, Throwable throwable) {
log.warn("릴리즈 개수 조회 서비스 연결 실패");
throw new ReleaseServiceUnavailableException(throwable);
}

@SuppressWarnings("unused")
private List<ReleaseResult> getReleasesFallback(Long creatorId, Throwable throwable) {
throw buildReleaseServiceUnavailableException(throwable);
}

private ReleaseServiceUnavailableException buildReleaseServiceUnavailableException(Throwable throwable) {
private CompletableFuture<List<ReleaseResult>> getReleasesFallback(Long creatorId, Throwable throwable) {
log.warn("릴리즈 목록 조회 서비스 연결 실패");
return new ReleaseServiceUnavailableException(throwable);
throw new ReleaseServiceUnavailableException(throwable);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package kr.magicbox.creator.adapter.out.communication.grpc;

import com.google.common.util.concurrent.ListenableFuture;
import io.github.resilience4j.circuitbreaker.annotation.CircuitBreaker;
import io.github.resilience4j.timelimiter.annotation.TimeLimiter;
import io.grpc.ManagedChannel;
import kr.magicbox.creator.adapter.out.communication.ServiceHost;
import kr.magicbox.creator.adapter.out.communication.grpc.exception.ReviewServiceUnavailableException;
Expand All @@ -14,7 +16,8 @@
import org.springframework.grpc.client.GrpcChannelFactory;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

@Component
@RequiredArgsConstructor
Expand All @@ -24,23 +27,29 @@ public class ReviewQueryGrpcAdapter implements ReviewRatingQueryPort {

@Override
@CircuitBreaker(name = "reviewService", fallbackMethod = "getReviewRatingFallback")
public ReviewRating getReviewRating(Long creatorId) {
@TimeLimiter(name = "reviewService", fallbackMethod = "getReviewRatingFallback")
public CompletableFuture<ReviewRating> getReviewRating(Long creatorId) {
GetReviewRatingRequest request = GetReviewRatingRequest.newBuilder()
.setCreatorId(creatorId)
.build();

ManagedChannel channel = grpcChannelFactory.createChannel(ServiceHost.REVIEW.getHostName());
ReviewServiceGrpc.ReviewServiceBlockingStub stub = ReviewServiceGrpc
.newBlockingStub(channel)
.withDeadlineAfter(2, TimeUnit.SECONDS);
GetReviewRatingResponse response = stub.getReviewRating(request);

return ReviewRating.of(response.getRating());
ReviewServiceGrpc.ReviewServiceFutureStub stub = ReviewServiceGrpc.newFutureStub(channel);
ListenableFuture<GetReviewRatingResponse> future = stub.getReviewRating(request);
try {
GetReviewRatingResponse response = future.get();
return CompletableFuture.completedFuture(ReviewRating.of(response.getRating()));
} catch (ExecutionException e) {
return CompletableFuture.failedFuture(e.getCause());
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
return CompletableFuture.failedFuture(e);
}
}

@SuppressWarnings("unused")
private ReviewRating getReviewRatingFallback(Long creatorId, Throwable throwable) {
private CompletableFuture<ReviewRating> getReviewRatingFallback(Long creatorId, Throwable throwable) {
log.warn("리뷰 서비스 연결 실패");
throw new ReviewServiceUnavailableException(throwable);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package kr.magicbox.creator.adapter.out.communication.grpc;

import com.google.common.util.concurrent.ListenableFuture;
import io.github.resilience4j.circuitbreaker.annotation.CircuitBreaker;
import io.github.resilience4j.timelimiter.annotation.TimeLimiter;
import io.grpc.ManagedChannel;
import kr.magicbox.creator.adapter.out.communication.ServiceHost;
import kr.magicbox.creator.adapter.out.communication.grpc.exception.ShortformServiceUnavailableException;
import kr.magicbox.creator.application.dto.result.ShortformId;
import kr.magicbox.creator.application.dto.result.ShortformResult;
import kr.magicbox.creator.application.port.out.ShortformQueryPort;
import kr.magicbox.creator.application.dto.result.ShortformId;
import kr.magicbox.creator.grpc.shortform.GetShortformsByCreatorIdRequest;
import kr.magicbox.creator.grpc.shortform.GetShortformsByCreatorIdResponse;
import kr.magicbox.creator.grpc.shortform.ShortformServiceGrpc;
Expand All @@ -16,7 +18,8 @@
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

@Component
@RequiredArgsConstructor
Expand All @@ -26,30 +29,37 @@ public class ShortformQueryGrpcAdapter implements ShortformQueryPort {

@Override
@CircuitBreaker(name = "shortformService", fallbackMethod = "getShortformsFallback")
public List<ShortformResult> getShortforms(Long creatorId) {
@TimeLimiter(name = "shortformService", fallbackMethod = "getShortformsFallback")
public CompletableFuture<List<ShortformResult>> getShortforms(Long creatorId) {
GetShortformsByCreatorIdRequest request = GetShortformsByCreatorIdRequest.newBuilder()
.setCreatorId(creatorId)
.build();

ManagedChannel channel = grpcChannelFactory.createChannel(ServiceHost.SHORTFORM.getHostName());
ShortformServiceGrpc.ShortformServiceBlockingStub stub = ShortformServiceGrpc
.newBlockingStub(channel)
.withDeadlineAfter(2, TimeUnit.SECONDS);
GetShortformsByCreatorIdResponse response = stub.getShortformsByCreatorId(request);

return response.getShortformsList().stream()
.map(shortform -> ShortformResult.builder()
.shortformId(ShortformId.of(shortform.getShortformId()))
.title(shortform.getTitle())
.thumbnailUrl(shortform.getThumbnailUrl())
.videoUrl(shortform.getVideoUrl())
.viewCount(shortform.getViewCount())
.build())
.toList();
ShortformServiceGrpc.ShortformServiceFutureStub stub = ShortformServiceGrpc.newFutureStub(channel);
ListenableFuture<GetShortformsByCreatorIdResponse> future = stub.getShortformsByCreatorId(request);
try {
GetShortformsByCreatorIdResponse response = future.get();
List<ShortformResult> shortforms = response.getShortformsList().stream()
.map(shortform -> ShortformResult.builder()
.shortformId(ShortformId.of(shortform.getShortformId()))
.title(shortform.getTitle())
.thumbnailUrl(shortform.getThumbnailUrl())
.videoUrl(shortform.getVideoUrl())
.viewCount(shortform.getViewCount())
.build())
.toList();
return CompletableFuture.completedFuture(shortforms);
} catch (ExecutionException e) {
return CompletableFuture.failedFuture(e.getCause());
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
return CompletableFuture.failedFuture(e);
}
}

@SuppressWarnings("unused")
private List<ShortformResult> getShortformsFallback(Long creatorId, Throwable throwable) {
private CompletableFuture<List<ShortformResult>> getShortformsFallback(Long creatorId, Throwable throwable) {
log.warn("숏폼 서비스 연결 실패");
throw new ShortformServiceUnavailableException(throwable);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package kr.magicbox.creator.adapter.out.communication.grpc;

import com.google.common.util.concurrent.ListenableFuture;
import io.github.resilience4j.circuitbreaker.annotation.CircuitBreaker;
import io.github.resilience4j.timelimiter.annotation.TimeLimiter;
import io.grpc.ManagedChannel;
import kr.magicbox.creator.adapter.out.communication.ServiceHost;
import kr.magicbox.creator.adapter.out.communication.grpc.exception.SubscribeServiceUnavailableException;
Expand All @@ -15,7 +17,8 @@
import org.springframework.grpc.client.GrpcChannelFactory;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

@Component
@RequiredArgsConstructor
Expand All @@ -25,46 +28,58 @@ public class SubscribeGrpcAdapter implements SubscribeQueryPort {

@Override
@CircuitBreaker(name = "subscribeService", fallbackMethod = "getSubscriberCountFallback")
public long getSubscriberCount(Long creatorId) {
@TimeLimiter(name = "subscribeService", fallbackMethod = "getSubscriberCountFallback")
public CompletableFuture<Long> getSubscriberCount(Long creatorId) {
GetSubscriberCountRequest request = GetSubscriberCountRequest.newBuilder()
.setCreatorId(creatorId)
.build();

ManagedChannel channel = grpcChannelFactory.createChannel(ServiceHost.SUBSCRIBE.getHostName());
SubscribeServiceGrpc.SubscribeServiceBlockingStub stub = SubscribeServiceGrpc
.newBlockingStub(channel)
.withDeadlineAfter(2, TimeUnit.SECONDS);
GetSubscriberCountResponse response = stub.getSubscriberCount(request);

return response.getSubscriberCount();
SubscribeServiceGrpc.SubscribeServiceFutureStub stub = SubscribeServiceGrpc.newFutureStub(channel);
ListenableFuture<GetSubscriberCountResponse> future = stub.getSubscriberCount(request);
try {
GetSubscriberCountResponse response = future.get();
return CompletableFuture.completedFuture(response.getSubscriberCount());
} catch (ExecutionException e) {
return CompletableFuture.failedFuture(e.getCause());
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
return CompletableFuture.failedFuture(e);
}
}

@Override
@CircuitBreaker(name = "subscribeService", fallbackMethod = "isSubscribedFallback")
public boolean isSubscribed(Long creatorId, Long userId) {
@TimeLimiter(name = "subscribeService", fallbackMethod = "isSubscribedFallback")
public CompletableFuture<Boolean> isSubscribed(Long creatorId, Long userId) {
IsSubscribedRequest request = IsSubscribedRequest.newBuilder()
.setCreatorId(creatorId)
.setUserId(userId)
.build();

ManagedChannel channel = grpcChannelFactory.createChannel(ServiceHost.SUBSCRIBE.getHostName());
SubscribeServiceGrpc.SubscribeServiceBlockingStub stub = SubscribeServiceGrpc
.newBlockingStub(channel)
.withDeadlineAfter(2, TimeUnit.SECONDS);
IsSubscribedResponse response = stub.isSubscribed(request);

return response.getSubscribed();
SubscribeServiceGrpc.SubscribeServiceFutureStub stub = SubscribeServiceGrpc.newFutureStub(channel);
ListenableFuture<IsSubscribedResponse> future = stub.isSubscribed(request);
try {
IsSubscribedResponse response = future.get();
return CompletableFuture.completedFuture(response.getSubscribed());
} catch (ExecutionException e) {
return CompletableFuture.failedFuture(e.getCause());
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
return CompletableFuture.failedFuture(e);
}
}

@SuppressWarnings("unused")
private long getSubscriberCountFallback(Long creatorId, Throwable throwable) {
private CompletableFuture<Long> getSubscriberCountFallback(Long creatorId, Throwable throwable) {
log.warn("구독 서비스 연결 실패");
throw new SubscribeServiceUnavailableException(throwable);
}

@SuppressWarnings("unused")
private boolean isSubscribedFallback(Long creatorId, Long userId, Throwable throwable) {
private CompletableFuture<Boolean> isSubscribedFallback(Long creatorId, Long userId, Throwable throwable) {
log.warn("구독 서비스 연결 실패");
throw new SubscribeServiceUnavailableException(throwable);
}
}
}
Loading
Loading