반응형
Spring Cloud로 개발하는 마이크로서비스 애플리케이션(MSA)
Orders Microservice
기능 | 마이크로서비스 | URI(API Gateway 사용 시) | HTTP Method |
사용자 별 상품 주문 | Orders Microservice | /order-service/{user_id}/orders | POST |
사용자 별 주문 내역 조회 | Orders Microservice | /order-service/{user_id}/orders | GET |
프로젝트 생성
강의에서는 Spring Boot 버전을 2.4.2를 선택했고, 나는 3.0.4 버전을 선택했다.
Model Mapper 라이브러리 추가
- build.gradle
implementation 'org.modelmapper:modelmapper:2.4.2'
application.yml
server:
port: 0
spring:
application:
name: order-service
h2:
console:
enabled: true
settings:
web-allow-others: true
path: /h2-console
jpa:
hibernate:
ddl-auto: update
defer-datasource-initialization: true
datasource:
driver-class-name: org.h2.Driver
url : jdbc:h2:mem:testdb
eureka:
instance:
instance-id: ${spring.application.name}:${spring.application.instance_id:${random.value}}
client:
register-with-eureka: true
fetch-registry: true
service-url:
defaultZone: http://localhost:8761/eureka
logging:
level:
com.example.orderservice: DEBUG
OrderEntity.java
@Data
@Entity
@Table(name = "orders")
public class OrderEntity implements Serializable {
// 직렬화를 사용하는 이유 : 가지고 있는 객체를 전송하거나 DB에 보관하기 위해 마샬링 작업을 위해 사용
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(nullable = false, length = 120, unique = true)
private String productId;
@Column(nullable = false)
private Integer qty;
@Column(nullable = false)
private Integer unitPrice;
@Column(nullable = false)
private Integer totalPrice;
@Column(nullable = false)
private String userId;
@Column(nullable = false, unique = true)
private String orderId;
@Column(nullable = false, updatable = false, insertable = false)
@ColumnDefault(value = "CURRENT_TIMESTAMP")
private LocalDateTime createdAt;
}
OrderRepository.Interface
public interface OrderRepository extends CrudRepository<OrderRepository, Long> {
OrderEntity findByOrderId(String orderId);
Iterable<OrderEntity> findByUserId(String userId);
}
OrderDto.java
@Data
public class OrderDto implements Serializable {
private String productId;
private Integer qty;
private Integer unitPrice;
private Integer totalPrice;
private String orderId;
private String userId;
}
ResponseOrder.java
@Data
@JsonInclude(JsonInclude.Include.NON_NULL)
public class ResponseOrder {
private String productId;
private Integer qty;
private Integer unitPrice;
private Integer totalPrice;
private LocalDateTime createdAt;
private String orderId;
}
OrderService.interface
public interface OrderService {
OrderDto createOrder(OrderDto orderDetails);
OrderDto getOrderByOrderId(String orderId);
Iterable<OrderEntity> getAllOrders();
}
OrderServiceImpl.java
@Service
public class OrderServiceImpl implements OrderService{
OrderRepository orderRepository;
@Autowired
public OrderServiceImpl(OrderRepository orderRepository) {
this.orderRepository = orderRepository;
}
@Override
public OrderDto createOrder(OrderDto orderDto) {
orderDto.setOrderId(UUID.randomUUID().toString());
orderDto.setTotalPrice(orderDto.getQty() * orderDto.getUnitPrice());
ModelMapper mapper = new ModelMapper();
mapper.getConfiguration().setMatchingStrategy(MatchingStrategies.STRICT);
OrderEntity orderEntity = mapper.map(orderDto, OrderEntity.class);
S save = orderRepository.save(orderEntity); //오류나서 임시로 S타입 매개변수 사용
OrderDto returnValue = mapper.map(orderEntity, OrderDto.class);
return returnValue;
}
@Override
public OrderDto getOrderByOrderId(String orderId) {
OrderEntity orderEntity = orderRepository.findByOrderId(orderId);
OrderDto orderDto = new ModelMapper().map(orderEntity, OrderDto.class);
return orderDto;
}
}
- 출처 : 인프런 Spring Cloud로 개발하는 마이크로서비스 애플리케이션(MSA) 강의
반응형