不绑定 ==> 领事 KV + Kotlin 弹簧引导@ConfigurationProperties @ConstructorBinding



我需要将application.yml配置转移到Hashicorp的Consul。我创建了一个简单的Spring Boot应用程序来完成所需的更改。它将配置数据从application.yml加载到一个强类型类中。

然而,在注释掉application.yml文件并在bootstrap.yml文件中启用consul后,当我试图访问consul KV中的相同配置时,配置数据总是为空,该配置在我的系统上作为Docker映像运行。我一定错过了一些简单的东西,但我完全不知道它是什么。以下是相关信息。

////////////////////////////////////////
// MyConsulApplication.kt
////////////////////////////////////////
@SpringBootApplication(scanBasePackages = ["com.example.myconsul"])
class MyConsulApplication
fun main(args: Array<String>) {
runApplication<MyConsulApplication>(*args)
}
////////////////////////////////////////
// application.yml
////////////////////////////////////////
#myconsul:
#  message: howdy
////////////////////////////////////////
// bootstrap.yml
////////////////////////////////////////
server:
port: 8080
spring:
application:
name: myconsul
profiles:
active: default
cloud:
consul:
host: localhost
port: 8500
config:
enabled: true  # Set to false to use the local application.yml file rather than the Consul configuration server.
prefix: config
defaultContext: application
discovery:
enabled: true
instanceId: ${spring.application.name}:${random.value}
healthCheckPath: /
healthCheckInterval: 30s
////////////////////////////////////////
// AppProperties.kt
////////////////////////////////////////
@ConstructorBinding
//@RefreshScope
//@Scope("singleton")  // This is the default, but I wanted to be explicit about it.
@EnableConfigurationProperties(AppProperties::class)
@ConfigurationProperties("myconsul")
data class AppProperties(val message: String?) {
@PostConstruct
fun postConstruct() {
println("====> $message")
}
}
////////////////////////////////////////
// Consul KT Export
////////////////////////////////////////
[
{
"key": "config/myconsul/message",
"flags": 0,
"value": "SGFwcHkgSGFsbG93ZWVuIQ=="
}
]
////////////////////////////////////////
// build.gradle.kts Dependencies
////////////////////////////////////////
extra["springCloudVersion"] = "Hoxton.BUILD-SNAPSHOT"
dependencies {
// kapt dependencies required for IntelliJ auto complete of kotlin config properties class
kapt("org.springframework.boot:spring-boot-configuration-processor")
annotationProcessor("org.springframework.boot:spring-boot-configuration-processor")
implementation("org.springframework.cloud:spring-cloud-starter-consul-config")
implementation("org.springframework.cloud:spring-cloud-starter-consul-discovery")
implementation("org.springframework.boot:spring-boot-starter-actuator")
implementation("org.springframework.boot:spring-boot-starter-webflux")
implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
implementation("org.jetbrains.kotlin:kotlin-reflect")
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-reactor")
}
////////////////////////////////////////
// Console Logging of Launch
////////////////////////////////////////
2019-11-02 22:49:04.510  INFO 5401 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.retry.annotation.RetryConfiguration' of type [org.springframework.retry.annotation.RetryConfiguration$$EnhancerBySpringCGLIB$$1d6cbd94] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2019-11-02 22:49:04.519  INFO 5401 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration' of type [org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration$$EnhancerBySpringCGLIB$$43c3286c] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
:: Spring Boot ::  (v2.2.1.BUILD-SNAPSHOT)
2019-11-02 22:49:05.521  INFO 5401 --- [           main] b.c.PropertySourceBootstrapConfiguration : Located property source: CompositePropertySource {name='consul', propertySources=[ConsulPropertySource {name='config/myconsul,default/'}, ConsulPropertySource {name='config/myconsul/'}, ConsulPropertySource {name='config/application,default/'}, ConsulPropertySource {name='config/application/'}]}
2019-11-02 22:49:05.526  INFO 5401 --- [           main] c.e.myconsul.MyConsulApplicationKt       : The following profiles are active: default
2019-11-02 22:49:06.453  WARN 5401 --- [           main] o.s.boot.actuate.endpoint.EndpointId     : Endpoint ID 'bus-env' contains invalid characters, please migrate to a valid format.
2019-11-02 22:49:06.658  WARN 5401 --- [           main] o.s.boot.actuate.endpoint.EndpointId     : Endpoint ID 'bus-refresh' contains invalid characters, please migrate to a valid format.
2019-11-02 22:49:06.745  WARN 5401 --- [           main] o.s.boot.actuate.endpoint.EndpointId     : Endpoint ID 'service-registry' contains invalid characters, please migrate to a valid format.
2019-11-02 22:49:06.901  INFO 5401 --- [           main] o.s.cloud.context.scope.GenericScope     : BeanFactory id=6becbf98-b3f5-3e17-8c2f-cd8e1a8f2d5d
2019-11-02 22:49:06.956  INFO 5401 --- [           main] faultConfiguringBeanFactoryPostProcessor : No bean named 'errorChannel' has been explicitly defined. Therefore, a default PublishSubscribeChannel will be created.
2019-11-02 22:49:06.961  INFO 5401 --- [           main] faultConfiguringBeanFactoryPostProcessor : No bean named 'taskScheduler' has been explicitly defined. Therefore, a default ThreadPoolTaskScheduler will be created.
2019-11-02 22:49:06.965  INFO 5401 --- [           main] faultConfiguringBeanFactoryPostProcessor : No bean named 'integrationHeaderChannelRegistry' has been explicitly defined. Therefore, a default DefaultHeaderChannelRegistry will be created.
2019-11-02 22:49:07.006  INFO 5401 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.retry.annotation.RetryConfiguration' of type [org.springframework.retry.annotation.RetryConfiguration$$EnhancerBySpringCGLIB$$1d6cbd94] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2019-11-02 22:49:07.041  INFO 5401 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'integrationChannelResolver' of type [org.springframework.integration.support.channel.BeanFactoryChannelResolver] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2019-11-02 22:49:07.046  INFO 5401 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'integrationDisposableAutoCreatedBeans' of type [org.springframework.integration.config.annotation.Disposables] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2019-11-02 22:49:07.058  INFO 5401 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.integration.config.IntegrationManagementConfiguration' of type [org.springframework.integration.config.IntegrationManagementConfiguration] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2019-11-02 22:49:07.063  INFO 5401 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.boot.autoconfigure.integration.IntegrationAutoConfiguration$IntegrationJmxConfiguration' of type [org.springframework.boot.autoconfigure.integration.IntegrationAutoConfiguration$IntegrationJmxConfiguration] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2019-11-02 22:49:07.070  INFO 5401 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.boot.autoconfigure.jmx.JmxAutoConfiguration' of type [org.springframework.boot.autoconfigure.jmx.JmxAutoConfiguration] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2019-11-02 22:49:07.074  INFO 5401 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'mbeanServer' of type [com.sun.jmx.mbeanserver.JmxMBeanServer] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2019-11-02 22:49:07.090  INFO 5401 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration' of type [org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration$$EnhancerBySpringCGLIB$$43c3286c] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2019-11-02 22:49:07.594  WARN 5401 --- [           main] c.n.c.sources.URLConfigurationSource     : No URLs will be polled as dynamic configuration sources.
2019-11-02 22:49:07.594  INFO 5401 --- [           main] c.n.c.sources.URLConfigurationSource     : To enable URLs as dynamic configuration sources, define System property archaius.configurationSource.additionalUrls or make config.properties available on classpath.
2019-11-02 22:49:07.601  INFO 5401 --- [           main] c.netflix.config.DynamicPropertyFactory  : DynamicPropertyFactory is initialized with configuration sources: com.netflix.config.ConcurrentCompositeConfiguration@83b0d0f
2019-11-02 22:49:07.752  INFO 5401 --- [           main] o.s.s.c.ThreadPoolTaskScheduler          : Initializing ExecutorService 'taskScheduler'
2019-11-02 22:49:07.946  INFO 5401 --- [           main] o.s.b.a.e.web.EndpointLinksResolver      : Exposing 2 endpoint(s) beneath base path '/actuator'
====> null
2019-11-02 22:49:08.105  WARN 5401 --- [           main] c.n.c.sources.URLConfigurationSource     : No URLs will be polled as dynamic configuration sources.
2019-11-02 22:49:08.105  INFO 5401 --- [           main] c.n.c.sources.URLConfigurationSource     : To enable URLs as dynamic configuration sources, define System property archaius.configurationSource.additionalUrls or make config.properties available on classpath.
2019-11-02 22:49:08.287  INFO 5401 --- [           main] c.f.c.c.BeanFactoryAwareFunctionRegistry : Looking up function 'null' with acceptedOutputTypes: []
2019-11-02 22:49:08.474  WARN 5401 --- [           main] ockingLoadBalancerClientRibbonWarnLogger : You already have RibbonLoadBalancerClient on your classpath. It will be used by default. As Spring Cloud Ribbon is in maintenance mode. We recommend switching to BlockingLoadBalancerClient instead. In order to use it, set the value of `spring.cloud.loadbalancer.ribbon.enabled` to `false` or remove spring-cloud-starter-netflix-ribbon from your project.
2019-11-02 22:49:08.507  WARN 5401 --- [           main] eactorLoadBalancerClientRibbonWarnLogger : You have RibbonLoadBalancerClient on your classpath. LoadBalancerExchangeFilterFunction that uses it under the hood will be used by default. Spring Cloud Ribbon is now in maintenance mode, so we suggest switching to ReactorLoadBalancerExchangeFilterFunction instead. In order to use it, set the value of `spring.cloud.loadbalancer.ribbon.enabled` to `false` or remove spring-cloud-starter-netflix-ribbon from your project.
2019-11-02 22:49:08.555  INFO 5401 --- [           main] o.s.s.c.ThreadPoolTaskScheduler          : Initializing ExecutorService 'configWatchTaskScheduler'
2019-11-02 22:49:08.565  INFO 5401 --- [           main] o.s.s.c.ThreadPoolTaskScheduler          : Initializing ExecutorService 'catalogWatchTaskScheduler'
2019-11-02 22:49:08.668  INFO 5401 --- [           main] o.s.c.s.m.DirectWithAttributesChannel    : Channel 'myconsul-1.springCloudBusInput' has 1 subscriber(s).
2019-11-02 22:49:08.777  INFO 5401 --- [           main] o.s.i.monitor.IntegrationMBeanExporter   : Registering MessageChannel springCloudBusInput
2019-11-02 22:49:08.860  INFO 5401 --- [           main] o.s.i.monitor.IntegrationMBeanExporter   : Registering MessageChannel errorChannel
2019-11-02 22:49:08.896  INFO 5401 --- [           main] o.s.i.monitor.IntegrationMBeanExporter   : Registering MessageChannel nullChannel
2019-11-02 22:49:08.910  INFO 5401 --- [           main] o.s.i.monitor.IntegrationMBeanExporter   : Registering MessageChannel springCloudBusOutput
2019-11-02 22:49:08.925  INFO 5401 --- [           main] o.s.i.monitor.IntegrationMBeanExporter   : Registering MessageHandler org.springframework.cloud.stream.binding.StreamListenerMessageHandler@7497a554
2019-11-02 22:49:08.973  INFO 5401 --- [           main] o.s.i.monitor.IntegrationMBeanExporter   : Registering MessageHandler errorLogger
2019-11-02 22:49:09.058  INFO 5401 --- [           main] o.s.i.endpoint.EventDrivenConsumer       : Adding {logging-channel-adapter:_org.springframework.integration.errorLogger} as a subscriber to the 'errorChannel' channel
2019-11-02 22:49:09.058  INFO 5401 --- [           main] o.s.i.channel.PublishSubscribeChannel    : Channel 'myconsul-1.errorChannel' has 1 subscriber(s).
2019-11-02 22:49:09.058  INFO 5401 --- [           main] o.s.i.endpoint.EventDrivenConsumer       : started bean '_org.springframework.integration.errorLogger'
2019-11-02 22:49:09.121  INFO 5401 --- [           main] b.c.PropertySourceBootstrapConfiguration : Located property source: CompositePropertySource {name='consul', propertySources=[ConsulPropertySource {name='config/myconsul,default/'}, ConsulPropertySource {name='config/myconsul/'}, ConsulPropertySource {name='config/application,default/'}, ConsulPropertySource {name='config/application/'}]}
2019-11-02 22:49:09.157  INFO 5401 --- [           main] o.s.i.endpoint.EventDrivenConsumer       : Adding {message-handler:outbound.springCloudBus} as a subscriber to the 'springCloudBusOutput' channel
2019-11-02 22:49:09.157  INFO 5401 --- [           main] o.s.c.s.m.DirectWithAttributesChannel    : Channel 'myconsul-1.springCloudBusOutput' has 1 subscriber(s).
2019-11-02 22:49:09.157  INFO 5401 --- [           main] o.s.i.endpoint.EventDrivenConsumer       : started bean 'outbound.springCloudBus'
2019-11-02 22:49:09.170  INFO 5401 --- [           main] o.s.c.c.b.ConsulInboundMessageProducer   : started org.springframework.cloud.consul.binder.ConsulInboundMessageProducer@878feb2
2019-11-02 22:49:09.339  INFO 5401 --- [           main] o.s.b.web.embedded.netty.NettyWebServer  : Netty started on port(s): 8080
2019-11-02 22:49:09.375  INFO 5401 --- [           main] o.s.c.c.s.ConsulServiceRegistry          : Registering service with consul: NewService{id='myconsul-9d80f0a272c1126ed0ec586ee888be1f', name='myconsul', tags=[secure=false], address='192.168.0.11', meta=null, port=8080, enableTagOverride=null, check=Check{script='null', interval='30s', ttl='null', http='http://192.168.0.11:8080/', method='null', header={}, tcp='null', timeout='null', deregisterCriticalServiceAfter='null', tlsSkipVerify=null, status='null'}, checks=null}
2019-11-02 22:49:09.404  INFO 5401 --- [           main] c.e.myconsul.MyConsulApplicationKt       : Started MyConsulApplicationKt in 6.34 seconds (JVM running for 7.778)

