基于spring-boot-admin实现对应用、数据库、nginx等监控

1、服务端相关配置

1.1 yml配置

复制代码
service:
  application:
    name: spring-boot-monitor-server
server:
  port: 9900
spring:
  boot:
    admin:
      ui:
        title: 运维监控中心
        brand: <span class="brand">运维监控中心</span>   # 纯文字即可
        default-language: zh   # 启动即中文
        # 让 SBA 使用内置登录模板
        login-icon: "assets/img/icon-spring-boot-admin.svg"
  web:
    resources:
      static-locations: classpath:/static/,classpath:/i18n/

1.2 pom.xml配置

复制代码
<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.5.7</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.maker.monitor</groupId>
    <artifactId>maker-monitor</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>maker-monitor</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>17</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- 存在于 spring-boot-admin-starter-server,但需手动再引一次 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.json</include>
                    <include>**/*.yml</include>
                </includes>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

1.3 服务端增加认证授权配置

复制代码
package com.maker.monitor.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.NoOpPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;
import org.springframework.security.web.SecurityFilterChain;

@Configuration
@EnableWebSecurity   // 必须
public class AdminSecurityConfig {

    /* 内存账号,生产可换 JDBC / LDAP */
    @Bean
    public UserDetailsService users() {
        UserDetails user = User.builder()
                .username("admin")
                /* 明文 123456 */
                .password("123456")
                .roles("ADMIN")
                .build();
        return new InMemoryUserDetailsManager(user);
    }

    /* 密码编码器 */
    @Bean
    public PasswordEncoder passwordEncoder() {
//        return new BCryptPasswordEncoder();
        return NoOpPasswordEncoder.getInstance(); // **明文比对**
    }

    /* 安全规则 */
    @Bean
    public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
        http
                .authorizeHttpRequests(auth -> auth
                        /* 1. 登录页、静态资源放行 */
                        .requestMatchers(HttpMethod.GET,"/applications/**", "/instances/**","/login", "/assets/**", "/actuator/**").permitAll()
                        .requestMatchers(HttpMethod.POST, "/instances").permitAll()
                        /* 2. 其余都需要认证 */
                        .anyRequest().hasRole("ADMIN"))
                /* 3. 表单登录 */
                .formLogin(form -> form
                        .loginPage("/login")          // 用 SBA 自带登录页
                        .defaultSuccessUrl("/", true)
                        .permitAll())
                /* 4. 登出 */
                .logout(logout -> logout
                        .logoutUrl("/logout")
                        .logoutSuccessUrl("/login?logout"))
                /* 5. 关闭 csrf(如用 postman 测试) */
                .csrf(csrf -> csrf.disable());
        return http.build();
    }
}

2、客户端相关配置

2.1 yml配置

复制代码
server:
  port: 8080
