Spring boot consul 健康检查失败

问题描述 投票:0回答:2

我有一个 consul 服务器在我的本地计算机上以开发模式运行,并启用了 ACL。这是我的名为 web 的服务的 ACL 策略:

service "web" {
   policy = "write"
}

我有一个令牌,我已在其中附加了此政策。

我正在尝试注册使用 spring boot V2.6.1 编写的名为“web”的服务,该服务正在注册,但健康检查失败。

以下是Spring Boot应用的详细信息:

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.6.1</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.ms</groupId>
    <artifactId>web</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>web</name>
    <description>Demo project for Spring Boot</description>
    <packaging>jar</packaging>
    <properties>
        <java.version>11</java.version>
        <spring-cloud.version>2021.0.0</spring-cloud.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-consul-config</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-consul-discovery</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </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>
        </dependencies>
    </dependencyManagement>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

应用程序.yml

server:
  port: 8080

spring:
  application:
    name: web
  cloud:
    consul:
      config:
        enabled: false
        
      host: localhost
      port: 8500  
      discovery:
        instance-id: "${spring.application.name}-${server.port}" 
        prefer-ip-address: true
        health-check-critical-timeout: "1m"
        health-check-path: /actuator/health
        health-check-interval: 10s
        acl-token: "8a0a8090-08df-2cae-cb5d-9b6f91f34933"

我是否缺少有关健康检查的任何配置?

spring-boot maven consul spring-cloud-consul consul-health-check
2个回答
0
投票

领事服务健康检查有两种方式工作:

  1. 定期调用执行器端点,这就是您需要执行器依赖的原因。在这种情况下,你应该确保 consul 可以通过指定端口到达你的机器。
  2. 心跳,在这种情况下,您的服务将调用 consul http 端点来获取健康检查状态。我建议您使用健康检查属性配置并检查问题是否存在。如果问题消失,则意味着您的问题是第 1 点中的 consul 连接。

0
投票

我也有同样的问题。 consul 中的日志显示使用的健康检查 URL 是 http://localhost:4244/actuator/health 而不考虑 server.servlet.context-path。

修改配置中的健康检查路径后,就成功了。

consul:
  discovery:
    hostname: localhost
    health-check-path: /InfyGoBoot/flight/actuator/health
    health-check-interval: 10s

现在在 Consul 中,它会点击正确的 URL 进行健康检查,

HTTP GET http://localhost:4244/InfyGoBoot/flight/actuator/health: 200 输出:{"status":"UP"}

© www.soinside.com 2019 - 2024. All rights reserved.