<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.3.176</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.modelmapper</groupId>
<artifactId>modelmapper</artifactId>
<version>2.3.8</version>
</dependency>
pom 수정 및 추가
server:
port: 0 #랜덤으로 포트 설정
spring:
application:
name: catalog-service #Eureka에 등록되는 서비스 이름
h2:
console:
enabled: true
settings:
web-allow-others: true
path: /h2-console
jpa:
hibernate:
ddl-auto: create-drop
show-sql: true
generate-ddl: true
database: h2
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://127.0.0.1:8761/eureka
insert into catalog(product_id, product_name, stock, unit_price)
values('CTALOG-001', '상품명1', 100, 1500);
insert into catalog(product_id, product_name, stock, unit_price)
values('CTALOG-002', '상품명2', 200, 2000);
insert into catalog(product_id, product_name, stock, unit_price)
values('CTALOG-003', '상품명3', 300, 2300);
@Data
@Entity
@Table(name = "catalog")
public class CatalogEntity implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "product_id", nullable = false, length = 120, unique = true)
private String productId;
@Column(name = "product_name", 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 LocalDateTime createAt;
}
public interface CatalogRepository extends CrudRepository<CatalogEntity, Long> {
CatalogEntity findByProductId(String productId);
}
@Data
public class CatalogDto implements Serializable {
private String productId;
private Integer qty;
private Integer unitPrice;
private Integer totalPrice;
private String orderId;
private String userId;
}
@Data
@JsonInclude(JsonInclude.Include.NON_NULL)
public class ResponseCatalog {
private String productId;
private String productName;
private Integer unitPrice;
private Integer stock;
private LocalDateTime createAt;
}
public interface CatalogService {
Iterable<CatalogEntity> getAllCatalogs();
}
@Service
@RequiredArgsConstructor
public class CatalogServiceImpl implements CatalogService{
private final CatalogRepository catalogRepository;
@Override
public Iterable<CatalogEntity> getAllCatalogs() {
return catalogRepository.findAll();
}
}
@RestController
@RequestMapping("/catalog-service")
@RequiredArgsConstructor
public class CatalogController {
private final Environment env;
private final CatalogService catalogService;
@GetMapping("/health_check")
public String status(){
return String.format("It's Working in User Service on Port %s",env.getProperty("local.server.port"));
}
@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.ok().body(result);
}
}
...
spring:
application: #gateway service 이름
name: apigateway-service
cloud:
gateway: #gateway 설정
default-filters:
- name : GlobalFilter #Global Filter로 지정된 java 파일 이름
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
데이터 정상 반환 확인