Spring Boot livenessState: DOWN 和 readinessState: OUT_OF_SERVICE 即使应用程序正常运行

huangapple go评论66阅读模式
英文:

Spring Boot livenessState: DOWN and readinessState: OUT_OF_SERVICE even if application is running fine

问题

I'm developing/running a SpringBoot App on Kubernetes 1.24, which I'm also using to learn several techniques with mostly using WebClient and with my newest attempt, also sending E-Mails with JavaMailSender, etc.

Lately, I was updating a few parts in my code, where I'm not sure anymore what was leading to the issue in the subject.

But I suspect it could have something to do with adding a new @Component for my EmailSender class or that I had to use @ComponentScan to access the mail package, as it was outside of my controller class package.

As e.g.:

[...]
@Configuration
public class EmailConfiguration {
    @Bean
    public JavaMailSender getJavaMailSender() {
        [...]
    }
}

and also:

@Component
public class EmailSender {
    @Autowired
    private JavaMailSender emailSender;
    @Bean
    public void sendSimpleMessage(String to, String subject, String text) {
        [...]
    }
}

Also, I've updated from Eclipse Temurin 17.0.6+10 to 17.0.7+7

To get more details, I've added the following to the application.properties:

management.server.port=8081
management.server.address=0.0.0.0
management.endpoints.web.exposure.include=health,info,metrics,prometheus
management.endpoint.health.probes.enabled=true
management.health.livenessState.enabled=true
management.health.readinessState.enabled=true
management.endpoint.health.show-details=always
management.endpoints.web.base-path=/actuator

So, I've got the following response calling http://localhost:8081/actuator/health:

{
  "status": "DOWN",
  "components": {
    "db": {
      "status": "UP",
      "details": {
        "database": "H2",
        "validationQuery": "isValid()"
      }
    },
    "diskSpace": {
      "status": "UP",
      "details": {
        "total": 1022565265408,
        "free": 861403496448,
        "threshold": 10485760,
        "path": "C:\\Users\\nick\\eclipse-workspace\\demoapp\\.",
        "exists": true
      }
    },
    "livenessState": {
      "status": "DOWN"
    },
    "ping": {
      "status": "UP"
    },
    "readinessState": {
      "status": "OUT_OF_SERVICE"
    }
  },
  "groups": [
    "liveness",
    "readiness"
  ]
}

My 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>3.0.6</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>
    <groupId>my.demo.springboot</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>Demo SpringBoot</name>
    <description>Demo Spring Boot Example</description>

    <properties>
        <java.version>17</java.version>
        <log4j2.version>2.20.0</log4j2.version>
        <netty.version>4.1.92.Final</netty.version>
        <snakeyaml.version>2.0</snakeyaml.version>
    </properties>

    <dependencies>
        <!-- Dependencies here -->
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

I was reading on the following at the official documentation:

Tasks expected to run during startup should be executed by CommandLineRunner and ApplicationRunner components instead of using Spring component lifecycle callbacks such as @PostConstruct.

But I did not modify any behavior on such, so I'm currently not sure where and what to look for.

As I'm running on Kubernetes, I'm using both endpoints to monitor my application, but now it keeps restarting as both health checks fail with 503.

I'm hoping that it is just something simple I've forgotten/changed, and anyone knows or had that issue already.

Let me know if any more information is required.

Thanks!

Update:

This only happens with Spring Boot 3.0.6, with 3.0.5 everything is running fine with:

{
  "status": "UP",
  "components": {
    "diskSpace": {
      "status": "UP",
      "details": {
        "total": 1022565265408,
        "free": 863173750784,
        "threshold": 10485760,
        "path": "C:\\<censored>\\.",
        "exists": true
      }
    },
    "livenessState": {
      "status": "UP"
    },
    "mail": {
      "status": "UP",
      "details": {
        "location": "<censored>.net:25"
      }
    },
    "ping": {
      "status": "UP"
    },
    "readinessState": {
      "status": "UP"
    }
  },
  "groups": [
    "liveness",
    "readiness"
  ]
}

This issue on GitHub might be related.

英文:

i'm developing/running a SpringBoot App on Kubernetes 1.24, which i'm also using to learn several techniques with mostly using WebClient and with my newest attempt, also sending E-Mails with JavaMailSender etc.

Lately i was updating a few parts in my code, where i'm not sure anymore what was leading to the issue in the subject.

But i suspect it could have something to do with adding a new @Component for my EmailSender class or that i had to use @ComponentScan to access the mail package, as it was outside of my controller class package.

As e.g.:

[...]
@Configuration
public class EmailConfiguration {
	@Bean
	public JavaMailSender getJavaMailSender() {
           [...]
        }
}

