10. Order-service와 Spring Cloud Gateway 연동
- 📚 Spring/Spring Cloud
- 2023. 1. 28. 23:07
| Order-service
💻 실습소스
https://github.com/yOneChu/springcloud_HomeToy/tree/master/order-service
💻 OrderEntity
package com.example.orderservice.jpa;
import lombok.Data;
import org.hibernate.annotations.ColumnDefault;
import javax.persistence.*;
import java.io.Serializable;
import java.util.Date;
@Data
@Entity
@Table(name = "orders")
public class OrderEntity implements Serializable {
@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 Date createdAt;
}
💻 OrderServiceImpl
package com.example.orderservice.service;
import lombok.RequiredArgsConstructor;
import org.modelmapper.ModelMapper;
import org.modelmapper.convention.MatchingStrategies;
import org.springframework.stereotype.Service;
import java.util.UUID;
@Service
@RequiredArgsConstructor
public class OrderServiceImpl implements OrderService {
private final 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);
orderRepository.save(orderEntity);
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;
}
@Override
public Iterable<OrderEntity> getOrdersByUserId(String userId) {
return orderRepository.findByUserId(userId);
}
}
💻 application.yml
server:
port: 0
spring:
application:
name: order-service
datasource:
url: jdbc:postgresql://localhost:5432/basic
username: postgres
password: wcadmin
driver-class-name: org.postgresql.Driver
jpa:
hibernate:
ddl-auto: create
properties:
hibernate:
show_sql: true
format_sql: true
eureka:
instance:
hostname: localhost
instance-id: ${spring.cloud.client.hostname}:${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
'📚 Spring > Spring Cloud' 카테고리의 다른 글
12. User-Service JWT 생성 (0) | 2023.02.12 |
---|---|
11. User-Service에 인증권한 추가 (Authentication) (0) | 2023.02.05 |
9. Catalog-service와 Spring Cloud Gateway 연동 (0) | 2023.01.28 |
8. User-service와 Spring Cloud Gateway 연동 (0) | 2023.01.27 |
Spring Cloud Config의 이해 (0) | 2022.01.29 |