Каждый сервис построен на Spring Boot 3.2 с единообразной структурой. Пример конфигурации customer-service:
# application.yml
spring:
application:
name: customer-service
data:
mongodb:
uri: ${MONGODB_URI:mongodb://localhost:27017}
database: customers
profiles:
active: ${SPRING_PROFILES_ACTIVE:local}
server:
port: 8080
servlet:
context-path: /customer
# Actuator для мониторинга
management:
endpoints:
web:
exposure:
include: health, prometheus, info, metrics
metrics:
tags:
application: ${spring.application.name}
distribution:
percentiles-histogram:
http.server.requests: true
health:
mongo:
enabled: true
Ключевые зависимости в build.gradle:
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-data-mongodb'
implementation 'org.springframework.boot:spring-boot-starter-validation'
implementation 'org.springframework.boot:spring-boot-starter-actuator'
// Метрики для Prometheus
implementation 'io.micrometer:micrometer-registry-prometheus'
// Распределённые трейсы
implementation 'io.micrometer:micrometer-tracing-bridge-otel'
implementation 'io.opentelemetry:opentelemetry-exporter-otlp'
// Межсервисное взаимодействие
implementation 'org.springframework.cloud:spring-cloud-starter-openfeign'
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
REST-контроллер с валидацией и обработкой ошибок:
@RestController
@RequestMapping("/api/v1/customers")
@RequiredArgsConstructor
public class CustomerController {
private final CustomerService customerService;
@PostMapping
@ResponseStatus(HttpStatus.CREATED)
public CustomerResponse create(@Valid @RequestBody CustomerRequest request) {
return customerService.create(request);
}
@GetMapping("/{id}")
public CustomerResponse getById(@PathVariable String id) {
return customerService.findById(id)
.orElseThrow(() -> new ResourceNotFoundException("Customer", id));
}
@GetMapping("/search")
public Page<CustomerResponse> search(
@RequestParam(required = false) String email,
@RequestParam(required = false) String phone,
Pageable pageable) {
return customerService.search(email, phone, pageable);
}
}
Оставить комментарий