spring boot2.x暴露监控endpoint并配置prometheus及grafana对多个targets进行监控

1. build.gradle中添加依赖

compile group: 'org.springframework.boot', name: 'spring-boot-starter-actuator'

compile group: 'org.springframework.boot', name: 'spring-boot-starter-security'

compile('io.micrometer:micrometer-registry-prometheus')

2. spring boot 2.x暴露信息

不同于之前的Actuator 1.x,Actuator 2.x 的大多数端点默认被禁掉。 Actuator 2.x 中的默认端点增加了/actuator前缀。

3. spring boot服务端改造

配置信息

server.port=8085
management.endpoints.web.exposure.include=prometheus

management.endpoint.metrics.enabled=true

spring.security.user.name=xxx

spring.security.user.password=123456

spring.security.user.roles=ACTUATOR_ADMIN



management.server.port=8090

management.endpoints.web.base-path=/admin

 

指定权限

@Configuration

public class ActuatorSecurityConfig extends WebSecurityConfigurerAdapter {



    @Override

    protected void configure(HttpSecurity http) throws Exception {

        http.requestMatcher(EndpointRequest.toAnyEndpoint()).authorizeRequests().antMatchers("/actuator/*").hasRole(

                "ACTUATOR_ADMIN").and().httpBasic();

    }

}

 

这样我们可以看到:

这个spring boot服务启动了两个端口:

8085端口,进行正常的业务处理

8090端口,admin,暴露出监控信息,并通过spring security进行权限控制

 

打开浏览器访问http://192.168.211.2:8090/admin/prometheus

输入用户名 密码

4. Prometheus监控多个spring boot服务

下载prometheus-2.11.1.linux-amd64.tar.gz

编写配置文件prometheus.yml

 

global:

  scrape_interval: 10s

  scrape_timeout: 10s

  evaluation_interval: 10m

scrape_configs:

  - job_name: app-gateway

    scrape_interval: 5s

    scrape_timeout: 5s

    metrics_path: /admin/prometheus

    scheme: http

    basic_auth:

      username: root

      password: 1230456

    static_configs:

      - targets:

        - 192.168.211.2:8099

  - job_name: search

    scrape_interval: 5s

    scrape_timeout: 5s

    metrics_path: /admin/prometheus

    scheme: http

    basic_auth:

      username: root

      password: 1230456

    static_configs:

      - targets:

        - 192.168.211.2:8090

其中,我的spring boot服务部署在192.168.211.2上,而prometheus和grafana都部署在192.168.211.101上

 

配置了两个服务

 

 

执行以下命令启动 ./prometheus --config.file=prometheus.yml &

 

访问http://192.168.211.101:9090/targets

可以看到

5. grafana可视化监控多个prometheus targets

下载grafana-6.2.5.linux-amd64.tar.gz

启动grafana

./grafana-server &

http://192.168.211.101:3000

在dashboard中配置时,要采用这样的方式

jvm_gc_pause_seconds_count{action="end of minor GC",cause="Metadata GC Threshold",instance="192.168.211.2:8090",job="search"}

 

通过instance和job来进行区分prometheus的targets

 

可以看到dashboard中有两个prometheus的targets

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值