jrpc-gateway is bridge between json-rpc 2.0 and gRPC
This repository consists of:
- jrpc package
- protoc-gen-jrpc-gateway protoc plugin
- protoc-gen-jrpc-doc protoc plugin
go get github.com/pacviewer/jrpc-gateway/jrpc
protoc-gen-jrpc-gateway generates json-rpc to grpc bridge code based on proto files
go install github.com/pacviewer/jrpc-gateway/protoc-gen-jrpc-gateway@v0.1.4
greeting.proto
syntax = "proto3";
package greeting;
option go_package = "/greeting";
service GreetingService {
rpc Greeting(GreetingReq) returns(GreetingResp) {}
}
message GreetingReq {
string name = 1;
}
message GreetingResp {
string message = 2;
}
mkdir gen
protoc --go_out=gen --go_opt=paths=source_relative \
--go-grpc_out=gen --go-grpc_opt=paths=source_relative \
--jrpc-gateway_out=gen \
greeting.proto
these three files will be created for you in gen directory:
- greeting_grpc.pb.go
- greeting.pb.go
- greeting_jgw.pb.go
go mod tidy
grpcConn, err := grpc.DialContext(
context.Background(),
"127.0.0.1:8686", // grpc server address
grpc.WithTransportCredentials(insecure.NewCredentials()),
)
if err != nil {
log.Fatalln(err)
}
greeting := pb.NewGreetingServiceClient(grpcConn)
greetingService := pb.NewGreetingServiceJsonRpcService(greeting)
jgw := jrpc.NewServer()
jgw.RegisterServices(&greetingService)
// json-rpc listener
lis, err = net.Listen("tcp", "localhost:8687")
if err != nil {
log.Fatalln(err)
}
mux := http.NewServeMux()
mux.HandleFunc("/", jgw.HttpHandler)
server := &http.Server{
Handler: mux,
Addr: lis.Addr().String(),
ReadHeaderTimeout: 3 * time.Second,
}
if err := server.Serve(lis); err != nil {
log.Fatalln(err)
}
curl -X POST -H 'Content-Type: application/json' \
-d '{"jsonrpc":"2.0","id":"1111","method":"greeting.greeting_service.greeting", "params":{"name":"john"}}' \
http://localhost:8687
{"jsonrpc":"2.0","id":"1111","result":{"message":"Hello john"}}
go install github.com/pacviewer/jrpc-gateway/protoc-gen-jrpc-doc/cmd/protoc-gen-jrpc-doc@v0.1.4
protoc --jrpc-doc_out=gen --jrpc-doc_opt=./json-rpc-md.tmpl,json-rpc.md \
greeting.proto