We have a series that covers the full practice of microservices from requirements to live, from code to K8S deployment, from logging to monitoring, and more.
The whole project uses the micro-services developed by GO-Zero, and basically includes some middleware developed by Go-Zero and related GO-Zero authors. The technology stack used is basically self-developed components of go-Zero project team, which is basically the whole Family of Go-Zero.
Actual combat project address: github.com/Mikaelemmmm…
1, an overview of the
A good service must be monitored in real time, and in Go-Zero-Looklook we used the currently popular Prometheus as a monitoring tool and grafana to display it
Go-zero has already integrated Prometheus for us in the code
// StartAgent starts a prometheus agent.
func StartAgent(c Config) {
if len(c.Host) == 0 {
return
}
once.Do(func(a) {
enabled.Set(true)
threading.GoSafe(func(a) {
http.Handle(c.Path, promhttp.Handler())
addr := fmt.Sprintf("%s:%d", c.Host, c.Port)
logx.Infof("Starting prometheus agent at %s", addr)
if err := http.ListenAndServe(addr, nil); err ! =nil {
logx.Error(err)
}
})
})
}
Copy the code
Whenever we start the API, RPC will start an additional Goroutine to provide Prometheus services
[Note] If the order-MQ service is managed by serviceGroup, you need to explicitly call it in the startup file main. The API and RPC are not required, and the configuration is the same
package main
.....
func main(a){...// log, Prometheus, Trace, metricsUrl.
iferr := c.SetUp(); err ! =nil {
panic(err)
}
......
}
Copy the code
2, implementation,
2.1 Configuring Prometheus and Grafana
In the docker-comement-env.yml file under the project
Us to deploy/Prometheus/server/Prometheus. Yml see Prometheus configuration file
global:
scrape_interval:
external_labels:
monitor: 'codelab-monitor'
This represents the configuration of fetching objects
scrape_configs:
- job_name: 'prometheus'
scrape_interval: 5s # Rewrote the global fetching interval from 15 seconds to 5 seconds
static_configs:
- targets: ['127.0.0.1:9090']
- job_name: 'order-api'
static_configs:
- targets: ['order-api:9091']
labels:
job: order-api
app: order-api
env: dev
- job_name: 'order-rpc'
static_configs:
- targets: ['order-rpc:9091']
labels:
job: order-rpc
app: order-rpc
env: dev
- job_name: 'order-mq'
static_configs:
- targets: ['order-mq:9091']
labels:
job: order-mq
app: order-mq
env: dev
- job_name: 'usercenter-api'
static_configs:
- targets: ['usercenter-api:9091']
labels:
job: usercenter-api
app: usercenter-api
env: dev
- job_name: 'usercenter-rpc'
static_configs:
- targets: ['usercenter-rpc:9091']
labels:
job: usercenter-rpc
app: usercenter-rpc
env: dev
- job_name: 'travel-api'
static_configs:
- targets: ['travel-api:9091']
labels:
job: travel-api
app: travel-api
env: dev
- job_name: 'travel-rpc'
static_configs:
- targets: ['travel-rpc:9091']
labels:
job: travel-rpc
app: travel-rpc
env: dev
- job_name: 'payment-api'
static_configs:
- targets: ['payment-api:9091']
labels:
job: payment-api
app: payment-api
env: dev
- job_name: 'payment-rpc'
static_configs:
- targets: ['payment-rpc:9091']
labels:
job: payment-rpc
app: payment-rpc
env: dev
- job_name: 'mqueue-rpc'
static_configs:
- targets: ['mqueue-rpc:9091']
labels:
job: mqueue-rpc
app: mqueue-rpc
env: dev
- job_name: 'message-mq'
static_configs:
- targets: ['message-mq:9091']
labels:
job: message-mq
app: message-mq
env: dev
- job_name: 'identity-api'
static_configs:
- targets: ['identity-api:9091']
labels:
job: identity-api
app: identity-api
env: dev
- job_name: 'identity-rpc'
static_configs:
- targets: [ 'identity-rpc:9091' ]
labels:
job: identity-rpc
app: identity-rpc
env: dev
Copy the code
2.2 Service Configuration
We don’t need to add any code to implement our business (except for services managed by serviceGroup)
We just need to configure it in the business configuration file, let’s take userCenter for example
1) API
2) the RPC
3) MQ (serviceGroup)
[Note] (again) If a service like Order-MQ is managed by serviceGroup, the startup file main should show a call, not the API or RPC
package main
.....
func main(a){...// log, Prometheus, Trace, metricsUrl.
iferr := c.SetUp(); err ! =nil {
panic(err)
}
......
}
Copy the code
2.3 check the
Visit http://127.0.0.1:9090/, click the above menu “Status”, and then click Targets, the blue one means the startup is successful, the red one means the startup is not successful
2.4 configuration grafana
The default user name and password for accessing http://127.0.0.1:3001 are admin
The configuration data source is Prometheus
Then configure
[note] this is configured in docker, so HTTP url cannot write 127.0.0.1
Check whether the configuration is successful
Configuration dashboard
Then click on the first one
We add a CPU metric and enter CPU selection below
Then we can see the monitoring indicators that we want to see
3, the end
You can configure the other metrics you want to see. You can also add an Alert configuration to Grafana. This is not a demo
The project address
Github.com/zeromicro/g…
Welcome to Go-Zero and star support us!
Wechat communication group
Pay attention to the public account of “micro-service Practice” and click on the exchange group to obtain the QR code of the community group.