본문 바로가기

IT

일반적인 api 호출이 아닌 브릿지 호출, 콜백url 호출하는 이유와 방식 차이는?

728x90
반응형

 

 

 

 

API 호출 방식 중 일반 호출, 브릿지 호출(Bridge call), **콜백 URL 호출(Callback URL call)**은 목적과 데이터 흐름 방식이 다릅니다. 하나씩 비교해서 설명드릴게요.


✅ 1. 일반적인 API 호출 (Synchronous call)

📌 개요

  • 클라이언트가 API 서버에 직접 요청(request)을 보내고, 응답(response)을 즉시 기다림
  • 대부분의 REST API, GraphQL API가 이 방식

✅ 특징

  • 요청 → 응답 1:1
  • 결과가 즉시 필요한 경우 사용 (예: 로그인, 상품조회 등)
  • 타임아웃 위험 존재 (서버가 오래 걸리는 작업일 경우)

🧾 예시

POST /api/pay → 응답: 200 OK { "status": "success" }
 

✅ 2. 브릿지 호출 (Bridge call)

📌 개요

  • **중간 서버(브릿지 서버)**를 통해 다른 시스템에 요청을 전달
  • A → B → C 구조로 동작하며, A는 C와 직접 통신하지 않음

✅ 사용 이유

  • 시스템 간 직접 통신이 어려울 때 (예: 보안, 네트워크 격리)
  • 요청을 중계하고 가공하거나 로그 처리 등 중간 로직 필요할 때

🧾 예시

Client → Bridge Server → 제3의 API 서버
  • A 시스템이 B(브릿지)에 요청
  • B가 필요한 데이터 가공 후 C 시스템에 API 호출
  • 결과를 다시 A에 전달 (또는 비동기로 처리)

✅ 3. 콜백 URL 호출 (Callback or Webhook)

📌 개요

  • 비동기 처리 방식.
    클라이언트는 작업을 요청하고, 결과는 나중에 콜백 URL로 서버가 호출

✅ 사용 이유

  • 작업 시간이 오래 걸릴 때 (파일 처리, 결제 승인, 배치 작업 등)
  • 요청자가 기다릴 필요 없이, 나중에 결과를 전달받음

🧾 동작 흐름

1. Client → Server (요청) 
2. Server → "처리 중, 콜백으로 결과 줄게" 
3. 처리 완료 후 → Client의 콜백 URL로 HTTP POST 전송

🔄 예시 (결제 시스템)

1. Client: "결제 요청" (callback_url 포함) 
2. Server: "결제 접수됨" 
3. Server: 결제 완료 후 → POST callback_url {결과}

🎯 요약 비교

항목 일반 API 브릿지 콜백 url
처리 방식 동기(Sync) 중계(Sync or Async) 비동기(Async)
응답 시점 즉시 즉시 또는 중간 처리 후 나중에 콜백 URL로 전송
사용 목적 단순 요청/응답 중간 처리, 시스템 간 격리 장시간 작업, 푸시형 알림
예시 로그인, 조회 VPN 내부 호출, 연동 가공 결제 결과 알림, 알림톡
 

🔍 결론

  • 일반 API 호출: 즉시 응답이 필요한 단순한 요청
  • 브릿지 호출: 시스템 간 중재, 로직 분리, 보안 요구 시
  • 콜백 호출: 비동기 처리, 상태 알림, 오래 걸리는 작업 처리

 

 

아래는 일반 API 호출, 브릿지(중계 서버) 호출, 콜백 URL 방식 호출에 대한 Java 코드 예제입니다. 각각의 구조적 차이를 간단히 설명한 후, Spring Boot 환경 기준으로 RestTemplate 사용 예제를 드릴게요.


✅ 1. 일반 API 호출 (Client → API Server)

RestTemplate restTemplate = new RestTemplate();
String url = "https://api.example.com/data";

HttpHeaders headers = new HttpHeaders();
headers.set("Authorization", "Bearer your-token");
HttpEntity entity = new HttpEntity<>(headers);

ResponseEntity response = restTemplate.exchange(
    url,
    HttpMethod.GET,
    entity,
    String.class
);

System.out.println("API 응답: " + response.getBody());

✅ 2. 브릿지 호출 (Client → Bridge → 외부 API)

📌 구조

Client는 직접 외부 API를 호출하지 않고 **중간 서버(Bridge Server)**를 통해 요청합니다.

▶️ Bridge Server 예제 (Spring Boot Controller)

@RestController
public class BridgeController {

    @Autowired
    private RestTemplate restTemplate;

    @PostMapping("/bridge/send")
    public ResponseEntity bridgeApi(@RequestBody Map<string, object=""> body) {
        String thirdPartyUrl = "https://thirdparty.com/api/receive";

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);

        HttpEntity<map<string, object="">> request = new HttpEntity<>(body, headers);
        ResponseEntity response = restTemplate.postForEntity(thirdPartyUrl, request, String.class);

        return ResponseEntity.ok("전송 결과: " + response.getBody());
    }
}
</map<string,></string,>

✅ 3. 콜백 URL 방식 (Client → API → 콜백 수신 URL)

📌 구조

  • Client는 서버에 요청을 보낼 때 callbackUrl을 함께 보냄
  • 서버는 처리 완료 후, callbackUrl로 결과를 POST

▶️ 요청 보내는 Client 측 코드

Map<string, object=""> requestBody = new HashMap<>();
requestBody.put("data", "example");
requestBody.put("callbackUrl", "https://my-service.com/callback");

HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);

HttpEntity<map<string, object="">> entity = new HttpEntity<>(requestBody, headers);

RestTemplate restTemplate = new RestTemplate();
restTemplate.postForEntity("https://api.example.com/start-task", entity, String.class);
</map<string,></string,>

▶️ 서버 처리 후 콜백 전송

public void sendCallback(String callbackUrl, String result) {
    Map<String, Object> callbackData = new HashMap<>();
    callbackData.put("status", "done");
    callbackData.put("result", result);

    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_JSON);

    HttpEntity<Map<String, Object>> callbackRequest = new HttpEntity<>(callbackData, headers);

    RestTemplate restTemplate = new RestTemplate();
    restTemplate.postForEntity(callbackUrl, callbackRequest, String.class);
}

📝 요약

호출 방식 특징

일반 API Client가 직접 요청
브릿지 호출 Client는 내부 서버로 보내고, 내부 서버가 외부 호출
콜백 호출 Client가 callback URL을 주고, 나중에 결과를 비동기 전달받음

필요하시면 Spring WebClient나 비동기 처리(@Async, CompletableFuture) 예제도 제공해 드릴 수 있습니다.

 

 

 

 

300x250
반응형