pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>catalog-service</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>catalog-service</name>
<description>catalog-service</description>
<properties>
<java.version>11</java.version>
<spring-cloud.version>2020.0.0</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<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>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
application.yml
이전과 달리 jpa의 hibernate설정을 추가하여 초기 sql를 실행하여 데이터를 설정할 수 있다.
server:
port: 0
spring:
application:
name: catalog-service
h2:
console:
enabled: true
settings:
web-allow-others: true
path: /h2-console
jpa:
hibernate:
ddl-auto: create-drop
show-sql: true
generate-ddl: true
datasource:
driver-class-name: org.h2.Driver
url: jdbc:h2:mem:testdb
# username: sa
# password: 1234
eureka:
instance:
hostname: localhost
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
logging:
level:
com.example.catalogservice: DEBUG
resource > data.sql
초기 설정 테이블 데이터 생성
insert into catalog(product_id, product_name, stock, unit_price)
values ('CATALOG-001', 'Berlin', 100, 1500);
insert into catalog(product_id, product_name, stock, unit_price)
values ('CATALOG-002', 'Tokyo', 110, 1500);
insert into catalog(product_id, product_name, stock, unit_price)
values ('CATALOG-003', 'Stockholm', 120, 2000);
생성할 파일 구조
controller > CatalogController.java
package com.example.catalogservice.controller;
import com.example.catalogservice.repository.CatalogEntity;
import com.example.catalogservice.service.CatalogService;
import com.example.catalogservice.vo.ResponseCatalog;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
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
@RequestMapping("/catalog-service")
public class CatalogController {
Environment env;
private CatalogService catalogService;
@Autowired
public CatalogController(CatalogService catalogService) {
this.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>> getCatalogs() {
List<CatalogEntity> catalogList = catalogService.getAllCatalogs();
List<ResponseCatalog> result = new ArrayList<>();
catalogList.forEach(user -> {
result.add(new ModelMapper().map(user, ResponseCatalog.class));
});
return ResponseEntity.status(HttpStatus.OK).body(result);
}
}
dto > CatalogDto.java
package com.example.catalogservice.dto;
import lombok.Data;
import java.io.Serializable;
@Data
public class CatalogDto implements Serializable {
private String productId;
private Integer qty; //수량
private Integer unitPrice; //단가
private Integer totalPrice; //총계
private String orderId; //주문id
private String userId; //주문한 회원id
}
vo > ResponseCatalog
package com.example.catalogservice.vo;
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Data;
import java.util.Date;
@Data
@JsonInclude(JsonInclude.Include.NON_NULL) //널값 반환 x
public class ResponseCatalog {
private String productId;
private String productName;
private Integer unitPrice;
private Integer stock;
private Date createdAt;
}
repository > CatalogEntity.java
package com.example.catalogservice.repository;
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;
}
repository > CatalogRepository
package com.example.catalogservice.repository;
import org.springframework.data.jpa.repository.JpaRepository;
public interface CatalogRepository extends JpaRepository<CatalogEntity, Long> {
CatalogEntity findByProductId(String productId);
}
service > CatalogService
package com.example.catalogservice.service;
import com.example.catalogservice.repository.CatalogEntity;
import java.util.List;
public interface CatalogService {
List<CatalogEntity> getAllCatalogs();
}
service > CatalogServiceImpl
package com.example.catalogservice.service;
import com.example.catalogservice.repository.CatalogEntity;
import com.example.catalogservice.repository.CatalogRepository;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Data
@Slf4j
@Service
public class CatalogImpl implements CatalogService {
private CatalogRepository catalogRepository;
@Autowired
public CatalogImpl(CatalogRepository catalogRepository) {
this.catalogRepository = catalogRepository;
}
@Override
public List<CatalogEntity> getAllCatalogs() {
return catalogRepository.findAll();
}
}
카탈로그 서버 실행 했을 경우 위에서 data.sql에 설정한 sql구문을 실행하여 초기 데이터가 들어간 것을 확인 할 수 있다.
이제 Catalog-Service는 생성했지만 apigateway-service에 카탈로그 서비스의 라우터를 등록해줘야됩니다.
apigateway-service 프로젝트 > application.yml
server:
port: 8000
eureka:
instance:
hostname: localhost
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/**
- id: catalog-service
uri: lb://CATALOG-SERVICE
predicates:
- Path=/catalog-service/**
# - id: first-service
# uri: lb://MY-FIRST-SERVICE
# predicates:
# - Path=/first-service/**
# filters:
## - AddRequestHeader=first-request, first-request-header2
## - AddResponseHeader=first-response, first-response-header2
## - CustomFilter
# - id: second-service
# uri: lb://MY-SECOND-SERVICE
# predicates:
# - Path=/second-service/**
# filters:
## - AddRequestHeader=second-request, second-request-header2
## - AddResponseHeader=second-response, second-response-header2
# - name: CustomFilter
# - name: LoggingFilter
# args:
# baseMessage: Hi, there
# preLogger: true
# postLogger: true
apigateway-service로 catalog-service api호출을 테스트해보자!
정상적으로 상품정보가 호출 된 것을 알 수 있습니다.
다음 게시글로는 Order-Service 마이크로 서비스를 만들어 보겠습니다.
SpringBoot MSA (12) - MicroService / Order 서비스 생성 (1) | 2023.10.17 |
---|---|
SpringBoot MSA (10) - MicroService / UserService 기타 기능 추가 (0) | 2023.10.17 |
SpringBoot MSA (10) - MicroService / UserService API Gateway 처리 (0) | 2023.10.17 |
SpringBoot MSA (10) - MicroService / UserService 비밀번호 암호화 (3) (0) | 2023.10.17 |
SpringBoot MSA (10) - MicroService / UserService DB API (2) (0) | 2023.10.17 |
댓글 영역