Prometheus的安装
wget https://github.com/prometheus/prometheus/releases/download/v2.8.1/prometheus-2.8.1.linux-amd64.tar.gz tar -zxvf prometheus-2.8.1.linux-amd64.tar.gz -C /usr/local/ cd /usr/local mv prometheus-2.8.1.linux-amd64/ prometheus cd prometheus/ ./prometheus --version ./prometheusgrafana的安装与使用
#!/bin/bash wget https://dl.grafana.com/oss/release/grafana-8.1.1-1.x86_64.rpm yum -y install urw-fonts rpm -ivh grafana-8.1.1-1.x86_64.rpm systemctl enable grafana-server systemctl start grafana-server2. Springboot
2.1 pom文件
spring-boot-starter-actuator
micrometer-registry-prometheus
micrometer-jvm-extras
2.2 Application 类中增加
@Bean
MeterRegistryCustomizer
return (registry) -> registry.config().commonTags("application", applicationName);
}
2.3 properties
#spring-boot-starter-actuator相关配置
spring.application.name=yzy
management.endpoints.web.exposure.include=*
management.metrics.tags.application=${spring.application.name}
#禁止trace功能
management.endpoint.httptrace.enabled:=false
#禁止configprops功能
management.endpoint.configprops.enabled= false
#禁止shutdown功能
management.endpoint.shutdown.enabled=false
#设置访问密码
spring.security.user.name: admin
spring.security.user.password: admin123456
2.4 提供告警方法供Grafana调用(使用Grafana告警功能时添加)
#spring-boot-starter-actuator相关配置
spring.application.name=yzy
management.endpoints.web.exposure.include=*
management.metrics.tags.application=${spring.application.name}
#禁止trace功能
management.endpoint.httptrace.enabled:=false
#禁止configprops功能
management.endpoint.configprops.enabled= false
#禁止shutdown功能
management.endpoint.shutdown.enabled=false
#设置访问密码
spring.security.user.name: admin
spring.security.user.password: admin123456
2.4 提供告警方法供Grafana调用(使用Grafana告警功能时添加)
@PostMapping("/alerting")
public void alerting(@RequestBody String body){
System.out.println(body);
log.info("告警信息,body:{}",body);
// 调用工具发送告警
}
注:
使用security需要配置httpSercurity,开启httpBasic验证方式,并配置需要验证的页面
@Configuration
@EnableWebSecurity
public class ActuatorWebSecurityConfigurationAdapter extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable()
.authorizeRequests()
// 监控页面需要添加登录校验
.antMatchers("/actuator/**").authenticated()
//普通的接口不需要校验
.antMatchers("/**").permitAll()
.and()
.httpBasic()
.and()
.formLogin()
;
}
}
3. Prometheus配置
Prometheus配置文件为prometheus.yml,默认端口号9090
在文件中配置任务,获取监控数据
# 配置名为prometheus的job,
# 取数时间间隔为5秒,
# 取数地址/actuator/prometheus,
# 服务器地址11.19.19.113,11.24.20.212
# basic_auth中用户名与密码对应Springboot中配置的spring.security.user.name和spring.security.user.password
- job_name: 'prometheus'
scrape_interval: 5s
metrics_path: '/actuator/prometheus'
# scheme defaults to 'http'.
static_configs:
- targets: ['11.19.19.113','11.24.20.212']
basic_auth:
username: 'yzy'
password: 'yzy123456'
4. Grafana配置
需修改domain,从localhost更改为你的域名.
其他配置项,可参考Grafana常用定制修改_evandeng2009-CSDN博客
在Grafana的页面上配置data source和notification channels,然后即可使用报表和报警功能
默认端口号3000



