springboot monitor 监控中心搭建

本文介绍了如何创建一个Spring Boot Admin监控中心,并配置Eureka客户端进行服务监控。首先,展示了监控中心的启动类,启用AdminServer和DiscoveryClient,然后详细配置了Eureka的相关参数。接着,解释了客户端的配置,同样启用EurekaClient,并特别注意了servlet路径的配置。最后,列出了监控中心和客户端的依赖项。通过这些配置,可以实现微服务的健康检查和详细状态展示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

创建监控中心,在这之前需要已经有了eureka注册中心。
EnableAdminServer 表示为监控中心
EnableDiscoveryClient表示为监控的客户端

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

```java
server:
  port: 28762
spring:
  security:
    user:
      name: admin
      password: admin
  application:
    name: springboot-monitor
management:
  endpoints:
    web:
      exposure:
        include: "*"
  endpoint:
    health:
      show-details: always

eureka:
  instance:
    lease-expiration-duration-in-seconds: 30 # 心跳,租约续约频率,单位:秒
    lease-renewal-interval-in-seconds: 10 #  eureka server多久没有收到心跳,则表示对应的实例过期,单位:秒。
    health-check-url-path: /actuator/health
    statusPageUrlPath: /actuator/health
    prefer-ip-address: true #以IP地址注册到服务中心,相互注册使用IP地址
    # ip-address: 127.0.0.1 #强制指定IP地址,默认会获取本机的IP地址
    instance-id: ${spring.application.name}:${spring.cloud.client.ip-address}:${server.port}
  client:
    fetch-registry: true #通过设置fetch-registry与register-with-eureka 表明自己是一个eureka服务
    register-with-eureka: true #服务注册中心也会将自己作为客户端来尝试注册自己,true(默认)时自动生效
    service-url:
      defaultZone: http://localhost:28761/eureka/

pom配置

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

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

之后是创建客户端 ,基本和监控中心差不多,但是需要注意的是如果配置了servlet 路径,需要在eureka 里面额外指定出来,不然监控不到很多数据。

启动类

@EnableTransactionManagement
@MapperScan("com.aaa.aaa.aaa.mapper")
@EnableCircuitBreaker
**@EnableEurekaClient**
@EnableFeignClients(basePackages ={"com.aaa.*"})
@ComponentScan(basePackages = {"com.aaa.aaa.*"})
@SpringBootApplication
public class DeviceApplication {

    public static void main(String[] args) {
        SpringApplication.run(DeviceApplication.class, args);
    }
spring:
  application:
    name: baop-device

server:
  port: 8888
  servlet:
    context-path: /device #注意这个对监控的影响

management:
  endpoints:
    web:
      exposure:
        include: "*"
  endpoint:
    health:
      show-details: ALWAYS
#上面两段配置就是支持admin监控的配置
eureka:
  instance:
    lease-expiration-duration-in-seconds: 30 # 心跳,租约续约频率,单位:秒
    lease-renewal-interval-in-seconds: 10 #  eureka server多久没有收到心跳,则表示对应的实例过期,单位:秒。
    health-check-url-path: /actuator/health
    statusPageUrlPath: /actuator/health
    prefer-ip-address: true #以IP地址注册到服务中心,相互注册使用IP地址
    instance-id: ${spring.application.name}:${spring.cloud.client.ip-address}:${server.port}
    metadata-map:
      management:
        context-path: ${server.servlet.context-path}/actuator#注意这个配置 如果没有就不用配置 其他都是常见的eureka配置
  client:
    fetch-registry: true #通过设置fetch-registry与register-with-eureka 表明自己是一个eureka服务
    register-with-eureka: true #服务注册中心也会将自己作为客户端来尝试注册自己,true(默认)时自动生效
    service-url:
      defaultZone: http://localhost:28761/eureka/
 <dependency>
                <groupId>de.codecentric</groupId>
                <artifactId>spring-boot-admin-starter-client</artifactId>
                <version>2.4.1</version>
            </dependency>
			<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-actuator</artifactId>
		</dependency>

最后的效果如下图:
监控两个应用,一个是admin监控本身,一个是客服端
点开应用如下图:
在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值