基于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

相关推荐
行者游学3 小时前
ETCD 权限配置
数据库·etcd
一叶飘零_sweeeet3 小时前
手写 RPC 框架
java·网络·网络协议·rpc
Zhao_yani4 小时前
Apache Drill 连接 MySQL 或 PostgreSQL 数据库
数据库·mysql·postgresql·drill
脸大是真的好~4 小时前
黑马JAVAWeb-01 Maven依赖管理-生命周期-单元测试
java·maven
IT小哥哥呀5 小时前
Nginx高可用配置实战:负载均衡 + 健康检查 + 动态扩展
运维·nginx·负载均衡·devops·日志分析·openresty·动态扩展
惺忪97985 小时前
QAbstractListModel 详细解析
数据库
zhangkaixuan4565 小时前
Apache Paimon 查询全流程深度分析
java·apache·paimon
cici158746 小时前
MyBatis注解的运用于条件搜索实践
java·tomcat·mybatis
国服第二切图仔6 小时前
Rust开发实战之操作SQLite数据库——从零构建数据持久化应用
数据库·rust·sqlite