9. Catalog-service와 Spring Cloud Gateway 연동
- 📚 Spring/Spring Cloud
- 2023. 1. 28. 23:05
| Catalog-service
💻 실습소스
https://github.com/yOneChu/springcloud_HomeToy/tree/master/catalog-service
💻 1. API-Gateway에 설정
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 # user-service
uri: lb://USER-SERVICE
predicates:
- Path=/user-service/**
- id: catalog-service # catalog-service
uri: lb://CATALOG-SERVICE
predicates:
- Path=/catalog-service/**
💻 CatalogController
package com.example.catalogservice.controller;
import com.example.catalogservice.jpa.CatalogEntity;
import com.example.catalogservice.service.CatalogService;
import com.example.catalogservice.vo.ResponseCatalog;
import lombok.RequiredArgsConstructor;
import org.modelmapper.ModelMapper;
import org.springframework.core.env.Environment;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
import java.util.List;
@RestController
@RequiredArgsConstructor
@RequestMapping("/catalog-service")
public class CatalogController {
private final Environment env;
private final CatalogService catalogService;
@GetMapping("/health_check")
public String status() {
return "It's working in Catalog Service";
}
@GetMapping("/catalogs")
public ResponseEntity<List<ResponseCatalog>> getUsers() {
Iterable<CatalogEntity> userList = catalogService.getAllCatalogs();
List<ResponseCatalog> result = new ArrayList<>();
userList.forEach(v -> {
result.add(new ModelMapper().map(v, ResponseCatalog.class));
});
return ResponseEntity.status(HttpStatus.OK).body(result);
}
}
package com.example.catalogservice.jpa;
import lombok.Data;
import org.hibernate.annotations.ColumnDefault;
import javax.persistence.*;
import java.io.Serializable;
import java.util.Date;
@Data
@Entity
@Table(name = "catalog")
public class CatalogEntity implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(nullable = false, length = 120, unique = true)
private String productId;
@Column(nullable = false)
private String productName;
@Column(nullable = false)
private Integer stock;
@Column(nullable = false)
private Integer unitPrice;
@Column(nullable = false, updatable = false, insertable = false)
@ColumnDefault(value = "CURRENT_TIMESTAMP")
private Date createdAt;
}
'📚 Spring > Spring Cloud' 카테고리의 다른 글
11. User-Service에 인증권한 추가 (Authentication) (0) | 2023.02.05 |
---|---|
10. Order-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 |
SpringCloud의 이해 (0) | 2022.01.28 |