2021년 8월 12일 목요일

springboot 2.5 x & spring-cloud-starter-vault-config 오류 해결하기

 Springboot 2.5.x 버전은 spring-cloud-starter-vault-config 2.2.X 버전과 현재(2021.08.12)는 되지 않는다.


이런 오류가 난다.

Caused by: java.lang.IllegalStateException: Failed to introspect Class [org.springframework.cloud.context.properties.ConfigurationPropertiesBeans] from ClassLoader [sun.misc.Launcher$AppClassLoader@18b4aac2]
at org.springframework.util.ReflectionUtils.getDeclaredMethods(ReflectionUtils.java:481) ~[spring-core-5.3.8.jar:5.3.8]
at org.springframework.util.ReflectionUtils.doWithLocalMethods(ReflectionUtils.java:321) ~[spring-core-5.3.8.jar:5.3.8]
at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor.buildPersistenceMetadata(PersistenceAnnotationBeanPostProcessor.java:417) ~[spring-orm-5.3.8.jar:5.3.8]
at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor.findPersistenceMetadata(PersistenceAnnotationBeanPostProcessor.java:388) ~[spring-orm-5.3.8.jar:5.3.8]
at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor.postProcessMergedBeanDefinition(PersistenceAnnotationBeanPostProcessor.java:335) ~[spring-orm-5.3.8.jar:5.3.8]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyMergedBeanDefinitionPostProcessors(AbstractAutowireCapableBeanFactory.java:1098) ~[spring-beans-5.3.8.jar:5.3.8]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:576) ~[spring-beans-5.3.8.jar:5.3.8]
... 30 common frames omitted
Caused by: java.lang.NoClassDefFoundError: org/springframework/boot/context/properties/ConfigurationBeanFactoryMetadata
at java.lang.Class.getDeclaredMethods0(Native Method) ~[na:1.8.0_252]
at java.lang.Class.privateGetDeclaredMethods(Class.java:2701) ~[na:1.8.0_252]
at java.lang.Class.getDeclaredMethods(Class.java:1975) ~[na:1.8.0_252]
at org.springframework.util.ReflectionUtils.getDeclaredMethods(ReflectionUtils.java:463) ~[spring-core-5.3.8.jar:5.3.8]
... 36 common frames omitted
Caused by: java.lang.ClassNotFoundException: org.springframework.boot.context.properties.ConfigurationBeanFactoryMetadata
at java.net.URLClassLoader.findClass(URLClassLoader.java:382) ~[na:1.8.0_252]
at java.lang.ClassLoader.loadClass(ClassLoader.java:418) ~[na:1.8.0_252]
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:352) ~[na:1.8.0_252]
at java.lang.ClassLoader.loadClass(ClassLoader.java:351) ~[na:1.8.0_252]
... 40 common frames omitted

대충 보면 ConfigurationBeanFactoryMetadata class를 못찾는다는 뜻이다.

이유는 Springboot와 Springcloud의 Release Train이 다르기 때문에 아직 의존성이 깔끔하게 해결되지 않아서다.

참조: https://spring.io/projects/spring-cloud#overview 

Adding Spring Cloud To An Existing Spring Boot Application

If you an existing Spring Boot app you want to add Spring Cloud to that app, the first step is to determine the version of Spring Cloud you should use. The version you use in your app will depend on the version of Spring Boot you are using.

The table below outlines which version of Spring Cloud maps to which version of Spring Boot.

Table 1. Release train Spring Boot compatibility
Release TrainBoot Version

2020.0.x aka Ilford

2.4.x, 2.5.x (Starting with 2020.0.3)

Hoxton

2.2.x, 2.3.x (Starting with SR5)

Greenwich

2.1.x

Finchley

2.0.x

Edgware

1.5.x

Dalston

1.5.x

Spring Cloud Dalston, Edgware, Finchley, and Greenwich have all reached end of life status and are no longer supported.

해서 이 둘을 연결하려면 gradle 설정이 좀 필요하다.

gradle 설정은 대충 다음처럼 하면 된다. (참고로 아래코드는 kotlin dsl을 이용했따)


ext {
set("springCloudVersion", "2020.0.3")
}
configure<io.spring.gradle.dependencymanagement.dsl.DependencyManagementExtension> {
imports(delegateClosureOf<io.spring.gradle.dependencymanagement.dsl.ImportsHandler> {
mavenBom("org.springframework.cloud:spring-cloud-dependencies:2020.0.3")
})
}


그리고 한가지 더 

원래 vault config는 bootstrap.yml을 읽어서 vault config를 하게 되었는데 

springcloud bootstrap 2020.X 버전 부터는 기본적인 application.yml 안에서 vault 설정을 읽게 바뀌었다.