and also:

@Component
public class EmailSender {
	@Autowired
	private JavaMailSender emailSender;
	@Bean
    public void sendSimpleMessage(String to, String subject, String text) {
       [...]
    }
}

Also i've updated from Eclipse Temurin 17.0.6+10 to 17.0.7+7

To get more details, i've added the following to the application.properties:

management.server.port=8081
management.server.address=0.0.0.0
management.endpoints.web.exposure.include=health,info,metrics,prometheus
management.endpoint.health.probes.enabled=true
management.health.livenessState.enabled=true
management.health.readinessState.enabled=true
management.endpoint.health.show-details=always
management.endpoints.web.base-path=/actuator

So i've got the following response calling http://localhost:8081/actuator/health:

{
  &quot;status&quot;: &quot;DOWN&quot;,
  &quot;components&quot;: {
    &quot;db&quot;: {
      &quot;status&quot;: &quot;UP&quot;,
      &quot;details&quot;: {
        &quot;database&quot;: &quot;H2&quot;,
        &quot;validationQuery&quot;: &quot;isValid()&quot;
      }
    },
    &quot;diskSpace&quot;: {
      &quot;status&quot;: &quot;UP&quot;,
      &quot;details&quot;: {
        &quot;total&quot;: 1022565265408,
        &quot;free&quot;: 861403496448,
        &quot;threshold&quot;: 10485760,
        &quot;path&quot;: &quot;C:\\Users\\nick\\eclipse-workspace\\demoapp\\.&quot;,
        &quot;exists&quot;: true
      }
    },
    &quot;livenessState&quot;: {
      &quot;status&quot;: &quot;DOWN&quot;
    },
    &quot;ping&quot;: {
      &quot;status&quot;: &quot;UP&quot;
    },
    &quot;readinessState&quot;: {
      &quot;status&quot;: &quot;OUT_OF_SERVICE&quot;
    }
  },
  &quot;groups&quot;: [
    &quot;liveness&quot;,
    &quot;readiness&quot;
  ]
}

