1
0
mirror of https://github.com/go-micro/go-micro.git synced 2025-06-18 22:17:44 +02:00
Files
go-micro/internal/website/docs/client-server.md
2025-05-21 12:03:24 +00:00

886 B

Client/Server

Go Micro uses a client/server model for RPC communication between services.

Client

The client is used to make requests to other services.

Server

The server handles incoming requests.

Both client and server are pluggable and support middleware wrappers for additional functionality.

Example Usage

Here's how to define a simple handler and register it with a Go Micro server:

package main

import (
    "context"
    "go-micro.dev/v5"
    "log"
)

type Greeter struct{}

func (g *Greeter) Hello(ctx context.Context, req *struct{}, rsp *struct{Msg string}) error {
    rsp.Msg = "Hello, world!"
    return nil
}

func main() {
    service := micro.NewService(
        micro.Name("greeter"),
    )
    service.Init()
    micro.RegisterHandler(service.Server(), new(Greeter))
    if err := service.Run(); err != nil {
        log.Fatal(err)
    }
}