前言
利用SpringBoot作为微服务单元的实例化技术选型时,我们不可避免的要面对的一个问题就是如何实时监控应用的运行状况数据,比如:健康度、运行指标、日志信息、线程状况等等。spring-boot-admin(SBA)就是一个很好的服务健康监测工具,使用的spring-boot-admin 2.x版本的服务治理开源框架,可用于项目运行过程中对服务的健康检查,状态监控,及在线日志级别修改等功能;
由于现在公司的项目基本都是spring-boot1.5x版本或者更低,然而SBA的1.5.x版本的UI实在是有点丑,尤其是在尝试了2.x的版本过后,所以,这里介绍的是spring-boot2.0以下版本的交给SBA 2.x版本管理的流程
在线演示实例:点我查看演示 账号:admin 密码:123456
下面是一些启动信息:
登陆
首页
应用
应用详情
日志查看
下面将介绍如何使用spring-boot-admin管理服务
1、需要搭建SBA服务端,类似一个注册中心,仅需要进行配置而无需硬编码 2、客户端配置,同样也是添加配置,无需硬编码
SBA服务端搭建:
pom依赖
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.alan</groupId>
<artifactId>cloud</artifactId>
<version>0.0.11</version>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>io.spring.platform</groupId>
<artifactId>platform-bom</artifactId>
<version>Cairo-SR3</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<!-- 若解开此依赖,则需在配置文件中配置eureka地址,SBA将会从eureka中自动读取服务信息 -->
<!-- <dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency> -->
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-server</artifactId>
<version>2.0.3</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<!--解决SpringBoot打包成jar后运行提示没有主清单属性-->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
application.yml配置
server:
port: 8231
spring:
application:
name: SpringBootAdmin
security:
user:
name: "admin" #登陆用户名
password: "123456" #登陆密码
boot:
admin:
ui:
title: adminTest #管理页面的title
management:
endpoints:
web:
exposure:
include: "*"
endpoint:
health:
show-details: ALWAYS
#eureka:
# client:
# service-url: #服务注册中心的配置内容,指定服务注册中心的位置
# defaultZone: http://admin:123456@127.0.0.1:8761/eureka/
启动类:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler;
import org.springframework.security.web.csrf.CookieCsrfTokenRepository;
import de.codecentric.boot.admin.server.config.AdminServerProperties;
import de.codecentric.boot.admin.server.config.EnableAdminServer;
@EnableAdminServer
//@EnableEurekaClient
@SpringBootApplication
@Configuration
@EnableAutoConfiguration
public class AdminServerApplication {
public static void main(String[] args) {
SpringApplication.run(AdminServerApplication.class, args);
}
@Configuration
public static class SecuritySecureConfig extends WebSecurityConfigurerAdapter {
private final String adminContextPath;
public SecuritySecureConfig(AdminServerProperties adminServerProperties) {
this.adminContextPath = adminServerProperties.getContextPath();
}
@Override
protected void configure(HttpSecurity http) throws Exception {
SavedRequestAwareAuthenticationSuccessHandler successHandler = new SavedRequestAwareAuthenticationSuccessHandler();
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().and()
.csrf()
.csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
.ignoringAntMatchers(
adminContextPath + "/instances",
adminContextPath + "/actuator/**"
);
}
}
}
到这SBA服务端就搭建好了,直接启动运行就ok
客户端:
客户端配置:
Maven的pom文件中需要添加如下依赖:
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-client</artifactId>
<version>1.5.6</version>
</dependency>
在配置文件(application.yml/application.properties/bootstrap.yml/bootstrap.properties)中添加如下配置:
*.yml文件的需要添加如下配置:
# spring-boot-admin 监控配置 start
# spring-boot-admin 监控配置 start
boot: # 注意,这是spring节点下的
admin:
auto-registration: true
url : http://localhost:8231 #运行的admin服务端ip和端口
api-path: instances
username: admin # 1.5x的做法
password: 123456 # 1.5x的做法
#spring.boot.admin.client.username=admin
#spring.boot.admin.client.password=123456
management:
security:
enabled : false
# spring-boot-admin 监控配置 end
#可在线查看日志
endpoints:
logfile:
enabled: true
shutdown:
enabled: true
logging:
file: log.log #输出日志文件地址
#保护客户端端点数据
security:
user:
name: admin
password: 123456
客户端启动入口:
@SpringBootApplication
public class FileServiceApp {
public static void main(String[] args) {
SpringApplication.run(FileServiceApp.class, args);
}
}
到这SBA客户端就搭建好了,直接启动运行就ok;