Spring cloud config client is not getting/loading configuration files from config server after upgrading to 2.4.0 - spring

spring-cloud-config-client is not able to read configuration files from the spring-cloud-config-server after upgrading to 2.4.0 with spring-cloud version 2020.0.0-M6

From spring-boot 2.4.0 version, bootstrapping is not enabled by default, need to add the following dependency in your build.gradle
implementation 'org.springframework.cloud:spring-cloud-starter-bootstrap'
or pom.xml
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>

Adding to the application file (.properties or .yml) the property spring.config.import , with this it is not necessary to change/add the project's dependencies
Example:
To connect with the default location of "http://localhost:8888" or de value defined in the property spring.cloud.config.uri
spring.config.import=optional:configserver:
For more info:
https://docs.spring.io/spring-cloud-config/docs/3.0.0/reference/html/#config-data-import

For new spring cloud versions please don't use the legacy dependency spring-cloud-starter-bootstrap instead you need to use application.yml/application.properties instead of bootstrap.yml/bootstrap.properties and then set into that file the following properties:
spring:
config:
import: configserver:${your_config_server_url} # example: import: configserver:http://192.168.0.4:8080
cloud:
config:
username: ${your_config_server_auth_user} # This is required only if your config server use authentication
password: ${your_config_server_auth_password} # This is required only if your config server use authentication

Related

Spring boot application with Spring-boot-starter-data-jpa and spring-boot-starter-test makes spring does not find application properties

I have created a small application with spring boot that access to a database (mySql) using spring data:
spring-boot-starter-parent: 2.4.2
spring-boot-starter-web: 2.4.2
spring-boot-starter-data-jpa: 2.4.2
mysql-connector-java: 8.0.22
spring-boot-starter-test: 2.4.2
org.junit.jupiter: 5.7.0
junit-jupiter-engine: 5.7.0
I have configured my application.properties, inside src/main/resources, and I have configured the properties for url, username, password, driver, ..., like this:
spring.datasource.url=jdbc:mysql://localhost:3306/BBDD
spring.datasource.username=XXX
spring.datasource.password=XXXX
spring.datasource.driver=com.mysql.jdbc.Driver
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.dialect:org.hibernate.dialect.MySQL5Dialect
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
I have an interface for my repository for access to my database:
#Repository
public interface ArticleRepository extends PagingAndSortingRepository<ArticuloEntity, Integer> {
}
also, I have defined my entity.
The issue consists on when I start my webapp, I get an error because it does not read the parameters for configuring the datasource from the application.properties:
Description:
Failed to configure a DataSource: 'url' attribute is not specified and
no embedded datasource could be configured.
Reason: Failed to determine a suitable driver class
Action:
Consider the following: If you want an embedded database (H2, HSQL or
Derby), please put it on the classpath. If you have database settings
to be loaded from a particular profile you may need to activate it (no
profiles are currently active).
If I remove the dependencies for spring-boot-starter-test and junit, the application loads fine, and it access to database. But if I add the dependency for adding my junits, it does not read my application.properties file and I get the previous error.
How can I have my application configured with junits and reading the configuration for my application with the application.properties file?
Thank you in advance!
Your test/application.properties needs to be configured as well. Try adding this to your test application properties file
spring.datasource.url=jdbc:h2:mem:BBDD;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE
and this to your pom if you don't already support h2.
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>test</scope>
</dependency>
check your application.properties
changing
spring.datasource.driver=com.mysql.jdbc.Driver
to
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

Hazelcast Cache metric error with Spring Boot 2.1.9.RELEASE

