Spring Boot Admin的使用

简介: Spring Boot Admin的使用

文章目录



Spring Boot Admin的使用


前面的文章我们讲了Spring Boot的Actuator。但是Spring Boot Actuator只是提供了一个个的接口,需要我们自行集成到监控程序中。今天我们将会讲解一个优秀的监控工具Spring Boot Admin。 它采用图形化的界面,让我们的Spring Boot管理更加简单。


先上图给大家看一下Spring Boot Admin的界面:


image.png


从界面上面我们可以看到Spring Boot Admin提供了众多强大的监控功能。那么开始我们的学习吧。


配置Admin Server


既然是管理程序,肯定有一个server,配置server很简单,我们添加这个依赖即可:


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


同时我们需要在main程序中添加@EnableAdminServer来启动admin server。


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


配置admin client


有了server,我们接下来配置需要监控的client应用程序,在本文中,我们自己监控自己,添加client依赖如下:


<dependency>
    <groupId>de.codecentric</groupId>
    <artifactId>spring-boot-admin-starter-client</artifactId>
    <version>2.2.2</version>
</dependency>


我们需要为client指定要注册到的admin server:


spring.boot.admin.client.url=http://localhost:8080


因为Spring Boot Admin依赖于 Spring Boot Actuator, 从Spring Boot2 之后,我们需要主动开启暴露的主键,如下:


management.endpoints.web.exposure.include=*
management.endpoint.health.show-details=always


配置安全主键


通常来说,我们需要一个登陆界面,以防止未经授权的人访问。spring boot admin提供了一个UI供我们使用,同时我们添加Spring Security依赖:


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


添加了Spring Security,我们需要自定义一些配置:


@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    private final AdminServerProperties adminServer;
    public WebSecurityConfig(AdminServerProperties adminServer) {
        this.adminServer = adminServer;
    }
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        SavedRequestAwareAuthenticationSuccessHandler successHandler = 
          new SavedRequestAwareAuthenticationSuccessHandler();
        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(
                  new AntPathRequestMatcher(this.adminServer.getContextPath() + 
                    "/instances", HttpMethod.POST.toString()), 
                  new AntPathRequestMatcher(this.adminServer.getContextPath() + 
                    "/instances/*", HttpMethod.DELETE.toString()),
                  new AntPathRequestMatcher(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集群


Spring Boot Admin 支持Hazelcast的集群,我们先添加依赖如下:


<dependency>
    <groupId>com.hazelcast</groupId>
    <artifactId>hazelcast</artifactId>
    <version>3.12.2</version>
</dependency>


然后添加Hazelcast的配置:


@Configuration
public class HazelcastConfig {
    @Bean
    public Config hazelcast() {
        MapConfig eventStoreMap = new MapConfig("spring-boot-admin-event-store")
          .setInMemoryFormat(InMemoryFormat.OBJECT)
          .setBackupCount(1)
          .setEvictionPolicy(EvictionPolicy.NONE)
          .setMergePolicyConfig(new MergePolicyConfig(PutIfAbsentMapMergePolicy.class.getName(), 100));
        MapConfig sentNotificationsMap = new MapConfig("spring-boot-admin-application-store")
          .setInMemoryFormat(InMemoryFormat.OBJECT)
          .setBackupCount(1)
          .setEvictionPolicy(EvictionPolicy.LRU)
          .setMergePolicyConfig(new MergePolicyConfig(PutIfAbsentMapMergePolicy.class.getName(), 100));
        Config config = new Config();
        config.addMapConfig(eventStoreMap);
        config.addMapConfig(sentNotificationsMap);
        config.setProperty("hazelcast.jmx", "true");
        config.getNetworkConfig()
          .getJoin()
          .getMulticastConfig()
          .setEnabled(false);
        TcpIpConfig tcpIpConfig = config.getNetworkConfig()
          .getJoin()
          .getTcpIpConfig();
        tcpIpConfig.setEnabled(true);
        tcpIpConfig.setMembers(Collections.singletonList("127.0.0.1"));
        return config;
    }
}
相关文章
|
6天前
|
缓存 NoSQL Java
Springboot自定义注解+aop实现redis自动清除缓存功能
通过上述步骤,我们不仅实现了一个高度灵活的缓存管理机制,还保证了代码的整洁与可维护性。自定义注解与AOP的结合,让缓存清除逻辑与业务逻辑分离,便于未来的扩展和修改。这种设计模式非常适合需要频繁更新缓存的应用场景,大大提高了开发效率和系统的响应速度。
24 2
|
2月前
|
Java Spring
Spring Boot Admin 授权配置
Spring Boot Admin 授权配置
27 0
|
2月前
|
Java Spring
Spring Boot Admin 自定义健康检查
Spring Boot Admin 自定义健康检查
32 0
|
2月前
|
监控 Java Spring
Spring Boot Admin 配置应用
Spring Boot Admin 配置应用
29 0
|
2月前
|
Java Spring
Spring Boot Admin 离线实例
Spring Boot Admin 离线实例
21 0
|
2月前
|
Java Spring
Spring Boot Admin 查看 Client 日志
Spring Boot Admin 查看 Client 日志
37 0
|
3月前
|
缓存 监控 Java
通用快照方案问题之Spring Boot Admin的定义如何解决
通用快照方案问题之Spring Boot Admin的定义如何解决
54 0
|
4月前
|
运维 Java 关系型数据库
Spring运维之boot项目bean属性的绑定读取与校验
Spring运维之boot项目bean属性的绑定读取与校验
49 2
|
4月前
|
存储 运维 Java
Spring运维之boot项目开发关键之日志操作以及用文件记录日志
Spring运维之boot项目开发关键之日志操作以及用文件记录日志
53 2
|
4月前
|
Java Maven
springboot项目打jar包后,如何部署到服务器
springboot项目打jar包后,如何部署到服务器
286 1