티스토리 뷰
728x90
처리 방식 | 설명 | 사용 예 |
1. Proxy 방식 | 들어온 요청을 그대로 Kafka Bridge로 forward | Gateway가 중계만 할 때 |
2. Custom 처리 | API Gateway가 JSON 파싱 후 Kafka로 직접 전송 | 사용자 정의 처리 필요할 때 |
🧭 1. Proxy 방식 (요청 그대로 Kafka Bridge로 전달)
🔁 흐름
[Client] → [API Gateway] → [Kafka Bridge (REST)] → Kafka
✅ 구현: Spring Cloud Gateway (또는 WebFlux)로 프록시 라우팅
# application.yml
spring:
cloud:
gateway:
routes:
- id: kafka-bridge
uri: http://bridge-service:8080
predicates:
- Path=/topics/**
filters:
- StripPrefix=0
🔥 이 설정이면 /topics/my-topic으로 들어온 요청이
→ http://bridge-service:8080/topics/my-topic으로 프록시됩니다!
즉, API Gateway가 Kafka Bridge를 외부 대신 대신 호출해주는 중계자 역할을 합니다.
🛠️ 2. Custom 처리 방식 (직접 JSON 파싱해서 Kafka에 전송)
🔁 흐름
[Client] → [API Gateway (Spring Controller)] → KafkaTemplate.send()
✅ 예제 코드
@RestController
@RequestMapping("/topics")
@RequiredArgsConstructor
public class KafkaApiGatewayController {
private final KafkaTemplate<String, String> kafkaTemplate;
private final ObjectMapper objectMapper;
@PostMapping("/{topic}")
public ResponseEntity<?> forwardToKafka(
@PathVariable String topic,
@RequestBody KafkaBridgeRequest request) throws JsonProcessingException {
for (KafkaBridgeRecord record : request.getRecords()) {
String jsonValue = objectMapper.writeValueAsString(record.getValue());
kafkaTemplate.send(topic, record.getKey(), jsonValue);
}
return ResponseEntity.ok(Map.of("status", "ok"));
}
}
🔷 DTO 클래스
@Data
public class KafkaBridgeRequest {
private List<KafkaBridgeRecord> records;
}
@Data
public class KafkaBridgeRecord {
private String key;
private Object value;
}
🔍 장단점 비교
방식 | 장점 | 단점 |
Proxy 방식 | 간단, 설정만으로 동작 | 응답 구조 통제 어려움 |
Custom 처리 | 자유도 높음, 응답 조작 가능 | 코드 필요, 유효성 검증 필요 |
🧪 들어오는 요청 예
POST /topics/my-topic
Content-Type: application/json
{
"records": [
{ "key": "user1", "value": { "name": "홍길동", "age": 30 } },
{ "key": "user2", "value": { "name": "김철수", "age": 25 } }
]
}
이 구조 그대로 받아서 위 방식 중 하나로 처리하면 됩니다.
✅ 결론
질문 | 답변 |
Spring API Gateway에서 Kafka Bridge API와 같은 구조 처리 가능? | ✅ 가능합니다 |
Proxy로 중계만 할 수 있나요? | ✅ 가능합니다 (Spring Cloud Gateway) |
JSON을 직접 처리해서 Kafka에 보내는 건요? | ✅ 가능합니다 (KafkaTemplate 이용) |
728x90
'Programming' 카테고리의 다른 글
Lua 언어란? (0) | 2025.03.31 |
---|---|
UUID (Universally Unique Identifier) 란? (0) | 2025.03.31 |
Apache Pulsar란? (0) | 2025.03.31 |
HTTP/REST, gRPC, 메시지 브로커 기반 이벤트 소개 (0) | 2025.03.30 |
@ReflectionHint란? (0) | 2025.03.24 |