Micronaut 应用无法访问 Kubernetes 配置映射中的值
Micronaut app fails to access values from Kubernetes config map
我正在使用 Micronaut 3.2.3 与 Kubernetes 集成来从配置映射和机密中注入配置值。
依赖关系:
implementation("io.micronaut.kubernetes:micronaut-kubernetes-client")
implementation("io.micronaut.kubernetes:micronaut-kubernetes-discovery-client")
bootstrap.yml
micronaut:
application:
name: ingestor
config-client:
enabled: true
kubernetes:
client:
config-maps:
enabled: true
includes:
- application
- ingestor
secrets:
enabled: true
includes:
- application
- ingestor
如您所见,应用程序配置映射包括 kafka.brokers 值:
kubectl get configmaps application -o yaml
apiVersion: v1
data:
application.yml: |
kafka.brokers: xxx
kubectl get configmaps ingestor -o yaml
apiVersion: v1
data:
application.yml: |
prop1: value1
我添加了一个简单的单例class检查projerty是否可以注入:
@Singleton
class SomeConfiguration(@Value("${kafka.brokers}") private val kafkaBrokers: String) {
init {
println(kafkaBrokers)
}
}
日志跟踪似乎表明这些配置映射已被正确访问:
15:59:24.206 [OkHttp https://10.222.0.1/...] - - DEBUG i.m.k.c.KubernetesConfigurationClient - Adding config map with name application
15:59:24.218 [OkHttp https://10.222.0.1/...] - - DEBUG i.m.k.c.KubernetesConfigurationClient - Adding config map with name ingestor
但是应用程序崩溃了,因为它找不到它:
fun main(args: Array<String>) {
build()
.args(*args)
.eagerInitSingletons(true)
15:59:25.484 [main] - - ERROR io.micronaut.runtime.Micronaut - Error starting Micronaut server: Bean definition [xxx.SomeConfiguration] could not be loaded: Failed to inject value for parameter [kafkaBrokers] of class: xxx.configuration.SomeConfiguration
Message: Failed to inject value for parameter [kafkaBrokers] of class: xxx.configuration.SomeConfiguration
Message: Error resolving property value [${kafka.brokers}]. Property doesn't exist
这个问题有点棘手。关键是配置映射中使用的 yml 文件的名称,应用程序和摄取器配置映射都将其定义为 application.yml:
apiVersion: v1
data:
application.yml: |
它们不能重叠,所以我将摄取器配置图更改为:
kubectl get configmaps ingestor -o yaml
apiVersion: v1
data:
ingestor.yml: |
prop1: value1
现在找到并注入 kafka.brokers
值。
我正在使用 Micronaut 3.2.3 与 Kubernetes 集成来从配置映射和机密中注入配置值。
依赖关系:
implementation("io.micronaut.kubernetes:micronaut-kubernetes-client")
implementation("io.micronaut.kubernetes:micronaut-kubernetes-discovery-client")
bootstrap.yml
micronaut:
application:
name: ingestor
config-client:
enabled: true
kubernetes:
client:
config-maps:
enabled: true
includes:
- application
- ingestor
secrets:
enabled: true
includes:
- application
- ingestor
如您所见,应用程序配置映射包括 kafka.brokers 值:
kubectl get configmaps application -o yaml
apiVersion: v1
data:
application.yml: |
kafka.brokers: xxx
kubectl get configmaps ingestor -o yaml
apiVersion: v1
data:
application.yml: |
prop1: value1
我添加了一个简单的单例class检查projerty是否可以注入:
@Singleton
class SomeConfiguration(@Value("${kafka.brokers}") private val kafkaBrokers: String) {
init {
println(kafkaBrokers)
}
}
日志跟踪似乎表明这些配置映射已被正确访问:
15:59:24.206 [OkHttp https://10.222.0.1/...] - - DEBUG i.m.k.c.KubernetesConfigurationClient - Adding config map with name application
15:59:24.218 [OkHttp https://10.222.0.1/...] - - DEBUG i.m.k.c.KubernetesConfigurationClient - Adding config map with name ingestor
但是应用程序崩溃了,因为它找不到它:
fun main(args: Array<String>) {
build()
.args(*args)
.eagerInitSingletons(true)
15:59:25.484 [main] - - ERROR io.micronaut.runtime.Micronaut - Error starting Micronaut server: Bean definition [xxx.SomeConfiguration] could not be loaded: Failed to inject value for parameter [kafkaBrokers] of class: xxx.configuration.SomeConfiguration
Message: Failed to inject value for parameter [kafkaBrokers] of class: xxx.configuration.SomeConfiguration
Message: Error resolving property value [${kafka.brokers}]. Property doesn't exist
这个问题有点棘手。关键是配置映射中使用的 yml 文件的名称,应用程序和摄取器配置映射都将其定义为 application.yml:
apiVersion: v1
data:
application.yml: |
它们不能重叠,所以我将摄取器配置图更改为:
kubectl get configmaps ingestor -o yaml
apiVersion: v1
data:
ingestor.yml: |
prop1: value1
现在找到并注入 kafka.brokers
值。