티스토리 뷰
api에 mapping 추가
package org.example.api.core.product;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
public interface ProductService {
@GetMapping(
value = "/product/{productId}",
produces = "application/json"
)
Product getProduct(@PathVariable int productId);
}
util에 간단한 기능 추가 - getPort()
package org.example.util.http;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class ServiceUtil {
private static final Logger LOG = LoggerFactory.getLogger(ServiceUtil.class);
private final String port;
@Autowired
public ServiceUtil(
@Value("${server.port}") String port) {
this.port = port;
}
public String getPort() {
return port;
}
}
product-service에서 api, util 사용하기
그냥 import해서 쓰면 됌.
package org.example.microservices.core.product.services;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;
import org.example.api.core.product.Product;
import org.example.api.core.product.ProductService;
import org.example.util.http.ServiceUtil;
@RestController
public class ProductServiceImpl implements ProductService {
private static final Logger LOG = LoggerFactory.getLogger(ProductServiceImpl.class);
private final ServiceUtil serviceUtil;
@Autowired
public ProductServiceImpl(ServiceUtil serviceUtil) {
this.serviceUtil = serviceUtil;
}
@Override
public Product getProduct(int productId) {
LOG.debug("productId={}", productId);
return new Product();
}
}
이게 product-service 서버만 껐다 키고
브라우저에서 http://localhost:7001/product/1 요청해보면 log가 찍힌다.
이제 product-composite-service로 받은 요청이
방금 만든 mapping "localhost:7001/product/1"을 호출하도록 해보자.
api에 mapping 추가
package org.example.api.composite.product;
import org.example.api.core.product.Product;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
public interface ProductCompositeService {
@GetMapping(
value = "/product-composite/{productId}",
produces = "application/json"
)
Product getProduct(@PathVariable int productId);
}
product-composite-service에서 구현체 만들기
package org.example.microservices.composite.product.services;
import org.example.api.composite.product.ProductCompositeService;
import org.example.api.core.product.Product;
import org.example.util.http.ServiceUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ProductCompositeServiceImpl implements ProductCompositeService {
private static final Logger LOG = LoggerFactory.getLogger(ProductCompositeServiceImpl.class);
private final ServiceUtil serviceUtil;
private ProductCompositeIntegration integration;
@Autowired
public ProductCompositeServiceImpl(ServiceUtil serviceUtil, ProductCompositeIntegration integration) {
this.serviceUtil = serviceUtil;
this.integration = integration;
}
@Override
public Product getProduct(int productId) {
LOG.debug("ProductCompositeServiceImpl.productId={}", productId);
Product product = integration.getProduct(productId);
return new Product(200);
}
}
ProductCompositeIntegration에서 각 서비스로 요청
package org.example.microservices.composite.product.services;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.example.api.core.product.Product;
import org.example.api.core.product.ProductService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
@Component
public class ProductCompositeIntegration implements ProductService/*, RecommendationService, ReviewService*/ {
private static final Logger LOG = LoggerFactory.getLogger(ProductCompositeIntegration.class);
private final RestTemplate restTemplate;
private final ObjectMapper mapper;
private final String productServiceUrl;
@Autowired
public ProductCompositeIntegration(
RestTemplate restTemplate,
ObjectMapper mapper,
@Value("${app.product-service.host}") String productServiceHost,
@Value("${app.product-service.port}") int productServicePort // ,
// @Value("${app.recommendation-service.host}") String recommendationServiceHost,
// @Value("${app.recommendation-service.port}") int recommendationServicePort,
// @Value("${app.review-service.host}") String reviewServiceHost,
// @Value("${app.review-service.port}") int reviewServicePort
) {
this.restTemplate = restTemplate;
this.mapper = mapper;
productServiceUrl = "http://" + productServiceHost + ":" + productServicePort + "/product/";
// recommendationServiceUrl = "http://" + recommendationServiceHost + ":" + recommendationServicePort + "/recommendation?productId=";
// reviewServiceUrl = "http://" + reviewServiceHost + ":" + reviewServicePort + "/review?productId=";
}
public Product getProduct(int productId) {
try {
String url = productServiceUrl + productId;
LOG.debug("Will call getProduct API on URL: {}", url);
Product product = restTemplate.getForObject(url, Product.class);
LOG.debug("Found a product: {}", product);
return product;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
아래 코드는 application.properties에서 설정한 "app.product-service.host" 값을 productServiceHost에 담는 역할
@Value("${app.product-service.host}") String productServiceHost
RestTemplate Bean등록
RestTemplate을 사용하기 위해선 ProductCompositeServiceApplication.java에서 먼저 Bean에 등록해둔다.
...
public class ProductCompositeServiceApplication {
@Bean
RestTemplate restTemplate() {
return new RestTemplate();
}
public static void main(String[] args) {
SpringApplication.run(ProductCompositeServiceApplication.class, args);
}
}
이제 나머지 2개 프로젝트도 비슷한 방법으로 작성해 나가면 됌
'spring(boot)' 카테고리의 다른 글
스프링부트 3 (0) | 2023.05.02 |
---|---|
관련 기본 용어들 (0) | 2023.05.01 |
[microservices]1.프로젝트 기본 뼈대(총 5개 프로젝트) (0) | 2023.01.30 |
[junit]jacoco로 coverage보기 (0) | 2022.11.23 |
[security]jwt (+react) (0) | 2022.11.16 |