Spring Boot Admin(监控工具)的使用
前面的文章我们讲了SpringBoot的Actuator。但是SpringBootActuator只是提供了一个个的接口,需要我们自行集成到监控程序中。今天我们将会讲解一个优秀的监控工具SpringBootAdmin。它采用图形化的界面,让我们的SpringBoot管理更加简单。
先上图给大家看一下SpringBootAdmin的界面:
从界面上面我们可以看到SpringBootAdmin提供了众多强大的监控功能。那么开始我们的学习吧。
配置AdminServer
既然是管理程序,肯定有一个server,配置server很简单,我们添加这个依赖即可:
de.codecentric spring-boot-admin-starter-server 2.2.2
同时我们需要在main程序中添加@EnableAdminServer来启动adminserver。
@EnableAdminServer
@SpringBootApplication
publicclassSpringBootAdminServerApplication{
publicstaticvoidmain(String[]args){
SpringApplication.run(SpringBootAdminServerApplication.class,args);
}
}
配置adminclient
有了server,我们接下来配置需要监控的client应用程序,在本文中,我们自己监控自己,添加client依赖如下:
de.codecentric spring-boot-admin-starter-client 2.2.2
我们需要为client指定要注册到的adminserver:
spring.boot.admin.client.url=http://localhost:8080
因为SpringBootAdmin依赖于SpringBootActuator,从SpringBoot2之后,我们需要主动开启暴露的主键,如下:
management.endpoints.web.exposure.include=* management.endpoint.health.show-details=always
配置安全主键
通常来说,我们需要一个登陆界面,以防止未经授权的人访问。springbootadmin提供了一个UI供我们使用,同时我们添加SpringSecurity依赖:
de.codecentric spring-boot-admin-server-ui-login 1.5.7 org.springframework.boot spring-boot-starter-security
添加了SpringSecurity,我们需要自定义一些配置:
@Configuration
publicclassWebSecurityConfigextendsWebSecurityConfigurerAdapter{
privatefinalAdminServerPropertiesadminServer;
publicWebSecurityConfig(AdminServerPropertiesadminServer){
this.adminServer=adminServer;
}
@Override
protectedvoidconfigure(HttpSecurityhttp)throwsException{
SavedRequestAwareAuthenticationSuccessHandlersuccessHandler=
newSavedRequestAwareAuthenticationSuccessHandler();
successHandler.setTargetUrlParameter("redirectTo");
successHandler.setDefaultTargetUrl(this.adminServer.getContextPath()+"/");
http
.authorizeRequests()
.antMatchers(this.adminServer.getContextPath()+"/assets/**").permitAll()
.antMatchers(this.adminServer.getContextPath()+"/login").permitAll()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage(this.adminServer.getContextPath()+"/login")
.successHandler(successHandler)
.and()
.logout()
.logoutUrl(this.adminServer.getContextPath()+"/logout")
.and()
.httpBasic()
.and()
.csrf()
.csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
.ignoringRequestMatchers(
newAntPathRequestMatcher(this.adminServer.getContextPath()+
"/instances",HttpMethod.POST.toString()),
newAntPathRequestMatcher(this.adminServer.getContextPath()+
"/instances/*",HttpMethod.DELETE.toString()),
newAntPathRequestMatcher(this.adminServer.getContextPath()+"/actuator/**"))
.and()
.rememberMe()
.key(UUID.randomUUID().toString())
.tokenValiditySeconds(1209600);
}
}
接下来,我们在配置文件中指定服务器的用户名和密码:
spring.boot.admin.client.username=admin spring.boot.admin.client.password=admin
作为一个客户端,连接服务器的时候,我们也需要提供相应的认证信息如下:
spring.boot.admin.client.instance.metadata.user.name=admin spring.boot.admin.client.instance.metadata.user.password=admin spring.boot.admin.client.username=admin spring.boot.admin.client.password=admin
好了,登录页面和权限认证也完成了。
Hazelcast集群
SpringBootAdmin支持Hazelcast的集群,我们先添加依赖如下:
com.hazelcast hazelcast 3.12.2
然后添加Hazelcast的配置:
@Configuration
publicclassHazelcastConfig{
@Bean
publicConfighazelcast(){
MapConfigeventStoreMap=newMapConfig("spring-boot-admin-event-store")
.setInMemoryFormat(InMemoryFormat.OBJECT)
.setBackupCount(1)
.setEvictionPolicy(EvictionPolicy.NONE)
.setMergePolicyConfig(newMergePolicyConfig(PutIfAbsentMapMergePolicy.class.getName(),100));
MapConfigsentNotificationsMap=newMapConfig("spring-boot-admin-application-store")
.setInMemoryFormat(InMemoryFormat.OBJECT)
.setBackupCount(1)
.setEvictionPolicy(EvictionPolicy.LRU)
.setMergePolicyConfig(newMergePolicyConfig(PutIfAbsentMapMergePolicy.class.getName(),100));
Configconfig=newConfig();
config.addMapConfig(eventStoreMap);
config.addMapConfig(sentNotificationsMap);
config.setProperty("hazelcast.jmx","true");
config.getNetworkConfig()
.getJoin()
.getMulticastConfig()
.setEnabled(false);
TcpIpConfigtcpIpConfig=config.getNetworkConfig()
.getJoin()
.getTcpIpConfig();
tcpIpConfig.setEnabled(true);
tcpIpConfig.setMembers(Collections.singletonList("127.0.0.1"));
returnconfig;
}
}
本文的例子可以参考https://github.com/ddean2009/learn-springboot2/tree/master/springboot-admin
总结
以上所述是小编给大家介绍的SpringBootAdmin(监控工具)的使用,希望对大家有所帮助!
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。