go-micro.dev/v5@v5.12.0/internal/website/docs/getting-started.md (about)

     1  ---
     2  layout: default
     3  ---
     4  
     5  # Getting Started
     6  
     7  To make use of Go Micro 
     8  
     9  ```bash
    10  go get go-micro.dev/v5@latest
    11  ```
    12  
    13  ## Create a service
    14  
    15  This is a basic example of how you'd create a service and register a handler in pure Go.
    16  
    17  ```bash
    18  mkdir helloworld
    19  cd helloworld
    20  go mod init
    21  go get go-micro.dev/v5@latest
    22  ```
    23  
    24  Write the following into `main.go`
    25  
    26  ```go
    27  package main
    28  
    29  import (
    30          "go-micro.dev/v5"
    31  )
    32  
    33  type Request struct {
    34          Name string `json:"name"`
    35  }
    36  
    37  type Response struct {
    38          Message string `json:"message"`
    39  }
    40  
    41  type Say struct{}
    42  
    43  func (h *Say) Hello(ctx context.Context, req *Request, rsp *Response) error {
    44          rsp.Message = "Hello " + req.Name
    45          return nil
    46  }
    47  
    48  func main() {
    49          // create the service
    50          service := micro.New("helloworld")
    51  
    52          // initialise service
    53          service.Init()
    54  
    55          // register handler
    56          service.Handle(new(Say))
    57  
    58          // run the service
    59          service.Run()
    60  }
    61  ```
    62  
    63  Now run the service
    64  
    65  ```bash
    66  go run main.go
    67  ```
    68  
    69  Take a note of the address with the log line
    70  
    71  ```text
    72  Transport [http] Listening on [::]:35823
    73  ```
    74  
    75  Now you can call the service
    76  
    77  ```bash
    78  curl -XPOST \
    79       -H 'Content-Type: application/json' \
    80       -H 'Micro-Endpoint: Say.Hello' \
    81       -d '{"name": "alice"}' \
    82        http://localhost:35823
    83  ```
    84  
    85  ## Set a fixed address
    86  
    87  To set a fixed address by specifying it as an option to service, note the change from `New` to `NewService`
    88  
    89  ```go
    90  service := micro.NewService(
    91      micro.Name("helloworld"),
    92      micro.Address(":8080"),
    93  )
    94  ```
    95  
    96  Alternatively use `MICRO_SERVER_ADDRESS=:8080` as an env var
    97  
    98  ```bash
    99  curl -XPOST \
   100       -H 'Content-Type: application/json' \
   101       -H 'Micro-Endpoint: Say.Hello' \
   102       -d '{"name": "alice"}' \
   103        http://localhost:8080
   104  ```
   105  
   106  ## Protobuf
   107  
   108  If you want to define services with protobuf you can use protoc-gen-micro (go-micro.dev/v5/cmd/protoc-gen-micro).
   109  
   110  Install the generator:
   111  
   112  ```bash
   113  go install go-micro.dev/v5/cmd/protoc-gen-micro@latest
   114  ```
   115  
   116  ```bash
   117  cd helloworld
   118  mkdir proto
   119  ```
   120  
   121  Edit a file `proto/helloworld.proto`
   122  
   123  ```proto
   124  syntax = "proto3";
   125  
   126  package greeter;
   127  option go_package = "/proto;helloworld";
   128  
   129  service Say {
   130          rpc Hello(Request) returns (Response) {}
   131  }
   132  
   133  message Request {
   134          string name = 1;
   135  }
   136  
   137  message Response {
   138          string message = 1;
   139  }
   140  ```
   141  
   142  You can now generate a client/server like so (ensure `$GOBIN` is on your `$PATH` so `protoc` can find `protoc-gen-micro`):
   143  
   144  ```bash
   145  protoc --proto_path=. --micro_out=. --go_out=. helloworld.proto
   146  ```
   147  
   148  In your `main.go` update the code to reference the generated code
   149  
   150  ```go
   151  package main
   152  
   153  import (
   154          "go-micro.dev/v5"
   155  
   156          pb "github.com/micro/helloworld/proto"
   157  )
   158  
   159  type Say struct{}
   160  
   161  func (h *Say) Hello(ctx context.Context, req *pb.Request, rsp *pb.Response) error {
   162          rsp.Message = "Hello " + req.Name
   163          return nil
   164  }
   165  
   166  func main() {
   167          // create the service
   168          service := micro.New("helloworld")
   169  
   170          // initialise service
   171          service.Init()
   172  
   173          // register handler
   174          pb.RegisterSayHandler(service.Server(), &Say{})
   175  
   176          // run the service
   177          service.Run()
   178  }
   179  ```
   180  
   181  Now I can run this again
   182  
   183  ```bash
   184  go run main.go
   185  ```
   186  
   187  ## Call via a client
   188  
   189  The generated code provides us a client
   190  
   191  ```go
   192  package main
   193  
   194  import (
   195          "context"
   196          "fmt"
   197  
   198          "go-micro.dev/v5"
   199          pb "github.com/micro/helloworld/proto"
   200  )
   201  
   202  func main() {
   203          service := micro.New("helloworld")
   204          service.Init()
   205  
   206          say := pb.NewSayService("helloworld", service.Client())
   207  
   208          rsp, err := say.Hello(context.TODO(), &pb.Request{
   209              Name: "John",
   210          })
   211          if err != nil {
   212                  fmt.Println(err)
   213                  return
   214          }
   215  
   216          fmt.Println(rsp.Message)
   217  }
   218  ```
   219  
   220  ## Command line
   221  
   222  Install the Micro CLI:
   223  
   224  ```
   225  go install go-micro.dev/v5/cmd/micro@latest
   226  ```
   227  
   228  Call a running service via RPC:
   229  
   230  ```
   231  micro call helloworld Say.Hello '{"name": "John"}'
   232  ```
   233  
   234  Alternative using the dynamic CLI commands:
   235  
   236  ```
   237  micro helloworld say hello --name="John"
   238  ```