Springboot+actuator+prometheus+Grafana集成_全球新视野
来源:博客园 时间:2023-06-03 16:41:32
(资料图片)
本次示例以Windows示例推荐到官网去下载:Windows版的应用程序
下载最新版 prometheus-2.37.8.windows-amd64 压缩包:解压就行
准备一个Springboot的项目:下载最新版 grafana-9.5.2 压缩包:解压就行
导入相关的监控依赖
org.springframework.boot spring-boot-starter-actuator io.micrometer micrometer-registry-prometheus 1.10.5 org.springframework.boot spring-boot-starter-security
springSecurity的配置#springSecurity 配置spring.security.user.name=rootspring.security.user.password=rootspring.security.user.roles=ADMIN
spring-actuator配置#增加开启springboot actuator监控的配置management: endpoint: shutdown: enabled: true # 开启端点 health: show-details: always # 是否展示健康检查详情 endpoints: web: exposure: include: - prometheus - health metrics: tags: application: ${spring.application.name}
springSecurity的白名单接口配置-SecurityConfigpackage com.gton.config;import org.springframework.context.annotation.Configuration;import org.springframework.security.config.annotation.web.builders.HttpSecurity;import org.springframework.security.config.annotation.web.builders.WebSecurity;import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;/** * @description: * @author: GuoTong * @createTime: 2023-06-01 21:44:49 * @since JDK 1.8 OR 11 **/@Configurationpublic class SecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http.formLogin().and().authorizeRequests() .antMatchers("/actuator/**", "/favicon.ico", "/doc.html").permitAll() .antMatchers("/static/**").permitAll() .antMatchers("/favicon.ico").permitAll() // swagger .antMatchers("/swagger**/**").permitAll() .antMatchers("/webjars/**").permitAll() .antMatchers("/v2/**").permitAll() .anyRequest().authenticated().and().csrf().disable(); //关闭csrf保护 } /** * Description: 忽略一些借口 * * @author: GuoTong * @date: 2023-06-01 21:44:49 * @return: */ @Override public void configure(WebSecurity web) throws Exception { web.ignoring() .antMatchers( "/doc.html", "/swagger-resources/configuration/ui", "/swagger*", "/swagger**/**", "/webjars/**", "/favicon.ico", "/**/*.css", "/**/*.js", "/**/*.png", "/**/*.gif", "/v2/**", "/**/*.ttf", "/actuator/**" ); }}
springboot的相关的配置@Value("${auth.global.enable:false}") private boolean enableGlobalAuth; @Bean @LoadBalanced public RestTemplate restTemplate() { return new RestTemplate(); } /** * Description: 添加全局跨域CORS处理 */ @Override public void addCorsMappings(CorsRegistry registry) { // 设置允许跨域的路径 registry.addMapping("/**") //设置允许跨域请求的域名 .allowedOrigins("http://127.0.0.1:8787") // 是否允许证书 .allowCredentials(true) // 设置允许的方法 .allowedMethods("GET", "POST", "DELETE", "PUT") // 设置允许的header属性 .allowedHeaders("*") // 跨域允许时间 .maxAge(3600); } /** * Description: 静态资源过滤 */ @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { //ClassPath:/Static/** 静态资源释放 registry.addResourceHandler("/**").addResourceLocations("classpath:/static/"); //释放swagger registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/"); //释放webjars registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/"); } /** * 解决springboot升到2.6.x之后,knife4j报错 * * @param webEndpointsSupplier the web endpoints supplier * @param servletEndpointsSupplier the servlet endpoints supplier * @param controllerEndpointsSupplier the controller endpoints supplier * @param endpointMediaTypes the endpoint media types * @param corsEndpointProperties the cors properties * @param webEndpointProperties the web endpoints properties * @param environment the environment * @return the web mvc endpoint handler mapping */ @Bean public WebMvcEndpointHandlerMapping webMvcEndpointHandlerMapping(WebEndpointsSupplier webEndpointsSupplier, ServletEndpointsSupplier servletEndpointsSupplier, ControllerEndpointsSupplier controllerEndpointsSupplier, EndpointMediaTypes endpointMediaTypes, CorsEndpointProperties corsEndpointProperties, WebEndpointProperties webEndpointProperties, Environment environment) { List> allEndpoints = new ArrayList<>(); Collection webEndpoints = webEndpointsSupplier.getEndpoints(); allEndpoints.addAll(webEndpoints); allEndpoints.addAll(servletEndpointsSupplier.getEndpoints()); allEndpoints.addAll(controllerEndpointsSupplier.getEndpoints()); String basePath = webEndpointProperties.getBasePath(); EndpointMapping endpointMapping = new EndpointMapping(basePath); boolean shouldRegisterLinksMapping = shouldRegisterLinksMapping(webEndpointProperties, environment, basePath); return new WebMvcEndpointHandlerMapping(endpointMapping, webEndpoints, endpointMediaTypes, corsEndpointProperties.toCorsConfiguration(), new EndpointLinksResolver( allEndpoints, basePath), shouldRegisterLinksMapping, null); } /** * shouldRegisterLinksMapping * * @param webEndpointProperties * @param environment * @param basePath * @return */ private boolean shouldRegisterLinksMapping(WebEndpointProperties webEndpointProperties, Environment environment, String basePath) { return webEndpointProperties.getDiscovery().isEnabled() && (StringUtils.hasText(basePath) || ManagementPortType.get(environment).equals(ManagementPortType.DIFFERENT)); } /** * Description: 过滤器 * * @param registry * @author: GuoTong * @date: 2023-06-03 12:32:39 * @return:void */ @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(new MicrometerTPSInterceptor()).addPathPatterns("/**") .excludePathPatterns("/doc.html") .excludePathPatterns("/swagger-resources/**") .excludePathPatterns("/webjars/**") .excludePathPatterns("/v2/**") .excludePathPatterns("/favicon.ico") .excludePathPatterns("/sso/**") .excludePathPatterns("/swagger-ui.html/**"); } /** * Description: Bean 如下来监控 JVM 性能指标信息: * http://localhost:8889/actuator/prometheus 指标地址 * * @param applicationName * @author: GuoTong * @date: 2023-06-03 12:34:36 * @return:org.springframework.boot.actuate.autoconfigure.metrics.MeterRegistryCustomizer */ @Bean MeterRegistryCustomizer configurer(@Value("${spring.application.name}") String applicationName) { return registry -> registry.config().commonTags("application", applicationName); }
启动访问监控-actuator的看板:http://localhost:port/actuator-prometheus的看板:http://localhost:port/actuator/prometheus配置Prometheus的对于本Springboot微服务站点的监控添加配置 以下内容为SpringBoot应用配置# Prometheus 启动完成之后 http://localhost:9090/targets# my global configglobal: scrape_interval: 15s # Set the scrape interval to every 15 seconds. Default is every 1 minute. evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute. # scrape_timeout is set to the global default (10s).# Alertmanager configurationalerting: alertmanagers: - static_configs: - targets: # - alertmanager:9093# Load rules once and periodically evaluate them according to the global "evaluation_interval".rule_files:# - "first_rules.yml"# - "second_rules.yml"# A scrape configuration containing exactly one endpoint to scrape:# Here it"s Prometheus itself.scrape_configs: - job_name: "prometheus" static_configs: - targets: ["127.0.0.1:9090"] ###以下内容为SpringBoot应用配置 - job_name: "BackStageApp" scrape_interval: 5s metrics_path: "/actuator/prometheus" static_configs: - targets: ["127.0.0.1:8889"] labels: instance: "BackStageApp-prometheus" service: "BackStageApp-8889-prometheus"
点击这个执行:prometheus的应用启动成功如下访问prometheus的应用:http://localhost:9090/可以点击链接跳转然后启动Grafana启动成功如下:初始化--启动有点久,耐性一点打开Grafana看板:http://localhost:3000/login显示的是:http://sky-20200720fyp:8889/actuator/prometheus说明Prometheus配置完成
第一次进入如下配置Prometheus的数据源第一步选这个管理配置菜单第二步选这个Datasorce第三步选这个添加新的Datasorce第四步选这个Prometheus数据源第五步配置Prometheus数据源的地址和名称,然后保存第六步配置Prometheus的看板导入对应的监控 JVM 的 Dashboard 模板,模板编号为 4701。,点击load填写这些必填项;导入自动加载后其他可以不用管,必须选择下面的刚刚配置的prometheus数据源,然后选择import第七步监控JVM首次登录使用 admin:admin 然后可以设置自己的账号密码,也可以跳过Skip
上一步点击然后选择import,会进入这个界面,什么都没有
选择自己项目的站点配置的application和instance就行了,刷新左上角的时间
创建文件组很多看板自己研究把
可以把监控看板移加入分类分组
标签:
- Springboot+actuator+prometheus+Grafana集成_全球新视野
- 股份有限公司跟有限公司的区别在哪里-短讯
- 儿童节催热亲子游
- 焦点关注:Identity Dental Marketing提供免费的营销资源
- 全球热资讯!创建数据透视表的操作步骤_创建数据透视表
- 元宵的由来简短10字左右_元宵的由来简介介绍-世界报道
- 非主流文案热爱生命,就要勇敢地去追求自己想要的东西,即使不跟着主流,我们也要勇往|全球百事通
- 西门子医疗深化本土化战略
- 时隔两年半!险资又动手了 举牌这家A股公司!嗅到了什么?
- 苹果全球首播不带货:佛系直播 130万人围观
- 155的女生67㎏减肥怎么做? 世界球精选
- 预知未来炒股炒期货的小说_预知未来
- 每日速看!手机等电子设备禁入考点 北京今年高考首次实行两次安检
- “局长与女子不雅聊天记录”还涉及另一局长 广西柳州纪委:正在核实 快报
- 出让市场化商品房是什么意思_出让
- 改编作家徐则臣小说,电视剧《北上》开机
- 世界消息!电视剧《决胜》(决胜1一40集完整版)
- 一汽丰田销量劲增91.6% RAV4涨近5倍 市占率提升|焦点资讯
- 大江东|孩子们的痛与爱,上海民警哥哥姐姐都愿聆听
- ✌莱奥续约后晒在车内身穿米兰球衣照:红黑球衣很适合我
- 当前短讯!海口美兰区依法查处取缔一非法储存经营柴油窝点
- 西安!含光门外的皮肤病医院能治疗好白癜风吗?女性容易患上白癜风的常见因素是什么?
- 【世界独家】彩票中奖怎么领取是现金还是打卡_彩票中奖怎么领
- 元成股份6月2日盘中涨幅达5% 微速讯
- 上交所:本周对杭州热电等严重异动股票重点监控
- 每日资讯:Meta宣布其Quest3VR耳机售价499.99美元
- 松茸刺身的做法(松茸刺身的做法和配料) 动态
- 广西平南县:小盆景栽出好“钱”景
- *ST新海:股东拟增持不超过5.09%且不低于4.73的股份
- 当前最新:江西赣州可提供奥克斯冰箱维修服务地址在哪
x
广告
x
广告