Spring-Boot-Admin(服务监控)

  1. 版本说明
    SpringBoot3.0 + SpringBootAdmin3.0.Y-->测试发现监控中心服务端无法监控服务端自身服务,安全配
    置存在类和方法找不到的问题,后来将版本改为SpringBoot2.7 + SpringBootAdmin2.7.Y-->问题解决。
  2. pom.xml
    <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-security -->
    <dependency>
    	<groupId>org.springframework.boot</groupId>
    	<artifactId>spring-boot-starter-security</artifactId>
    	<version>2.7.6</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/de.codecentric/spring-boot-admin-starter-server -->
    <dependency>
    	<groupId>de.codecentric</groupId>
    	<artifactId>spring-boot-admin-starter-server</artifactId>
    	<version>2.7.7</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/de.codecentric/spring-boot-admin-starter-client -->
    <dependency>
    	<groupId>de.codecentric</groupId>
    	<artifactId>spring-boot-admin-starter-client</artifactId>
    	<version>2.7.7</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
    <dependency>
    	<groupId>org.projectlombok</groupId>
    	<artifactId>lombok</artifactId>
    	<version>1.18.24</version>
    	<scope>provided</scope>
    </dependency>
  3. application.yml
    server:
      port: 8181
      servlet:
        context-path: /admin-server
    spring:
      application:
        name: admin-server
    
    --- # 监控中心服务端配置
    spring:
      security:
        user:
          name: vvvvvv
          password: 123456
      boot:
        admin:
          ui:
            title: MY服务监控中心
          context-path: /kkkkkk
    
    --- # Actuator 监控端点的配置项
    management:
      endpoints:
        web:
          exposure:
            include: '*'
      endpoint:
        health:
          show-details: ALWAYS
        logfile:
          external-file: ./logs/admin-server.log
    
    --- # 监控配置
    spring.boot.admin.client:
      # 增加客户端开关
      enabled: true
      # 设置 Spring Boot Admin Server 地址
      url: http://127.0.0.1:8181/admin-server/kkkkkk
      instance:
        service-url: http://127.0.0.1:8181/admin-server
      username: vvvvvv
      password: 123456
    
  4. 补充说明
    1.项目访问地址:http://localhost:8181/admin-server
    server:
      port: 8181
      servlet:
        context-path: /admin-server
    
    2.监控中心该项目的服务名称:admin-server
    spring:
      application:
        name: admin-server
    
    3.监控中心访问地址:http://localhost:8181/admin-server/kkkkkk (用户名:vvvvvv 密码:123456)
    spring:
      security:
        user:
          name: vvvvvv
          password: 123456
      boot:
        admin:
          ui:
            title: MY服务监控中心
          context-path: /kkkkkk
    
    4.将监控中心自身服务添加到监控中心
    --- # Actuator 监控端点的配置项
    management:
      endpoints:
        web:
          exposure:
            include: '*'
      endpoint:
        health:
          show-details: ALWAYS
        logfile:
          external-file: ./logs/admin-server.log
     
    --- # 监控配置
    spring.boot.admin.client:
      # 增加客户端开关
      enabled: true
      # 设置 Spring Boot Admin Server 地址
      url: http://127.0.0.1:8181/admin-server/kkkkkk # 监控中心访问地址
      instance:
        service-url: http://127.0.0.1:8181/admin-server # 项目访问地址
      username: vvvvvv
      password: 123456
  5. 监控配置
     
    @Configuration
    @EnableAdminServer
    public class AdminServerConfig {
    
        @Lazy
        @Bean(name = TaskExecutionAutoConfiguration.APPLICATION_TASK_EXECUTOR_BEAN_NAME)
        @ConditionalOnMissingBean(Executor.class)
        public ThreadPoolTaskExecutor applicationTaskExecutor(TaskExecutorBuilder builder) {
            return builder.build();
        }
    
    }
  6. 安全配置
    @EnableWebSecurity
    public class SecurityConfig {
    
        private final String adminContextPath;
    
        public SecurityConfig(AdminServerProperties adminServerProperties) {
            this.adminContextPath = adminServerProperties.getContextPath();
        }
    
        @Bean
        public SecurityFilterChain filterChain(HttpSecurity httpSecurity) throws Exception {
            SavedRequestAwareAuthenticationSuccessHandler successHandler = new SavedRequestAwareAuthenticationSuccessHandler();
            successHandler.setTargetUrlParameter("redirectTo");
            successHandler.setDefaultTargetUrl(adminContextPath + "/");
    
            return httpSecurity
                    .headers().frameOptions().disable()
                    .and().authorizeRequests()
                    .antMatchers(adminContextPath + "/assets/**"
                        , adminContextPath + "/login"
                        , "/actuator"
                        , "/actuator/**"
                    ).permitAll()
                    .anyRequest().authenticated()
                    .and()
                    .formLogin().loginPage(adminContextPath + "/login")
                    .successHandler(successHandler).and()
                    .logout().logoutUrl(adminContextPath + "/logout")
                    .and()
                    .httpBasic().and()
                    .csrf()
                    .disable()
                    .build();
        }
    
    }
  7. 监控通知
    被监控的服务上线或者下线将触发监控通知,可在此处发邮件或者短信通知相关运维人员。
    @Slf4j
    @Component
    public class CustomNotifier extends AbstractEventNotifier {
    
        protected CustomNotifier(InstanceRepository repository) {
            super(repository);
        }
    
        @Override
        @SuppressWarnings("all")
        protected Mono<Void> doNotify(InstanceEvent event, Instance instance) {
            return Mono.fromRunnable(() -> {
                // 实例状态改变事件
                if (event instanceof InstanceStatusChangedEvent) {
                    String registName = instance.getRegistration().getName();
                    String instanceId = event.getInstance().getValue();
                    String status = ((InstanceStatusChangedEvent) event).getStatusInfo().getStatus();
                    log.info("Instance Status Change: [{}],[{}],[{}]", registName, instanceId, status);
                }
    
            });
        }
    
    }
  8. 监控中心
    主要的功能点有:显示应用程序的监控状态、应用程序上下线监控、查看 JVM、线程
    信息、可视化的查看日志以及下载日志文件、动态切换日志级别、Http 请求信息跟踪


  9. 添加服务
    1.pom.xml
    <!-- https://mvnrepository.com/artifact/de.codecentric/spring-boot-admin-starter-client -->
    <dependency>
    	<groupId>de.codecentric</groupId>
    	<artifactId>spring-boot-admin-starter-client</artifactId>
    	<version>2.7.7</version>
    </dependency>
    
    2.application.yml
    server:
      port: 8282
      servlet:
        context-path: /admin-client
    spring:
      application:
        name: admin-client
    
    --- # Actuator 监控端点的配置项
    management:
      endpoints:
        web:
          exposure:
            include: '*'
      endpoint:
        health:
          show-details: ALWAYS
        logfile:
          external-file: ./logs/admin-client.log
    
    --- # 监控配置
    spring.boot.admin.client:
      # 增加客户端开关
      enabled: true
      # 设置 Spring Boot Admin Server 地址
      url: http://127.0.0.1:8181/admin-server/kkkkkk
      instance:
        service-url: http://127.0.0.1:8282/admin-client
        name: client
      username: vvvvvv
      password: 123456


  10. 项目简介

    用于对 Spring Boot 应用的管理和监控。可以用来监控服务是否健康、是否在线、以及一些jvm数据等等。
    
    Spring Boot Admin 分为服务端(spring-boot-admin-server)和客户端(spring-boot-admin-client),
    服务端和客户端之间采用 http 通讯方式实现数据交互;单体项目中需要整合 spring-boot-admin-client
    才能让应用被监控。
    
    在 SpringCloud 项目中,spring-boot-admin-server 是直接从注册中心抓取应用信息,不需要每个微服
    务应用整合 spring-boot-admin-client 就可以实现应用的管理和监控。
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
要将nacos和spring-boot-admin集成,需要进行以下步骤: 1. 在pom.xml中添加以下依赖: ```xml <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> <dependency> <groupId>de.codecentric</groupId> <artifactId>spring-boot-admin-starter-server</artifactId> <version>2.2.3</version> </dependency> ``` 2. 在application.properties中添加以下配置: ```properties spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848 # nacos服务地址 spring.application.name=spring-boot-admin-server # 应用名 server.port=8080 # 服务端口 spring.boot.admin.server.url=http://localhost:${server.port} # spring-boot-admin服务地址 spring.boot.admin.client.url=http://localhost:${server.port} # spring-boot-admin客户端地址 management.endpoints.web.exposure.include=* # 开启所有端点 ``` 3. 在启动类上添加@EnableDiscoveryClient和@EnableAdminServer注解: ```java @SpringBootApplication @EnableDiscoveryClient @EnableAdminServer public class SpringBootAdminApplication { public static void main(String[] args) { SpringApplication.run(SpringBootAdminApplication.class, args); } } ``` 4. 在需要监控的应用中添加spring-boot-starter-actuator依赖,并在application.properties中添加以下配置: ```properties spring.boot.admin.client.url=http://localhost:8080 # spring-boot-admin服务地址 spring.boot.admin.client.username=admin # 认证用户名 spring.boot.admin.client.password=admin # 认证密码 ``` 5. 启动nacos和spring-boot-admin服务,然后启动需要监控的应用,即可在spring-boot-admin控制台中查看应用的监控信息。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

童心同萌

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值