Spring Cloud - 14. Users Microservice Order Service

목차

Users Microservice Order Service

설정

server:
port: 0

spring:
application:
name: order-service

h2:
console:
enabled: true
settings:
web-allow-others: true
datasource:
driver-class-name: org.h2.Driver
username: sa
password:
url: jdbc:h2:mem:testdb

jpa:
hibernate:
ddl-auto: create-drop
generate-ddl: true
show-sql: true
eureka:
client:
register-with-eureka: true
fetch-registry: true
service-url:
defaultZone: http://localhost:8761/eureka
instance:
instanceId: ${spring.application.name}:${spring.application.instance_id:${random.value}}
@Data
@Entity
@Table(name = "orders")
public class OrderEntity implements Serializable {

@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@Column(nullable = false, length = 120)
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;
}
@Repository
public interface OrderRepository extends CrudRepository<OrderEntity, Long> {
OrderEntity findByOrderId(String orderId);
Iterable<OrderEntity> findByUserId(String userId);
}
@Data
@JsonInclude(JsonInclude.Include.NON_NULL)
public class ResponseOrder {

private String productId;
private Integer qty;
private Integer unitPrice;
private Integer totalPrice;
private Date createdAt;

private String orderId;
}
@Data
public class RequestOrder {
private String productId;
private Integer qty;
private Integer unitPrice;
}
@Data
public class OrderDto implements Serializable {

private String productId;
private Integer qty;
private Integer unitPrice;
private Integer totalPrice;

private String orderId;
private String userId;
}
public interface OrderService {
OrderDto createOrder(OrderDto orderDetails);
OrderDto getOrderByOrderId(String orderId);
Iterable<OrderEntity> getOrdersByUserId(String userId);
}
@Service
public class OrderServiceImpl implements OrderService{
private OrderRepository orderRepository;

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 modelMapper = new ModelMapper();
modelMapper.getConfiguration().setMatchingStrategy(MatchingStrategies.STRICT);

OrderEntity orderEntity = modelMapper.map(orderDto, OrderEntity.class);

orderRepository.save(orderEntity);
OrderDto returnValue = modelMapper.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);
}
}
@RestController
@RequestMapping("/order-service/")
public class OrderController {

private OrderService orderService;

public OrderController(OrderService orderService){
this.orderService = orderService;
}

@PostMapping(value = "/{userId}/orders")
public ResponseEntity<ResponseOrder> createOrder(@PathVariable("userId") String userId,
@RequestBody RequestOrder orderDetails){
ModelMapper modelMapper = new ModelMapper();
modelMapper.getConfiguration().setMatchingStrategy(MatchingStrategies.STRICT);

OrderDto orderDto = modelMapper.map(orderDetails, OrderDto.class);
orderDto.setUserId(userId);
OrderDto createDto = orderService.createOrder(orderDto);
ResponseOrder returnValue = modelMapper.map(createDto, ResponseOrder.class);

return ResponseEntity.status(HttpStatus.CREATED).body(returnValue);
}

@GetMapping(value = "/{userId}/orders")
public ResponseEntity<List<ResponseOrder>> getOrder(@PathVariable("userId") String userId){
Iterable<OrderEntity> orderList = orderService.getOrdersByUserId(userId);

List<ResponseOrder> result = new ArrayList<>();
orderList.forEach(v -> {
result.add(new ModelMapper().map(v, ResponseOrder.class));
});

return ResponseEntity.status(HttpStatus.OK).body(result);
}
}

Gateway에 등록

- id: order-service
uri: lb://ORDER-SERVICE
predicates:
- Path=/order-service/**
filters:
- CustomFilter
server:
port: 8080

eureka:
client:
register-with-eureka: true
fetch-registry: true
service-url:
defaultZone: http://localhost:8761/eureka

spring:
application:
name: apigateway-service
cloud:
gateway:
default-filters:
- name: GlobalFilter
args:
baseMessage: Spring Cloud Gateway Global Filter
preLogger: true
postLogger: true

routes:
- id: user-service
uri: lb://USER-SERVICE
predicates:
- Path=/user-service/**
filters:
- CustomFilter

- id: catalog-service
uri: lb://CATALOG-SERVICE
predicates:
- Path=/catalog-service/**
filters:
- CustomFilter

- id: order-service
uri: lb://ORDER-SERVICE
predicates:
- Path=/order-service/**
filters:
- CustomFilter
Share