My pom:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot;
	xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
	xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd&quot;&gt;
	&lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
	&lt;parent&gt;
		&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
		&lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;
		&lt;version&gt;3.0.6&lt;/version&gt;
		&lt;relativePath /&gt; &lt;!-- lookup parent from repository --&gt;
	&lt;/parent&gt;
	&lt;groupId&gt;my.demo.springboot&lt;/groupId&gt;
	&lt;artifactId&gt;demo&lt;/artifactId&gt;
	&lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;
	&lt;name&gt;Demo SpringBoot&lt;/name&gt;
	&lt;description&gt;Demo Spring Boot Example&lt;/description&gt;

	&lt;properties&gt;
		&lt;java.version&gt;17&lt;/java.version&gt;
		&lt;log4j2.version&gt;2.20.0&lt;/log4j2.version&gt;
		&lt;netty.version&gt;4.1.92.Final&lt;/netty.version&gt;
		&lt;snakeyaml.version&gt;2.0&lt;/snakeyaml.version&gt;
	&lt;/properties&gt;

	&lt;dependencies&gt;
	
		&lt;dependency&gt;
			&lt;groupId&gt;commons-io&lt;/groupId&gt;
			&lt;artifactId&gt;commons-io&lt;/artifactId&gt;
			&lt;version&gt;2.11.0&lt;/version&gt;
		&lt;/dependency&gt;

		&lt;dependency&gt;
			&lt;groupId&gt;commons-codec&lt;/groupId&gt;
			&lt;artifactId&gt;commons-codec&lt;/artifactId&gt;
		&lt;/dependency&gt;
		
	&lt;dependency&gt;
      &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
      &lt;artifactId&gt;spring-boot-starter-data-jpa&lt;/artifactId&gt;
    &lt;/dependency&gt;

    &lt;dependency&gt;
      &lt;groupId&gt;com.h2database&lt;/groupId&gt;
      &lt;artifactId&gt;h2&lt;/artifactId&gt;
      &lt;scope&gt;runtime&lt;/scope&gt;
    &lt;/dependency&gt;


		&lt;dependency&gt;
			&lt;groupId&gt;io.micrometer&lt;/groupId&gt;
			&lt;artifactId&gt;micrometer-registry-prometheus&lt;/artifactId&gt;
			&lt;scope&gt;runtime&lt;/scope&gt;
		&lt;/dependency&gt;


		&lt;dependency&gt;
			&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
			&lt;artifactId&gt;spring-boot-starter-actuator&lt;/artifactId&gt;
		&lt;/dependency&gt;
		

		&lt;dependency&gt;
    		&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
    		&lt;artifactId&gt;spring-boot-starter-webflux&lt;/artifactId&gt;
		&lt;/dependency&gt;
		

		&lt;dependency&gt;
			&lt;groupId&gt;io.jsonwebtoken&lt;/groupId&gt;
			&lt;artifactId&gt;jjwt-api&lt;/artifactId&gt;
			&lt;version&gt;0.11.5&lt;/version&gt;
		&lt;/dependency&gt;
	
		&lt;dependency&gt;
		    &lt;groupId&gt;io.jsonwebtoken&lt;/groupId&gt;
		    &lt;artifactId&gt;jjwt-impl&lt;/artifactId&gt;
		    &lt;version&gt;0.11.5&lt;/version&gt;
		&lt;/dependency&gt;

		&lt;dependency&gt;
		    &lt;groupId&gt;io.jsonwebtoken&lt;/groupId&gt;
		    &lt;artifactId&gt;jjwt-jackson&lt;/artifactId&gt;
		    &lt;version&gt;0.11.5&lt;/version&gt;
		&lt;/dependency&gt;
		&lt;dependency&gt;
    		&lt;groupId&gt;com.auth0&lt;/groupId&gt;
    		&lt;artifactId&gt;java-jwt&lt;/artifactId&gt;
    		&lt;version&gt;4.4.0&lt;/version&gt;
		&lt;/dependency&gt;

		&lt;dependency&gt;
   			&lt;groupId&gt;org.ocpsoft.prettytime&lt;/groupId&gt;
   			&lt;artifactId&gt;prettytime&lt;/artifactId&gt;
   			&lt;version&gt;5.0.6.Final&lt;/version&gt;
		&lt;/dependency&gt;

		&lt;dependency&gt;
			&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
			&lt;artifactId&gt;spring-boot-starter-thymeleaf&lt;/artifactId&gt;
		&lt;/dependency&gt;
		
		&lt;dependency&gt;
    		&lt;groupId&gt;javax.xml.bind&lt;/groupId&gt;
    		&lt;artifactId&gt;jaxb-api&lt;/artifactId&gt;
    		&lt;version&gt;2.3.1&lt;/version&gt;
    	&lt;/dependency&gt;

		&lt;dependency&gt;
			&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
			&lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
		&lt;/dependency&gt;

        &lt;dependency&gt;
          &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
          &lt;artifactId&gt;spring-boot-starter-mail&lt;/artifactId&gt;
        &lt;/dependency&gt;

		&lt;dependency&gt;
			&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
			&lt;artifactId&gt;spring-boot-devtools&lt;/artifactId&gt;
			&lt;scope&gt;runtime&lt;/scope&gt;
			&lt;optional&gt;true&lt;/optional&gt;
		&lt;/dependency&gt;

		&lt;dependency&gt;
			&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
			&lt;artifactId&gt;spring-boot-starter-test&lt;/artifactId&gt;
			&lt;scope&gt;test&lt;/scope&gt;
			&lt;exclusions&gt;
				&lt;exclusion&gt;
					&lt;groupId&gt;org.junit.vintage&lt;/groupId&gt;
					&lt;artifactId&gt;junit-vintage-engine&lt;/artifactId&gt;
				&lt;/exclusion&gt;
			&lt;/exclusions&gt;
		&lt;/dependency&gt;
	&lt;/dependencies&gt;

	&lt;build&gt;
		&lt;plugins&gt;
			&lt;plugin&gt;
				&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
				&lt;artifactId&gt;spring-boot-maven-plugin&lt;/artifactId&gt;
			&lt;/plugin&gt;
		&lt;/plugins&gt;
	&lt;/build&gt;
&lt;/project&gt;

I was reading on the following at the official documentation:

> Tasks expected to run during startup should be executed by CommandLineRunner and ApplicationRunner components instead of using Spring component lifecycle callbacks such as @PostConstruct.

But i did not modified any behavior on such, so i'm currently not sure where and what to look for.

As i'm running on kubernetes, i'm using both endpoints to monitor my application, but now it keeps restarting as a both healthchecks fail with 503.

I'm hoping that it is just something simple i've forgot/changed and anyone knows or had that issue already.

Let me know if anyhow more information is required.

Thanks!

Update:

This only happens with Spring Boot 3.0.6, with 3.0.5 everything running fine with:

