这篇主要介绍服务注册和发现的实现
etcd的安装方式:https://etcd.io/
当部署完etcd以后会显示
etcd的默认客户端端口是2379,接下来就可以使用etcd做服务注册、服务发现
go环境:go-micro gin
目录结构
userserver/main.go
1 package main 2 3 import ( 4 "github.com/micro/go-micro/registry" 5 "github.com/micro/go-micro/registry/etcd" 6 "github.com/micro/go-micro/web" 7 "userserver/routers" 8 ) 9 10 var etcdReg registry.Registry 11 12 13 // 注册到etcd 14 func init(){ 15 etcdReg = etcd.NewRegistry( 16 registry.Addrs("127.0.0.1:2379"), 17 ) 18 } 19 20 func main(){ 21 // 初始化路由 22 router := routers.InitRouter() 23 microService := web.NewService( 24 web.Name("api.miku.com.userserver"), 25 web.Address(":10001"), 26 web.Handler(router), 27 web.Registry(etcdReg), 28 ) 29 microService.Run() 30 }
userserver/router.go
1 package routers 2 3 import ( 4 "github.com/gin-gonic/gin" 5 "net/http" 6 ) 7 8 func InitRouter()*gin.Engine{ 9 ginRouter := gin.Default() 10 ginRouter.POST("/users", func(context *gin.Context) { 11 context.JSON(http.StatusOK,gin.H{ 12 "code": 200, 13 "m g": "请求成功", 14 }) 15 }) 16 return ginRouter 17 }
orderserver/main.go 中实现了服务发现
1 package main 2 3 import ( 4 "bytes" 5 "fmt" 6 "github.com/micro/go-micro/client/selector" 7 "github.com/micro/go-micro/registry" 8 "github.com/micro/go-micro/registry/etcd" 9 "github.com/micro/go-micro/web" 10 "net/http" 11 "orderserver/router" 12 "time" 13 ) 14 15 var etcdReg registry.Registry 16 17 func init(){ 18 etcdReg = etcd.NewRegistry( 19 registry.Addrs("127.0.0.1:2379"), 20 ) 21 } 22 23 func main(){ 24 ginRouter := router.InitRouter() 25 microServce := web.NewService( 26 web.Name("api.miku.com.orderserver"), 27 web.Address(":10082"), 28 web.Handler(ginRouter), 29 web.Registry(etcdReg), 30 ) 31 // 获取服务地址 32 hostAddress := GetServiceAddr("api.miku.com.userserver") 33 if len(hostAddress) <= 0 { 34 fmt.Println("hostAddress is null") 35 }else{ 36 url := "http://" + hostAddress + "/users" 37 fmt.Println(url) 38 resp, _ := http.Post(url,"application/json;charset=utf-8",bytes.NewBuffer([]byte(""))) 39 fmt.Println(resp) 40 } 41 microServce.Run() 42 } 43 44 // 获取服务地址 45 func GetServiceAddr(serviceName string)(address string){ 46 var retryCount int 47 for { 48 servers,err := etcdReg.GetService(serviceName) 49 if err != nil{ 50 fmt.Println(err.Error()) 51 } 52 var services []*registry.Service 53 for _,value := range servers{ 54 fmt.Println(value.Name, ":", value.Version) 55 services = append(services, value) 56 } 57 next := selector.RoundRobin(services) 58 if node, err := next();err == nil{ 59 address = node.Address 60 } 61 if len(address) > 0 { 62 return 63 } 64 // 重试次数 65 retryCount ++ 66 time.Sleep(time.Second * 3) 67 if retryCount >= 5{ 68 return 69 } 70 } 71 }
1 package router 2 3 import ( 4 "github.com/gin-gonic/gin" 5 "net/http" 6 ) 7 8 func InitRouter()*gin.Engine{ 9 ginRouter := gin.Default() 10 ginRouter.POST("/orders/", func(c *gin.Context) { 11 c.String(http.StatusOK,"get orderinfo") 12 }) 13 return ginRouter 14 }
运行micro web会在8082端口打开web UI服务,在网页中会显示micro注册的服务
当运行所有的main.go文件 会看到orderserver成功调用了userserver的服务,这就实现了服务注册、服务发现功能
原作者:
技术修仙