Spring boot fails to start when add Kubernetes dependencies "The bean 'kubernetesPodUtils', defined in class path resource" - spring-boot

I trying to add Kubernetes to my project to use Service name rather than localhost. I added the below dependencies :
#AutoConfigureAfter
#EnableEurekaClient
#EnableFeignClients
#EnableDiscoveryClient
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-kubernetes-client-all -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-kubernetes-client-all</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-kubernetes-config</artifactId>
<version>1.1.10.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-kubernetes</artifactId>
<version>1.1.10.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-kubernetes-discovery</artifactId>
<version>2.1.3</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-kubernetes-core</artifactId>
<version>1.1.10.RELEASE</version>
</dependency>
<!-- MicroServices -->
But I got the below error :
***************************
APPLICATION FAILED TO START
***************************
Description:
The bean 'kubernetesPodUtils', defined in class path resource [org/springframework/cloud/kubernetes/KubernetesAutoConfiguration.class], could not be registered. A bean with that name has already been defined in class path resource [org/springframework/cloud/kubernetes/client/KubernetesClientAutoConfiguration.class] and overriding is disabled.
Action:
Consider renaming one of the beans or enabling overriding by setting spring.main.allow-bean-definition-overriding=true
2022-06-14 12:28:08.696 ERROR 1 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter :
***************************
APPLICATION FAILED TO START
***************************
Description:
The bean 'kubernetesPodUtils', defined in class path resource [org/springframework/cloud/kubernetes/KubernetesAutoConfiguration.class], could not be registered. A bean with that name has already been defined in class path resource [org/springframework/cloud/kubernetes/client/KubernetesClientAutoConfiguration.class] and overriding is disabled.
Action:
Consider renaming one of the beans or enabling overriding by setting spring.main.allow-bean-definition-overriding=true

Looks like KubernetesAutoConfiguration is going to be defined in multiple dependencies. Based on your need, try to identify the dependencies which are required. In your pom file you have spring-cloud-starter-kubernetes-client-all alongwith individual dependecies too.
https://docs.spring.io/spring-cloud-kubernetes/docs/current/reference/html/#starters

Related

Tomcat 10, Java 17 - JavaMailSender cannot cast one of elements of java.lang.Object[]

After migrating application to Spring Boot 3 and Java 17 I tried to deploy it to Tomcat 10.1.x.
The deployment failed with exception:
Caused by: org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'mailSender' defined in class path
resource [org/springframework/boot/autoconfig
ure/mail/MailSenderPropertiesConfiguration.class]: Failed to
instantiate [org.springframework.mail.javamail.JavaMailSenderImpl]:
Factory method 'mailSender' threw exception with message: arraycopy:
element type mismatch: can not cast one of the elements of
java.lang.Object[] to the type of the destination array,
jakarta.activation.MimeTypeRegistry
The issue turned out to be caused by CXF library having dependency on:
com.sun.activation:jakarta.activation.
The solution is to exclude it from cxf:
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<exclusions>
<exclusion>
<groupId>com.sun.activation</groupId>
<artifactId>jakarta.activation</artifactId>
</exclusion>
</exclusions>
</dependency>
Instead include directly the jakarta.activation-api as below:
<dependency>
<groupId>jakarta.activation</groupId>
<artifactId>jakarta.activation-api</artifactId>
<version>2.1.1</version>
</dependency>
There might be other libraries with the same dependency in the application.
For above the approach with deploying application to webapps-javaee folder and allowing Tomcat to perform migration doesn't work.

Spring Cloud Kafka Stream Multibinder BeanCreationException: Error creating bean with name 'kafkaStreamsFunctionProcessorInvoker'

