springboot-actuator-admin-eureka

本文介绍如何使用spring-boot-actuator、spring-boot-eureka及spring-boot-admin实现应用信息查看、服务管理和监控等功能。通过实例展示了各组件的具体配置和使用方法。

首先是spring-boot-actuator具体点此  它可以查看应用的一些信息,具体使用为:

在springboot基础上,添加依赖

     <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>

        </dependency>

-----------------------------------------------------------------------

然后配置文件yml中:

server:
  port: 8080
management:
  security:
    enabled: false #关掉安全认证
  port: 8088 #管理端口可以设置
  context-path: /monitor #actuator的访问路径,访问时http://localhost:8088/monitor/sth
endpoints:
  shutdown:
    enabled: true

info:
   app:
      name: spring-boot-actuator

      version: 1.0.0

应用启动后,在http://localhost:8088/monitor/后加上info或health等就可以查看应用的信息了。

-------------------------------------------------------------------------------------------------------

然后是spring-boot-eureka,应用可以注册到这上面去,它管理各种服务功能包括服务的注册、发现、熔断、负载、降级等。

具体点此  和

blog.csdn.net/u010066934/article/details/54232622#commentBox

简单使用:

A,Server端

依赖:

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.9.RELEASE</version>
        <relativePath />
    </parent>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <spring-cloud.version>Edgware.SR2</spring-cloud.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka-server</artifactId>
        </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>
        </dependencies>

    </dependencyManagement>

-------------------------------------------------------------

app类上加上@EnableEurekaServer注解

----------------------------------------------

properties配置:

server.port=8761
spring.application.name=registry-center
#secure
security.basic.enabled=true
security.user.name=user1
security.user.password=123456
security.user.role=admin

eureka.instance.hostname=peer1

#下面2个禁止注册自己,不然端口抢占出错

eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
eureka.instance.status-page-url-path=/info
eureka.instance.health-check-url-path=/health

---------------------------------------------------------------

Client端

依赖:spring-cloud-starter-eureka

----------------------------------------------

配置:

server.port=8762
spring.application.name=app1

eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/

-----------------------------------------------------------------------------

主类加上@EnableDiscoveryClient注解

------------------------------------------------------

然后依次启动server和client,client可以多开几个,端口号不重就行。访问8761,就可以看到注册的服务啦。

----------------------------------------------------------------------------------------------------------------

最后spring-boot-admin,因为它只能监控单个应用,所以一般配合eureka或zookeeper来使用。具体点此

因为admin的单独使用没什么意义,所以下面只展示配合eureka一起使用的操作。

构建了父级maven项目,pom如下:

<modules>
        <module>spring-boot-admin-server</module>
        <module>spring-cloud-eureka</module>
        <module>spring-cloud-producer</module>
        <module>spring-cloud-producer-2</module>
    </modules>

    <packaging>pom</packaging>

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.9.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <spring-cloud.version>Edgware.SR1</spring-cloud.version>
    </properties>

    <dependencies>
        <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>
        </dependencies>

    </dependencyManagement>

----------------------------------------------

其下面设置了4个子项目,一个是eureka注册中心,首先启动。一个是admin-server,将自己注册到eureka,还有2个admin-client也注册到eureka。

首先,eureka子项目:

pom:父项目和一个依赖就ok

<parent>
        <groupId>com.neo</groupId>
        <artifactId>spring-boot-admin-eureka</artifactId>
        <version>1.0.0.BUILD-SNAPSHOT</version>

    </parent>

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

        </dependency>

--------------------------------------------------------------------------

yml配置文件:

server:
  port: 8761
spring:
  application:
    name: eureka
eureka:
  client:
    register-with-eureka: false
    fetch-registry: false
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
management:
  security:

    enabled: false

-----------------------------------------------------

主类也是加上@SpringBootApplication和@EnableEurekaServer 2个注解

------------------------------------------------------------------------------

然后是admin-server

pom:父项目,一个eureka2个admin,加上一个mail的依赖

<parent>
        <groupId>com.neo</groupId>
        <artifactId>spring-boot-admin-eureka</artifactId>
        <version>1.0.0.BUILD-SNAPSHOT</version>

    </parent>

<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>
        <dependency>
            <groupId>de.codecentric</groupId>
            <artifactId>spring-boot-admin-server</artifactId>
            <version>1.5.6</version>
        </dependency>
        <dependency>
            <groupId>de.codecentric</groupId>
            <artifactId>spring-boot-admin-server-ui</artifactId>
            <version>1.5.6</version>

        </dependency>

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
        </dependency>

----------------------------------------------------------------

主类的注解为@Configuration、@EnableAutoConfiguration、@EnableDiscoveryClient和@EnableAdminServer,比单纯使用admin多一个@EnableDiscoveryClient。

--------------------------------------------------------

yml配置:

server:
  port: 8000
spring:
  application:

    name: admin-server

