Spring Boot Admin 环境搭建与基本使用

Spring Boot Admin 环境搭建与基本使用

一、Spring Boot Admin是什么

它是用于监控和管理Spring Boot应用程序的开源工具。它为开发人员或者是运维人员提供了友好的Web界面。可以实时监控管理部署在不同环境中的Spring Boot应用。

二、提供了那些功能

  1. 应用程序监控:可以显示程序的基本信息:内存使用情况、线程信息。
  2. 应用程序管理:可以管理监控的应用:动态配置日志的级别。
  3. 通知和报警:可以配置通知和警报,当应用程序出现问题或者叨叨预定的阈值时及时通知相关人员。
  4. 微服务支持:可以适用微服务架构,一次性监控和管理多个微服务应用。
  5. 安全性:可以与Spring Security集成,实现对监控和管理界面的访问控制。

三、 使用Spring Boot Admin

示例项目整体结构:

这里为什么要使用Eureka,主要是想体现复用的思想。所有服务都注册到了Eureka之后,而Spring Boot Admin只要集成了Eureka之后就能够获取到所有的服务信息注册信息。能够对所有注册到Eureka中的服务进行监控和管理。

Eureka的搭建可以参考这篇博客:【Spring Cloud 三】Eureka服务注册与服务发现

3.1搭建Spring Boot Admin服务

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>2.3.12.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.wangwei</groupId>
    <artifactId>admin-server-05</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>05-admin-server</name>
    <description>05-admin-server</description>
    <properties>
        <java.version>8</java.version>
        <spring-boot-admin.version>2.3.0</spring-boot-admin.version>
        <spring-cloud.version>Hoxton.SR12</spring-cloud.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>de.codecentric</groupId>
            <artifactId>spring-boot-admin-starter-server</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

            <dependency>
                <groupId>de.codecentric</groupId>
                <artifactId>spring-boot-admin-dependencies</artifactId>
                <version>${spring-boot-admin.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

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

</project>

yml配置文件

yaml 复制代码
##???
server:
  port: 10086  #端口号 0-65535

spring:
    application:
      name: admin-server


eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka
    register-with-eureka: true #设置为fasle 不往eureka-server注册,默认为true
    fetch-registry: true #应用是否拉取服务列表到本地
    registry-fetch-interval-seconds: 10 #为了缓解服务列表的脏读问题,时间越短脏读越少 性能相应的消耗回答


  instance: #实例的配置
    instance-id: ${eureka.instance.hostname}:${spring.application.name}:${server.port}
    hostname: localhost #主机名称或者服务ip
    prefer-ip-address: true #以ip的形式显示具体的服务信息
    lease-renewal-interval-in-seconds: 10 #服务实例的续约时间间隔

management:
  endpoints:
    web:
      exposure:
        include:  '*' #暴露所有的监控端点 #如果一个服务需要被监控,那么就要将自身的一些清苦(一些信息接口)暴露出去

启动类

java 复制代码
@SpringBootApplication
@EnableEurekaClient
@EnableAdminServer //#开启admin服务端
public class AdminServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(AdminServerApplication.class, args);
    }

}

启动admin服务效果

3.2 common-api

这个模块是抽离出来的提供接口用于两个服务之间的跨服务调用。之后由服务消费者集成。

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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>feign-project</artifactId>
        <groupId>com.wangwei</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>common-api</artifactId>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>com.wangwei</groupId>
            <artifactId>project-domain</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
        </dependency>

       

    </dependencies>

</project>
feign
java 复制代码
@FeignClient(value = "order-service",fallback = UserOrderFeignHystrix.class)
public interface UserOrderFeign {

    @GetMapping("order/getOrderByUserId/{id}")
    Order getOrderByUserId (@PathVariable("id")Integer id);

}
hystrix
java 复制代码
@Component
public class UserOrderFeignHystrix implements UserOrderFeign {
    /**
     * 一般远程调用的熔断可以直接返回null
     * @param id
     * @return
     */
    @Override
    public Order getOrderByUserId(Integer id) {
        return null;
    }
}

3.3服务消费者

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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>feign-project</artifactId>
        <groupId>com.wangwei</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>user-center</artifactId>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>
    <dependencies>
        <!--用于在应用程序中添加各种监控和管理功能-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>com.wangwei</groupId>
            <artifactId>common-api</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>

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

</project>

yml配置文件

yaml 复制代码
server:
  port: 8081

spring:
    application:
      name: user-service
    


