๊ธ์ ๋ถ๋ฌ์ค๋ ์คโฆ
๊ธ์ ๋ถ๋ฌ์ค๋ ์คโฆ
๐ React ํ๋ก์ ํธ์์ Protocol Buffers์ gRPC ๊ฐ๋ ์ ๋ฆฌ ๐ฆ 1. Protocol Buffers (proto)๋? Protocol Buffers (protobuf)๋ Google์์ ๊ฐ๋ฐํ ๋ฐ์ดํฐ ์ง๋ ฌํ(Serialization) ํฌ๋งท์ ๋๋ค. ๋ฐ์ดํฐ ๊ตฌ์กฐ๋ฅผ ์ ์ํโฆ
**Protocol Buffers (protobuf)**๋ Google์์ ๊ฐ๋ฐํ ๋ฐ์ดํฐ ์ง๋ ฌํ(Serialization) ํฌ๋งท์ ๋๋ค.
๋ฐ์ดํฐ ๊ตฌ์กฐ๋ฅผ ์ ์ํ๊ณ , ์ด๋ฅผ ์ด์ง(Binary) ํ์์ผ๋ก ๋น ๋ฅด๊ฒ ์ง๋ ฌํ/์ญ์ง๋ ฌํํ ์ ์๊ฒ ํด์ค๋๋ค.
JSON, XML ๊ฐ์ ํฌ๋งท๊ณผ ๋น์ทํ ์ญํ ์ ํ์ง๋ง, ๋ ๋น ๋ฅด๊ณ , ๋ ๊ฐ๋ณ๊ณ , ๋ ํจ์จ์ ํฉ๋๋ค.
1๏ธโฃ ๋ฐ์ดํฐ ์ ์ก์ ํจ์จ์ฑ
2๏ธโฃ ๋ช ํํ ๋ฐ์ดํฐ ๊ตฌ์กฐ (Type Safety)
3๏ธโฃ gRPC ํต์ ์ง์
proto๋ gRPC์ ํต์ฌ ๊ธฐ์ ๋ก, ํจ์จ์ ์ธ API ํต์ ์ ์ํด ํ์์ ์
๋๋ค.syntax = "proto3";
message User {
string id = 1;
string name = 2;
int32 age = 3;
}
import { User } from './message_pb';
const user = new User();
user.setId('123');
user.setName('Alice');
user.setAge(30);
const binaryData = user.serializeBinary();
const receivedUser = User.deserializeBinary(binaryData);
console.log(receivedUser.getName()); // "Alice"
gRPC๋ Google์์ ๊ฐ๋ฐํ ๊ณ ์ฑ๋ฅ, ๋ฒ์ฉ ์๊ฒฉ ํ๋ก์์ ํธ์ถ(Remote Procedure Call, RPC) ํ๋ ์์ํฌ์ ๋๋ค.
1๏ธโฃ HTTP/2 ๊ธฐ๋ฐ
2๏ธโฃ Protocol Buffers ์ฌ์ฉ
3๏ธโฃ ์๋ฐฉํฅ ์คํธ๋ฆฌ๋ฐ ์ง์
| ํน์ง | REST API | gRPC |
| ์ ์ก ํ๋กํ ์ฝ | HTTP/1.1 | HTTP/2 |
| ๋ฐ์ดํฐ ํฌ๋งท | JSON | Protocol Buffers (์ด์ง) |
| ์คํธ๋ฆฌ๋ฐ ์ง์ | ์ ํ์ | ์๋ฐฉํฅ ์คํธ๋ฆฌ๋ฐ ์ง์ |
| ์ฑ๋ฅ | ์ผ๋ฐ์ | ๋ ๋น ๋ฆ |
| ํ์ ๊ฒ์ฆ | ์์ | ๊ฐ๋ ฅํ ํ์ ์์คํ |
syntax = "proto3";
service UserService {
rpc GetUser (UserRequest) returns (UserResponse);
}
message UserRequest {
int32 id = 1;
}
message UserResponse {
int32 id = 1;
string name = 2;
int32 age = 3;
}
const grpc = require('@grpc/grpc-js');
const protoLoader = require('@grpc/proto-loader');
const packageDefinition = protoLoader.loadSync('user.proto');
const userProto = grpc.loadPackageDefinition(packageDefinition).UserService;
const server = new grpc.Server();
server.addService(userProto.service, {
GetUser: (call, callback) => {
const user = { id: call.request.id, name: 'Alice', age: 30 };
callback(null, user);
},
});
server.bindAsync('0.0.0.0:50051', grpc.ServerCredentials.createInsecure(), () => {
server.start();
});
const grpc = require('@grpc/grpc-js');
const protoLoader = require('@grpc/proto-loader');
const packageDefinition = protoLoader.loadSync('user.proto');
const UserService = grpc.loadPackageDefinition(packageDefinition).UserService;
const client = new UserService('localhost:50051', grpc.credentials.createInsecure());
client.GetUser({ id: 1 }, (error, response) => {
if (!error) {
console.log(response); // { id: 1, name: 'Alice', age: 30 }
}
});
Protocol Buffers: ๋น ๋ฅด๊ณ ํจ์จ์ ์ธ ๋ฐ์ดํฐ ์ง๋ ฌํ ํฌ๋งท
gRPC: HTTP/2 ๊ธฐ๋ฐ์ ๊ณ ์ฑ๋ฅ ์๊ฒฉ ํ๋ก์์ ํธ์ถ ํ๋ ์์ํฌ
React ํ๋ก์ ํธ์์ ์ฑ๋ฅ ์ต์ ํ, ํ์ ์์ ์ฑ, ์ค์๊ฐ ํต์ ์ด ํ์ํ๋ค๋ฉด ๊ฐ๋ ฅํ ๋๊ตฌ๊ฐ ๋ ์ ์์ต๋๋ค.
๐ ์ฑ๋ฅ + ํ์ฅ์ฑ + ์ค์๊ฐ ๋ฐ์ดํฐ ์ฒ๋ฆฌ๊ฐ ํ์ํ ํ๋ก์ ํธ๋ผ๋ฉด gRPC์ Protocol Buffers๋ฅผ ๊ณ ๋ คํด๋ณด์ธ์!