0Pricing
gRPC & High Performance APIs · บทเรียน

การกำหนดสคีมา Protobuf

เรียนรู้การกำหนดข้อความและบริการด้วยไวยากรณ์ Protocol Buffers (Protobuf) เพื่อให้มีชนิดข้อมูลที่ชัดเจน

การกำหนดสคีมา Protobuf เป็นบทเรียน gRPC & High Performance APIs ฟรีบน CoddyKit นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน gRPC & High Performance APIs และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส gRPC & High Performance APIs มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

What are Protocol Buffers?

Welcome! In this lesson, we'll dive into Protocol Buffers, often called Protobuf. It's a key technology for gRPC.

  • Protobuf is a language-neutral, platform-neutral, extensible mechanism for serializing structured data. Think of it as a highly efficient way to define and exchange data.
  • It's like JSON or XML, but smaller, faster, and simpler.
  • gRPC uses Protobuf to define the service interface and the structure of the payload messages.

The .proto File

Protobuf schemas are defined in special files ending with the .proto extension. These files act as contracts for your data.

  • Every .proto file starts with a syntax declaration, usually syntax = "proto3";. This tells the Protobuf compiler which version of the syntax to use.
  • It's crucial for forward and backward compatibility.
syntax = "proto3";

// Your Protobuf definitions go here

Defining Your First Message

In Protobuf, a message is a structured record of information. It's similar to a class in object-oriented programming or a struct in C.

You define a message using the message keyword, followed by its name and a body containing its fields.

syntax = "proto3";

message MyMessage {
  // fields will go here
}

Fields, Types & Numbers

Inside a message, you define fields, each with a specific data type and a unique field number.

  • Data Type: Specifies the type of data (e.g., string, int32, bool).
  • Field Name: A unique identifier for the field within the message.
  • Field Number: A unique, positive integer tag (from 1 to 229-1). These numbers are critical for identifying fields in the binary format and must remain stable for compatibility.

Common Scalar Data Types

Protobuf supports a range of scalar data types. Here are some of the most common ones:

  • int32, int64: Integer numbers.
  • float, double: Floating-point numbers.
  • bool: Boolean (true/false).
  • string: UTF-8 encoded text.
  • bytes: Raw byte sequences.

Choose the type that best fits your data to optimize storage and transmission.

Example: A Simple Person Message

Let's define a Person message with a few common fields. Notice the unique field numbers assigned to each field.

These numbers are like unique IDs for your data pieces. If you change a field's name, its number must stay the same for older systems to understand it.

syntax = "proto3";

message Person {
  string name = 1;
  int32 age = 2;
  bool is_active = 3;
}

Using Enums for Fixed Choices

Sometimes you need a field to have a value from a predefined list of options. This is where enums come in handy.

  • Enums define a set of named integer constants.
  • The first value in an enum must be 0, as it's the default value when a field isn't set.
syntax = "proto3";

enum UserRole {
  GUEST = 0;
  MEMBER = 1;
  ADMIN = 2;
}

Example: User Message with Role

Now, let's create a User message and include our UserRole enum as a field. This ensures that the user's role can only be one of the defined values.

It provides strong typing and prevents invalid data from being sent.

syntax = "proto3";

enum UserRole {
  GUEST = 0;
  MEMBER = 1;
  ADMIN = 2;
}

message User {
  string id = 1;
  string username = 2;
  UserRole role = 3;
}

Nesting Messages for Structure

For more complex data, you can nest messages within other messages. This helps organize your schema and represent hierarchical data naturally.

Think of it like having an object inside another object in programming languages.

syntax = "proto3";

message UserProfile {
  string email = 1;
  message Address {
    string street = 1;
    string city = 2;
    string zip_code = 3;
  }
  Address home_address = 2;
}

Quick Check: Protobuf Basics

Consider the following Protobuf message definition:

syntax = "proto3";

message Product {
  string name = 1;
  int32 price_cents = 2;
  bool in_stock = 3;
  enum ProductCategory {
    ELECTRONICS = 0;
    BOOKS = 1;
    CLOTHING = 2;
  }
  ProductCategory category = 4;
}

Recap: Protobuf Schema

Great job! You've learned the fundamentals of defining Protobuf schemas:

  • .proto Files: The contract for your data.
  • Messages: Structured data definitions, similar to classes.
  • Fields: Each has a type, name, and unique field number.
  • Scalar Types: Common types like string, int32, bool.
  • Enums: For predefined sets of values, starting with 0.
  • Nesting: Organizing complex data by embedding messages.

Next, we'll see how to turn these definitions into code!

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

บทเรียน “การกำหนดสคีมา Protobuf” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “การกำหนดสคีมา Protobuf” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส gRPC & High Performance APIs ให้อัปเกรดเป็น CoddyKit PRO คอร์ส gRPC & High Performance APIs มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “การกำหนดสคีมา Protobuf”

เรียนรู้การกำหนดข้อความและบริการด้วยไวยากรณ์ Protocol Buffers (Protobuf) เพื่อให้มีชนิดข้อมูลที่ชัดเจน คุณปฏิบัติ gRPC & High Performance APIs ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน gRPC & High Performance APIs หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน gRPC & High Performance APIs บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน

บทเรียน “การกำหนดสคีมา Protobuf” ใช้เวลานานแค่ไหน

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

ฉันเขียนและรันโค้ดในบทเรียน gRPC & High Performance APIs นี้ได้ไหม

ได้ บทเรียน gRPC & High Performance APIs ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

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

  1. การกำหนดสคีมา Protobuf
  2. การสร้างโค้ด gRPC
  3. บริการ gRPC แบบยูนารีอย่างง่าย
  4. RPC แบบสตรีม: เซิร์ฟเวอร์ ไคลเอนต์ และสองทิศทาง
← กลับไปที่ gRPC & High Performance APIs