forked from printfcoder/stack-rpc-tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.go
More file actions
39 lines (31 loc) · 849 Bytes
/
server.go
File metadata and controls
39 lines (31 loc) · 849 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package main
import (
"context"
"fmt"
"time"
proto "github.com/micro-in-cn/tutorials/examples/basic-practices/micro-service/proto"
"github.com/micro/go-micro"
)
type Greeter struct{}
func (g *Greeter) Hello(ctx context.Context, req *proto.HelloRequest, rsp *proto.HelloResponse) error {
time.Sleep(time.Second * 9)
rsp.Greeting = "你好," + req.Name
return nil
}
func main() {
// 创建服务,除了服务名,其它选项可加可不加,比如Version版本号、Metadata元数据等
service := micro.NewService(
micro.Name("timeout.service"),
micro.Version("latest"),
micro.Metadata(map[string]string{
"type": "你好,世界",
}),
)
service.Init()
// 注册服务
proto.RegisterGreeterHandler(service.Server(), new(Greeter))
// 启动服务
if err := service.Run(); err != nil {
fmt.Println(err)
}
}