서비스 바인딩 및 통합
Service Bindings를 사용하여 Workers를 다른 Cloudflare 서비스 및 외부 API에 연결합니다.
서비스 바인딩 및 통합은(는) CoddyKit의 무료 Edge Computing with Cloudflare Workers & Deno 강의입니다. 이것은 4개 중 3번째 강의입니다. 아래에서 전체 강의를 무료로 읽을 수 있으며, 내장 코드 에디터와 24/7 AI 튜터와 함께 브라우저에서 직접 실습할 수 있습니다. 이 강의는 Edge Computing with Cloudflare Workers & Deno 학습 경로의 일부이며, 진행 상황이 웹과 CoddyKit 앱에 동기화됩니다. Edge Computing with Cloudflare Workers & Deno 강의에는 총 4개의 강의가 포함되어 있습니다.
이 강의의 일부는 아직 번역되지 않았으며 영어로 표시됩니다.
Intro to Service Bindings
Welcome to Service Bindings! In this lesson, you'll learn how Cloudflare Workers can communicate with each other and other Cloudflare services directly.
Think of Service Bindings as secure, internal connections that unlock powerful modularity for your edge applications.
Why Use Service Bindings?
Service Bindings offer several key advantages:
- Modularity: Break down complex Workers into smaller, reusable components.
- Performance: Direct internal communication is faster than public HTTP requests.
- Security: Keep internal API calls off the public internet.
- Abstraction: Treat other Workers or services like local modules.
Worker-to-Worker Communication
One of the most common uses for Service Bindings is allowing one Worker to invoke another. This is perfect for creating microservices at the edge.
You can call a 'target' Worker from an 'invoking' Worker, passing requests and receiving responses as if they were part of the same application.
Code: The Target Worker
First, let's create a simple 'target' Worker. This Worker will respond with a specific message when called.
Save this as target-worker/src/index.js:
export default {
async fetch(request, env, ctx) {
return new Response("Hello from the Target Worker!", {
status: 200,
headers: { 'Content-Type': 'text/plain' }
});
},
};Code: The Invoking Worker
Now, let's create an 'invoking' Worker that uses a binding to call our 'target' Worker. Notice how env.TARGET_WORKER.fetch(request) is used.
Save this as invoking-worker/src/index.js:
export default {
async fetch(request, env, ctx) {
// 'TARGET_WORKER' is the binding name defined in wrangler.toml
const response = await env.TARGET_WORKER.fetch(request);
const text = await response.text();
return new Response(`Invoker received: ${text}`, {
status: 200,
headers: { 'Content-Type': 'text/plain' }
});
},
};Configuring Worker Bindings
To connect the 'invoking' Worker to the 'target' Worker, you need to add a service_binding entry in the wrangler.toml file of your invoking Worker.
This tells Cloudflare which Worker project to connect to and what name (e.g., TARGET_WORKER) to use in your code.
# In the invoking-worker's wrangler.toml
name = "invoking-worker"
main = "src/index.js"
compatibility_date = "2023-12-01"
[[service_bindings]]
name = "TARGET_WORKER"
service = "target-worker" # Name of the target Worker project
Make sure both Workers are deployed to Cloudflare.
Bindings to Cloudflare Services
Service Bindings aren't limited to Workers! You can also bind your Worker directly to other Cloudflare services like KV Namespaces, R2 buckets, D1 databases, and Durable Objects.
This provides your Worker with direct, secure, and performant access to these resources via the env object.
Code: KV Namespace Binding
Here's how you'd configure a binding to a KV Namespace in your wrangler.toml:
# In your Worker's wrangler.toml
name = "my-worker"
main = "src/index.js"
compatibility_date = "2023-12-01"
[[kv_namespaces]]
binding = "MY_KV" # The variable name in your Worker
id = "YOUR_KV_NAMESPACE_ID" # Replace with your actual KV ID
Then, access it in your Worker's code:
export default {
async fetch(request, env, ctx) {
// 'MY_KV' is the binding name from wrangler.toml
await env.MY_KV.put("greeting", "Hello from KV!");
const message = await env.MY_KV.get("greeting");
return new Response(`KV says: ${message}`, {
status: 200,
headers: { 'Content-Type': 'text/plain' }
});
},
};Test Your Knowledge!
Choose the best answer.
Recap: Bindings & Integrations
You've learned about Cloudflare Worker Service Bindings!
- They provide a powerful way for Workers to communicate with other Workers or Cloudflare services directly.
- Benefits include modularity, performance, and security.
- You configure bindings in your
wrangler.tomlfile. - They enable complex architectures where Workers act as building blocks.
Keep exploring how these bindings can simplify your edge development!
자주 묻는 질문
“서비스 바인딩 및 통합” 강의는 무료인가요?
네 — “서비스 바인딩 및 통합” 전체 내용을 이 웹사이트에서 무료로 읽을 수 있습니다. 인터랙티브하게 실습하려면(내장 코드 에디터와 24/7 AI 튜터), CoddyKit PRO로 업그레이드하면 Edge Computing with Cloudflare Workers & Deno 강의 전체를 잠금 해제할 수 있습니다. Edge Computing with Cloudflare Workers & Deno 강의에는 총 4개의 강의가 포함되어 있습니다.
“서비스 바인딩 및 통합”에서 뭘 배우나요?
Service Bindings를 사용하여 Workers를 다른 Cloudflare 서비스 및 외부 API에 연결합니다. 브라우저에서 직접 실행하는 실습 코드로 Edge Computing with Cloudflare Workers & Deno을(를) 배우며, 24/7 AI 튜터가 강의를 진행하면서 질문에 답변해줍니다.
Edge Computing with Cloudflare Workers & Deno을(를) 시작하는 데 경험이 필요한가요?
사전 경험은 필요하지 않습니다. CoddyKit의 Edge Computing with Cloudflare Workers & Deno은(는) 초급자부터 고급 학습자까지를 위해 구성되어 있으므로, 여기서 시작하거나 처음부터 시작할 수 있으며 자신의 속도대로 진행할 수 있습니다. 이것은 4개 중 3번째 강의입니다.
“서비스 바인딩 및 통합” 강의는 얼마나 걸리나요?
대부분의 CoddyKit 강의는 약 5~10분이 소요됩니다. 각 강의는 간결하고 인터랙티브하여 꾸준한 진행이 가능하며, 웹과 앱에서 중단한 부분부터 바로 시작할 수 있습니다.
이 Edge Computing with Cloudflare Workers & Deno 강의에서 코드를 작성하고 실행할 수 있나요?
네. 모든 Edge Computing with Cloudflare Workers & Deno 강의에는 내장 코드 에디터가 포함되어 있으므로, 브라우저에서 바로 실제 코드를 작성하고 실행한 후 즉시 AI 피드백을 받을 수 있습니다 — 로컬 설정이 필요 없습니다.
이 강의의 모든 강의
- WebSockets 및 실시간 기능
- 대기열 및 비동기 작업
- 서비스 바인딩 및 통합
- Cron 트리거 및 예약된 워커