๋คํธ์ํฌ ํต์ ํ๋กํ ์ฝ gRPC์ websocket์ ๋น๊ตํด๋ณด์๋ค
[gRPC] gRPC๊ณผ websocket ๋น๊ต ์ ๋ฆฌ
๊ธฐ์กด์ ๊ฐ๋ฐํ๋ ์๋น์ค์์ ํด๋ผ์ด์ธํธ์ ์๋ฒ ํต์ ์ websocket ์ผ๋ก ๊ตฌํํ๋ค.๋ค ๋๋ด๊ณ ๋์ ํด๋ผ์ด์ธํธ, ์๋ฒ ํต์ ํ๋ ๋ฐฉ๋ฒ์ ์ข ๋ ์์นํด๋ณด๊ณ ์ถ์๋ค๊ทธ๋ ๊ฒ ์๊ฒ๋ gRPC. ์ ๋ฆฌํด๋ณด์ โ gRPC
bonory.tistory.com
์ต์ํ์ง ์์ gRPC๋ฅผ python์ผ๋ก ๊ฐ๋จํ๊ฒ ๊ตฌํํด๋ณด์๋ค
1. Protocol Buffers (Protobuf) ์ ์
gRPC์ ํต์ฌ์ API ์ธํฐํ์ด์ค๋ฅผ ์ ์ํ๋ Protobuf ํ์ผ์ด๋ค!
.proto ํ์ผ ์์ฑํ๋ค = ์๋น์ค์ ๋ฉ์์ง์ ์คํค๋ง๋ฅผ ์ ์ํ๋ค
greeter.proto
syntax = "proto3";
service Greeter {
rpc SayHello (HelloRequest) returns (HelloResponse);
}
message HelloRequest {
string name = 1;
}
message HelloResponse {
string message = 1;
}
2. gRPC ์ปดํ์ผ๋ฌ(protoc)๋ฅผ ์ฌ์ฉํด ์๋ฒ์ ํด๋ผ์ด์ธํธ ์ฝ๋๋ฅผ ์์ฑ
2-1. Protocol Buffers ์ปดํ์ผ๋ฌ๊ฐ ์ค์น๋์ด ์์ง ์๋ค๋ฉด ์ค์นํ๋ค.
# Mac
brew install protobuf
2-2. ํจํค์ง๋ ์ค์นํ๋ค.
pip install --upgrade grpcio grpcio-tools
2-3. ์๋ฒ, ํด๋ผ์ด์ธํธ ์ฝ๋ ์์ฑํ๋ค.
protoc --proto_path=. --python_out=. --grpc_python_out=. greeter.proto
# ์ง์ grpc_tools.protoc ๋ชจ๋์ Python ๋ช
๋ น์ด๋ก ์คํ
python -m grpc_tools.protoc -I=. --python_out=. --grpc_python_out=. greeter.proto
๋ช ๋ น์ด๊ฐ ์ ์์ ์ผ๋ก ์คํ๋๋ฉด ํ์ผ์ด 2๊ฐ ์์ฑ๋๋ค.
- greeter_pb2.py
- Protocol Buffers ๋ฉ์์ง ํด๋์ค ์ ์
- .proto ํ์ผ์ ์ ์๋ ๋ฉ์์ง(message)์ ๊ด๋ จ๋ Python ํด๋์ค๋ค์ด ํฌํจํ๋ค.
- ์: HelloRequest, HelloResponse
- greeter_pb2_grpc.py
- gRPC ์๋น์ค Stub ๋ฐ ์๋ฒ ์ธํฐํ์ด์ค ์ ์
- ํด๋ผ์ด์ธํธ์ ์๋ฒ๊ฐ ํธ์ถํ ์ ์๋ gRPC ๋ฉ์๋ Stub๊ณผ ์๋น์ค ํด๋์ค๋ฅผ ํฌํจํ๋ค.
- ์: GreeterStub (ํด๋ผ์ด์ธํธ์ฉ), GreeterServicer (์๋ฒ์ฉ)
3. ์์ฑ๋ ํ์ผ์ ๊ธฐ์ค์ผ๋ก ์๋ฒ, ํด๋ผ์ด์ธํธ ํ์ผ ์์ฑ
server.py
from concurrent import futures
import grpc
import greeter_pb2
import greeter_pb2_grpc
class GreeterService(greeter_pb2_grpc.GreeterServicer):
def SayHello(self, request, context):
print(f"Received request: {request.name}")
return greeter_pb2.HelloResponse(message=f"Hello, {request.name}!")
def serve():
server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
greeter_pb2_grpc.add_GreeterServicer_to_server(GreeterService(), server)
server.add_insecure_port('[::]:50051')
print("Server started on port 50051")
server.start()
server.wait_for_termination()
if __name__ == '__main__':
serve()
client.py
import grpc
import greeter_pb2
import greeter_pb2_grpc
channel = grpc.insecure_channel('localhost:50051')
stub = greeter_pb2_grpc.GreeterStub(channel)
response = stub.SayHello(greeter_pb2.HelloRequest(name="World"))
print(response.message)
+ ํ์ผ ๊ตฌ์ฑ ์์
gRPC_test/
โโโ greeter.proto # .proto ํ์ผ
โโโ greeter_pb2.py # protoc์ ์ํด ์์ฑ๋ ํ์ผ (Protocol Buffers ๋ฉ์์ง ํด๋์ค)
โโโ greeter_pb2_grpc.py # protoc์ ์ํด ์์ฑ๋ ํ์ผ (gRPC Stub ๋ฐ ์๋น์ค ์ธํฐํ์ด์ค)
โโโ server.py # ์๋ฒ ์ฝ๋
โโโ client.py # ํด๋ผ์ด์ธํธ ์ฝ๋
4. ์คํ
1. ์๋ฒ ์คํ
python3 server.py
> Server started on port 50051
2. ํด๋ผ์ด์ธํธ ์คํ
python3 client.py
> Hello, World!
ํด๋ผ์ด์ธํธ ์คํํ์ ๋, ์๋ฒ ์ชฝ์๋ ์๋์ ๊ฐ์ด ์ฐํ๋ฉด ์์ฑ!
Received request: World
โ ํด๋ผ์ด์ธํธ PC์ ํ์ํ ํ์ผ
- client.py: ํด๋ผ์ด์ธํธ ์ฝ๋
- greeter_pb2.py: protoc์ ์ํด ์์ฑ๋ Protocol Buffers ๋ฉ์์ง ํด๋์ค ํ์ผ
- greeter_pb2_grpc.py: protoc์ ์ํด ์์ฑ๋ gRPC Stub ์ ์ ํ์ผ
โ ์๋ฒ PC์ ํ์ํ ํ์ผ
- server.py: ์๋ฒ ์ฝ๋
- greeter_pb2.py: protoc์ ์ํด ์์ฑ๋ Protocol Buffers ๋ฉ์์ง ํด๋์ค ํ์ผ
- greeter_pb2_grpc.py: protoc์ ์ํด ์์ฑ๋ gRPC Stub ์ ์ ํ์ผ
- greeter.proto: ํ์ ์ ์๋ฒ ์ธก์์ .proto ํ์ผ์ ๋ณ๊ฒฝํ๊ณ ๋ค์ ์ปดํ์ผํ ๊ฒฝ์ฐ
์๋ฒ, ํด๋ผ์ด์ธํธ ์ฝ๋ ์์ฑํ๋ ๋ถ๋ถ์์ ์๊พธ ๋ค์๊ณผ ๊ฐ์ ์๋ฌ๊ฐ ์์๋ค โผ๏ธ
/Users/~/protoc-gen-grpc_python: program not found or is not executable
Please specify a program using absolute path or make sure the program is available in your PATH system variable
--grpc_python_out: protoc-gen-grpc_python: Plugin failed with status code 1.
→ protoc๊ฐ gRPC Python ํ๋ฌ๊ทธ์ธ(protoc-gen-grpc_python)์ ์ฐพ์ง ๋ชปํ๊ฑฐ๋ ์คํ ๊ถํ์ด ์๋ ๊ฒฝ์ฐ ๋ฐ์
→ ์คํ๋์ด์ผ ํ protoc-gen-grpc_python ์ด /Users/~/venv/bin ์ ์ค์น๊ฐ ์๋์ด์์๋ค
๊ทธ๋์ ์ง์ ์์ฑํด์คฌ๋ค
1. /Users/~/venv/bin/protoc-gen-grpc_python ํ์ผ์ ์์ฑ
2. ์์ฑํ ํ์ผ์ ๋ด์ฉ ์ถ๊ฐ
#!/bin/bash
exec /Users/~/venv/bin/python -m grpc_tools.protoc "$@"
3. ์คํ ๊ถํ ๋ถ์ฌ
chmod +x /Users/~/venv/bin/protoc-gen-grpc_python
๊ทธ ๋ค์์ protoc๋ฅผ ํตํด ์๋ฒ, ํด๋ผ์ด์ธํธ ํ์ผ์ ์์ฑํ๋ฉด ๋๋๋ฐ
protoc --proto_path=. --python_out=. --grpc_python_out=. greeter.proto
์๋์ ๊ฐ์ ์๋ฌ ๋ฉ์ธ์ง๊ฐ ์ถ๋ ฅ๋์๋ค
Missing input file.
--grpc_python_out: protoc-gen-grpc_python: Plugin failed with status code 1.
์ง์ grpc_tools.protoc ๋ชจ๋์ python ๋ช ๋ น์ด๋ก ์คํํ๋ค
python -m grpc_tools.protoc -I=. --python_out=. --grpc_python_out=. greeter.proto
→ ๊ทธ์ ์์ผ ์๋ฒ, ํด๋ผ์ด์ธํธ ํ์ผ์ด ์ ์์ฑ๋ ๊ฑธ ํ์ธํ๋ค ๐ฅฒ ๐
'๐ฉ๐ปโ๐ป > gRPC' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
RPC vs gRPC (0) | 2025.01.14 |
---|