java UnsatisfiedDependencyException:使用GraalVM本机映像时创建名为“healthContributorRegistry”的Bean时出错

qfe3c7zg  于 2023-10-14  发布在  Java
关注(0)|答案(2)|浏览(158)

我使用的是Spring 3.1.2,可以使用一个REST文件在k8s集群中成功部署一个非常简单的REST应用程序。该应用程序正在向Eureka 服务器注册,如果我将其部署为一个JAR文件,则可以正常工作。
现在的问题是,当我使用mvn clean -Pnative native:compile编译应用程序并部署相同的应用程序时,我在日志文件中得到以下错误:

.   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v3.1.2)

2023-10-09T22:33:25.141-05:00  INFO 30700 --- [           main] c.e.creditscore.CreditscoreApplication   : Starting AOT-processed CreditscoreApplication using Java 17.0.8 with PID 30700 (/Users/atael/Documents/GitHub/microservices-datadriven/cloudbank-v2/spring-apps-spring3/creditscore/target/creditscore started by atael in /Users/atael/Documents/GitHub/microservices-datadriven/cloudbank-v2/spring-apps-spring3/creditscore/target)
2023-10-09T22:33:25.142-05:00  INFO 30700 --- [           main] c.e.creditscore.CreditscoreApplication   : No active profile set, falling back to 1 default profile: "default"
2023-10-09T22:33:25.144-05:00  INFO 30700 --- [           main] o.s.cloud.context.scope.GenericScope     : BeanFactory id=368d3e47-1089-3f9a-85bc-0c3f5dcda5ab
2023-10-09T22:33:25.162-05:00  INFO 30700 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2023-10-09T22:33:25.163-05:00  INFO 30700 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2023-10-09T22:33:25.163-05:00  INFO 30700 --- [           main] o.apache.catalina.core.StandardEngine    : Starting Servlet engine: [Apache Tomcat/10.1.11]
2023-10-09T22:33:25.170-05:00  INFO 30700 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2023-10-09T22:33:25.170-05:00  INFO 30700 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 28 ms
2023-10-09T22:33:25.195-05:00  WARN 30700 --- [           main] w.s.c.ServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'healthContributorRegistry': Unsatisfied dependency expressed through method 'healthContributorRegistry' parameter 2: Error creating bean with name 'discoveryCompositeHealthContributor': Unsatisfied dependency expressed through method 'discoveryCompositeHealthContributor' parameter 0: Error creating bean with name 'eurekaHealthIndicator': Unsatisfied dependency expressed through method 'eurekaHealthIndicator' parameter 0: No qualifying bean of type 'com.netflix.discovery.EurekaClient' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}

我的pom.xml包含以下内容:

<plugin>
    <groupId>org.graalvm.buildtools</groupId>
    <artifactId>native-maven-plugin</artifactId>
</plugin>

我使用的是这个版本的Java:

java version "17.0.8" 2023-07-18 LTS
Java(TM) SE Runtime Environment Oracle GraalVM 17.0.8+9.1 (build 17.0.8+9-LTS-jvmci-23.0-b14)
Java HotSpot(TM) 64-Bit Server VM Oracle GraalVM 17.0.8+9.1 (build 17.0.8+9-LTS-jvmci-23.0-b14, mixed mode, sharing)

这是应用程序:

package com.example.creditscore;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@EnableDiscoveryClient
@SpringBootApplication
public class CreditscoreApplication {

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

}
package com.example.creditscore.controller;

import org.springframework.web.bind.annotation.*;

import lombok.extern.slf4j.Slf4j;

import java.security.SecureRandom;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api/v1")
@Slf4j
public class CreditScoreController {

    @GetMapping("/creditscore")
    public Map<String, String> getCreditScore() {
        log.info("CREDITSCORE: getCreditScore");
        int max = 900;
        int min = 500;
        SecureRandom secureRandom = new SecureRandom();
        HashMap<String, String> map = new HashMap<>();
        map.put("Credit Score", String.valueOf(secureRandom.nextInt(max - min) + min));
        map.put("Date", String.valueOf(java.time.LocalDate.now()));
        return map;
    }
}

应用程序.yaml看起来像这样:

spring:
  application:
    name: creditscore
  zipkin:
    base-url: ${zipkin.base-url}

eureka:
  instance:
    hostname: ${spring.application.name}
    preferIpAddress: true
  client:
    service-url:
      defaultZone: ${eureka.service-url}
    fetch-registry: true
    register-with-eureka: true
    enabled: true

management:
  endpoint:
    health:
      show-details: always
  endpoints:
    web:
      exposure:
        include: "*"
  metrics:
    tags:
      application: ${spring.application.name}

如果我做一个kubectl describe部署,我会得到这样的信息:

Name:                   creditscore
Namespace:              nativetest
CreationTimestamp:      Mon, 09 Oct 2023 15:53:20 -0500
Labels:                 <none>
Annotations:            deployment.kubernetes.io/revision: 1
Selector:               app=creditscore
Replicas:               1 desired | 1 updated | 1 total | 0 available | 1 unavailable
StrategyType:           RollingUpdate
MinReadySeconds:        0
RollingUpdateStrategy:  25% max unavailable, 25% max surge
Pod Template:
  Labels:  app=creditscore
           version=0.0.1
  Containers:
   creditscore:
    Image:      <notforpublic>
    Port:       8080/TCP
    Host Port:  0/TCP
    Environment:
      app.container.port:                     8080
      spring.profiles.active:                 default
      spring.config.label:                    0.0.1
      eureka.instance.preferIpAddress:        true
      eureka.instance.hostname:               creditscore.nativetest
      MP_LRA_COORDINATOR_URL:                 http://otmm-tcs.otmm.svc.cluster.local:9000/api/v1/lra-coordinator
      MP_LRA_PARTICIPANT_URL:                 http://creditscore.nativetest.svc.cluster.local:8080
      eureka.client.register-with-eureka:     true
      eureka.client.fetch-registry:           true
      eureka.client.service-url.defaultZone:  http://eureka.eureka:8761/eureka
      zipkin.base-url:                        http://jaegertracing-collector.observability:9411
      otel.exporter.otlp.endpoint:            http://open-telemetry-opentelemetry-collector.open-telemetry:4317
      hystrix.metrics.enabled:                true
    Mounts:                                   <none>
  Volumes:                                    <none>
Conditions:
  Type           Status  Reason
  ----           ------  ------
  Progressing    True    NewReplicaSetAvailable
  Available      False   MinimumReplicasUnavailable
OldReplicaSets:  <none>
NewReplicaSet:   creditscore-5774cb4757 (1/1 replicas created)
Events:          <none>

我做错了什么有什么好主意吗?

n1bvdmb6

n1bvdmb61#

请尝试指定健康终结点属性以仅包括所需的健康指示器。您可以将以下配置添加到您的应用程序.yaml:

management:
  ...
  health:
    diskspace:
      enabled: true
    db:
      enabled: true
    ...
iih3973s

iih3973s2#

在我的例子中,解决这个问题的方法似乎是添加spring.cloud.refresh.enabled=false

相关问题