#邮箱通知功能,具体见spring-boot-mail那一篇,也可以不用都注释掉

  mail:
    host: smtp.163.com
    username: xxx@163.com
    password: xxxxxx
    properties:
      mail:
        smtp:
          auth: true
          starttls:
            enable: true
            required: true
  boot:
    admin:
      notify:
        mail:
          from: xxx@163.com
          to: xxxxx@qq.com
# http://codecentric.github.io/spring-boot-admin/1.5.6/#mail-notifications
eureka:
  instance:
    leaseRenewalIntervalInSeconds: 10
  client:
    #register-with-eureka: false
    #fetch-registry: false
    registryFetchIntervalSeconds: 5
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/

management.security.enabled: false

--------------------------------------------------------------------

然后是admin-client,这个可以配置多个供admin管理

pom:有的博文说依赖需要actuator,但是我看maven的包继承关系,spring-boot-admin-starter-client这个包是包含了actuator的。

<parent>
        <groupId>com.neo</groupId>
        <artifactId>spring-boot-admin-eureka</artifactId>
        <version>1.0.0.BUILD-SNAPSHOT</version>

    </parent>

<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>
        <dependency>
            <groupId>de.codecentric</groupId>
            <artifactId>spring-boot-admin-starter-client</artifactId>
            <version>1.5.6</version>
        </dependency>

--------------------------------------------------------------------------

主类注解2个@SpringBootApplication和@EnableDiscoveryClient

-----------------------------------------------------------------------

yml配置:

server:
  port: 9000
spring:
  application:
    name: producer
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
management:
  security:

    enabled: false

---------------------------------------------------------

最后,依次启动eureka,admin-server和其clients,登录eureka默认端口8761,可以看到各个应用,登录admin端口8000也可以看到(这个是我们需要的)。mail的功能是应用启动或关闭时有邮件提醒,这个可以自定义具体功能。








