0Pricing
Edge Computing with Cloudflare Workers & Deno · บทเรียน

การเชื่อมโยงบริการและการผสานระบบ

เชื่อมต่อ Workers กับบริการอื่นของ Cloudflare และ API ภายนอกโดยใช้การเชื่อมโยงบริการ

การเชื่อมโยงบริการและการผสานระบบ เป็นบทเรียน Edge Computing with Cloudflare Workers & Deno ฟรีบน CoddyKit นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน 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.toml file.
  • They enable complex architectures where Workers act as building blocks.

Keep exploring how these bindings can simplify your edge development!

คำถามที่พบบ่อย

บทเรียน “การเชื่อมโยงบริการและการผสานระบบ” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “การเชื่อมโยงบริการและการผสานระบบ” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Edge Computing with Cloudflare Workers & Deno ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Edge Computing with Cloudflare Workers & Deno มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “การเชื่อมโยงบริการและการผสานระบบ”

เชื่อมต่อ Workers กับบริการอื่นของ Cloudflare และ API ภายนอกโดยใช้การเชื่อมโยงบริการ คุณปฏิบัติ Edge Computing with Cloudflare Workers & Deno ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Edge Computing with Cloudflare Workers & Deno หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Edge Computing with Cloudflare Workers & Deno บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน

บทเรียน “การเชื่อมโยงบริการและการผสานระบบ” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Edge Computing with Cloudflare Workers & Deno นี้ได้ไหม

ได้ บทเรียน Edge Computing with Cloudflare Workers & Deno ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. WebSockets และแบบเรียลไทม์
  2. คิวและงานแบบอะซิงโครนัส
  3. การเชื่อมโยงบริการและการผสานระบบ
  4. ทริกเกอร์ Cron และ Workers ตามกำหนดเวลา
← กลับไปที่ Edge Computing with Cloudflare Workers & Deno