package main
import (
micro2 "account/common/micro"
"account/config/logger"
"account/config/mysql"
"account/config/redis"
"account/domain/repository"
"account/domain/service"
"account/handler"
"account/proto/account"
"fmt"
_ "github.com/jinzhu/gorm/dialects/mysql"
"github.com/micro/go-micro/v2"
"github.com/micro/go-micro/v2/registry"
"github.com/micro/go-plugins/registry/consul/v2"
"github.com/micro/go-plugins/wrapper/monitoring/prometheus/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"
"go.uber.org/zap"
)
func main() {
// 1.設定中心
consulConfig, err := micro2.GetConsulConfig("localhost", 8500, "/micro/config")
if err != nil {
fmt.Printf("Init consulConfig failed, err: %v\n", err)
}
// 2.註冊中心
consulRegistry := consul.NewRegistry(func(options *registry.Options) {
options.Addrs = []string{
"127.0.0.1:8500",
}
})
if err := micro2.GetAccountFromConsul(consulConfig, "account"); err != nil {
fmt.Printf("Init consul failed, err: %v\n", err)
}
fmt.Println(micro2.ConsulInfo)
// 3.zap紀錄檔初始化
if err := logger.Init(); err != nil {
fmt.Printf("Init logger failed, err: %v\n", err)
return
}
defer zap.L().Sync()
// 4.jaeger 鏈路追蹤
t, io, err := micro2.NewTracer(micro2.ConsulInfo.Jaeger.ServiceName, micro2.ConsulInfo.Jaeger.Addr)
if err != nil {
logger.Error(err)
return
}
defer io.Close()
opentracing.SetGlobalTracer(t)
// 5.初始化資料庫
db, err := mysql.MysqlInit(micro2.ConsulInfo.Mysql.User, micro2.ConsulInfo.Mysql.Pwd, micro2.ConsulInfo.Mysql.Database)
if err != nil {
logger.Error(err)
return
}
defer db.Close()
// 建立範例
accountService := service.NewUserService(repository.NewUserRepository(db))
// 6.初始化Redis連線
if err := redis.Init(); err != nil {
logger.Error(err)
return
}
defer redis.Close()
// 7.暴露監控地址
micro2.PrometheusBoot(micro2.ConsulInfo.Prometheus.Host, int(micro2.ConsulInfo.Prometheus.Port))
// 8.註冊服務
registryService := micro.NewService(
micro.Name(micro2.ConsulInfo.Micro.Name),
micro.Version(micro2.ConsulInfo.Micro.Version),
//暴露的服務地址
micro.Address(micro2.ConsulInfo.Micro.Address),
//新增consul 註冊中心
micro.Registry(consulRegistry),
//新增鏈路追蹤
micro.WrapHandler(opentracing2.NewHandlerWrapper(opentracing.GlobalTracer())),
//新增限流
micro.WrapHandler(ratelimit.NewHandlerWrapper(int(micro2.ConsulInfo.Ratelimit.QPS))),
//新增監控
micro.WrapHandler(prometheus.NewHandlerWrapper()),
)
// 9.初始化服務
registryService.Init()
// 10.註冊Handle
account.RegisterAccountHandler(registryService.Server(), &handler.Account{AccountService: accountService})
// 11.啟動服務
if err := registryService.Run(); err != nil {
logger.Fatal(err)
}
}
package main
import (
micro2 "account/common/micro"
"account/config/logger"
"account/proto/account"
"context"
"fmt"
"github.com/micro/go-micro/v2"
"github.com/micro/go-micro/v2/registry"
"github.com/micro/go-plugins/registry/consul/v2"
"github.com/micro/go-plugins/wrapper/select/roundrobin/v2"
opentracing2 "github.com/micro/go-plugins/wrapper/trace/opentracing/v2"
"github.com/opentracing/opentracing-go"
"go.uber.org/zap"
)
func main() {
// 1.設定中心
consulConfig, err := micro2.GetConsulConfig("localhost", 8500, "/micro/config")
if err != nil {
fmt.Printf("Init consulConfig failed, err: %v\n", err)
}
// 2.註冊中心
newConsul := consul.NewRegistry(func(options *registry.Options) {
options.Addrs = []string{
"127.0.0.1:8500",
}
})
if err := micro2.GetAccountFromConsul(consulConfig, "account"); err != nil {
fmt.Printf("Init consul failed, err: %v\n", err)
}
fmt.Println(micro2.ConsulInfo)
// 3.zap紀錄檔初始化
if err := logger.Init(); err != nil {
fmt.Printf("Init logger failed, err: %v\n", err)
return
}
defer zap.L().Sync()
logger.Debug("logger init success...")
// 4.鏈路追蹤
t, io, err := micro2.NewTracer("go.micro.service.account", "localhost:6831")
if err != nil {
logger.Fatal(err)
}
defer io.Close()
opentracing.SetGlobalTracer(t)
// 5.監控
micro2.PrometheusBoot("127.0.0.1", 9292)
// 6.設定服務
service := micro.NewService(
micro.Name("go.micro.service.account.client"),
micro.Version("latest"),
//暴露的服務地址
micro.Address("127.0.0.1:9580"),
//新增註冊中心
micro.Registry(newConsul),
//繫結鏈路追蹤
micro.WrapClient(opentracing2.NewClientWrapper(opentracing.GlobalTracer())),
//新增監控
micro.WrapHandler(opentracing2.NewHandlerWrapper(opentracing.GlobalTracer())),
//作為伺服器端存取時生效
micro.WrapClient(opentracing2.NewClientWrapper(opentracing.GlobalTracer())),
//負載均衡
micro.WrapClient(roundrobin.NewClientWrapper()),
)
// 7.建立服務
accountService := account.NewAccountService("go.micro.service.account", service.Client())
// 8.傳送註冊郵件
registerMail := &account.SendMailRequest{Email: "[email protected]"}
registerMailResponse, err := accountService.SendRegisterMail(context.TODO(), registerMail)
if err != nil {
logger.Error(err)
}
fmt.Println(registerMailResponse)
// 9.實現註冊功能
accountAdd := &account.RegisterRequest{
RegisterRequest: &account.UserInfoResponse{
Username: "夏沫の夢",
FirstName: "qi66",
Password: "123456",
Email: "[email protected]",
LastName: "admin",
},
Code: registerMailResponse.Code,
}
registerResponse, err := accountService.Register(context.TODO(), accountAdd)
if err != nil {
logger.Error(err)
}
fmt.Println(registerResponse)
// 10.查詢使用者功能
getUser := &account.UserIdRequest{UserId: registerResponse.UserId}
userInfoResponse, err := accountService.GetUserInfo(context.TODO(), getUser)
if err != nil {
logger.Error(err)
}
fmt.Println(userInfoResponse)
執行專案之前,要把相關服務啟動起來,如consul、jaeger、prometheus、grafana、ELK等等
把郵箱改成你自己的哦
consul啟動之後,記得把設定資料建立一下
第一次啟動,記得把mysql資料表初始化一下,之後要把初始化語句去掉,否則會報錯
執行main.go檔案
go run main.go
go run client/account.go
至此,go-micro微服務專案使用者端開發(使用負載均衡)使用工作就正式完成。
接下來就開始專案經驗總結了,這是本專欄的最後一篇文章,希望大家關注博主和關注專欄,第一時間獲取最新內容,每篇部落格都乾貨滿滿。