蓝迷梦

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

springboot admin 监控搭建

一.admin服务端搭建:

1.新建一个springboot项目,版本2.3.7.RELEASE,

引入pom文件:

复制代码
        <!--springAdmin-->
        <dependency>
            <groupId>de.codecentric</groupId>
            <artifactId>spring-boot-admin-starter-server</artifactId>
        </dependency>

        <!--springboot 的安全框架-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>

        <!--告警邮件通知-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
        </dependency>
复制代码

2.添加注解:

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

 

 3.配置

yml配置文件

复制代码
# 应用服务 WEB 访问端口

server.port: 9999
spring:
  application:
    name: spring-boot-adminer
# ##############boot admin 配置##############
  boot:
    admin:
      notify:
        mail:
          to: xx@qq.com
      monitor:
        info-interval: 5s
        info-lifetime: 5s
        status-interval: 5s
        status-lifetime: 5s
# ##############安全认证配置###############
  security:
    user:
      name: admin
      password: 123456
# ##############邮件配置##############
  mail:
    host: smtp.qq.com
    username: xx@qq.com
    password:
复制代码

安全配置类:官方配置,可直接使用

复制代码
@Configuration(proxyBeanMethods = false)
class SecurityConfig extends WebSecurityConfigurerAdapter {

    private final AdminServerProperties adminServer;

    private final SecurityProperties security;

    public SecurityConfig(AdminServerProperties adminServer, SecurityProperties security) {
        this.adminServer = adminServer;
        this.security = security;
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        SavedRequestAwareAuthenticationSuccessHandler successHandler = new SavedRequestAwareAuthenticationSuccessHandler();
        successHandler.setTargetUrlParameter("redirectTo");
        successHandler.setDefaultTargetUrl(this.adminServer.path("/"));

        http.authorizeRequests(
                        (authorizeRequests) -> authorizeRequests.antMatchers(this.adminServer.path("/assets/**")).permitAll()
                                .antMatchers(this.adminServer.path("/actuator/info")).permitAll()
                                .antMatchers(this.adminServer.path("/actuator/health")).permitAll()
                                .antMatchers(this.adminServer.path("/login")).permitAll().anyRequest().authenticated()
                ).formLogin(
                        (formLogin) -> formLogin.loginPage(this.adminServer.path("/login")).successHandler(successHandler).and()
                ).logout((logout) -> logout.logoutUrl(this.adminServer.path("/logout"))).httpBasic(Customizer.withDefaults())
                .csrf((csrf) -> csrf.csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
                        .ignoringRequestMatchers(
                                new AntPathRequestMatcher(this.adminServer.path("/instances"),
                                        HttpMethod.POST.toString()),
                                new AntPathRequestMatcher(this.adminServer.path("/instances/*"),
                                        HttpMethod.DELETE.toString()),
                                new AntPathRequestMatcher(this.adminServer.path("/actuator/**"))
                        ))
                .rememberMe((rememberMe) -> rememberMe.key(UUID.randomUUID().toString()).tokenValiditySeconds(1209600));
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication().withUser(security.getUser().getName())
                .password("{noop}" + security.getUser().getPassword()).roles("USER");
    }

}
复制代码

二、客户端搭建

1.在需要被监控的应用中引入pom依赖:

复制代码
        <!--spring-boot 的actuator 端点-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <!--spring-boot admin 的客户端-->
        <dependency>
            <groupId>de.codecentric</groupId>
            <artifactId>spring-boot-admin-starter-client</artifactId>
        </dependency>
复制代码

2.客户端配置:

 

复制代码
spring:
# #############bootAdmin的监控配置#############
boot:
admin:
client:
# ############bootAdmin的服务端的地址和用户密码############
url: http://localhost:9999
username: admin
password: 123456
instance:
prefer-ip: true
复制代码

 

复制代码
# springboot actuator 端点
management:
  endpoints:
    web:
      exposure:
# ##############开放所有端点##############
        include: '*'
  endpoint:
    health:
# ##############显示监控的详细信息##############
      show-details: always
复制代码

三、基于微服务注册中心的方式配置:admin-server会自动从注册中心拉取各个服务的信息进行监控,就不需要在每个服务服务节点单独进行配置了。

1.注册中心服务搭建

假设使用的是eureka注册中心,则注册中心服务引入pom依赖:

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

yml配置:

复制代码
spring:
  application:
    name: eureka-server
server:
  port: 8761
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka
    register-with-eureka: false
    fetch-registry: false
management:
  endpoints:
    web:
      exposure:
        include: "*"
  endpoint:
    health:
      show-details: ALWAYS
复制代码

2.admin-server更改配置

引入新的pom依赖:

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

 yml配置文件中添加配置:

复制代码
eureka:
  client:
    registryFetchIntervalSeconds: 5
    service-url:
      defaultZone: ${EUREKA_SERVICE_URL:http://localhost:8761}/eureka/
  instance:
    leaseRenewalIntervalInSeconds: 10
    health-check-url-path: /actuator/health
   metadata-map:
    user.name: ${spring.security.user.name}
    user.password: ${spring.security.user.password} management: endpoints: web: exposure: include:
"*" endpoint: health: show-details: ALWAYS
复制代码

 

启动类添加注解:@EnableDiscoveryClient

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

 

posted on   蓝迷梦  阅读(199)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)
点击右上角即可分享
微信分享提示