欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 文旅 > 文化 > 24.4 基于consul服务发现模式

24.4 基于consul服务发现模式

2024/11/30 11:45:19 来源:https://blog.csdn.net/weixin_48502062/article/details/142821802  浏览:    关键词:24.4 基于consul服务发现模式

本节重点介绍 :

  • consul 安装
  • consul go代码注册服务,注销服务,获取服务
  • node_exporter改造为consul服务发现
  • 在数量比较大时,在注册服务的时候,关闭check,可以降低consul的压力

consul 安装

准备工作


# 下载consul
wget -O /opt/tgzs/consul_1.9.4_linux_amd64.zip  https://releases.hashicorp.com/consul/1.9.4/consul_1.9.4_linux_amd64.zip cd /opt/tgzs/
unzip consul_1.9.4_linux_amd64.zip/bin/cp -f consul /usr/bin/

启动单机版consul


# 
mkdir  /opt/app/consul# 准备配置文件
cat <<EOF > /opt/app/consul/single_server.json
{"datacenter": "dc1","node_name": "consul-svr-01","server": true,"bootstrap_expect": 1,"data_dir": "/opt/app/consul/","log_level": "INFO","log_file": "/opt/logs/","ui": true,"bind_addr": "0.0.0.0","client_addr": "0.0.0.0","retry_interval": "10s","raft_protocol": 3,"enable_debug": false,"rejoin_after_leave": true,"enable_syslog": false
}
EOF# 多个ip地址时,将bind_addr 改为一个内网的ip# 写入service文件
cat <<EOF > /etc/systemd/system/consul.service
[Unit]
Description=consul server
Wants=network-online.target
After=network-online.target[Service]
ExecStart=/usr/bin/consul agent  -config-file=/opt/app/consul/single_server.json
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=consul
[Install]
WantedBy=default.target
EOF# 启动服务
systemctl daemon-reload && systemctl start consul   systemctl status consul 

验证访问

  • http://localhost:8500/

node_exporter的job改造为consul的服务发现

编写go代码注册服务到consul

初始化consul

  • 使用包 github.com/hashicorp/consul/api
import ("fmt"consul "github.com/hashicorp/consul/api""log"
)type client struct {consul *consul.Client
}func NewConsulClient(addr string) (*client, error) {config := consul.DefaultConfig()config.Address = addrc, err := consul.NewClient(config)if err != nil {return nil, err}return &client{consul: c}, nil
}

编写注册服务方法

  • 需要指定参数为
    • 服务的名称
    • 实例地址
    • 实例端口
    • 实例探活path
    • 实例标签map
  • check.HTTP 代表使用http类型的check
  • 调用 consul.Agent().ServiceRegister(reg)注册服务
// 注册服务
func (c *client) ServiceRegister(srvName, srvHost string, srvPort int, healthyCheckPath string, metaMap map[string]string) error {reg := new(consul.AgentServiceRegistration)reg.Name = srvNamethisId := fmt.Sprintf("%s_%d", srvHost, srvPort)reg.ID = thisIdreg.Port = srvPortreg.Address = srvHostreg.Meta = metaMaplog.Printf("ServiceRegisterStart :%v", thisId)//增加checkcheck := new(consul.AgentServiceCheck)check.HTTP = fmt.Sprintf("http://%s:%d%s", reg.Address, reg.Port, healthyCheckPath)//设置超时 5s。check.Timeout = "2s"check.DeregisterCriticalServiceAfter = "5s"//设置间隔 5s。check.Interval = "5s"//注册check服务。reg.Check = checkreturn c.consul.Agent().ServiceRegister(reg)
}

编写获取服务信息的方法

  • 使用consul.Health().Service获取 passing的服务
// Service return a service
func (c *client) GetService(service, tag string) ([]*consul.ServiceEntry, error) {passingOnly := truess, _, err := c.consul.Health().Service(service, tag, passingOnly, nil)if len(ss) == 0 && err == nil {return nil, fmt.Errorf("service ( %s ) was not found", service)}return ss, err
}

编写根据服务id注销服务的方法

// 根据server id注销服务
func (c *client) DeRegister(id string) error {return c.consul.Agent().ServiceDeregister(id)
}

注册node_exporter服务

