Add grpc client and example
This commit is contained in:
		| @@ -14,6 +14,8 @@ import ( | |||||||
| 	rpc "github.com/youtube/vitess/go/rpcplus" | 	rpc "github.com/youtube/vitess/go/rpcplus" | ||||||
| 	js "github.com/youtube/vitess/go/rpcplus/jsonrpc" | 	js "github.com/youtube/vitess/go/rpcplus/jsonrpc" | ||||||
| 	pb "github.com/youtube/vitess/go/rpcplus/pbrpc" | 	pb "github.com/youtube/vitess/go/rpcplus/pbrpc" | ||||||
|  | 	ctx "golang.org/x/net/context" | ||||||
|  | 	"google.golang.org/grpc" | ||||||
| ) | ) | ||||||
|  |  | ||||||
| type headerRoundTripper struct { | type headerRoundTripper struct { | ||||||
| @@ -32,6 +34,18 @@ func (t *headerRoundTripper) RoundTrip(r *http.Request) (*http.Response, error) | |||||||
| } | } | ||||||
|  |  | ||||||
| func (r *RpcClient) call(address, path string, request Request, response interface{}) error { | func (r *RpcClient) call(address, path string, request Request, response interface{}) error { | ||||||
|  | 	switch request.ContentType() { | ||||||
|  | 	case "application/grpc": | ||||||
|  | 		cc, err := grpc.Dial(address) | ||||||
|  | 		if err != nil { | ||||||
|  | 			return errors.InternalServerError("go.micro.client", fmt.Sprintf("Error connecting to server: %v", err)) | ||||||
|  | 		} | ||||||
|  | 		if err := grpc.Invoke(ctx.Background(), path, request.Request(), response, cc); err != nil { | ||||||
|  | 			return errors.InternalServerError("go.micro.client", fmt.Sprintf("Error sending request: %v", err)) | ||||||
|  | 		} | ||||||
|  | 		return nil | ||||||
|  | 	} | ||||||
|  |  | ||||||
| 	pReq := &rpc.Request{ | 	pReq := &rpc.Request{ | ||||||
| 		ServiceMethod: request.Method(), | 		ServiceMethod: request.Method(), | ||||||
| 	} | 	} | ||||||
|   | |||||||
							
								
								
									
										23
									
								
								examples/grpc_client.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										23
									
								
								examples/grpc_client.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,23 @@ | |||||||
|  | package main | ||||||
|  |  | ||||||
|  | import ( | ||||||
|  | 	"fmt" | ||||||
|  |  | ||||||
|  | 	h "github.com/grpc/grpc-common/go/helloworld" | ||||||
|  | 	"github.com/myodc/go-micro/client" | ||||||
|  | ) | ||||||
|  |  | ||||||
|  | // run github.com/grpc/grpc-common/go/greeter_server/main.go | ||||||
|  | func main() { | ||||||
|  | 	req := client.NewRpcRequest("helloworld.Greeter", "SayHello", &h.HelloRequest{ | ||||||
|  | 		Name: "John", | ||||||
|  | 	}, "application/grpc") | ||||||
|  |  | ||||||
|  | 	rsp := &h.HelloReply{} | ||||||
|  | 	err := client.CallRemote("localhost:50051", "helloworld.Greeter/SayHello", req, rsp) | ||||||
|  | 	if err != nil { | ||||||
|  | 		fmt.Println(err) | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	fmt.Println(rsp.Message) | ||||||
|  | } | ||||||
		Reference in New Issue
	
	Block a user