服务监控之SpringBoot Admin

简介

Spring Boot Admin是一个管理和健康SpringBoot应用的应用,有点绕口,其实就是用来监控SpringBoot应用的,这些应用可以通过Spring Boot Admin Client或Spring Cloud自动发现的方式注册到Admin Server。

一、Admin Client方式注册

Admin Server搭建

  • 引入server依赖的包

    <dependency>
      <groupId>de.codecentric</groupId>
      <artifactId>spring-boot-admin-starter-server</artifactId>
      <version>2.2.2</version>
    </dependency>
    
  • 启动类加上@EnableAdminServer注解

    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import de.codecentric.boot.admin.server.config.EnableAdminServer;
    
    @SpringBootApplication
    @EnableAdminServer
    public class SpringBootAdminApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(SpringBootAdminApplication.class, args);
        }
    
    }
    

启动项目,然后访问http://localhost:8080/,到此Admin Server就搭建好了,跟Eureka Server似的,一包一注释,仗势走天涯。

image-20200421161507822

Admin Client注册

  • 引入client依赖包

    Spring Boot Admin是使用actuator实现的服务监控,所以在client应用中需要引入actuator的包,并开放相关的接口,否则监控的信息不完整。

    <dependency>
      <groupId>de.codecentric</groupId>
      <artifactId>spring-boot-admin-starter-client</artifactId>
      <version>2.2.2</version>
    </dependency>
    
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    
  • 配置

    server:
      port: 8888
    spring:
      application:
        name: boot-log
      boot:
        admin:
          client:
            url: http://127.0.0.1:8080 # 指定admin-server注册地址,和Eureka更像了
    management:
      endpoints:
        web:
          exposure:
            include: "*" # 暴露actuator所有接口
    

至此就完成了Admin Client端的配置,可以启动服务了,查看admin页面

image-20200421163624584

二、Eureka注册中心自动发现

使用Eureka可以解放Client端的配置,不再需要给Client端配置任何东西(除actuator暴露接口列表)

Eureka Server搭建

搭建Eureka Server就不做特殊解释了,直接贴配置代码。

<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
server:
  port: 9001 #服务端口
eureka:
  instance:
    hostname: localhost
  client:
    register-with-eureka: false #是否将eureka自身作为应用注册到eureka注册中心
    fetch-registry: false #为true时,可以启动,但报异常:Cannot execute request on any known server
    serviceUrl:
      defaultZone: http://localhost:9001/eureka/
  server:
    enable-self-preservation: false
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

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

启动之后访问http://127.0.0.1:9001/进入Eureka控制台

Admin Server搭建

Admin Server的搭建方式和第一种相差无几,唯一的差别就是需要把Server应用注册到Eureka中

  • 在启动类加上@EnableEurekaClient注解
  • 在配置文件中加上eureka.client.service-url.defaultZone=http://127.0.0.1:9001/eureka配置

Admin Client搭建

Admin Client正常配置注册到Eureka,然后暴露actuator的相关接口management.endpoints.web.exposure.include=*

启动client,查看Eureka和Admin控制台

  • Eureka Server

    image-20200421171038407

  • Admin Server

    image-20200421171100096

三、Spring Boot Admin提供了哪些功能

image-20200421171313326

就不一一赘述了,搭建了玩玩就都明白了。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值