Fallback option in Spring application properties in AWS - java

I have a REST service built with Spring Boot where I'm using an application.yml file to set up environment variables. The REST service will be deployed to Amazon Elastic Beanstalk. I have set up Environment Properties in the configuration for the Environment (Configuration -> Software Configuration -> Environment Properties)
I would like for the REST service to pick up the environment variables from AWS Environment Properties when they are available and then use environment variables specified in the application.yml file when the Environment Properties are not available as a fallback.
spring:
profiles: default
datasource:
type: org.mariadb.jdbc.MariaDbDataSource
driverClassName: org.mariadb.jdbc.Driver
url: jdbc:mariadb://localhost:3306/someDB
username: someUserName
password: somePassword
What I'm trying to achieve is that I wouldn't have to change the profile used in application.yml when I deploy on AWS or do development locally.
I've tried this which doesn't work
spring:
profiles: aws
datasource:
type: org.mariadb.jdbc.MariaDbDataSource
driverClassName: org.mariadb.jdbc.Driver
url: ${ADDRESS_FOR_RDS:jdbc:mariadb://localhost:3306/someDB}
username: ${USERNAME:someUserName}
password: ${PASSWORD:somePassword}
Trying to specify as ${AWS_PROPERTY_NAME:FALLBACK_OPTION} as per this Stack Overflow answer.
I have been able to get this working using Spring profiles, per this Stack Overflow question.

Related

When restarting Spring Boot I see 'no profiles are currently active'

My Spring Boot version is 2.3.4-RELEASE. I configure DataSource this way in application-dev.yml:
spring:
profiles: dev
datasource:
master:
driver-class-name: com.mysql.jdbc.Driver
url: myUrl
slave:
driver-class-name: com.mysql.jdbc.Driver
url: myUrl
When I activate the 'dev' profile, I can start my Spring Boot container successfully, but when I restart it, I get this error:
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).
I tried to add the config: spring.datasource.url. It works, but I need to distinguish master and slave. How can I do that?
You must activate your profile using property spring.profiles.active
If you have database settings to be loaded from a particular profile
you may need to activate it (no profiles are currently active).
Read set the active Spring profiles to see how to do it

Monitoring jar application using Spring boot Admin

I have an spring boot application .jar, based on spring batch,and it is not a web application, and I'm using a ksh script to Launch it on my server side.
for some performance observation, we decide to install Spring Boot Admin for this app, using a "sba.war" that is already deployed in another web application on another server, I tried to drop the sba.war beside my applicationBatch.war after updating the application.yml file and import the SBA dependency on my build.gradle file :
spring:
datasource:
# BDD DEV
url: jdbc:oracle:thin:#xxxx:9999:AAAAA
username: xxxxxx
password: xxxxxx
# SBA DEV
boot:
admin:
client:
enabled: true
instance:
service-base-url: https://xxxxx.com
url: https://xxxxx.com/sba
username: username
password: password
so the problem is, that my application is not deployed on a tomcat server, it is runned by the ksh script on it's embeded server.
So can I anyhow use the embeded server of SBA to run it ?

How to get multiple Consul KVs from different Consul context?

I have these KVs sources store on Consul:
config/books/<key>
config/common/<key>
And in my spring boot app application.yml, I have config it as following:
spring:
application:
name: sampleapp
cloud:
consul:
host: localhost
port: 8500
config:
enabled: true
prefix: config
defaultContext: books
I know that this configuration that I made has pointed the app to read from config/books .
But I can't figure out how can I retrieve the Consul KV config from both config/common and config/books.
It's not really flexible there, but it can be done if you rename your project and use defaultContext as well. Based on the documentation Spring Cloud Consul Config takes next paths:
config/testApp,dev/
config/testApp/
config/application,dev/
config/application/
where testApp - name of your app, application - default context
So with the following configuration it will work
spring:
application:
name: books
cloud:
consul:
host: localhost
port: 8500
config:
enabled: true
prefix: config
defaultContext: common
Logs after running this configuration:
Located property source: CompositePropertySource {name='consul', propertySources=[ConsulPropertySource {name='config/books/'}, ConsulPropertySource {name='config/common/'}]}

spring boot + docker + kubernetes

we have three environment as (dev, test and prod) and we have database configuration as below
spring:
jpa:
hibernate:
ddl-auto: update
datasource:
url: ${URL}
username: ${USERNAME}
password: ${PASSWORD}
What i am trying is that i create the jar and then build the image and when deploying in to the kubernetes , i will be using the dev , test and prod respective deployment.yaml in which i will be loading the url,username and password to env so the application will read it during prod start up
So when i am trying to build jar application try to connect to the database and it failed to create the jar.
Please let me know my understanding is wrong or right if wrong then how to correct it and just one thing is that i can't change the process i.e jar+ image + kubernetes
In Kubernetes you can put your configuration in Configmap or Secret. You can package the spring boot application and provide the Configmap entry as env variable of your container as exposed here
Using Spring Cloud Kubernetes you can also read these properties without any configuration on the container as explained in this article

Spring Boot Registration Server(Eureka Server)

I am working on a Spring Boot Registration Server(Eureka Server).
Currently it is working with below configuration.
Project Name: registration-service
Inside main method: System.setProperty("spring.config.name", "registration-service");
"yml file":
file name: registration-service
content:
eureka:
instance:
hostname: eureka-server
server:
enableSelfPreservation: false
client:
register-with-eureka: false
fetch-registry: false
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
server:
port: 2323 # HTTP (Tomcat) port
spring:
application:
name: eureka-server
With above configuration,application start running on 2323.
But if i change spring.config.name,it does not work,start giving connection refused exception.
why it is happening? even though this spring.config.name is no where used in yml file. Should it be necessarily same as project name? or it is specific to #EnableEurekaServer enabled spring boot application.
And in yml we have to write
spring:
application:
name: eureka-server
though in other spring boot application we give name of current project(here it should be registration-service).why we have to write here eureka-server? I know,I am missing something(or a lot of things).please help me in understanding the missing part.
Spring boot by default looks for file application.yml. If u have different profiles in your application it can also look for application-{profilename}.yml. This is the default convention followed.
spring.config.name property is used to override this default behaviour. When u override this property with register-service then spring boot looks for a file register-service.yml and loads config from that.
So your eureka server url which is given in the register-service.yml file may not be available in the default application.yml file. Hence when u change the value Spring boot may not be avle to find the Eureka server url.
Keep the names unchanged, as much as possible. If u have config file as register-service.yml then keep the spring.config.name=register-service. If you change this value then u need to create the new file with config.name value and then add eureka configuration to that again.

Categories