func main() {c, err := NewConsulClient("http://172.20.70.205:8500")if err != nil {log.Printf("NewConsulClient.err:%v", err)return}nodes := []string{"172.20.70.205","172.20.70.215",}nodeExporterSrv := "node_exporter"for _, h := range nodes {m := map[string]string{"region": "bj", "cloud": "huawei"}err = c.ServiceRegister(nodeExporterSrv, h, 9100, "/", m)if err != nil {log.Printf("[ServiceRegister.err][srv:%v][host:%v][err:%v]", nodeExporterSrv, h, err)} else {log.Printf("[ServiceRegister.success][srv:%v][host:%v]", nodeExporterSrv, h)}}ss, err := c.GetService(nodeExporterSrv, "")for _, s := range ss {log.Printf("[c.GetService][service_id:%v][err:%v]", s.Service.ID, err)//c.DeRegister(s.Service.ID)}}

完整的go代码

package mainimport ("fmt"consul "github.com/hashicorp/consul/api""log"
)type client struct {consul *consul.Client
}func NewConsulClient(addr string) (*client, error) {config := consul.DefaultConfig()config.Address = addrc, err := consul.NewClient(config)if err != nil {return nil, err}return &client{consul: c}, nil
}// 注册服务
func (c *client) ServiceRegister(srvName, srvHost string, srvPort int, healthyCheckPath string, metaMap map[string]string) error {reg := new(consul.AgentServiceRegistration)reg.Name = srvNamethisId := fmt.Sprintf("%s_%d", srvHost, srvPort)reg.ID = thisIdreg.Port = srvPortreg.Address = srvHostreg.Meta = metaMaplog.Printf("ServiceRegisterStart :%v", thisId)//增加checkcheck := new(consul.AgentServiceCheck)check.HTTP = fmt.Sprintf("http://%s:%d%s", reg.Address, reg.Port, healthyCheckPath)//设置超时 5s。check.Timeout = "2s"check.DeregisterCriticalServiceAfter = "5s"//设置间隔 5s。check.Interval = "5s"//注册check服务。reg.Check = checkreturn c.consul.Agent().ServiceRegister(reg)
}// 根据server id注销服务
func (c *client) DeRegister(id string) error {return c.consul.Agent().ServiceDeregister(id)
}// Service return a service
func (c *client) GetService(service, tag string) ([]*consul.ServiceEntry, error) {passingOnly := truess, _, err := c.consul.Health().Service(service, tag, passingOnly, nil)if len(ss) == 0 && err == nil {return nil, fmt.Errorf("service ( %s ) was not found", service)}return ss, err
}func main() {c, err := NewConsulClient("http://172.20.70.205:8500")if err != nil {log.Printf("NewConsulClient.err:%v", err)return}nodes := []string{"172.20.70.205","172.20.70.215",}nodeExporterSrv := "node_exporter"for _, h := range nodes {m := map[string]string{"region": "bj", "cloud": "huawei"}err = c.ServiceRegister(nodeExporterSrv, h, 9100, "/", m)if err != nil {log.Printf("[ServiceRegister.err][srv:%v][host:%v][err:%v]", nodeExporterSrv, h, err)} else {log.Printf("[ServiceRegister.success][srv:%v][host:%v]", nodeExporterSrv, h)}}ss, err := c.GetService(nodeExporterSrv, "")for _, s := range ss {log.Printf("[c.GetService][service_id:%v][err:%v]", s.Service.ID, err)//c.DeRegister(s.Service.ID)}}

注册服务的结果

注册service

image.png

注销服务

配置 node_exporter的job为consul服务发现模式

  • 配置文档
  • 配置文件
  - job_name: 'node_exporter'honor_timestamps: truescrape_interval: 15sscrape_timeout: 10smetrics_path: /metricsscheme: httpconsul_sd_configs:- server: 172.20.70.205:8500services:- node_exporterrelabel_configs:- source_labels:  ["__meta_consul_dc"]target_label: "dc"- separator: ;regex: __meta_consul_service_metadata_(.+)replacement: $1action: labelmap
  • target页面和service discovery 页面观察服务发现结果
  • image.png
  • image.png

本节重点总结 :

  • consul 安装
  • consul go代码注册服务,注销服务,获取服务
  • node_exporter改造为consul服务发现
  • 在数量比较大时,在注册服务的时候,关闭check,可以降低consul的压力

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com