I am trying to create a spring boot application using spring cloud kafka stream which reads input from kafka cluster 1 and send it to kafka cluster 2 using single kafkastream application.
I am getting following exception during startup.
***************************
APPLICATION FAILED TO START
***************************
Description:
A component required a bean named 'sessionBrokers-KafkaStreamsBinderConfigurationProperties' that could not be found.
Action:
Consider defining a bean named 'sessionBrokers-KafkaStreamsBinderConfigurationProperties' in your configuration.
My configuration is as below
spring.cloud.function.definition=itemConsumedStream
#Input Configuration
spring.cloud.stream.bindings.itemConsumedStream-in-0.binder=sessionBrokers
spring.cloud.stream.bindings.itemConsumedStream-in-0.destination=${session.event.data.input.topic}
spring.cloud.stream.kafka.bindings.itemConsumedStream-in-0.consumer.configuration.key.deserializer=org.apache.kafka.common.serialization.StringSerializer
spring.cloud.stream.kafka.bindings.itemConsumedStream-in-0.consumer.configuration.value.deserializer=in.custom.JsonNodeDeserializer
#Output Configuration
spring.cloud.stream.bindings.itemConsumedStream-out-0.binder=searchBrokers
spring.cloud.stream.bindings.itemConsumedStream-out-0.destination=${itemConsumed.events.output.topic}
spring.cloud.stream.kafka.bindings.itemConsumedStream-out-0.producer.configuration.key.serializer=org.apache.kafka.common.serialization.StringSerializer
spring.cloud.stream.kafka.bindings.itemConsumedStream-out-0.producer.configuration.value.serializer=in.custom.JsonSerializer
#Input Binder Configurations
spring.cloud.stream.binders.sessionBrokers.type=kafka
spring.cloud.stream.binders.sessionBrokers.environment.spring.cloud.stream.kafka.streams.binder.brokers=${session.event.data.input.kafka.brokers}
#Output Binder Configurations
spring.cloud.stream.binders.searchBrokers.type=kafka
spring.cloud.stream.binders.searchBrokers.environment.spring.cloud.stream.kafka.binder.brokers=${search.kafka.brokers}
pom dependencies are
spring boot version
2.7.1
spring cloud version
2021.0.0
<dependency>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka-streams</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-stream</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-stream-binder-kafka-streams</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-stream-binder-kafka</artifactId>
</dependency>
This is working if I use single binder ie
spring.cloud.stream.kafka.streams.binder.brokers=${session.event.data.input.kafka.brokers}
instead of multiple binders

Autowire CacheManger (Caffeine & Ehcache) without using #SpringBootApplication

