forked from printfcoder/stack-rpc-tutorials
-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
54 lines (45 loc) · 1.3 KB
/
main.go
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package main
import (
"fmt"
"github.com/micro-in-cn/tutorials/microservice-in-micro/part1/user-web/basic"
"github.com/micro-in-cn/tutorials/microservice-in-micro/part1/user-web/basic/config"
"github.com/micro-in-cn/tutorials/microservice-in-micro/part1/user-web/handler"
"github.com/micro/cli/v2"
log "github.com/micro/go-micro/v2/logger"
"github.com/micro/go-micro/v2/registry"
"github.com/micro/go-micro/v2/registry/etcd"
"github.com/micro/go-micro/v2/web"
)
func main() {
// 初始化配置
basic.Init()
// 使用etcd注册
micReg := etcd.NewRegistry(registryOptions)
// 创建新服务
service := web.NewService(
// 后面两个web,第一个是指是web类型的服务,第二个是服务自身的名字
web.Name("mu.micro.book.web.user"),
web.Version("latest"),
web.Registry(micReg),
web.Address(":8088"),
)
// 初始化服务
if err := service.Init(
web.Action(func(c *cli.Context) {
// 初始化handler
handler.Init()
}),
); err != nil {
log.Fatal(err)
}
// 注册登录接口
service.HandleFunc("/user/login", handler.Login)
// 运行服务
if err := service.Run(); err != nil {
log.Fatal(err)
}
}
func registryOptions(ops *registry.Options) {
etcdCfg := config.GetEtcdConfig()
ops.Addrs = []string{fmt.Sprintf("%s:%d", etcdCfg.GetHost(), etcdCfg.GetPort())}
}