Skip to content

Commit

Permalink
Merge pull request #34 from SWM-WeLike2Coding/feat/distributedTracing
Browse files Browse the repository at this point in the history
feat: 마이크로서비스 분산 추적을 위한 관련 의존성 및 로그 추가
  • Loading branch information
kjungw1025 authored Aug 4, 2024
2 parents 09851f4 + 81990d0 commit 7267140
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 15 deletions.
7 changes: 7 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@ dependencies {
implementation 'org.springframework.cloud:spring-cloud-starter-openfeign'
implementation 'org.springframework.cloud:spring-cloud-starter-circuitbreaker-resilience4j'

// Zipkin + Micrometer Tracing
implementation 'io.micrometer:micrometer-tracing-bridge-brave'
implementation 'io.zipkin.reporter2:zipkin-reporter-brave'
implementation 'io.github.openfeign:feign-micrometer'
implementation 'org.springframework.boot:spring-boot-starter-actuator'
implementation 'io.micrometer:micrometer-registry-prometheus'

// lombok
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package com.wl2c.elswhereuserservice.domain.user.controller;

import com.wl2c.elswhereuserservice.client.product.dto.response.ResponseSummarizedProductDto;
import com.wl2c.elswhereuserservice.domain.user.model.dto.request.RequestCreateInterestDto;
import com.wl2c.elswhereuserservice.domain.user.model.dto.response.ResponseUserInterestDto;
import com.wl2c.elswhereuserservice.domain.user.model.dto.list.SummarizedUserInterestDto;
import com.wl2c.elswhereuserservice.domain.user.service.UserInterestService;
import com.wl2c.elswhereuserservice.global.model.dto.ResponseIdDto;
import io.swagger.v3.oas.annotations.tags.Tag;
Expand Down Expand Up @@ -41,7 +40,7 @@ public ResponseIdDto create(HttpServletRequest request,
* @return 사용자가 등록한 관심 상품 리스트
*/
@GetMapping
public List<ResponseUserInterestDto> read(HttpServletRequest request) {
public List<SummarizedUserInterestDto> read(HttpServletRequest request) {
return userInterestService.read(parseLong(request.getHeader("requestId")));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
package com.wl2c.elswhereuserservice.domain.user.model.dto.response;
package com.wl2c.elswhereuserservice.domain.user.model.dto.list;

import com.wl2c.elswhereuserservice.client.product.ProductType;
import com.wl2c.elswhereuserservice.client.product.dto.response.ResponseSummarizedProductDto;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Builder;
import lombok.Getter;
import lombok.NonNull;

import java.math.BigDecimal;
import java.time.LocalDate;

@Getter
public class ResponseUserInterestDto {
public class SummarizedUserInterestDto {

@Schema(description = "사용자 관심 상품 id", example = "1")
private final Long interestId;
Expand Down Expand Up @@ -43,8 +42,8 @@ public class ResponseUserInterestDto {
@Schema(description = "청약 마감일", example = "2024-06-21")
private final LocalDate subscriptionEndDate;

public ResponseUserInterestDto(@NonNull Long interestId,
@NonNull ResponseSummarizedProductDto responseSummarizedProductDto) {
public SummarizedUserInterestDto(@NonNull Long interestId,
@NonNull ResponseSummarizedProductDto responseSummarizedProductDto) {
this.interestId = interestId;
this.productId = responseSummarizedProductDto.getId();
this.issuer = responseSummarizedProductDto.getIssuer();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,18 @@

import com.wl2c.elswhereuserservice.client.product.api.ProductServiceClient;
import com.wl2c.elswhereuserservice.client.product.dto.request.RequestProductIdListDto;
import com.wl2c.elswhereuserservice.client.product.dto.response.ResponseSingleProductDto;
import com.wl2c.elswhereuserservice.client.product.dto.response.ResponseSummarizedProductDto;
import com.wl2c.elswhereuserservice.client.product.exception.ProductNotFoundException;
import com.wl2c.elswhereuserservice.domain.user.exception.AlreadyInterestException;
import com.wl2c.elswhereuserservice.domain.user.exception.InterestNotFoundException;
import com.wl2c.elswhereuserservice.domain.user.exception.UserNotFoundException;
import com.wl2c.elswhereuserservice.domain.user.model.dto.request.RequestCreateInterestDto;
import com.wl2c.elswhereuserservice.domain.user.model.dto.response.ResponseUserInterestDto;
import com.wl2c.elswhereuserservice.domain.user.model.dto.list.SummarizedUserInterestDto;
import com.wl2c.elswhereuserservice.domain.user.model.entity.Interest;
import com.wl2c.elswhereuserservice.domain.user.model.entity.User;
import com.wl2c.elswhereuserservice.domain.user.repository.UserInterestRepository;
import com.wl2c.elswhereuserservice.domain.user.repository.UserRepository;
import com.wl2c.elswhereuserservice.global.model.dto.ResponseIdDto;
import feign.FeignException;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.client.circuitbreaker.CircuitBreaker;
Expand All @@ -25,7 +23,6 @@

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
Expand All @@ -45,9 +42,11 @@ public ResponseIdDto create(Long userId, RequestCreateInterestDto requestCreateI

User user = userRepository.findById(userId).orElseThrow(UserNotFoundException::new);

log.info("Before call the product microservice");
CircuitBreaker circuitBreaker = circuitBreakerFactory.create("interestCreateCircuitBreaker");
circuitBreaker.run(() -> productServiceClient.getProduct(requestCreateInterestDto.getProductId()),
throwable -> new ProductNotFoundException());
log.info("After called the product microservice");

if (userInterestRepository.findByUserIdAndProductId(userId, requestCreateInterestDto.getProductId()).isPresent()) {
throw new AlreadyInterestException();
Expand All @@ -62,7 +61,7 @@ public ResponseIdDto create(Long userId, RequestCreateInterestDto requestCreateI
}
}

public List<ResponseUserInterestDto> read(Long userId) {
public List<SummarizedUserInterestDto> read(Long userId) {
List<Interest> interestList = userInterestRepository.findAllByUserId(userId);
if (interestList.isEmpty()) {
throw new InterestNotFoundException();
Expand All @@ -72,16 +71,18 @@ public List<ResponseUserInterestDto> read(Long userId) {
.map(Interest::getProductId)
.toList();

log.info("Before call the product microservice");
CircuitBreaker circuitBreaker = circuitBreakerFactory.create("interestReadCircuitBreaker");
List<ResponseSummarizedProductDto> responseSummarizedProductDtoList =
circuitBreaker.run(() -> productServiceClient.listByProductIds(new RequestProductIdListDto(productIdList)),
throwable -> new ArrayList<>());
log.info("fter called the product microservice");

List<ResponseUserInterestDto> result = new ArrayList<>();
List<SummarizedUserInterestDto> result = new ArrayList<>();
for (Interest interest : interestList) {
for (ResponseSummarizedProductDto responseSummarizedProductDto : responseSummarizedProductDtoList) {
if (interest.getProductId().equals(responseSummarizedProductDto.getId())) {
result.add(new ResponseUserInterestDto(interest.getId(), responseSummarizedProductDto));
result.add(new SummarizedUserInterestDto(interest.getId(), responseSummarizedProductDto));
break;
}
}
Expand Down

0 comments on commit 7267140

Please sign in to comment.