I have a common caching module which has spring boot starter cache (version 2.2.4.RELEASE) and for caching has dependencies of ehcache & caffeine. Below is the pom file
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency>
<groupId>javax.cache</groupId>
<artifactId>cache-api</artifactId>
</dependency>
<dependency>
<groupId>org.ehcache</groupId>
<artifactId>ehcache</artifactId>
</dependency>
<dependency>
<groupId>com.github.ben-manes.caffeine</groupId>
<artifactId>caffeine</artifactId>
</dependency>
</dependencies>
In this module, Autowired the CacheManger and has cache update & get method using spring cache.
#Autowired
private CacheManager cacheManager;
This module is added as dependency in the app1 which is springboot application and as per the property "spring.cache.type", when app1 starts it will initialize the respective cache
For caffeine cache
spring.cache.type=caffeine
spring.cache.caffeine.spec=maximumSize=10000
For Ehcache3
spring.cache.type=caffeine
spring.cache.jcache.provider=org.ehcache.jsr107.EhcacheCachingProvider
spring.cache.jcache.config=classpath:ehcache.xml
This is all working fine for springboot application app1.
Now i am using the same caching module in app2 which has functionality same as app1 but it is not springboot application. In app2 using #Configuration , #ComponentScan to resolve the dependency. Now app2 is being used in app3 which is springboot application. All the other dependencies are resolved and working fine in app3 except the cacheManger. Getting the below error while running the app3
*\r\nAPPLICATION FAILED TO START\r\n***************************\r\n\r\nDescription:\r\n\r\nField cacheManager in commonCacheService required a bean of type 'org.springframework.cache.CacheManager' that could not be found.\r\n\r\nThe injection point has the following annotations:\r\n\t- #org.springframework.beans.factory.annotation.Autowired(required=true)\r\n\r\nThe following candidates were found but could not be injected:\r\n\t- Bean method 'cacheManager' in 'EhCacheCacheConfiguration' not loaded because #ConditionalOnClass did not find required class 'net.sf.ehcache.Cache'\r\n\t- Bean method 'cacheManager' in 'GenericCacheConfiguration' not loaded because Cache org.springframework.boot.autoconfigure.cache.GenericCacheConfiguration unknown cache type\r\n\t- Bean method 'cacheManager' in 'JCacheCacheConfiguration' not loaded because Cache org.springframework.boot.autoconfigure.cache.JCacheCacheConfiguration unknown cache type\r\n\t- Bean method 'cacheManager' in 'NoOpCacheConfiguration' not loaded because Cache org.springframework.boot.autoconfigure.cache.NoOpCacheConfiguration unknown cache type\r\n\t- Bean method 'cacheManager' in 'SimpleCacheConfiguration' not loaded because Cache org.springframework.boot.autoconfigure.cache.SimpleCacheConfiguration unknown cache type\r\n\r\n\r\nAction:\r\n\r\nConsider revisiting the entries above or defining a bean of type 'org.springframework.cache.CacheManager' in your configuration.\r\n"}
It is working if i create bean in common cache module like below
#Bean
#ConditionalOnProperty(name="spring.cache.type", havingValue="caffeine")
#Primary
public CacheManager cacheManager() {
CaffeineCacheManager cacheManager = new CaffeineCacheManager();
cacheManager.setCaffeine(Caffeine.newBuilder().maximumSize(125000));
return cacheManager;
}
But i want to avoid the bean creation in common module. Is there any annotation/configuration required in app2 (without changing app3 as don't have any update control on app3)so that it will also work in same way as it is working in app1 )?
I know it is quite late, but I faced the same issue. It was resolved by adding dependency
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency>
<groupId>javax.cache</groupId>
<artifactId>cache-api</artifactId>
<version>1.1.1</version>
</dependency>
And add
#EnableCaching
in the SpringBootApplciation Class

Do Spring LDAP and Spring Cloud Consul work together?

I had a project with Spring LDAP in place. I am trying to put in Spring Consul into this project and have been facing issues with respect to it.
Here is the pom.xml :
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-all</artifactId>
<version>2.0.0.RELEASE</version>
</dependency>
Here is application properties :
#consul properties
spring.cloud.consul.config.format=PROPERTIES
spring.cloud.consul.discovery.healthCheckPath=/<root>/health
spring.cloud.consul.discovery.healthCheckInterval=15s
spring.application.name=<App_Name>
spring.profiles.active=dev
And I enabled discovery client using #EnableDiscoveryClient on SpringBootApplication class.
But, I am ending up with this error and the app never starts :
***************************
APPLICATION FAILED TO START
***************************
Description:
Field ldapTemplate in com.<package>.config.LdapClient required a bean of type 'org.springframework.ldap.core.LdapTemplate' that could not be found.
- Bean method 'ldapTemplate' not loaded because #ConditionalOnClass did not find required class 'org.springframework.data.ldap.repository.LdapRepository'
Action:
Consider revisiting the conditions above or defining a bean of type 'org.springframework.ldap.core.LdapTemplate' in your configuration.
Disconnected from the target VM, address: '127.0.0.1:62703', transport: 'socket'
I figure it has something to do with autodiscovery of Ldap and introducing Consul is causing this issue, but, I am not able to pin-point the problem.
Can someone please help me resolve this issue?

Spring Cloud Contract producer cannot send message

I'm trying to incorporate Spring Cloud Contract into an existing project. I've had some success with REST but I'm struggling to set up the messaging side.
Thus far I've set up a contract on the producer, which does produce a test in target/generated-test-sources/contracts. I've also set up a base class for the test.
I can't get past this error:
2017-09-08 17:10:51.759 ERROR - --[]- [ main] o.s.c.c.v.m.stream.StreamStubMessages : Exception took place while trying to resolve the destination. Will assume the name [invites]
org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.cloud.stream.config.ChannelBindingServiceProperties' available
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:353)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:340)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1093)
at org.springframework.cloud.contract.verifier.messaging.stream.StreamStubMessages.resolvedDestination(StreamStubMessages.java:86)
at org.springframework.cloud.contract.verifier.messaging.stream.StreamStubMessages.receive(StreamStubMessages.java:73)
at org.springframework.cloud.contract.verifier.messaging.stream.StreamStubMessages.receive(StreamStubMessages.java:110)
at org.springframework.cloud.contract.verifier.messaging.stream.StreamStubMessages.receive(StreamStubMessages.java:36)
at org.springframework.cloud.contract.verifier.messaging.internal.ContractVerifierMessaging.receive(ContractVerifierMessaging.java:40)
at org.springframework.cloud.contract.verifier.tests.email.MessagingTest.validate_invitedContract(MessagingTest.java:27)
and later
2017-09-08 17:10:51.759 ERROR - --[]- [ main] o.s.c.c.v.m.stream.StreamStubMessages : Exception occurred while trying to read a message from a channel with name [invites]
org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'invites' available
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:687)
at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1207)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:284)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
My application.yml file under src/test/resources:
spring:
cloud:
stream:
bindings:
output:
content-type: application/json
destination: invites
I have the following dependencies:
`
<!-- Spring Cloud Contract Deps -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-stream</artifactId>
<version>1.2.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-contract-verifier</artifactId>
<version>1.1.3.RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-stream-test-support</artifactId>
<version>1.2.2.RELEASE</version>
<scope>test</scope>
</dependency>
<!-- END SCC Deps -->`
I've combed through the docs, watched Marcin's talk and looked through the samples for Spring Cloud Contract but I'm stuck. Any help would be greatly appreciated.
First issue:
Please use the release train. In the release train, we know that there are no invalid dependencies.
Second issue:
You don't have the #EnableBinding(Source.class) annotation. That's why Stream doesn't know how to bind to the output channel.
If you go to the Spring Cloud Contract samples you'll notice this line on the main application class (https://github.com/spring-cloud-samples/spring-cloud-contract-samples/blob/master/producer/src/main/java/com/example/ProducerApplication.java#L9). After I've added this line to your code, the context started but the tests failed cause the message wasn't sent.

Resources