php是最好的语言

go 使用consul 做服务注册与发现

先去安装consul

docker pull consul

#配置consul集群 可以在consul的ui界面上看见对应的信息

docker run -d  -p 8500:8500/tcp --name consul_server_1  consul agent -server -node=1 -ui -bootstrap -client=0.0.0.0


docker run -d  --name consul_server_2  consul agent -server -node=2 -join='172.17.0.2' -client=0.0.0.0


docker run -d  --name consul_server_3  consul agent -server -node=3 -join='172.17.0.2' -client=0.0.0.0


docker run -d  --name consul_server_4  consul agent -client -node=4 -join='172.17.0.2'  -client=0.0.0.0


#或者通过配置文件操作


docker run -d   -p 8500:8500/tcp -p 53:53/tcp -v C:\Users\readline\Desktop\xutao\golang\rpc\consul.d:/usr/local/consul/consul.d --name consul_server_1  consul agent -server -node=1 -ui -bootstrap -client=0.0.0.0 -dns-port=53 -config-dir /usr/local/consul/consul.d


docker run -d  -v C:\Users\readline\Desktop\xutao\golang\rpc\consul.d:/usr/local/consul/consul.d  --name consul_server_2  consul agent -server -node=2 -join='172.17.0.3' -client=0.0.0.0 -config-dir /usr/local/consul/consul.d



参考(https://blog.csdn.net/u013536232/article/details/104235282)

一、代码注册

package main

import (
   "fmt"
   consulapi "github.com/hashicorp/consul/api"
   "net/http"
)

const (
   consulAddress = "127.0.0.1:8500"
   localIp       = "127.0.0.1"
   localPort     = 81
)

func consulRegister()  {
   // 创建连接consul服务配置
   config := consulapi.DefaultConfig()
   config.Address = consulAddress
   client, err := consulapi.NewClient(config)
   if err != nil {
      fmt.Println("consul client error : ", err)
   }

   // 创建注册到consul的服务到
   registration := new(consulapi.AgentServiceRegistration)
   registration.ID = "337"
   registration.Name = "service337"
   registration.Port = localPort
   registration.Tags = []string{"testService"}
   registration.Address = localIp

   // 增加consul健康检查回调函数
   check := new(consulapi.AgentServiceCheck)
   check.HTTP = fmt.Sprintf("http://%s:%d", registration.Address, registration.Port)
   check.Timeout = "5s"
   check.Interval = "5s"
   check.DeregisterCriticalServiceAfter = "30s" // 故障检查失败30s后 consul自动将注册服务删除
   registration.Check = check

   // 注册服务到consul
   err = client.Agent().ServiceRegister(registration)
}

func Handler(w http.ResponseWriter, r *http.Request) {
   w.Write([]byte("you are visiting health check api"))
}

func main()  {
   //注册服务
   consulRegister()
   //定义一个http接口
   http.HandleFunc("/", Handler)
   err := http.ListenAndServe("0.0.0.0:81", nil)
   if err != nil {
      fmt.Println("error: ", err.Error())
   }
}

二、配置文件注册(参考 http://www.36nu.com/post/260)

{
	"service": {
		"id": "337",
		"name": "service337",
		"address": "192.168.1.112",
		"tags": [
			"webapi"
		],
		"port": 81,
		"checks": [{
			"http": "http://192.168.1.112:81/",
			"interval": "5s"
		}]
	}
}


三、发现服务

package main

import (
   "fmt"
   consulapi "github.com/hashicorp/consul/api"
)

const (
   consulAgentAddress = "127.0.0.1:8500"
)

// 从consul中发现服务
func ConsulFindServer()  {
   // 创建连接consul服务配置
   config := consulapi.DefaultConfig()
   config.Address = consulAgentAddress
   client, err := consulapi.NewClient(config)
   if err != nil {
      fmt.Println("consul client error : ", err)
   }

   // 获取指定service
   service, _, err := client.Agent().Service("337", nil)
   if err == nil{
      fmt.Println(service.Address)
      fmt.Println(service.Port)
   }

   //只获取健康的service
   //serviceHealthy, _, err := client.Health().Service("service337", "", true, nil)
   //if err == nil{
   // fmt.Println(serviceHealthy[0].Service.Address)
   //}

}

func main()  {
   ConsulFindServer()
}


作者:xTao 分类:LNMP 浏览:2238 评论:0