We are unable to start our Spring Boot application (version 2.1.9.RELEASE) when using Hazelcast (artifact hazelcast-all and version 4.1). I get error
An attempt was made to call a method that does not exist. The attempt was made from the following location: org.springframework.boot.actuate.metrics.cache.HazelcastCacheMeterBinderProvider.getMeterBinder(HazelcastCacheMeterBinderProvider.java)
The following method did not exist: com.hazelcast.spring.cache.HazelcastCache.getNativeCache()Lcom/hazelcast/core/IMap;
The method's class, com.hazelcast.spring.cache.HazelcastCache, is available from the following locations: jar:file:<path>/.m2/repository/com/hazelcast/hazelcast-all/4.1/hazelcast-all-4.1.jar!/com/hazelcast/spring/cache/HazelcastCache.class
It was loaded from the following location: file:<path>/.m2/repository/com/hazelcast/hazelcast-all/4.1/hazelcast-all-4.1.jar
Action: Correct the classpath of your application so that it contains a single, compatible version of com.hazelcast.spring.cache.HazelcastCache We checked for IMap class - package com.hazelcast.map.IMap. Any solution/suggestion on this issue?
Trying to see if the following compatibility works.
Just looking at https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-actuator-autoconfigure/2.1.9.RELEASE , the HazelCast version is 3.11.4. Can you change the version of hazelcast from 4.1 to 3.11.4 (on the POM) and try it again.
<dependency>
<groupId>com.hazelcast</groupId>
<artifactId>hazelcast-all</artifactId>
<version>3.11.4</version>
</dependency>

Spring Cloud not picking up configured profile in cloud.aws.credentials.profile-name

I have a spring boot application where I've been using Spring Cloud AWS.
I'm trying to add a new profile to my ~/.aws/credentials file like so:
[local-dev]
aws_access_key_id=localkey
aws_secret_access_key=localkey
in my application.yml file I've specified which profile to use.
cloud:
aws:
credentials:
profile-name: local-dev
region:
static: us-east-1
whenever I start up my Application I get a error saying:
No AWS profile named 'default', com.amazonaws.auth.EC2ContainerCredentialsProviderWrapper#1b26fac2: Failed to connect to service endpoint:
When I set the profile name to "default" it does work though. It's like spring is ignoring the profile-name that I've given it in the configuration.
Currently I have set this version of spring cloud in my maven POM file.
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-aws -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-aws</artifactId>
<version>2.2.1.RELEASE</version>
</dependency>
I faced the same issue when using the same Spring Cloud Libraries version - 2.2.1.RELEASE.
After updating the Spring Cloud libraries to 2.2.6.RELEASE everything works fine.

Active profile ignored

Sample code
I have already added "spring.profiles.active=local" in application.properties file
But while running the application it is not able to recognize the profile and running with default profile
application.properties
# Tomcat Server Config
server.port=8071
#Profile Config
spring.profiles.active=local
application-local.properties
# Spring JPA Config
spring.datasource.url=jdbc:postgresql://localhost:5432/User
spring.datasource.username=postgres
spring.datasource.password=amt123`
# Hibernate Config
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
spring.jpa.properties.hibernate.id.new_generator_mappings=false
spring.jpa.properties.hibernate.format_sql=true
spring.jpa.hibernate.ddl-auto=none
"spring.devtools.add-properties" default value is true
Once it is set to false, spring boot disables profile configuration.
For me even if I set the value to true, still profile configuration was not enabled
I fixed this issue by removing the dev-tool dependency and adding it again.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
I am not sure on the internal workflow.
If anyone has a proper explanation for this, please comment

info endpoint - application.yml maven filtering not working

We are using spring boot 1.3.1.
I have following details in my application.yml file.
info:
build:
artifact: ${project.artifactId}
name: ${project.name}
description: ${project.description}
version: ${project.version}
I am building the war file and deploying it to tomcat. When I access /info endpoint, i don't see values are substituted.
I am not using 'spring-boot-maven-plugin' as i don't need to run the application standalone.
Does the filtering work for YML files by default?
Also, is there any example of showing application version on banner.txt file using maven project.version property?
After Dave's comment, I updated my application.yml file as shown below and it is working.
info:
build:
artifact: #project.artifactId#
name: #project.name#
description: #project.description#
version: #project.version#
Also, I found that application.version is not picked up in standalone tomcat for banner.txt file. Tomcat fails to read manifest.mf file.
Spring boot issue: https://github.com/spring-projects/spring-boot/issues/3406
I don't think filtering is switched on in maven by default. If you use the spring-boot-starter-parent it is enabled, but the key for the placeholders is #..# not ${..}.
I don't know which version of spring boot you are using,
But for me version: #project.version# did not work.
Instead version: #project.version# worked for me.
My project is having following parent pom config :
<parent>
<artifactId>spring-boot-starter-parent</artifactId>
<groupId>org.springframework.boot</groupId>
<version>1.4.1.RELEASE</version>
<relativePath />
</parent>

Resources