mirror of
https://github.com/go-micro/go-micro.git
synced 2025-11-29 21:47:44 +02:00
Add examples
This commit is contained in:
150
examples/client/codegen/README.md
Normal file
150
examples/client/codegen/README.md
Normal file
@@ -0,0 +1,150 @@
|
||||
# Code Generation [Experimental]
|
||||
|
||||
We're experimenting with code generation to reduce the amount of boiler plate code written.
|
||||
|
||||
## Example
|
||||
|
||||
Going from this
|
||||
```golang
|
||||
req := client.NewRequest("go.micro.srv.example", "Example.Call", &example.Request{
|
||||
Name: "John",
|
||||
})
|
||||
|
||||
rsp := &example.Response{}
|
||||
|
||||
if err := client.Call(context.Background(), req, rsp); err != nil {
|
||||
return err
|
||||
}
|
||||
```
|
||||
|
||||
To
|
||||
|
||||
```golang
|
||||
rsp, err := cl.Call(context.Background(), &example.Request{Name: "John"})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
```
|
||||
|
||||
## Generation of stub code for the example service
|
||||
|
||||
```shell
|
||||
go get github.com/micro/protobuf/protoc-gen-go
|
||||
cd examples/server/proto/example
|
||||
protoc --go_out=plugins=micro:. example.proto
|
||||
```
|
||||
|
||||
Look at examples/server/proto/example/example.pb.go
|
||||
to see the generated code.
|
||||
|
||||
## Guide
|
||||
|
||||
### Download the protoc-gen-go code
|
||||
|
||||
```shell
|
||||
go get github.com/micro/protobuf/protoc-gen-go
|
||||
```
|
||||
|
||||
### Define your proto service.
|
||||
|
||||
hello.proto
|
||||
```shell
|
||||
syntax = "proto3";
|
||||
|
||||
// package name is used as the service name for discovery
|
||||
// if service name is not passed in when initialising the
|
||||
// client
|
||||
package go.micro.srv.greeter;
|
||||
|
||||
service Say {
|
||||
rpc Hello(Request) returns (Response) {}
|
||||
}
|
||||
|
||||
message Request {
|
||||
optional string name = 1;
|
||||
}
|
||||
|
||||
message Response {
|
||||
optional string msg = 1;
|
||||
}
|
||||
```
|
||||
|
||||
**Note: Remember to set package name in the proto, it's used to generate
|
||||
the service for discovery.**
|
||||
|
||||
### Generate code
|
||||
|
||||
```shell
|
||||
protoc --go_out=plugins=micro:. hello.proto
|
||||
```
|
||||
|
||||
### Generated code
|
||||
|
||||
```shell
|
||||
// Client API for Say service
|
||||
|
||||
type SayClient interface {
|
||||
Hello(ctx context.Context, in *Request) (*Response, error)
|
||||
}
|
||||
|
||||
type sayClient struct {
|
||||
c client.Client
|
||||
serviceName string
|
||||
}
|
||||
|
||||
func NewSayClient(serviceName string, c client.Client) SayClient {
|
||||
if c == nil {
|
||||
c = client.NewClient()
|
||||
}
|
||||
if len(serviceName) == 0 {
|
||||
serviceName = "go.micro.srv.greeter"
|
||||
}
|
||||
return &sayClient{
|
||||
c: c,
|
||||
serviceName: serviceName,
|
||||
}
|
||||
}
|
||||
|
||||
func (c *sayClient) Hello(ctx context.Context, in *Request) (*Response, error) {
|
||||
req := c.c.NewRequest(c.serviceName, "Say.Hello", in)
|
||||
out := new(Response)
|
||||
err := c.c.Call(ctx, req, out)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// Server API for Say service
|
||||
|
||||
type SayHandler interface {
|
||||
Hello(context.Context, *Request, *Response) error
|
||||
}
|
||||
|
||||
func RegisterSayHandler(s server.Server, hdlr SayHandler) {
|
||||
s.Handle(s.NewHandler(hdlr))
|
||||
}
|
||||
```
|
||||
|
||||
### Use the client
|
||||
```golang
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"context"
|
||||
"github.com/micro/go-micro/v2/client"
|
||||
hello "path/to/hello/proto"
|
||||
)
|
||||
|
||||
func main() {
|
||||
cl := hello.NewSayClient("go.micro.srv.greeter", client.DefaultClient)
|
||||
// alternative initialisation
|
||||
// cl := hello.NewSayClient("", nil)
|
||||
|
||||
rsp, err := cl.Hello(contex.Background(), &hello.Request{"Name": "John"})
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
}
|
||||
```
|
||||
79
examples/client/codegen/codegen.go
Normal file
79
examples/client/codegen/codegen.go
Normal file
@@ -0,0 +1,79 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"context"
|
||||
example "github.com/micro/examples/server/proto/example"
|
||||
"github.com/micro/go-micro/v2/config/cmd"
|
||||
)
|
||||
|
||||
var (
|
||||
cl = example.NewExampleService("go.micro.srv.example", nil)
|
||||
)
|
||||
|
||||
func call(i int) {
|
||||
rsp, err := cl.Call(context.Background(), &example.Request{Name: "John"})
|
||||
if err != nil {
|
||||
fmt.Println("call err: ", err, rsp)
|
||||
return
|
||||
}
|
||||
fmt.Println("Call:", i, "rsp:", rsp.Msg)
|
||||
}
|
||||
|
||||
func stream(i int) {
|
||||
stream, err := cl.Stream(context.Background(), &example.StreamingRequest{Count: int64(i)})
|
||||
if err != nil {
|
||||
fmt.Println("err:", err)
|
||||
return
|
||||
}
|
||||
for j := 0; j < i; j++ {
|
||||
rsp, err := stream.Recv()
|
||||
if err != nil {
|
||||
fmt.Println("err:", err)
|
||||
break
|
||||
}
|
||||
fmt.Println("Stream: rsp:", rsp.Count)
|
||||
}
|
||||
if err := stream.Close(); err != nil {
|
||||
fmt.Println("stream close err:", err)
|
||||
}
|
||||
}
|
||||
|
||||
func pingPong(i int) {
|
||||
stream, err := cl.PingPong(context.Background())
|
||||
if err != nil {
|
||||
fmt.Println("err:", err)
|
||||
return
|
||||
}
|
||||
for j := 0; j < i; j++ {
|
||||
if err := stream.Send(&example.Ping{Stroke: int64(j)}); err != nil {
|
||||
fmt.Println("err:", err)
|
||||
return
|
||||
}
|
||||
rsp, err := stream.Recv()
|
||||
if err != nil {
|
||||
fmt.Println("recv err", err)
|
||||
break
|
||||
}
|
||||
fmt.Printf("Sent ping %v got pong %v\n", j, rsp.Stroke)
|
||||
}
|
||||
if err := stream.Close(); err != nil {
|
||||
fmt.Println("stream close err:", err)
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
cmd.Init()
|
||||
|
||||
fmt.Println("\n--- Call example ---")
|
||||
for i := 0; i < 10; i++ {
|
||||
call(i)
|
||||
}
|
||||
|
||||
fmt.Println("\n--- Streamer example ---")
|
||||
stream(10)
|
||||
|
||||
fmt.Println("\n--- Ping Pong example ---")
|
||||
pingPong(10)
|
||||
}
|
||||
Reference in New Issue
Block a user