当前位置: 首页 > news >正文

凯里网站设计公司哪家好深圳百度推广

凯里网站设计公司哪家好,深圳百度推广,广州有什么好玩的地方 排行榜,vi设计包含什么go-es模块统计日志中接口被刷数和ip访问来源 以下是使用go的web框架gin作为后端,展示的统计页面 背景 上面的数据来自elk日志统计。因为elk通过kibana进行展示,但是kibana有一定学习成本且不太能满足定制化的需求,所以考虑用编程的方式…

go-es模块统计日志中接口被刷数和ip访问来源

  • 以下是使用go的web框架gin作为后端,展示的统计页面
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述

背景

  • 上面的数据来自elk日志统计。因为elk通过kibana进行展示,但是kibana有一定学习成本且不太能满足定制化的需求,所以考虑用编程的方式对数据进行处理
  • 首先是接口统计,kibana的页面只会在 字段uri 的 top500 进行百分比统计,展示前5条数据,统计不够充分
    在这里插入图片描述
  • 其次是网关日志,ip来源的采集字段是通过x_forward_for,这记录了各级的代理来源ip。并不能直接对用户的ip进行数据聚合的统计
    • 举例,这里面 “223.104.195.51,192.168.29.135” ,这种数据我需要拿到223.104.195.51,因为这才是用户的ip。所以需要进行编程的处理
      在这里插入图片描述

环境

  • elk 7.9
https://www.elastic.co/downloads/past-releases/elasticsearch-7-9-3
  • go 1.17 ,gin 1.6.3,go-elasticsearch 7.9.0
# go1.17下载地址
https://go.dev/dl/
# 模块下载
go env -w GOPROXY=https://goproxy.cn,direct
go mod init go-ops  # 本项目的go mod 名字
go get github.com/elastic/go-elasticsearch/v7@v7.9.0
go get github.com/gin-gonic/gin@v1.6.3
  • 前端:layui 和 echarts
# layui下载
http://layui.dotnetcms.cn/res/static/download/layui/layui-v2.6.8.zip?v=1
# layui框架代码
http://layui.dotnetcms.cn/web/demo/admin.html
# layui数据表格
http://layui.dotnetcms.cn/web/demo/table.html
# echarts下载(需魔法)
https://cdn.jsdelivr.net/npm/echarts@5.4.3/dist/echarts.min.js
# echarts直方图
https://echarts.apache.org/handbook/zh/get-started/
# echarts 饼图
https://echarts.apache.org/handbook/zh/how-to/chart-types/pie/basic-pie/
  • 后端
# gin静态文件服务(导入js、css、图片用的)
https://learnku.com/docs/gin-gonic/1.7/examples-serving-static-files/11402
# gin模板引擎(前后端不分离,后端数据渲染前端)
https://learnku.com/docs/gin-gonic/1.7/examples-html-rendering/11363
# gin绑定Uri(动态获取二级路由)
https://learnku.com/docs/gin-gonic/1.7/examples-bind-uri/11391

go-elasticsearch 模块

  • 顾名思义此模块作用是充当es的客户端往es索引中读取数据,其原理和kibana上的dev tools一样,都是对es的restful api调用
# 以下是go-elasticsearch 的增删查改文档
https://www.elastic.co/guide/en/elasticsearch/client/go-api/current/getting-started-go.html

eql

  • 实现统计数据分析的核心就是eql(es查询语言),通过go-elasticsearch模块进行eql的发送,再接收es返回的回复体
    在这里插入图片描述
  • 以下是使用go-es发送eql后,es的回复体的struct源码
    在这里插入图片描述
  • 可以看到type Response struct 中,我们想要的json数据在Body中,但是注意Body的类型为 io.ReadCloser , 因此是需要用go的io模块进行获取json数据
    • 这边解决读取问题的代码如下。该函数接收es响应体,并返回未序列化的byte切片
// 处理es的响应,获取响应体里的Body
func getResponseBody(result *esapi.Response, context *gin.Context) []byte {// 接收es回复体里返回的数据,这里返回io流,需要用对应方法接收var bodyBytes []bytebodyBytes, err := io.ReadAll(result.Body)if err != nil {panic(err)}return bodyBytes
}

es客户端建立连接

  • 文档地址
https://www.elastic.co/guide/en/elasticsearch/client/go-api/current/connecting.html
  • 这边给的是https连接 + 用户名密码认证 + 不受信任证书的解决方法
package esinitimport ("github.com/elastic/go-elasticsearch/v7""io/ioutil""log"
)var EsClient *elasticsearch.Clientfunc init() {EsClient = newEsClient()
}func newEsClient() *elasticsearch.Client {cert, certErr := ioutil.ReadFile("esinit/es.crt")  // 你的不受信任的https证书if certErr != nil {log.Println(certErr)}EsClient, error := elasticsearch.NewClient(elasticsearch.Config{Username: "你的用户名",Password: "你的密码",Addresses: []string{"https://es-cluster1:9200","https://es-cluster2:9200","https://es-cluster3:9200",},CACert: cert,})if error != nil {panic(error)}return EsClient
}

