spring-projects / spring-ai

An Application Framework for AI Engineering
https://docs.spring.io/spring-ai/reference/index.html
Apache License 2.0
3.21k stars 814 forks source link

App fails to boot on Cloud Foundry when using Spring Cloud Services Service Discovery #1622

Open odedia opened 6 days ago

odedia commented 6 days ago

When running an application with both Spring AI and spring-cloud-services-starter-config-client as dependencies, the application fails to boot with the error below:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'retryTemplate' defined in class path resource [org/springframework/ai/autoconfigure/retry/SpringAiRetryAutoConfiguration.class]: Failed to instantiate [org.springframework.retry.support.RetryTemplate]: Factory method 'retryTemplate' threw exception with message: org/springframework/ai/retry/TransientAiException
    at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:648)
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:636)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1355)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1185)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:562)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:522)
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:337)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:335)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:975)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:971)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:625)
    at org.springframework.boot.web.reactive.context.ReactiveWebServerApplicationContext.refresh(ReactiveWebServerApplicationContext.java:66)
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:754)
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:456)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:335)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1363)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1352)
    at org.springframework.samples.petclinic.genai.GenAIServiceApplication.main(GenAIServiceApplication.java:30)
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.retry.support.RetryTemplate]: Factory method 'retryTemplate' threw exception with message: org/springframework/ai/retry/TransientAiException
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:178)
    at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:644)
    ... 19 common frames omitted
Caused by: java.lang.NoClassDefFoundError: org/springframework/ai/retry/TransientAiException
    at org.springframework.ai.autoconfigure.retry.SpringAiRetryAutoConfiguration.retryTemplate(SpringAiRetryAutoConfiguration.java:55)
    at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:103)
    at java.base/java.lang.reflect.Method.invoke(Method.java:580)
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:146)
    ... 20 common frames omitted
Caused by: java.lang.ClassNotFoundException: org.springframework.ai.retry.TransientAiException
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641)
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:526)
    ... 24 common frames omitted

Steps to reproduce: Although this fails when deploying to cloud foundry environment, it also fails when just having the dependency locally, so an easy way to check for it would be to add the dependency below to the pom.xml and try to boot:

        <dependency>
            <groupId>io.pivotal.spring.cloud</groupId>
            <artifactId>spring-cloud-services-starter-config-client</artifactId>
            <version>4.1.5</version>
            <type>pom</type>
        </dependency>

You can use the genai-service in this path for reference, just add the dependency above and try to boot. https://github.com/odedia/spring-petclinic-microservices/tree/main/spring-petclinic-genai-service

Thanks.

zhuyuy commented 1 day ago

When add dependency spring-cloud-services-starter-config-client, threre's RetryTemplate in your project. So SpringAiRetryAutoConfiguration start run.

@AutoConfiguration
@ConditionalOnClass(RetryTemplate.class)
@EnableConfigurationProperties({ SpringAiRetryProperties.class })
public class SpringAiRetryAutoConfiguration {}

For the time being, I think you can make the project keep running normally by add spring-ai-retry dependency. To fix it, the @ConditionalOnClass(RetryTemplate.class) might need to be replaced with another class relevant to spring-ai-retry.