AppProperties类不是spring管理的bean。您需要将@EnableConfigurationProperties(AppProperties::class)添加到spring托管bean:

@SpringBootApplication(scanBasePackages = ["com.example.myconsul"])
@EnableConfigurationProperties(AppProperties::class)
class MyConsulApplication

之后,您的属性类将由Spring上下文进行管理。

如果你想让它在@RefreshScope中是可变的和可刷新的,那么你需要删除@ConstructorBinding并像这样重写你的AppProperties:

@ConstructorBinding
@RefreshScope
@ConfigurationProperties("myconsul")
class AppProperties {
lateinit var message: String
@PostConstruct
fun postConstruct() {
println("====> $message")
}
}

由于一些明显的原因,consul使变量不是您想要的类型(在本例中为String?,这正是您使用null作为响应的原因(,而是可变映射,因为它希望为该属性设置超过1个值,因此您可以将第一个实现更改为此

@Component
@RefreshScope
@ConfigurationProperties("myconsul")
class AppProperties(val message: MutableList<String> = mutableListOf()) {
@PostConstruct
fun postConstruct() {
//
}
}

编辑:刚刚发现你可以使用显式类型,但它必须设置为var

@Component
@RefreshScope
@ConfigurationProperties("myconsul")
class AppProperties(var message: String? = null) {
@PostConstruct
fun postConstruct() {
//
}
}

因此,我通过创建一个java项目并使其工作来解决这个问题。接下来,基于我的Java项目,我创建了一个反应式的Kotlin版本。这两个项目都能正常工作,而且是独立的。我希望他们能帮助别人。以下是项目链接:

Java Consul键值示例

Kotlin Consul键值示例

最新更新