eureka:
  client:
    service-url: #??????
      defaultZone: http://localhost:8761/eureka
    register-with-eureka: true #设置为fasle 不往eureka-server注册
    fetch-registry: true #应用是否拉取服务列表到本地
    registry-fetch-interval-seconds: 10 #为了缓解服务列表的脏读问题,时间越短脏读越少 性能相应的消耗回答


  instance: #实例的配置
    instance-id: ${eureka.instance.hostname}:${spring.application.name}:${server.port}
    hostname: localhost #主机名称或者服务ip
    prefer-ip-address: true #以ip的形式显示具体的服务信息
    lease-renewal-interval-in-seconds: 10 #服务实例的续约时间间隔
feign:
  hystrix:
    enabled: true #开启熔断
management:
  endpoints:
    web:
      exposure:
        include: '*'

启动类

java 复制代码
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
public class UserServiceApplication {
    public static void main(String[] args) {
        SpringApplication.run(UserServiceApplication.class,args);
    }
}

controller

java 复制代码
@RestController
public class UserController {

    @Autowired
    private UserOrderFeign userOrderFeign;

    @GetMapping("findOrder")
    public Order findOrder(){
        return userOrderFeign.getOrderByUserId(1);
    }

}

3.4服务提供者

服务提供者与服务消费者的主要区别是没有依赖actuator以及对应的暴露端点的配置。所以在admin的Web页面中是不为看到服务提供者的详细信息。

xml 复制代码
 <!--用于在应用程序中添加各种监控和管理功能-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>feign-project</artifactId>
        <groupId>com.wangwei</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>order-center</artifactId>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>com.wangwei</groupId>
            <artifactId>common-api</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>

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

    </dependencies>

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

</project>

yml配置文件

yaml 复制代码
server:
  port: 8080

spring:
    application:
      name: order-service

    
eureka:
  client:
    service-url: #??????
      defaultZone: http://localhost:8761/eureka
    register-with-eureka: true #设置为fasle 不往eureka-server注册
    fetch-registry: true #应用是否拉取服务列表到本地
    registry-fetch-interval-seconds: 10 #为了缓解服务列表的脏读问题,时间越短脏读越少 性能相应的消耗回答


  instance: #实例的配置
    instance-id: ${eureka.instance.hostname}:${spring.application.name}:${server.port}
    hostname: localhost #主机名称或者服务ip
    prefer-ip-address: true #以ip的形式显示具体的服务信息
    lease-renewal-interval-in-seconds: 10 #服务实例的续约时间间隔

项目启动类

java 复制代码
@SpringBootApplication
@EnableEurekaClient
public class OrderServiceApplication {
    public static void main(String[] args) {
        SpringApplication.run(OrderServiceApplication.class,args);
    }
}

controller

java 复制代码
@RestController
public class OrderController  {

    @GetMapping("order/getOrderByUserId/{id}")
    Order getOrderByUserId (@PathVariable("id")Integer id){
        System.out.println(id);
        Order order=Order.builder()
                .name("青椒肉丝盖饭")
                .price(15D)
                .orderId(1)
                .build();
        return order;
    }



}

服务整体启动之后的效果



由于Eureka服务没有依赖actuator所以不能看到详细信息。

四、 总结

  1. 本篇博客主要是对于Spring Boot Admin的基本认识和基本运用,通过本篇博客能够对Spring Boot Admin有一个宏观认知和能够快速上手。
  2. Spring Boot Admin还可以设置通知可报警,本篇博客并没有涉及到。
相关推荐
码智社15 分钟前
AES加密原理详解及Java实现加解密实战
java·开发语言
萧瑟余晖1 小时前
JDK 26 新特性详解
java·开发语言
马优晨2 小时前
Freemarker 完整讲解(后端 Java 模板引擎)
java·开发语言·freemarker·freemarker 完整讲解·freemarker模板引擎
weixin_446260853 小时前
HACO:面向动态部署环境的对冲式智能计算可靠多智能体调度框架
后端·python·flask
ttwuai4 小时前
Cursor 生成 CRUD 后,Go 后台接口别只测 200:JWT、RBAC 和 tenant_id 怎么验
开发语言·后端·golang
用户8356290780514 小时前
Python 实现 Excel 页面布局与打印设置自动化
后端·python
维天说4 小时前
CLI-Switch 2026年3月版历史设计:Hook、TTY 隔离与 JSON 状态
java·服务器·json
用户9931441579845 小时前
微服务框架中获取用户信息
后端
xuanWb5 小时前
手写一个 LLM API 网关:Anthropic 与 OpenAI 协议转换的完整实现
后端