我正在使用 Spring 3.1.2,并且可以使用 JAR 文件在 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;
}
}
application.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 描述部署,我会得到以下信息:
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>
关于我做错的事情有什么好主意吗?
尝试指定运行状况端点属性以仅包含您需要的运行状况指标。您可以将以下配置添加到您的 application.yaml 中:
management:
...
health:
diskspace:
enabled: true
db:
enabled: true
...
在我的例子中似乎解决问题的是添加
spring.cloud.refresh.enabled=false