每当你有这种想法的时候,建议你一定要首先想想:开源世界是不是早就有人做了。
没错,有人为你做好了。其中最有名的当属spring-boot-admin这个项目了。
spring boot admin是个什么鬼呢?就是一个可以监控和管理spring boot应用的admin管理应用。原理很简单,就是把spring boot actuator中的那些endpoint暴露出来的json数据进行可视化显示到界面上。admin前端使用的是angularjs和bootstrap。一个spring boot应用要想接受监控和管理,很简单,只需要引入client依赖就可以了,如果你的spring boot是一个spring cloud管理的微服务应用的话。那么客户端几乎不用做什么,只需要在admin server上配置一下就可以监控服务注册中心的所有应用了,这里说的服务注册中心比如eureka,consul,zookeeper等。
创建 Spring Boot Admin Server
首先新建一个spring boot应用。
然后引入以下依赖:
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-server</artifactId>
<version>1.4.6</version>
</dependency>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-server-ui</artifactId>
<version>1.4.6</version>
</dependency>
note:现在最新的release是1.4.6
然后在你的spring boot main application上加上如下注解,主要是autoconfiguration、enableadminserver以及configuration:
@Configuration
@EnableAutoConfiguration
@EnableAdminServer
@EnableDiscoveryClient
@EnableTurbine
@EnableHystrixDashboard
public class SpringBootAdminApplication
eureka方式使用
然后你需要配置eureka所在的地址,在yml文件中:
eureka:
instance:
leaseRenewalIntervalInSeconds: 10
preferIpAddress: true
client:
registryFetchIntervalSeconds: 5
serviceUrl:
defaultZone: ${EUREKA_SERVICE_URL:http://localhost:1111}/
eureka/
如果你的eureka是个集群,那么url逗号隔开就是了。
这里只说了通过服务注册中心来获取spring boot应用的使用。
纯客户端使用
你的spring boot应用没有注册到spring cloud注册中心,那么你只需要在自己的
客户端中引入如下依赖:
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-client</artifactId>
<version>1.4.6</version>
</dependency>
然后在配置文件中添加spring boot admin server的url就是可以了:
spring:
boot:
admin:
url: http://localhost:8080
启动server
现在你启动你刚刚创建的admin server,就可以看到如下界面:
是不是很炫酷。
可以看到在应用列表中已经显示了我们刚刚在eureka上注册的应用了。
你也许看到了就是version信息以及info信息。默认这个是没有的。你需要配置插件。
另外记住就是spring boot应用要把endpoint开启:
management:
security:
enabled: false
这里为了演示,只是进行了简单的安全禁用。在实际生产中,你需要为此配置安全验证。
扩展增强
在应用列表中显示版本
你只需要在你的spring boot 应用的pom中加入如下plugin:
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>build-info</goal>
</goals>
</execution>
</executions>
</plugin>
然后执行下build-info就可以了:
日志级别管理
默认是不支持的。
你只需要在logback xml中加入:
<include resource="org/springframework/boot/logging/logback/base.xml"/>
<jmxConfigurator/>
然后加入依赖:
<dependency>
<groupId>org.jolokia</groupId>
<artifactId>jolokia-core</artifactId>
</dependency>
然后就可以了:
日志显示
只需要在你的spring boot应用的properties中配置如下:
logging:
path: /Users/hezhuofan/Downloads/
然后就可以显示日志了:
至于这个是什么原理呢?你懂的,actuator endpoint。
JMX-bean管理
加入这个依赖就可以了:
<dependency>
<groupId>org.jolokia</groupId>
<artifactId>jolokia-core</artifactId>
</dependency>
界面:
hystrix ui支持
spring boot admin还可以集成hystrix展示。
1、加入依赖:
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-server-ui-hystrix</artifactId>
<version>1.4.6</version>
</dependency>
2、添加/hystrix.stream endpint:
# tag::configuration-ui-hystrix[]
spring.boot.admin.routes.endpoints: env,metrics,trace,dump,jolokia,info,configprops,trace,logfile,refresh,flyway,liquibase,heapdump,hystrix.stream,activiti
# end::configuration-ui-hystrix[]
3、界面:
turbine ui支持
spring boot admin还可以集成turbine展示。
1、加入依赖:
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-server-ui-turbine</artifactId>
<version>1.4.6</version>
</dependency>
2、配置turbine的url和cluster,application.yml:
spring.boot.admin.turbine:
clusters: demo-cluster
url: ${SPRING_BOOT_ADMIN_TURBINE_URL:http://localhost:8989/turbine.stream}
3、界面:
activiti ui支持
1、添加依赖:
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-server-ui-activiti</artifactId>
<version>1.4.6</version>
</dependency>
2、添加activiti endpoint:
spring.boot.admin.routes.endpoints: env,metrics,trace,dump,jolokia,info,configprops,trace,logfile,refresh,flyway,liquibase,heapdump,hystrix.stream,activiti
login ui支持
登陆ui目前官方文档上还没介绍。但在github仓库里已经有了。
这里分别把和login ui的都贴出来吧。
application.yml:
---
logging:
file: "target/boot-admin-sample.log"
management:
context-path: "/actuator"
spring:
application:
name: "Spring Boot Admin Server"
profiles:
active:
secure
server:
port: 9090
---
management:
security:
enabled: false
security:
basic:
enabled: true
---
spring:
mail:
host:smtp.exmail.qq.com
profiles: secure
boot:
admin:
notify:
mail:
to:hezhuofan@qq.com
username: "${security.user.name}" #These two are needed so that the client
password: "${security.user.password}" #can register at the protected server api
client:
metadata:
user.name: "${security.user.name}" #These two are needed so that the server
user.password: "${security.user.password}" #can access the proteceted client endpoints
security:
user:
name: user
password: pass
代码:
@Profile("secure")
// tag::configuration-spring-security[]
@Configuration
public static class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
// Page with login form is served as /login.html and does a POST on /login
http.formLogin().loginPage("/login.html").loginProcessingUrl("/login").permitAll();
// The UI does a POST on /logout on logout
http.logout().logoutUrl("/logout");
// The ui currently doesn't support csrf
http.csrf().disable();
// Requests for the login page and the static assets are allowed
http.authorizeRequests()
.antMatchers("/login.html", "/**/*.css", "/img/**", "/third-party/**")
.permitAll();
// ... and any other request needs to be authorized
http.authorizeRequests().antMatchers("/**").authenticated();
// Enable so that the clients can authenticate via HTTP basic for registering
http.httpBasic();
}
}
// end::configuration-spring-security[]
前端代码:
具体代码稍后我会贴到“原文阅读”处。
效果:
用户名:user,密码:pass
然后进入:
看到没?右上角有个“退出”,点击退出就退出了。
你也许疑惑为什么是中文,那是因为我把js改了一下。默认会有一点乱码:
默认长这样:
此外,spring boot admin还支持通知告警功能。这里就不赘述了。你可以自己去官网查阅。
总结
spring boot admin作为一个把endpoint端点可视化的web应用,至少比你看json要好很多,而且还支持了其他监控管理功能。而且还对spring cloud支持友好。毫无疑问,是你在微服务化过程中或spring boot应用监控方案中一个不可获取的选择之一。
你也可以针对此admin进行自己的扩展,从而使用你所在公司的需求。
ps:上面说的login-ui 的代码 你可以去spring boot admin的github去复制粘贴,然后改改就可以了。相信未来这些都不用你来搞了,直接引入依赖就好了。
本文分享自 ImportSource 微信公众号,前往查看
如有侵权,请联系 cloudcommunity@tencent.com 删除。
本文参与 腾讯云自媒体同步曝光计划 ,欢迎热爱写作的你一起参与!