{
  &quot;status&quot;: &quot;UP&quot;,
  &quot;components&quot;: {
    &quot;diskSpace&quot;: {
      &quot;status&quot;: &quot;UP&quot;,
      &quot;details&quot;: {
        &quot;total&quot;: 1022565265408,
        &quot;free&quot;: 863173750784,
        &quot;threshold&quot;: 10485760,
        &quot;path&quot;: &quot;C:\\&lt;censored&gt;\\.&quot;,
        &quot;exists&quot;: true
      }
    },
    &quot;livenessState&quot;: {
      &quot;status&quot;: &quot;UP&quot;
    },
    &quot;mail&quot;: {
      &quot;status&quot;: &quot;UP&quot;,
      &quot;details&quot;: {
        &quot;location&quot;: &quot;&lt;censored&gt;.net:25&quot;
      }
    },
    &quot;ping&quot;: {
      &quot;status&quot;: &quot;UP&quot;
    },
    &quot;readinessState&quot;: {
      &quot;status&quot;: &quot;UP&quot;
    }
  },
  &quot;groups&quot;: [
    &quot;liveness&quot;,
    &quot;readiness&quot;
  ]
}

This issue on GitHub might be related.

答案1

得分: 0

与GitHub问题35161相关的问题。有一个修复和其描述:

> 启用惰性初始化时,活动性和可读性探测返回下降

升级到Spring Boot版本3.1.0对我有帮助(3.0.7应该也有修复,但我没有测试),健康端点再次正常工作(3.1.0刚刚在3天前发布)。

详情:
> 这是由于#34347的更改导致的回归。@Bean方法的返回类型从ApplicationAvailabilityBean更改为ApplicationAvailability。这意味着丢失了类型信息,bean工厂无法知道它是一个ApplicationListener。

{
  &quot;status&quot;: &quot;UP&quot;,
  &quot;components&quot;: {
    &quot;db&quot;: {
      &quot;status&quot;: &quot;UP&quot;,
      &quot;details&quot;: {
        &quot;database&quot;: &quot;H2&quot;,
        &quot;validationQuery&quot;: &quot;isValid()&quot;
      }
    },
    &quot;diskSpace&quot;: {
      &quot;status&quot;: &quot;UP&quot;,
      &quot;details&quot;: {
        &quot;total&quot;: 1022565265408,
        &quot;free&quot;: 862148792320,
        &quot;threshold&quot;: 10485760,
        &quot;path&quot;: &quot;C:\\&lt;censored&gt;\\eclipse-workspace\\demo\\.&quot;,
        &quot;exists&quot;: true
      }
    },
    &quot;livenessState&quot;: {
      &quot;status&quot;: &quot;UP&quot;
    },
    &quot;ping&quot;: {
      &quot;status&quot;: &quot;UP&quot;
    },
    &quot;readinessState&quot;: {
      &quot;status&quot;: &quot;UP&quot;
    }
  },
  &quot;groups&quot;: [
    &quot;liveness&quot;,
    &quot;readiness&quot;
  ]
}
英文:

Issue is related with GitHub issue 35161. With a fix and its description as:

> Liveness and readiness probes return down when lazy initialization is enabled

Updating to Spring Boot Version 3.1.0 solved it for me (also 3.0.7 should have the fix, but untested from my side) and the health endpoint is up again (3.1.0 just has been published 3 days ago).

Details:
> This is a regression caused by the changes for #34347. The return type of the @Bean method has changed from ApplicationAvailabilityBean to ApplicationAvailability. This means that type information is lost and the bean factory cannot tell that it's an ApplicationListener.

{
  &quot;status&quot;: &quot;UP&quot;,
  &quot;components&quot;: {
    &quot;db&quot;: {
      &quot;status&quot;: &quot;UP&quot;,
      &quot;details&quot;: {
        &quot;database&quot;: &quot;H2&quot;,
        &quot;validationQuery&quot;: &quot;isValid()&quot;
      }
    },
    &quot;diskSpace&quot;: {
      &quot;status&quot;: &quot;UP&quot;,
      &quot;details&quot;: {
        &quot;total&quot;: 1022565265408,
        &quot;free&quot;: 862148792320,
        &quot;threshold&quot;: 10485760,
        &quot;path&quot;: &quot;C:\\&lt;censored&gt;\\eclipse-workspace\\demo\\.&quot;,
        &quot;exists&quot;: true
      }
    },
    &quot;livenessState&quot;: {
      &quot;status&quot;: &quot;UP&quot;
    },
    &quot;ping&quot;: {
      &quot;status&quot;: &quot;UP&quot;
    },
    &quot;readinessState&quot;: {
      &quot;status&quot;: &quot;UP&quot;
    }
  },
  &quot;groups&quot;: [
    &quot;liveness&quot;,
    &quot;readiness&quot;
  ]
}

huangapple
  • 本文由 发表于 2023年5月10日 19:15:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/76217745.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定