管理是什么意思中文(跳靴系列)
admin
2023-09-12 20:26:32

  前言

跳靴项目部署起来后,如何实时监控项目的运行状况呢?本文记录使用弹簧靴-管理对服务进行监控。

弹簧靴-管理介绍:https://代码居中。github。io/spring-boot-admin/current/# _ what _ is _ spring _ boot _ admin

  工程结构

  服务端

服务器服务端

  客户端

客户端客户端

服务端、客户端都是独立的网项目,服务端是监控程序,客户端是被监控的程序,本文只测试了一个客户端接入

  代码编写

  服务端

服务器服务端引入相关依赖

2.2.0后管理的管理页面支持中文,因此我们引入此版本(父母不再是引入我们的父工程砰的一声了,直接引入跳羚的2.2.0)

!-引入管理相关依赖2.2.0页面支持中文显示,需要弹簧靴2。2 .0-依赖groupid de。code centric/groupid artifactid spring-boot-admin-starter-server/artifactid version 2。2 .0/版本/依赖项为了安全性,引入安全

!- springbootsecurity安全相关-dependencygroupidorg。spring框架。boot/groupid artifact id spring-boot-starter-security/artifact id/dependency解决控制台报错,移除雄猫,改用码头

!-报错:Java。郎。illegalstateexception :调用[async error()]isnotvalidforrequestwithsyncstate[MUST _ DISPATCH]解决:移除雄猫,换成jetty-dependency group pidorg。spring框架。boot/groupid artifactidspring-boot-starter-web/artifactidexclusionexclusiongroupidorg。spring框架。boot/groupid artifactId spring-boot-starter-Tomcat/artifactId/exclusion/exclusions/dependency pendycgroupidorg。spring框架。boot/groupid artifactId spring-boot-jetty/artifactId/dependency

监控系统,直接配置账号、密码,不用搞那么麻烦接入数据库

#配置一个账号和密码春天。安全。用户。name=管理弹簧。安全。用户。密码=123456做好安全配置