<think>我们正在解决SpringBootAdmin客户端注册失败的问题,结合用户提到的依赖问题(引用[1][2][3])和之前讨论的注册失败问题。用户当前问题:使用spring-boot-admin-starter-client(2.5.0版本)和spring-boot-starter-actuator时,应用注册失败,错误涉及ApplicationRegistrator。已知线索:1.用户可能遇到依赖下载问题(pom.xml中有红线,提示依赖未找到)——引用[1][2][3]。2.之前讨论的注册失败可能由多种原因导致(如EurekaServer的CSRF保护、健康端点不可访问等),但现在明确是SpringBootAdmin的客户端注册问题(使用spring-boot-admin-starter-client)。因此,我们将分两部分解决问题:A.解决依赖问题(确保所有依赖正确下载)B.解决SpringBootAdmin客户端注册失败问题步骤:A.解决依赖下载问题(pom.xml出现红线)根据引用[1][2][3],用户可能在创建Maven项目时,依赖没有正确下载。解决方法:1.检查Maven配置:确保IDEA中使用了正确的Maven设置(settings.xml和本地仓库路径)。2.强制重新下载依赖:-在IDEA中,打开Maven工具栏(通常右侧),点击刷新按钮(ReimportAllMavenProjects)。-或者,在命令行中进入项目目录,运行`mvncleaninstall-U`(-U参数强制更新快照依赖)。3.如果仍然有红线,检查依赖版本是否存在。特别地,用户引用了两个版本:-`spring-boot-starter-freemarker:2.1.4.RELEASE`(引用[1])和`spring-boot-starter-parent:2.5.0`(引用[2])可能冲突。-建议统一SpringBoot版本,比如都使用2.5.0(注意SpringBootAdmin的版本需要兼容)。4.确保SpringBootAdmin客户端的版本与SpringBoot版本匹配。例如,SpringBoot2.5.x对应SpringBootAdmin2.5.x。示例pom.xml片段:```xml<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.5.0</version><!--统一版本--></parent><dependencies><!--SpringBootAdminClient--><dependency><groupId>de.codecentric</groupId><artifactId>spring-boot-admin-starter-client</artifactId><version>2.5.0</version><!--版本与SpringBoot一致--></dependency><!--Actuator--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency></dependencies>```5.如果本地仓库确实没有该依赖,可能是网络问题无法从中央仓库下载,可尝试配置阿里云镜像(在settings.xml中):```xml<mirror><id>aliyunmaven</id><mirrorOf>*</mirrorOf><name>阿里云公共仓库</name><url>https://maven.aliyun.com/repository/public</url></mirror>```B.解决SpringBootAdmin客户端注册失败问题当依赖问题解决后,启动应用,如果仍然注册失败,请按照以下步骤:1.确保SpringBootAdminServer已经启动,并且客户端配置了正确的Server地址。在客户端的application.properties或application.yml中:```yamlspring:boot:admin:client:url:http://localhost:8080#替换为AdminServer的地址```2.确保Actuator端点已暴露,特别是健康端点(health)和信息端点(info)。默认情况下,Actuator只暴露了health和info,但其他端点可能也需要。在客户端配置:```yamlmanagement:endpoints:web:exposure:include:"*"#为了简单,暴露所有端点,但生产环境请根据需要暴露endpoint:health:show-details:always```3.检查网络连接:确保客户端可以访问AdminServer的地址。4.查看客户端日志:通常,注册失败会有详细的错误原因。常见原因包括:-连接被拒绝(AdminServer未启动或端口错误)-SSL证书问题(如果使用https,可能需要配置信任)-认证问题(如果AdminServer设置了安全登录,客户端需要配置用户名密码)如果AdminServer需要认证,客户端配置示例:```yamlspring:boot:admin:client:username:adminpassword:admin```5.如果上述步骤都无效,考虑在客户端中添加额外的重试机制(注册可能因网络抖动而失败):在pom.xml中添加:```xml<dependency><groupId>org.springframework.retry</groupId><artifactId>spring-retry</artifactId></dependency>```然后在启动类上添加`@EnableRetry`注解。6.如果使用服务发现(如Eureka)来注册AdminServer,则需要确保服务发现已配置并可用,然后设置:```yamlspring:boot:admin:client:discovery:enabled:true```注意:SpringBootAdmin的注册过程是客户端主动向AdminServer注册,所以错误往往在客户端日志中,务必仔细查看。总结:1.先解决依赖问题(确保pom.xml无红线,版本一致)。2.配置正确的AdminServer地址。3.暴露Actuator端点。4.处理认证(如果需要)。5.添加重试(可选)。最后,如果问题依然存在,请检查SpringBootAdmin的官方文档(https://codecentric.github.io/spring-boot-admin/current/)或提供错误日志的完整片段。§§相关问题§§1.如何配置SpringBootAdminServer接收客户端注册?2.如何自定义SpringBootAdmin的健康检查信息?3.如何监控多个微服务实例的详细信息?</think> ### 解决 Spring Boot Admin 客户端注册失败问题 根据您的描述,结合引用[1][2][3]中提到的依赖问题和历史上下文,以下是针对`spring-boot-admin-starter-client 2.5.0`注册失败的系统解决方案: #### 1. **检查依赖兼容性**(关键步骤) 引用[2]和[3]显示Spring Boot版本冲突是常见问题: ```xml <!-- pom.xml 必须保证版本一致 --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.5.0</version> <!--Admin Client版本匹配 --> </parent> <dependencies> <dependency> <groupId>de.codecentric</groupId> <artifactId>spring-boot-admin-starter-client</artifactId> <version>2.5.0</version> </dependency> <!-- Actuator必须存在且版本匹配 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> </dependencies> ``` **重要操作**: - 删除本地Maven仓库中`org/springframework/boot`和`de/codecentric`目录(强制重新下载) - 在IDEA中执行: 1. Maven面板 → 刷新按钮 → 执行`mvn clean install -U` 2. 检查`External Libraries`中是否存在红线依赖 #### 2. **修复Admin Server配置** ```yaml # application.yml spring: boot: admin: client: url: http://admin-server-host:port # Admin Server地址 instance: service-url: http://${spring.application.name}:${server.port} health-url: http://${spring.application.name}:${server.port}/actuator/health ``` #### 3. **确保Actuator端点可访问** ```yaml management: endpoints: web: exposure: include: "*" # 开放所有端点(临时调试) endpoint: health: show-details: always # 必须显示详细健康信息 restart: enabled: true # 需要端点支持 shutdown: enabled: true ``` #### 4. **验证网络连接性** 在应用服务器执行: ```bash telnet admin-server-host port # 测试Admin Server端口 curl http://localhost:8080/actuator/health # 验证本地健康检查 ``` #### 5. **解决注册超时问题** ```yaml # 添加注册重试机制 spring: boot: admin: client: retry: max-attempts: 10 initial-interval: 2000ms ``` #### 6. **检查安全配置** 若Admin Server启用了安全认证(常见原因): ```yaml spring: boot: admin: client: username: admin-user password: admin-pass ``` > **验证成功注册的标志**: > 在Admin Server日志中出现:`Registered instance Application [name=order-service...]` ### 问题排查流程图 $$ \text{注册失败问题} \begin{cases} 1. \text{检查依赖红线 → Maven刷新[^1][^2][^3]} \\ 2. \text{验证健康端点 → curl /actuator/health} \\ 3. \text{测试网络连通 → telnet} \\ 4. \text{检查Admin Server状态 → 日志监控} \end{cases} $$ ### 典型错误解决方案 | 错误现象 | 解决方案 | |---------|---------| | `ApplicationRegistrator: Failed to register` | 检查Actuator依赖+Admin Server URL | | `401 Unauthorized` | 配置Admin Server用户名/密码 | | `Connection refused` | 检查防火墙/端口开放状态 | | `health endpoint 404` | 添加`spring-boot-starter-actuator` |
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值