gRPC Gateway的实现相应的客户端和服务端代码就已经为我们生成了
我们只需要实现服务端的接口:
| |
| type TripServiceServer interface { |
| GetTrip(context.Context, *GetTripRequest) (*GetTripResponse, error) |
| } |
接口的实现:
| import ( |
| "context" |
| trippb "coolcar/proto/gen/go" |
| ) |
| |
| |
| |
| |
| |
| type Service struct{} |
| |
| func (*Service) GetTrip(con context.Context, req *trippb.GetTripRequest) (*trippb.GetTripResponse, error) { |
| return &trippb.GetTripResponse{ |
| |
| Id: req.Id, |
| Trip: &trippb.Trip{ |
| Statar: "北京" |
| End: "上海", |
| DurationSec: 3600, |
| FeeCent: 1000, |
| StatarPos: &trippb.Location{ |
| Latitude: 30, |
| Longitude: 120, |
| }, |
| EndPos: &trippb.Location{ |
| Latitude: 40, |
| Longitude: 125, |
| }, |
| PathLocations: []*trippb.Location{ |
| { |
| Latitude: 34, |
| Longitude: 123, |
| }, |
| { |
| Latitude: 38, |
| Longitude: 124, |
| }, |
| }, |
| Status: trippb.TripStatus_FINISHED, |
| }, |
| }, nil |
| } |
现在我们来实现获取行程的整个过程:
编写service端和gateway
| package main |
| |
| import ( |
| "context" |
| trippb "coolcar/proto/gen/go" |
| trip "coolcar/tripservice" |
| "fmt" |
| "log" |
| "net" |
| "net/http" |
| |
| "github.com/grpc-ecosystem/grpc-gateway/runtime" |
| "google.golang.org/grpc" |
| ) |
| |
| |
| func startGRPCGatway() { |
| c := context.Background() |
| c, cancel := context.WithCancel(c) |
| defer cancel() |
| |
| |
| mux := runtime.NewServeMux(runtime.WithMarshalerOption( |
| runtime.MIMEWildcard, &runtime.JSONPb{ |
| EnumsAsInts: true, |
| OrigName: true, |
| }, |
| )) |
| |
| err := trippb.RegisterTripServiceHandlerFromEndpoint( |
| c, |
| mux, |
| ":8081", |
| []grpc.DialOption{grpc.WithInsecure()}, |
| ) |
| if err != nil { |
| log.Fatalf("断开连接: %v", err) |
| } |
| |
| |
| err = http.ListenAndServe(":8080", mux) |
| if err != nil { |
| log.Fatalf("连接失败: %v", err) |
| } |
| } |
| |
| |
| |
| func main() { |
| fmt.Println("监听开始") |
| go startGRPCGatway() |
| list, err := net.Listen("tcp", ":8081") |
| if err != nil { |
| log.Fatalf("监听失败: %v", err) |
| } |
| s := grpc.NewServer() |
| trippb.RegisterTripServiceServer(s, &trip.Service{}) |
| fmt.Println("监听结束") |
| fmt.Println(list) |
| log.Fatal(s.Serve(list)) |
| |
| } |
编写客户端:
- 这里也可以通过grpc进行拨号:
| package main |
| |
| import ( |
| "context" |
| trippb "coolcar/proto/gen/go" |
| "fmt" |
| "log" |
| |
| "google.golang.org/grpc" |
| ) |
| |
| func main() { |
| |
| con, err := grpc.Dial("localhost:8080") |
| if err != nil { |
| log.Fatalf("连接失败: %v", err) |
| } |
| |
| tsClient := trippb.NewTripServiceClient(con) |
| res, err := tsClient.GetTrip(context.Background(), &trippb.GetTripRequest{ |
| Id: "trips01", |
| }) |
| if err != nil { |
| log.Fatalf("未获取到trips: %v", err) |
| } |
| fmt.Println(res) |
| } |
- 通过浏览器:
http://localhost:8080/trip?Id=trips01
这样整个流程就完了
返回结果:
id:"trips01" trip:{statar:"北京" statar_pos:{latitude:30 longitude:120} path_locations:{latitude:34 longitude:123} path_locations:{latitude:38 longitude:124} end:"上海" end_pos:{latitude:40 longitude:125} duration_sec:3600 fee_cent:1000 status:FINISHED}