<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                # GRPC網關 本指南有助于將grpc網關與go-micro服務結合使用。 [grpc-gateway](https://github.com/grpc-ecosystem/grpc-gateway)是[protoc](http://github.com/google/protobuf)的插件。它讀取[gRPC](http://github.com/grpc/grpc-common)服務定義,并生成將RESTful JSON API轉換為gRPC的反向代理服務器。 我們使用[go-grpc](https://github.com/micro/go-grpc)編寫后端服務。Go-GRPC是圍繞go-micro和用于客戶端和服務器的grpc插件的簡單包裝。當調用[grpc.NewService](https://godoc.org/github.com/micro/go-grpc#NewService)時,它返回一個[micro.Service](https://godoc.org/github.com/micro/go-micro#Service)。 ## code 在[examples/grpc](https://github.com/micro/examples/tree/master/grpc)找到示例代碼。 ## 前提 這些是一些先決條件 ### 安裝protobuf ``` mkdir tmp cd tmp git clone https://github.com/google/protobuf cd protobuf ./autogen.sh ./configure make make check sudo make install ``` ### 安裝插件 ``` go get -u github.com/grpc-ecosystem/grpc-gateway/protoc-gen-grpc-gateway go get -u github.com/micro/protobuf/protoc-gen-go ``` ## Greeter服務 在這個例子中,我們使用go-grpc創建了一個Greeter的微服務。 這項服務非常簡單。 原型如下: ``` syntax = "proto3"; package go.micro.srv.greeter; service Say { rpc Hello(Request) returns (Response) {} } message Request { string name = 1; } message Response { string msg = 1; } ``` 服務如下: ``` package main import ( "log" "time" hello "github.com/micro/examples/greeter/srv/proto/hello" "github.com/micro/go-grpc" "github.com/micro/go-micro" "golang.org/x/net/context" ) type Say struct{} func (s *Say) Hello(ctx context.Context, req *hello.Request, rsp *hello.Response) error { log.Print("Received Say.Hello request") rsp.Msg = "Hello " + req.Name return nil } func main() { service := grpc.NewService( micro.Name("go.micro.srv.greeter"), micro.RegisterTTL(time.Second*30), micro.RegisterInterval(time.Second*10), ) // optionally setup command line usage service.Init() // Register Handlers hello.RegisterSayHandler(service.Server(), new(Say)) // Run server if err := service.Run(); err != nil { log.Fatal(err) } } ``` ## GRPC網關 grpc網關使用與服務相同的協議,并增加一個http選項 ``` syntax = "proto3"; package greeter; import "google/api/annotations.proto"; service Say { rpc Hello(Request) returns (Response) { option (google.api.http) = { post: "/greeter/hello" body: "*" }; } } message Request { string name = 1; } message Response { string msg = 1; } ``` proto使用以下命令生成grpc stub和反向代理 ``` protoc -I/usr/local/include -I. \ -I$GOPATH/src \ -I$GOPATH/src/github.com/grpc-ecosystem/grpc-gateway/third_party/googleapis \ --go_out=plugins=grpc:. \ path/to/your_service.proto ``` ``` protoc -I/usr/local/include -I. \ -I$GOPATH/src \ -I$GOPATH/src/github.com/grpc-ecosystem/grpc-gateway/third_party/googleapis \ --grpc-gateway_out=logtostderr=true:. \ path/to/your_service.proto ``` 我們使用下面的代碼創建了greeter服務的示例api。將寫入類似的代碼來注冊其他端點。請注意,網關需要greeter服務的端口地址。 ``` package main import ( "flag" "net/http" "github.com/golang/glog" "github.com/grpc-ecosystem/grpc-gateway/runtime" "golang.org/x/net/context" "google.golang.org/grpc" hello "github.com/micro/examples/grpc/gateway/proto/hello" ) var ( // the go.micro.srv.greeter address endpoint = flag.String("endpoint", "localhost:9090", "go.micro.srv.greeter address") ) func run() error { ctx := context.Background() ctx, cancel := context.WithCancel(ctx) defer cancel() mux := runtime.NewServeMux() opts := []grpc.DialOption{grpc.WithInsecure()} err := hello.RegisterSayHandlerFromEndpoint(ctx, mux, *endpoint, opts) if err != nil { return err } return http.ListenAndServe(":8080", mux) } func main() { flag.Parse() defer glog.Flush() if err := run(); err != nil { glog.Fatal(err) } } ``` ## 運行示例 運行greeter服務。指定mdns,因為我們不需要發現。 ``` go run examples/grpc/greeter/srv/main.go --registry=mdns --server_address=localhost:9090 ``` 運行網關。它將默認為端點localhost:9090的greeter服務。 ``` go run examples/grpc/gateway/main.go ``` 使用curl在(localhost:8080)網關上發起請求。 ``` curl -d '{"name": "john"}' http://localhost:8080/greeter/hello ``` ## 限制 grpc網關的例子需要提供服務地址,而我們自己的micro api使用服務發現,動態路由和負載均衡。這使得grpc網關的集成性稍差一些。 訪問[github.com/micro/micro](https://github.com/micro/micro)了解更多信息
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看