mirror of
https://github.com/zeromicro/go-zero.git
synced 2026-06-22 09:51:56 +08:00
Co-authored-by: kevin <wanjunfeng@gmail.com> Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1.9 KiB
1.9 KiB
예제 10: 스트리밍 RPC
이 예제는 서버 스트리밍, 클라이언트 스트리밍, 양방향 스트리밍이라는 세 가지 gRPC 스트리밍 패턴을 모두 보여줍니다.
proto 정의
stream.proto는 각 스트리밍 패턴을 보여주는 세 개의 RPC 메서드를 정의합니다.
go_package는 전체 모듈 경로를 사용합니다.
option go_package = "example.com/demo/pb";
생성 명령
먼저 출력 디렉터리에 go.mod를 초기화합니다.
mkdir -p output && cd output && go mod init example.com/demo && cd ..
그런 다음 코드를 생성합니다.
goctl rpc protoc stream.proto \
--go_out=output \
--go-grpc_out=output \
--zrpc_out=output \
--go_opt=module=example.com/demo \
--go-grpc_opt=module=example.com/demo \
--module=example.com/demo \
-I .
생성되는 디렉터리 구조:
output/
├── etc
│ └── streamsvc.yaml
├── go.mod
├── internal
│ ├── config
│ │ └── config.go
│ ├── logic
│ │ ├── bidistreamlogic.go
│ │ ├── clientstreamlogic.go
│ │ └── serverstreamlogic.go
│ ├── server
│ │ └── streamserviceserver.go
│ └── svc
│ └── servicecontext.go
├── pb
│ ├── stream.pb.go
│ └── stream_grpc.pb.go
├── streamservice
│ └── streamservice.go
└── streamsvc.go
핵심 사항
- 세 가지 스트리밍 패턴을 지원합니다. 서버 스트리밍(응답에
stream), 클라이언트 스트리밍(요청에stream), 양방향 스트리밍(양쪽 모두stream). - goctl은 각 스트리밍 RPC 메서드마다 별도의 logic 파일을 생성합니다.
- goctl은 스트리밍 클라이언트 래퍼 메서드를 생성합니다. 반환된 gRPC stream을 사용해 메시지를 송수신하세요.