-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
executable file
·77 lines (63 loc) · 2.03 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package main
import (
"fmt"
"github.com/micro/go-micro/v2"
"github.com/micro/go-micro/v2/registry"
"github.com/micro/go-micro/v2/util/log"
"github.com/micro/go-plugins/registry/consul/v2"
"github.com/micro/go-plugins/wrapper/ratelimiter/uber/v2"
opentracingV2 "github.com/micro/go-plugins/wrapper/trace/opentracing/v2"
"github.com/opentracing/opentracing-go"
"github.com/zhhades/go-micro-user/common"
"github.com/zhhades/go-micro-user/domain/repository"
"github.com/zhhades/go-micro-user/domain/service"
"github.com/zhhades/go-micro-user/handler"
"github.com/zhhades/go-micro-user/proto/user"
"gorm.io/driver/mysql"
"gorm.io/gorm"
)
func main() {
consulConfig, err := common.GetConsulConfig("ddns.zhhades.top", 8500, "/micro/config")
if err != nil {
log.Error(err)
}
consulRegistry := consul.NewRegistry(func(options *registry.Options) {
options.Addrs = []string{
"ddns.zhhades.top:8500",
}
})
t, io, err := common.NewTracer("go.micro.service.user", "ddns.zhhades.top:6831")
if err != nil {
log.Error(err)
}
defer io.Close()
opentracing.SetGlobalTracer(t)
srv := micro.NewService(
micro.Name("go.micro.service.user"),
micro.Version("latest"),
micro.Address("127.0.0.1:8082"),
micro.Registry(consulRegistry),
micro.WrapHandler(opentracingV2.NewHandlerWrapper(opentracing.GlobalTracer())),
micro.WrapHandler(ratelimit.NewHandlerWrapper(1)),
)
srv.Init()
mysqlConfig := common.GetMysqlConfigFromConsul(consulConfig, "mysql")
dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=True&loc=Local",
mysqlConfig.User, mysqlConfig.Passwd, mysqlConfig.Host, mysqlConfig.Port, mysqlConfig.DataBase)
db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{})
if err != nil {
fmt.Println(err)
}
rp := repository.NewUserRepository(db)
err = rp.InitTable()
if err != nil {
fmt.Println(err)
}
userDataService := service.NewUserDataService(rp)
err = user.RegisterUserHandler(srv.Server(), &handler.User{
UserDataService: userDataService,
})
if err := srv.Run(); err != nil {
fmt.Println(err)
}
}