您的位置:首页 > 编程语言 > Java开发

使用Spirng Boot Admin监控Spring Cloud应用项目

2018-05-23 11:06 1491 查看

一. 介绍

GitHub: https://github.com/codecentric/spring-boot-admin

官方文档: http://codecentric.github.io/spring-boot-admin/1.5.7/ (此文档为1.5.7版本的文档)

The applications register with our Spring Boot Admin Client (via HTTP) or are discovered using Spring Cloud ® (e.g. Eureka, Consul).

官方文档中介绍中提到,使用Spring Boot Admin监控,需要一个Spring Boot Admin Server服务,其他被监控的服务即为一个Spring Boot Admin Client,或者是通过Spring Cloud中的服务注册发现组件如:Eureak,Consul来进行监控服务,这样就不需要特意设置相关被监控的服务作为一个client,只需注册到Eureka或者Consul中即可。

注:本文是基于Spring Cloud Eureka做为服务注册发现组件来实现对服务的监控。提前是存在了Eureka的服务。 另:没有使用eureka的需要在被监控服务中引入client的jar,具体情况请参考上面的官方文档。*

二.创建Spring Boot Admin Server服务

首先创建一个Spirng Boot项目,可以通过 http://start.spring.io/ 快速搭建。

1、添加Spring Boot Admin Server 依赖

pom.xml

<dependencies>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-server</artifactId>
<version>1.5.7</version>
</dependency>
</dependencies>

2、服务启动类添加

@EnableAdminServer
注解开启监控

@Configuration
@EnableAutoConfiguration
@EnableAdminServer
public class SpringBootAdminApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootAdminApplication.class, args);
}
}

3、将Spring Boot Admin Server 注册到Eureka

添加Eureka依赖

pom.xml

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>

服务启动类添加

@EnableDiscoveryClient
开启服务发现

@Configuration
@EnableAutoConfiguration
@EnableDiscoveryClient
@EnableAdminServer
public class SpringBootAdminApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootAdminApplication.class, args);
}
}

添加Eureka服务注册配置

eureka:
instance:
leaseRenewalIntervalInSeconds: 10
client:
registryFetchIntervalSeconds: 5
serviceUrl:
defaultZone: http://10.1.3.54:8761/eureka/
# 关闭spring boot actuator的安全,否则敏感路径访问是401
management:
security:
enabled: false

启动项目,访问ip:port 即进入管理的页面

如图:

图中显示的这些项目都是已经注册到Eureka上的服务,通过将Spring Boot Admin Server注册到Eureka上来监控项目。

三.配置设置

1、登录UI

Admin管理服务没有任何的安全措施,任何人知道ip和port就能访问,这就很不安全,而Spring Boot Admin也提供了登录页面。需要和Spring Security 配合使用。

添加Spring Boot Admin UI依赖:

<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-server-ui-login</artifactId>
<version>1.5.7</version>
</dependency>

添加Spring Security依赖:

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>

在项目启动类中添加配置代码:

package com.aspire.springbootadmin;
import de.codecentric.boot.admin.config.EnableAdminServer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
@Configuration
@EnableAutoConfiguration
@EnableDiscoveryClient
@EnableAdminServer
public class SpringbootAdminApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootAdminApplication.class, args);
}
@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();
}
}
}

添加配置文件

security:
user:
name: admin
password: 123123
basic:
enabled: false

再次访问会出现一个登陆页面

输入配置中配置的用户名和密码点击Login登录。进入后导航栏上也会多出一个退出按钮。点击后即返回到登录页面。

2、Client应用的版本信息

想要在Application列表中显示版本,使用maven的

build-info
插件,在pom.xml文件添加插件

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>build-info</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

如图:没有添加的就没有显示。

3、Client应用JMX bean管理

要在管理界面中与JMX-beans进行交互,您必须在应用程序中包含 Jolokia 。 添加pom依赖:



<dependency>
<groupId>org.jolokia</groupId>
<artifactId>jolokia-core</artifactId>
</dependency>

添加后在监控的应用detial菜单里就可以看到JMX菜单

4、Client应用添加日志Log

在Client应用的properties中配置如下:

logging:
path: /xxx/xxx/xxx/

指定日志输出路径,然后就可以显示日志了:

5、hystrix ui支持

spring boot admin还可以集成hystrix展示。 前提是Client端需要开启hystrix才可以。

在Spring Boot Admin Server 添加依赖

<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-server-ui-hystrix</artifactId>
<version>1.4.6</version>
</dependency>

在Server的配置文件中添加endpoints节点

复制代码 代码如下: spring.boot.admin.routes.endpoints: env,metrics,trace,dump,jolokia,info,configprops,trace,logfile,refresh,flyway,liquibase,heapdump,activiti,hystrix.stream

点击开启了Hystrix的Client,点击Hystrix菜单既能看到Hystrix信息

6.Turbine UI 支持

​ spring boot admin还可以集成turbine展示。

加入依赖:

<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-server-ui-turbine</artifactId>
<version>1.5.7</version>
</dependency>

配置turbine的cluster和location,clusters为turbin服务中配置的clusters一致,location需要指定注册到Eureka中的Turbine服务名称,例如你的Turbine服务名称为TURBINE-SERVER,配置就为下所示。(其实Spring Boot Admin Server本身就可以当做Turbine服务,如果之前就存在了Turbine服务的话就可以直接在这里配置。若不存在Turbine服务,就在Spring Boot Admin Server添加Turbine相关依赖和配置,这样Spring Boot Admin Server也就成了Turbine服务。)

spring.boot.admin.turbine:
clusters: default
location: TURBINE-SERVER

最后在配置文件中添加turbine.stream的endpoint

复制代码 代码如下: spring.boot.admin.routes.endpoints: env,metrics,trace,dump,jolokia,info,configprops,trace,logfile,refresh,flyway,liquibase,heapdump,activiti,hystrix.stream,turbine.stream

界面

导航栏中会多出TURBINE的Tab,点击就能看到Turbine信息

7.邮件通知

Spring Boot Admin 同时支持邮件通知。

添加依赖

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>

添加Spring邮件配置(这里记一个小坑,开始使用的是qq邮箱,结果死活连不上qq邮箱的smtp服务器,查了下原因是腾讯那边把公司内网ip给禁止了,结果连上4G网就没有问题了。)

spring:
mail:
host: mmmail.aspire-tech.com
password: xxxxx
port: 25
username: xxxx

添加Spring Boot Admin邮件配置

boot:
admin:
notify:
mail:
to: xxxx@aspirecn.com
from: chufule@aspirecn.com
enabled: true

效果如图,监控的服务启动,停止都会有邮件通知。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息