@ configurationpubliclasssecuritysecureconfigextendswebsecurityconfigureadapter {//项目应用路径privatefinalstringaddmincontextpath;publicsecuricecureconfig(adminserver properties adminserver properties){ this。admincontextpath=adminserver属性。getcontextpath();} @ overrideprectedvoidconfigure(http security http)抛出异常{保存

dRequestAwareAuthenticationSuccessHandlersuccessHandler=newSavedRequestAwareAuthenticationSuccessHandler();successHandler.setTargetUrlParameter("redirectTo");successHandler.setDefaultTargetUrl(adminContextPath+"/");http.authorizeRequests()//无需登录即可访问.antMatchers(adminContextPath+"/assets/**").permitAll().antMatchers(adminContextPath+"/login").permitAll().anyRequest().authenticated().and()//登录和登出路径.formLogin().loginPage(adminContextPath+"/login").successHandler(successHandler).and().logout().logoutUrl(adminContextPath+"/logout").and()//开启httpbasic支持,admin-client注册时需要使用.httpBasic().and().csrf()//开启基于cookie的csrf保护.csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())//忽略这些路径的csrf保护以便admin-client注册.ignoringAntMatchers(adminContextPath+"/instances",adminContextPath+"/actuator/**");}}

  客户端是要暴露actuator的web端口的,为了安全,客户端只允许服务端请求actuator的web接口,为了方便客户端区分请求来源,我们在请求头注入自定义参数

/***注入额外的请求头,方便客户端区分请求来源*/@ComponentpublicclassHttpHeadersProviderConfigimplementsHttpHeadersProvider{@Value("${server.port}")privateStringport;@OverridepublicHttpHeadersgetHeaders(Instanceinstance){HttpHeadershttpHeaders=newHttpHeaders();//设置约定好的请求头参数httpHeaders.add("spring-boot-admin-service",port);returnhttpHeaders;}}

  我们不可能整天上系统看监控数据,做好自定义通知,当实例状态发生改变,及时通知(发邮件、企业微信、钉钉都可以,自己实现)

/***自定义通知*继承AbstractStatusChangeNotifier类,实现了doNotify方法,*当应用状态改变的时候会回调doNotify方法。*/@ComponentpublicclassCustomNotifierConfigextendsAbstractStatusChangeNotifier{publicCustomNotifierConfig(InstanceRepositoryrepository){super(repository);}@OverrideprotectedMonodoNotify(InstanceEventevent,Instanceinstance){returnMono.fromRunnable(()->{if(eventinstanceofInstanceStatusChangedEvent){System.out.println("实例名称:"+instance.getRegistration().getName());System.out.println("实例服务地址:"+instance.getRegistration().getServiceUrl());Stringstatus=((InstanceStatusChangedEvent)event).getStatusInfo().getStatus();switch(status){case"DOWN":System.out.println("健康检查没通过!");break;case"OFFLINE":System.out.println("服务离线!");break;case"UP":System.out.println("服务上线!");break;case"UNKNOWN":System.out.println("服务未知异常!");break;default:System.out.println(status);break;}}});}}

  最后在启动打上@EnableAdminServer注解,开启服务监控

@EnableAdminServer//开启AdminServer功能@SpringBootApplicationpublicclassSpringBootAdminServerApplication{publicstaticvoidmain(String[]args){SpringApplication.run(SpringBootAdminServerApplication.class,args);}/***启动成功*/@BeanpublicApplicationRunnerapplicationRunner(){returnapplicationArguments->{System.out.println("启动成功!");};}}


  客户端

  服务端引入了2.2.0版本的依赖,因此客户端也要引入2.2.0依赖

de.codecentricspring-boot-admin-starter-client2.2.0

  在配置文件中,开启端口、配置admin的server地址,以及账号、密码

#启用端点,默认情况下,除shutdown以外的所有端点均已启用management.endpoint.shutdown.enabled=true#显示db、redis、rabbti连接情况等management.endpoint.health.show-details=always#公开所有端点web接口management.endpoints.web.exposure.include=*#admin-server地址,以及登录账号、密码spring.boot.admin.client.port=10010spring.boot.admin.client.url=http://localhost:${spring.boot.admin.client.port}spring.boot.admin.client.username=adminspring.boot.admin.client.password=123456


  为了方便测试其他东西

org.springframework.bootspring-boot-starter-cachemysqlmysql-connector-java

  同时创建测试接口、定时器、cache缓存、异步任务,就是为了看服务端能否监控到


  客户端是要暴露actuator的web端口的,为了安全,客户端只允许服务端请求actuator的web接口(通过约定好的请求头来判断)

/***针对actuator接口做安全限制,只允许服务端调用*/@WebFilter@ServletComponentScan@ComponentpublicclassActuatorFilterimplementsFilter{@Value("${spring.boot.admin.client.port}")privateStringadminServicePort;@OverridepublicvoiddoFilter(ServletRequestservletRequest,ServletResponseservletResponse,FilterChainfilterChain)throwsIOException,ServletException{HttpServletRequestrequest=(HttpServletRequest)servletRequest;//判断约定好的请求头参数if(request.getRequestURI().contains("/actuator")&&!adminServicePort.equals(request.getHeader("spring-boot-admin-service"))){thrownewRuntimeException("抱歉,你无权限访问,Actuator端口受保护!Sorry,youhavenopermissiontoaccessit,Actuatorportprotected!");}filterChain.doFilter(servletRequest,servletResponse);}}


  效果演示

  安全配置生效

  首先先看安全配置都生效了没有

  访问服务端,需要登录


  登录上去,客户端已经注册成功


  正常监控客户端中...


  浏览器直接访问客户端的actuator接口,直接抛出异常

  http://localhost:10011/actuator



  其他接口正常访问




  自定义通知

  注:客户端首次在服务端注册,并没有触发自定义通知

  再看下自定义通知

  停掉客户端服务、重启启动客户端,触发服务端自定义通知






  具体监控项

  具体客户端的监控首页,有我们在客户端写的info信息、磁盘监控、堆、非堆内存监控、进程、线程监控、垃圾回收监控

#添加描述info.describe=SpringBootAdmin,TestClientService!info.author=huanzi-qchinfo.version=1.0.0





  计划任务这里可以看到我们配置的定时器



  web映射可以看到所有的web接口


  http跟踪,可以查看具体请求的响应情况





  缓存菜单,可以看到我们使用到的缓存空间


  还可以下载jvmdump文件


  其他就不一一列举,自己把项目跑起来再看


  另外,这个版本好像不能查看异步任务?我并没有找到相关页面


  后记

  SpringBoot-Admin监控Client有两种模式:

  一种是在Client端引入spring-boot-admin-starter-client依赖,配置好Server的相关信息。

  另一种模式是将所有Client端注册到服务发现(Eureka)组件中去,同时把Server端也注册,这样Server端就可以监控所有Client端了,不用对Client都添加依赖。


  SpringBoot系列——admin服务监控暂时先记录到这,后续有空再进行补充


  代码开源


  代码已经开源、托管到我的GitHub、码云:

  GitHub:https://github.com/huanzi-qch/springBoot

  码云:https://gitee.com/huanzi-qch/springBoot


版权声明

作者:huanzi-qch

出处:https://www.cnblogs.com/huanzi-qch

若标题中有“转载”字样,则本文版权归原作者所有。若无转载字样,本文版权归作者所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接,否则保留追究法律责任的权利.

相关内容

热门资讯

金花创建房间/微信金花房卡怎么... 1.微信渠道:(荣耀联盟)大厅介绍:咨询房/卡添加微信:88355042 2.微信游戏中心:打开微...
金花房间卡/金花房卡如何购买/... 金花房间卡/金花房卡如何购买/新超圣金花房卡正版如何购买新超圣是一款非常受欢迎的游戏,咨询房/卡添加...
牛牛创建房间/金花房卡批发/神... 微信游戏中心:神牛大厅房卡在哪里买打开微信,添加客服【88355042】,进入游戏中心或相关小程序,...
链接牛牛/牛牛房卡游戏代理/鸿... 鸿运大厅房卡更多详情添加微:33549083、 2、在商城页面中选择房卡选项。 3、根...
科技实测!牛牛房卡怎么获得/乐... 微信游戏中心:乐酷大厅房卡在哪里买打开微信,添加客服【88355042】,进入游戏中心或相关小程序,...