欢迎来到代码驿站!

JAVA代码

当前位置:首页 > 软件编程 > JAVA代码

spring boot 添加admin监控的方法

时间:2021-05-14 10:40:36|栏目:JAVA代码|点击:

一、Spring Boot  Admin简介

spring boot admin github开源地址:https://github.com/codecentric/spring-boot-admin

它主要的作用是在Spring Boot Actuator的基础上提供简洁的WEB UI展示。

二、项目使用:

1、搭建一个maven web项目

2、pom依赖配置

<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> 
<dependency> 
  <groupId>de.codecentric</groupId> 
  <artifactId>spring-boot-admin-starter-client</artifactId> 
</dependency> 
<dependency> 
  <groupId>de.codecentric</groupId> 
  <artifactId>spring-boot-admin-server</artifactId> 
</dependency> 
<dependency> 
  <groupId>de.codecentric</groupId> 
  <artifactId>spring-boot-admin-server-ui</artifactId> 
</dependency> 
<dependency> 
  <groupId>de.codecentric</groupId> 
  <artifactId>spring-boot-admin-server-ui-login</artifactId> 
</dependency> 

在pom.xml中添加上以上配置

admin服务端:spring-boot-admin-server、spring-boot-admin-server-ui

admin客户端:spring-boot-admin-starter-client  (加上该项能监控服务端自身的运行状态,其他项目只需要引入client就可以引入监控)

安全:spring-boot-starter-security

登录验证:spring-boot-admin-server-ui-login (也可以自行添加简单的登录界面)

3、application.yml

info: 
 app: 
  name: imard 
  version: v1.0.0 
[html] view plain copy
logging: 
 file: "d:/logs/imard/boot.log" 
management: 
 context-path: "/actuator" 
spring: 
 application: 
  name: "@pom.artifactId@" 
 boot: 
  admin: 
   url: http://www.test.com:8080 
 profiles: 
  active: 
   - secure 
--- 
spring: 
 profiles: insecure 
management: 
 security: 
  enabled: false 
security: 
 basic: 
  enabled: false 
--- 
spring: 
 profiles: secure 
 boot: 
  admin: 
   username: "${security.user.name}" 
   password: "${security.user.password}" 
   client: 
    metadata: 
     user.name: "${security.user.name}" 
     user.password: "${security.user.password}" 
 
security: 
 user: 
  name: user 
  password: pass 

其中:spring.boot.admin.url声明admin服务端地址(其他项目会通过这个url主动的注册到admin监控中)
            info配置app的基本信息

            www.test.com  在本机hosts中做了映射

4、Application.java

@Configuration 
@EnableAutoConfiguration 
@EnableAdminServer 
public class Application extends SpringBootServletInitializer { 
  @Override 
  protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { 
    return application.sources(Application.class); 
  } 
 
  public static void main(String[] args) { 
    SpringApplication.run(Application.class, args); 
  } 
} 

@EnableAdminServer 添加上该注解启动监控

5、SecurityConfig

@Profile("secure") 
@Configuration 
public class SecurityConfig extends WebSecurityConfigurerAdapter { 
  @Override 
  protected void configure(HttpSecurity http) throws Exception { 
    http.formLogin().loginPage("/login.html").loginProcessingUrl("/login").permitAll(); 
    http.logout().logoutUrl("/logout"); 
    http.csrf().disable(); 
    http.authorizeRequests() 
      .antMatchers("/login.html", "/**/*.css", "/img/**", "/third-party/**").permitAll(); 
    http.authorizeRequests().antMatchers("/api/**").permitAll().antMatchers("/**") 
      .authenticated(); 
    // Enable so that the clients can authenticate via HTTP basic for registering 
    http.httpBasic(); 
  } 
} 

使用Spring Security配置一个基本的安全策略

6、监管管理

配置完1~5个步骤以后,使用application启动监控程序。

通过http://www.test.com:8080/login.html监控登录界面进行安全验证后,如下图:

进入details就可以看到具体的项目监控信息(Details、Log、Metrics、Environment、Logging、JMX、Threads、Audit、Trace、Heapdump)

总结

上一篇:Springboot集成JSR303参数校验的方法实现

栏    目:JAVA代码

下一篇:Java 实现声音播放程序

本文标题:spring boot 添加admin监控的方法

本文地址:http://www.codeinn.net/misctech/121499.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有