Spring Cloud - 13. Users Microservice Catalog

목차

13. Users Microservice Catalog

설정 정보

server:
port: 0

spring:
application:
name: catalog-service
h2:
console:
enabled: true
settings:
web-allow-others: true
datasource:
hikari:
driver-class-name: org.h2.Driver
url: jdbc:h2:mem:testdb
username: sa
password:
jpa:
hibernate:
ddl-auto: create-drop
show-sql: true
generate-ddl: true

eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka
#logging:
# level:
# com.example.catalogservice: DEBUG
@Data
@Entity
@Table(name = "catalog")
public class CatalogEntity {

@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
public interface CatalogRepository extends CrudRepository<CatalogEntity, Long> {
CatalogEntity findByProductId(String productId);
}
public interface CatalogService {
Iterable<CatalogEntity> getAllCatalogs();
}
@Service
@Slf4j
public class CatalogServiceImpl implements CatalogService{
CatalogRepository repository;
Environment env;

public CatalogServiceImpl(CatalogRepository repository, Environment env){
this.repository = repository;
this.env = env;
}

@Override
public Iterable<CatalogEntity> getAllCatalogs() {
return repository.findAll();
}
}
@RestController
@RequestMapping("/catalog-service")
public class CatalogController {
private Environment env;
CatalogService catalogService;

public CatalogController(Environment env, CatalogService catalogService){
this.env = env;
this.catalogService = catalogService;
}

@GetMapping("/health_check")
public String status(HttpServletRequest request){
return String.format("It's Working in Catalog Service on Port %s", request.getServerPort());
}
}
Share