欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 教育 > 培训 > 24.5 基于http服务发现模式

24.5 基于http服务发现模式

2025/2/25 22:18:04 来源:https://blog.csdn.net/weixin_48502062/article/details/142851673  浏览:    关键词:24.5 基于http服务发现模式

本节重点介绍 :

  • http型的服务发现的优点
  • 使用go语言编写 http服务发现源
  • 将blackbox-http job改造为 http服务发现类型

说明

对比file_sd的优点

  • 不再依赖文件做传输。不需要confd或者ansible copy file的机制
  • 直接在服务发现源(CMDB)启动一个接口
  • 返回 json的target数据
[{"targets": [ "<host>", ... ],"labels": {"<labelname>": "<labelvalue>", ...}},...
]

文档地址

  • http_sd_configs配置文档

编写go的http发现源

使用gin启动web

package mainimport ("flag""github.com/gin-gonic/gin""math/rand"
)func main() {listenAddress := flag.String("addr", ":8001","Address on which to expose metrics and web interface.")flag.Parse()r := gin.Default()r.GET("/prome_http_sd", httpSd)r.Run(*listenAddress) // listen and serve on 0.0.0.0:8080 (for windows "localhost:8080")
}

编写target数据结构

type target struct {Targets []string          `json:"targets"`Labels  map[string]string `json:"labels"`
}

编写 httpSd 处理函数

  • frn返回一个最大值为n的随机整数
  • randMapKeys 作为随机标签的key
  • randMapValues 作为随机标签的value
  • 遍历nodes切片mock target数据
  • 返回targets json数据
func httpSd(c *gin.Context) {nodes := []string{"172.20.70.205:9115","http://prometheus.io","http://www.baidu.com","https://www.baidu.com","https://github.com/",}randMapKeys := []string{"arch", "idc", "os", "job"}randMapValues := []string{"linux", "beijing", "centos", "arm64"}frn := func(n int) int {return rand.Intn(n)}targets := make([]target, 0)for _, n := range nodes {num := len(randMapKeys)m := make(map[string]string, num)for i := 0; i < num; i++ {m[randMapKeys[frn(len(randMapKeys)-1)]] = randMapValues[frn(len(randMapValues)-1)]}t := target{Targets: []string{n},Labels:  m,}targets = append(targets, t)}c.JSON(200, targets)
}

完整go代码

package mainimport ("flag""github.com/gin-gonic/gin""math/rand"
)func main() {listenAddress := flag.String("addr", ":8001","Address on which to expose metrics and web interface.")flag.Parse()r := gin.Default()r.GET("/prome_http_sd", httpSd)r.Run(*listenAddress) // listen and serve on 0.0.0.0:8080 (for windows "localhost:8080")
}type target struct {Targets []string          `json:"targets"`Labels  map[string]string `json:"labels"`
}func httpSd(c *gin.Context) {nodes := []string{"172.20.70.205:9115","http://prometheus.io","http://www.baidu.com","https://www.baidu.com","https://github.com/",}randMapKeys := []string{"arch", "idc", "os", "job"}randMapValues := []string{"linux", "beijing", "centos", "arm64"}frn := func(n int) int {return rand.Intn(n)}targets := make([]target, 0)for _, n := range nodes {num := len(randMapKeys)m := make(map[string]string, num)for i := 0; i < num; i++ {m[randMapKeys[frn(len(randMapKeys)-1)]] = randMapValues[frn(len(randMapValues)-1)]}t := target{Targets: []string{n},Labels:  m,}targets = append(targets, t)}c.JSON(200, targets)
}

请求接口看返回

[root@k8s-master01 ~]#  curl -s http://localhost:8001/prome_http_sd |python -m json.tool
[{"labels": {"arch": "linux","idc": "centos","os": "centos"},"targets": ["172.20.70.205:9115"]},{"labels": {"arch": "beijing","os": "beijing"},"targets": ["http://prometheus.io"]},{"labels": {"arch": "centos","os": "centos"},"targets": ["http://www.baidu.com"]},{"labels": {"arch": "beijing","idc": "beijing","os": "linux"},"targets": ["https://www.baidu.com"]},{"labels": {"arch": "beijing","idc": "linux"},"targets": ["https://github.com/"]}
]

将blackbox-http job改造为 http服务发现类型

修改prometheus配置文件

  • 传入http_sd_configs 的url
  • 其余relabel配置不变
  - job_name: 'blackbox-http-sd'metrics_path: /probe# 传入的参数params:module: [http_2xx]  # Look for a HTTP 200 response.scrape_interval: 15sscrape_timeout: 10sscheme: httphonor_timestamps: falsehttp_sd_configs:- url: http://172.20.70.205:8001/prome_http_sd relabel_configs:- source_labels: [__address__]target_label: __param_target- source_labels: [__param_target]target_label: instance- target_label: __address__replacement: 172.20.70.205:9115 

页面观察结果

  • target页面
  • image.png
  • discovery页面
  • image.png
  • http发现源侧看到的prometheus请求
  • image.png

本节重点总结 :

  • http型的服务发现的优点
  • 使用go语言编写 http服务发现源
  • 将blackbox-http job改造为 http服务发现类型

版权声明:

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

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

热搜词