golang promethus consul 服务发现
Posted Time-Traveler
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了golang promethus consul 服务发现相关的知识,希望对你有一定的参考价值。
软件安装:
所需组件为: consul, promethus, grafana consul, grafana全部装在了本地, promethus运行在docker里面docker pull consul
docker pull prom/prometheus
docker pull grafana/grafana
brew方式:
brew install grafana # grafana
brew tap hashicorp/tap # consul
brew install hashicorp/tap/consul # consul
brew install prometheus # prometheus 安装有点问题, 作者采用了docker方式
启动grafana:
brew services start grafana
访问:http://127.0.0.1:3000/
登录的时候默认用户名密码均为:admin
启动promethus:
docker run -d -p 8091:9090 --name=prometheus_server prom/prometheus # 主机9090被占用, 临时换成了8091
成功后访问http://127.0.0.1:8091/classic/graph
consul 前台启动:
consul agent -dev
这种方式只需要 ctrl + c 即可关闭 consul
正常启动:
consul agent -server -ui -bootstrap-expect=1 -data-dir=/Users/xxxxxx/consul_log -node=server-1 -client=0.0.0.0 -bind=192.168.3.241 -datacenter=dc1
启动成功后访问8500端口会出现如下界面:
自此所有组件启动完成(失败的情况请大家自行Google)
Promethus 配置pull方式
docker exec -it eb6c1878a8dc /bin/sh # eb6c1878a8dc 为promethus容器的id
vi /etc/prometheus/prometheus.yml
添加服务发现相关配置:
scrape_configs:
# - job_name: "prometheus"
# static_configs:
# - targets: ["192.168.3.241:22200","192.168.3.241:22201"]
- job_name: 'prometheus_monitor'
consul_sd_configs:
- server: '192.168.3.241:8500'
services: [prometheus_monitor]
static_configs中的targets为固定拉取部分,在容器化的今天,pod扩展后都需要手动修改很麻烦,采用服务发现的方式来发现节点才是理想的解决方式。services 是程序注册的服务名称,也支持正则的方式,具体请大家自行Google一下。
改完后的配置如下:
请确保server是可以访问到的地址,包括局域网或公网能否访问consul地址
客户端代码:
package main
import (
"flag"
"fmt"
"github.com/go-basic/uuid"
consulapi "github.com/hashicorp/consul/api"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/prometheus/client_golang/prometheus/promhttp"
"net"
"net/http"
"time"
)
var (
commandPort int
instanceId string // 实例Id
opsProcessed prometheus.Counter
)
const (
consulAddress = "127.0.0.1:8500"
)
func RecordMetrics()
for
opsProcessed.Inc()
time.Sleep(5 * time.Second)
func GetLocalIP() string
addressList, err := net.InterfaceAddrs()
if err != nil
return ""
for _, address := range addressList
if ipNet, ok := address.(*net.IPNet); ok && !ipNet.IP.IsLoopback()
if ipNet.IP.To4() != nil
return ipNet.IP.String()
return ""
func main()
flag.IntVar(&commandPort, "command_port", 22200, "command port")
flag.Parse()
fmt.Println("command_port: ", commandPort)
RegisterService()
opsProcessed = promauto.NewCounter(prometheus.CounterOpts
Name: "myapp_processed_ops_total",
Help: "The total number of processed events",
ConstLabels: map[string]string
"instance_id": instanceId,
,
)
go RecordMetrics()
http.HandleFunc("/", Handler)
http.Handle("/metrics", promhttp.Handler())
err := http.ListenAndServe(fmt.Sprintf(":%d", commandPort), nil)
if err != nil
fmt.Println("error: ", err.Error())
func RegisterService()
// 创建连接consul服务配置
config := consulapi.DefaultConfig()
config.Address = consulAddress
client, err := consulapi.NewClient(config)
if err != nil
fmt.Println("consul client error : ", err)
return
// 创建注册到consul的服务到
id := uuid.New()
instanceId = id
registration := new(consulapi.AgentServiceRegistration)
registration.ID = id
registration.Name = "prometheus_monitor" //根据这个名称来找这个服务
registration.Port = commandPort
registration.Address = GetLocalIP()
// 增加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("pong"))
启动程序:
go run main.go
go run main.go -command_port 22202
consul结果如下:
promethus结果如下:
grafana添加数据源:
选择add data source后选择promethus
填写promethus server的地址,底部有个save and test
当出现下面这种情况的时候说明数据源可用,不可用的话请检查consul, 检查promethus是否发现节点,以及服务端的 http://xxxxxxx:xxxx/metrics的地址是否能访问且有数据,正常情况如下:
选中dashboard, 添加一个空的panel
底部方框输入: myapp_processed_ops_total, 这是我们在代码里面添加的指标名称
最终显示结果如下
到此整个流程完成, 在代码里面动态注册服务到consul,promethus server通过consul拿到所有注册组件的地址,然后主动去拉取数据,最后grafana通过进行展示。
以上是关于golang promethus consul 服务发现的主要内容,如果未能解决你的问题,请参考以下文章