作者:汤圆

个人博客:

SpringBootAdmin

简介

用来监视SpringBoot程序,界面用 Vue 实现,功能类似 Actuator

示例

  • 先编写服务端

pom.xml

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

AdminServerApplication.java

@SpringBootApplication
@EnableAdminServer

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

application.yml

server:
  port: 8090

然后启动服务:mvn spring-boot:run

  • 再编写客户端(客户端要配置服务端的地址),客户端会自动注册到服务端

pom.xml

<dependencies>
    <dependency>
        <groupId>de.codecentric</groupId>
        	<artifactId>spring-boot-admin-starter-client</artifactId>
        <version>2.4.0</version>
    </dependency>
</dependencies>

AdminClientApplication.java

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

application.yml

spring:
  boot:
    admin:
      client:
        instance:
          # 配置admin-client地址,即客户端本地地址
          service-base-url: "http://localhost:8080"
        # 配置admin-server地址
        url: "http://localhost:8090"
management:
  endpoints:
    web:
      exposure:
        # 打开所有端点
        include: "*"

PS:感觉上面的两个URL命名有歧义,每次看都会混淆

再启动客户端:mvn spring-boot:run

  • 如果不出意外,就可以在http://localhost:8090监控应用了

image-20210308123007684

问题总结

问题1

  • 问题描述:

    ​ 在本地开启服务后,再启动客户端,注册失败,服务端报错nested exception is .UnknownHostException: failed to resolve 'Jalon' after 4 queries

  • 原因分析:

    ​ Jalon是我的计算机名称,这里报错指的是解析不了计算机名

  • 解决办法:

    ​ 客户端配置spring.boot.admin.client.instance.service-base-url="http://localhost:8080"即可;

    查看官网 spring-boot-admin-client 的配置参数,有一个spring.boot.admin.client.instance.service-base-url,

    这个就是用来配置客户端本地地址和端口号的,如下图

    image-20210226101413554

问题2

  • 问题描述:

    ​ 在本地开启服务后,再启动客户端,注册失败,客户端报错Failed to register application as Application at spring-boot-admin ([http://localhost:8090/instances]): 404

  • 原因分析:

    ​ 服务端没有开起来,缺少注解@EnableAdminServer

  • 解决办法:

    ​ 在启动类添加注解@EnableAdminServer

问题3

  • 问题描述:

    ​ 在本地开启服务后,再启动客户端,注册失败,客户端报错I/O error on POST re quest for "http://localhost:8090/instances": Read timed out; nested exception is .SocketTimeoutException: Read timed out

  • 原因分析:

    • 通过手动访问http://localhost:8090/instances,说明服务器是没问题的,所以问题应该在客户端这边
    • 结果客户端再次重启,就注册成功了,猜测应该是刚才服务器刚开起来,还没开启成功,客户端就开始启动注册
  • 解决办法:

    • 这种问题一般就是卡了时间差,解决办法就是重启客户端

参考

github地址