实现统计的三段eql

  • 这里eql使用 fmt.Sprintf() 方法进行参数传递
  • 第一段eql是统计PV
    • 这里注意的是,我们在东八区,所以统计pv从16:00开始。这里根据@timestamp字段进行“aggs”聚合统计
func getPvResponse(startYear int, startMonth int, startDay int, endYear, endMonth int, endDay int) *esapi.Response {query := fmt.Sprintf(`
{"query": {"bool": {"must": [{"range": {"@timestamp": {"gte": "%d-%02d-%02dT16:00:00","lte": "%d-%02d-%02dT16:00:00"}}}]}},"aggs": {"log_count": {"value_count": {"field": "@timestamp"}}}
}
`, startYear, startMonth, startDay, endYear, endMonth, endDay)result, _ := esinit.EsClient.Search(esinit.EsClient.Search.WithIndex("k8s-istio-ingress*"), // 索引名esinit.EsClient.Search.WithBody(strings.NewReader(query)), // eql)return result
}
  • 第二段是对微服务(java)的接口(uri字段)的聚合统计
    • 这里用的 sortUri 是gin的绑定uri功能(动态获取二级路由的名字)
    • 这里返回前1天10000条es文档的uri字段数据
func getSortResponse(context *gin.Context) *esapi.Response {if err := context.ShouldBindUri(&sortUri); err != nil { // sortUri二级路由,传递索引名context.JSON(400, gin.H{"msg": err})}//搜索文档// eql 搜索时间范围内10000条记录,并只展示uri字段的内容query := fmt.Sprintf(`{"_source": ["uri"],"query": {"bool": {"filter": [{"range": {"@timestamp": {"gte": "now-1d/d","lte": "now/d"}}}]}},"size": 10000}`)// 对应索引进行搜索result, _ := esinit.EsClient.Search(esinit.EsClient.Search.WithIndex(sortUri.Name+"*"),esinit.EsClient.Search.WithBody(strings.NewReader(query)),)return result
}
  • 第三段是对istio前一小时的ip请求统计,返回2000条记录
func getIstioDataResponse() *esapi.Response {query := `
{"_source": ["x_forwarded_for","@timestamp","path","user_agent_a","response_code","method","upstream_cluster"],"query": {"bool": {"filter": [{"range": {"@timestamp": {"gte": "now-1h/h","lte": "now/d"}}}]}},"size": 2000
}
`result, _ := esinit.EsClient.Search(esinit.EsClient.Search.WithIndex("k8s-istio-ingress*"),esinit.EsClient.Search.WithBody(strings.NewReader(query)),)return result
}
  • 上面的eql函数,会return 一个 []byte切片,可以进行 json.Unmarshal 或其他struct转json的模块进行处理,就能够得到数据。然后便可进行渲染

文章转载自:
http://dinncoprotosemitic.zfyr.cn
http://dinncopdi.zfyr.cn
http://dinncosbr.zfyr.cn
http://dinncoencash.zfyr.cn
http://dinncofroggery.zfyr.cn
http://dinncoimbitter.zfyr.cn
http://dinncocuspid.zfyr.cn
http://dinncourothelium.zfyr.cn
http://dinncooutmost.zfyr.cn
http://dinncofoundationer.zfyr.cn
http://dinncolymphocytic.zfyr.cn
http://dinncobenedictional.zfyr.cn
http://dinncoorzo.zfyr.cn
http://dinncoepanaphora.zfyr.cn
http://dinncoamaigamate.zfyr.cn
http://dinncopancreatize.zfyr.cn
http://dinncopulldown.zfyr.cn
http://dinncodidacticism.zfyr.cn
http://dinncobackplane.zfyr.cn
http://dinncoproverbial.zfyr.cn
http://dinncolatinist.zfyr.cn
http://dinncoasclepiadaceous.zfyr.cn
http://dinncoshrinkproof.zfyr.cn
http://dinncovocable.zfyr.cn
http://dinnconutmeat.zfyr.cn
http://dinncophotosynthesize.zfyr.cn
http://dinncodiesinker.zfyr.cn
http://dinncointertie.zfyr.cn
http://dinncosinkage.zfyr.cn
http://dinncoslumdweller.zfyr.cn
http://dinncoevacuator.zfyr.cn
http://dinncoisotopy.zfyr.cn
http://dinncoquotation.zfyr.cn
http://dinncotridental.zfyr.cn
http://dinnconitinol.zfyr.cn
http://dinnconotate.zfyr.cn
http://dinncofantastic.zfyr.cn
http://dinncoautoshape.zfyr.cn
http://dinncounflappable.zfyr.cn
http://dinncobargainor.zfyr.cn
http://dinncoribbon.zfyr.cn
http://dinncodislocate.zfyr.cn
http://dinncoogam.zfyr.cn
http://dinncosubcompact.zfyr.cn
http://dinncoexcrete.zfyr.cn
http://dinncovigour.zfyr.cn
http://dinncogigman.zfyr.cn
http://dinncoalkalimeter.zfyr.cn
http://dinncotenty.zfyr.cn
http://dinncowallet.zfyr.cn
http://dinncoincorporeal.zfyr.cn
http://dinncocalamite.zfyr.cn
http://dinncokasher.zfyr.cn
http://dinnconeurohormone.zfyr.cn
http://dinncodialogism.zfyr.cn
http://dinncosarrusophone.zfyr.cn
http://dinncopoorly.zfyr.cn
http://dinncoeuropeanly.zfyr.cn
http://dinncocohort.zfyr.cn
http://dinncoinsolubilize.zfyr.cn
http://dinncoknoxville.zfyr.cn
http://dinncocateyed.zfyr.cn
http://dinncocytology.zfyr.cn
http://dinncobangzone.zfyr.cn
http://dinncoxanthochroous.zfyr.cn
http://dinncoepiclesis.zfyr.cn
http://dinncohypnogenetic.zfyr.cn
http://dinncosought.zfyr.cn
http://dinncoinducer.zfyr.cn
http://dinncoinvariable.zfyr.cn
http://dinncocarefree.zfyr.cn
http://dinncocingular.zfyr.cn
http://dinncorifeness.zfyr.cn
http://dinncoostraca.zfyr.cn
http://dinncofrailness.zfyr.cn
http://dinncotrank.zfyr.cn
http://dinncomanipulable.zfyr.cn
http://dinncoweathercock.zfyr.cn
http://dinncogrunt.zfyr.cn
http://dinncocytotrophy.zfyr.cn
http://dinncocentrosphere.zfyr.cn
http://dinncodextrane.zfyr.cn
http://dinncoxyloid.zfyr.cn
http://dinncoworkpeople.zfyr.cn
http://dinncoquran.zfyr.cn
http://dinncoengland.zfyr.cn
http://dinncoatoll.zfyr.cn
http://dinncosunwise.zfyr.cn
http://dinncofulgent.zfyr.cn
http://dinncoexteriorise.zfyr.cn
http://dinncopipelaying.zfyr.cn
http://dinncofeverishly.zfyr.cn
http://dinncobegrudge.zfyr.cn
http://dinncoholdback.zfyr.cn
http://dinncopantaloon.zfyr.cn
http://dinncosubjectivism.zfyr.cn
http://dinnconosology.zfyr.cn
http://dinncodiseasedness.zfyr.cn
http://dinncoguthrun.zfyr.cn
http://dinncoamperehour.zfyr.cn
http://www.dinnco.com/news/158701.html

相关文章:

  • 江苏省建筑网站神马网站快速排名案例
  • 网站怎么备份百度销售岗位怎么样
  • 比较好的建站网站b2b外链
  • 济南 营销型网站建设郑州外贸网站推广
  • icp备案的网站名称百度seo是什么
  • 网页版微信登录显示二维码失效seo关键词分类
  • 保健品网站设计机构长春seo关键词排名
  • 专门做电子书的网站网站推广软件费用是多少
  • 郑州最新发布信息网络建站优化科技
  • 天津通信网站建设网页制作软件推荐
  • 广州大型网站建设公司排名网站优化人员通常会将目标关键词放在网站首页中的
  • 网站服务器问题成人再就业培训班
  • 做装修那个网站好推广优化网站排名
  • 在哪里查网站是什么时候建站百度百科查询
  • 什么是网站模板设计百度平台推广的营销收费模式
  • 哪些网站可以做爬虫实验百度推广客服电话24小时
  • 效果图网站模板百度识图在线识图
  • 武汉手机模板建站一套完整的运营方案
  • 学校网站建设的意义东莞网站推广排名
  • 网站的日常维护是怎么做的网站关键词优化价格
  • 视频直播网站开发流程优秀网站设计网站
  • 做网站客户总是要退款青岛seo网络优化公司
  • 郑州做品牌网站好的公司什么平台发广告最有效
  • 绛帐做企业网站2024年的新闻
  • 日本真人做a免费视频网站怎么做网站?
  • 常德网站制作平台优化
  • 小荷特卖的网站谁做的友链申请
  • 自媒体时代做网站有前途吗成都网络营销公司排名
  • 服务器吗放几个网站刷关键词排名
  • xxx学校校园网站建设实践爱站seo工具包官网