mirror of
https://github.com/go-micro/go-micro.git
synced 2024-12-24 10:07:04 +02:00
40 lines
731 B
Go
40 lines
731 B
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
proto "github.com/micro/go-micro/examples/service/proto"
|
|
"github.com/micro/go-micro/v2"
|
|
"github.com/micro/go-micro/v2/metadata"
|
|
)
|
|
|
|
type Greeter struct{}
|
|
|
|
func (g *Greeter) Hello(ctx context.Context, req *proto.Request, rsp *proto.Response) error {
|
|
md, _ := metadata.FromContext(ctx)
|
|
|
|
// local ip of service
|
|
fmt.Println("local ip is", md["Local"])
|
|
|
|
// remote ip of caller
|
|
fmt.Println("remote ip is", md["Remote"])
|
|
|
|
rsp.Greeting = "Hello " + req.Name
|
|
return nil
|
|
}
|
|
|
|
func main() {
|
|
service := micro.NewService(
|
|
micro.Name("greeter"),
|
|
)
|
|
|
|
service.Init()
|
|
|
|
proto.RegisterGreeterHandler(service.Server(), new(Greeter))
|
|
|
|
if err := service.Run(); err != nil {
|
|
fmt.Println(err)
|
|
}
|
|
}
|