代码拉取完成,页面将自动刷新
package main
import (
"category/common"
"category/domain/repository"
service2 "category/domain/service"
"category/handler"
category "category/proto/category"
"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"
)
func main() {
//配置中心
consulConfig,err := common.GetConsulConfig("127.0.0.1",8500,"/micro/config")
if err != nil{
log.Error(err)
}
//注册中心
consulRegistry := consul.NewRegistry(func(options *registry.Options) {
options.Addrs = []string{
"127.0.0.1:8500",
}
})
// New Service
service := micro.NewService(
micro.Name("go.micro.service.category"),
micro.Version("latest"),
//设置地址和需要暴露的端口
micro.Address("127.0.0.1:8082"),
//添加consul作为注册中心
micro.Registry(consulRegistry),
)
//获取mysql配置,路径中不带前缀
mysqlInfo := common.GetMysqlFromConsul(consulConfig,"mysql")
//连接数据库
db,err := gorm.Open("mysql",mysqlInfo.User+":"+mysqlInfo.Pwd+"@/"+mysqlInfo.Database+"?charset=utf8&parseTime=True&loc=Local")
if err !=nil {
log.Error(err)
}
defer db.Close()
//禁止复表
db.SingularTable(true)
// Initialise service
service.Init()
//数据表初始化,不能重复执行
rp := repository.NewCategoryRepository(db)
_ = rp.InitTable()
categoryDataService := service2.NewCategoryDataService(repository.NewCategoryRepository(db))
err = category.RegisterCategoryHandler(service.Server(),
&handler.Category{CategoryDataService:categoryDataService})
if err != nil{
log.Error(err)
}
// Run service
if err := service.Run(); err != nil {
log.Fatal(err)
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。