Skip to main content

consul 的 安装

下载

docker pull consul

运行

docker run -d --name=cs -p 8500:8500 consul agent -server -bootstrap -ui -client 0.0.0.0

访问 http://localhost:8500/

go 实操

高版本已经不支持 consul

go get -u -v github.com/micro/go-micro@v1.8.0

启动服务发现的例子

/**
* Created by GoLand.
* User: 清行
* Contact: 66500852@qq.com
* Date: 2020/7/24
* Time: 22:02
*/
package main

import (
"github.com/gin-gonic/gin"
"github.com/micro/go-micro/registry"
"github.com/micro/go-micro/registry/consul"
"github.com/micro/go-micro/web"
)

func main() {
consulReg := consul.NewRegistry(
registry.Addrs("192.168.3.9:8500"),
)
ginRoute:=gin.Default()
ginRoute.Handle("GET","/user", func(context *gin.Context) {
context.String(200,"user api")
})


server := web.NewService(
web.Name("asdasd"),
web.Address(":8001"),
web.Handler(ginRoute),
web.Registry(consulReg),
)
server.Run()

}