-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
72 lines (67 loc) · 2.21 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
package main
import (
"github.com/fs-platform/cart-micro-service/domain/repository"
service2 "github.com/fs-platform/cart-micro-service/domain/service"
"github.com/fs-platform/cart-micro-service/handler"
common "github.com/fs-platform/go-tool"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/mysql"
"github.com/micro/go-micro/v2"
log "github.com/micro/go-micro/v2/logger"
"github.com/micro/go-micro/v2/registry"
"github.com/micro/go-plugins/registry/consul/v2"
ratelimit "github.com/micro/go-plugins/wrapper/ratelimiter/uber/v2"
opentracing2 "github.com/micro/go-plugins/wrapper/trace/opentracing/v2"
"github.com/opentracing/opentracing-go"
"strconv"
cart "github.com/fs-platform/cart-micro-service/proto/cart"
)
func main() {
//配置中心
consulConfig, err := common.GetConsulConfig("127.0.0.1", 8500, "/micro/config")
if err != nil {
log.Error(err)
}
mysqlInfo := common.GetMysqlFromConsul(consulConfig, "mysql")
//注册中心
consulRegistry := consul.NewRegistry(func(options *registry.Options) {
options.Addrs = []string{
"127.0.0.1:8500",
}
})
//链路追踪
t, io, err := common.NewTracer("go.micro.service.cart", "localhost:6831")
if err != nil {
log.Error(err)
}
defer io.Close()
opentracing.SetGlobalTracer(t)
//数据库链接
db, err := gorm.Open("mysql", mysqlInfo.User+":"+mysqlInfo.Pwd+
"@tcp("+mysqlInfo.Host+":"+strconv.FormatInt(mysqlInfo.Port, 10)+")/"+mysqlInfo.Database+
"?charset=utf8&parseTime=True&loc=Local")
cartRepository := repository.NewCartRepository(db)
cartRepository.InitTable()
cartService := service2.NewCartDataService(cartRepository)
// New Service
service := micro.NewService(
micro.Name("go.micro.service.cart"),
micro.Version("latest"),
//微服务Ip
micro.Address("127.0.0.1:8400"),
//添加服务发现,注册中心
micro.Registry(consulRegistry),
//添加链路追踪
micro.WrapHandler(opentracing2.NewHandlerWrapper(opentracing.GlobalTracer())),
//添加限流,每秒100个请求
micro.WrapHandler(ratelimit.NewHandlerWrapper(100)),
)
// Register Handler
cart.RegisterCartHandler(service.Server(), &handler.Cart{
CartDataService: cartService,
})
// Run service
if err := service.Run(); err != nil {
log.Fatal(err)
}
}