栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

安装与配置

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

安装与配置

1. 安装
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
./prometheus
grafana的安装与使用
#!/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-server
2. Springboot
   2.1 pom文件

     
          org.springframework.boot
           spring-boot-starter-actuator
       

       
       
           io.micrometer
           micrometer-registry-prometheus
        1.0.3
       

     
       
           io.github.mweirauch
           micrometer-jvm-extras
           0.1.4
       

  2.2 Application 类中增加

    @Bean
    MeterRegistryCustomizer configurer(@Value("${spring.application.name}") String applicationName) {
        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告警功能时添加)


    @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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/389872.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号