spring:
  application:
    name: business-client
    # 1. Redis
  data:
    redis:
      host: ${REDIS_HOST:100.128.180}
      port: ${REDIS_PORT:9379}
      password: ${REDIS_PWD:1234567890}
    # 2. 达梦
    dameng:
      url: ${DM_URL:jdbc:dm://100.128.180.88:16325/xxxx}
      username: ${DM_USER:xxxx}
      password: ${DM_PWD:323423424}
    # 3. Nginx
    nginx:
      status-url: ${NGINX_STATUS:http://100.128.180.88/nginx_status}
    # 4. 东方通 TongWeb
    tongweb:
      jmx-url: ${TONGWEB_JMX:service:jmx:rmi:///jndi/rmi://10.128.180.88:9060/jmxrmi}
  boot:
    admin:
      client:
        url: http://localhost:9900  # 服务端地址
#        username: admin                     # 登录账号
#        password: 123456                    # 登录密码
        instance:
          # 3.x 已废弃 prefer-ip,直接给 service-url
          service-url: http://${HOST_IP:localhost}:${server.port:8080}
          name: ${spring.application.name}


management:
  server:
    port: ${server.port:8080}
  health:
    redis:
      enabled: true
    dameng:
      enabled: true
    nginx:
      enabled: true   # 需要你在项目里自己实现 NginxHealthIndicator
    tongweb:
      enabled: false   # 同样要求你自定义过 TongWebHealthIndicator
    # --- 5. 其他 ---
    elasticsearch:
      enabled: false
    mail:
      enabled: false
    diskSpace:
      enabled: true
    defaults:
      enabled: true
  endpoints:
    web:
      exposure:
        include: "*"   # 至少要有 env   # 至少暴露 health
  endpoint:
    env:
      enabled: true
      show-values: always
    info:
      enabled: true
    health:
      show-details: always
      probes:
        enabled: true      # 支持 k8s 探针,可选
probe:
  services:
    - name: 档案管理系统(OLD)
      url: http://110.8.174.71:8088/api/itsm/#/login
      expect-code: 200
    - name: GZW互联网系统
      url: https://opweb.sasac.gov.cn
      expect-code: 200
    - name: GZW官网
      url: http://www.sasac.gov.cn/
      expect-code: 200
    - name: GZW监管网系统
      url: https://opxxx.sasac.gov.cn
      expect-code: 200

2.2、pom.xml配置

复制代码
<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.5.7</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.maker.monitor.client</groupId>
    <artifactId>multi-middleware-monitor</artifactId>
    <version>1.0</version>
    <name>business-client</name>
    <description>business-client</description>

    <properties>
        <java.version>17</java.version>
    </properties>
    <dependencies>
        <!-- 一定要带 web 容器,否则启动完 main 方法就结束 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>de.codecentric</groupId>
            <artifactId>spring-boot-admin-starter-client</artifactId>
            <version>3.3.6</version>
        </dependency>
        <dependency>
            <groupId>com.zaxxer</groupId>
            <artifactId>HikariCP</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents.client5</groupId>
            <artifactId>httpclient5</artifactId>
        </dependency>

        <!--  必须暴露 actuator  -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <!-- Redis -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
        <!-- 达梦 JDBC(本地 lib 方式) -->
        <dependency>
            <groupId>com.dameng</groupId>
            <artifactId>DmJdbcDriver18</artifactId>
            <version>8.1.3.62</version>
        </dependency>
        <!-- 东方通 JMX 远程 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-validation</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

3、展示

4、代码下载

4.1服务端下载

通过网盘分享的文件:maker-monitor.zip

链接: https://pan.baidu.com/s/1iV9et3tXmluDv5BZFVcxyw?pwd=trwd 提取码: trwd

4.2客户端下载

通过网盘分享的文件:monitor-business-client.zip

链接: https://pan.baidu.com/s/1MqJ9oaeIpLCjkQ_WPnlm7A?pwd=svu5 提取码: svu5

相关推荐
vx_vxbs6623 分钟前
【SSM电影网站】(免费领源码+演示录像)|可做计算机毕设Java、Python、PHP、小程序APP、C#、爬虫大数据、单片机、文案
java·spring boot·python·mysql·小程序·php·idea
SunnyDays10111 小时前
如何使用 Java 删除 Word 文档中的水印
java·删除word文档水印
大锦终1 小时前
【MySQL】内置函数
数据库·mysql
猿小喵1 小时前
索引优化-MySQL性能优化
数据库·mysql·性能优化
毕设源码-邱学长1 小时前
【开题答辩全过程】以 基于Java企业人事工资管理系统为例,包含答辩的问题和答案
java·开发语言
转转技术团队1 小时前
回收系统架构演进实战:与Cursor结对扫清系统混沌
java·架构·cursor
AI分享猿1 小时前
Java后端实战:SpringBoot接口遇异常请求,轻量WAF兼顾安全与性能
java·spring boot·安全
a***59262 小时前
用nginx正向代理https网站
运维·nginx·https
稚辉君.MCA_P8_Java2 小时前
Gemini永久会员 Java中的四边形不等式优化
java·后端·算法
DKPT2 小时前
ZGC和G1收集器相比哪个更好?
java·jvm·笔记·学习·spring