Datasource details as System properties? - java

Is it possible to pass the properties of a datasource to quarkus by using System properties or environment variables for example instead of using the application properties file, pass
-Dquarkus.datasource.url=jdbc:mysql://localhost:3306/ib24_booking?useSSL=false
so that the values can be passed differently according to the environment I am in.

It should work as you said. You can find more details here https://quarkus.io/guides/datasource

Related

Application Configuration Properties on Payara (how to read them)

I would like to use Application Configuration Properties that you can configure using the user interface of payara (for example):
My question is: how can I access these values on java code side? Through annotation? Through some sort of context instance maybe? Is it even possible?
I naively tried with System.getProperty with no success.
I don't believe there is a way.
An alternative would be to use system properties along with variable substitution via ${} syntax or environment variable via ${ENV.XXX}

Spring-boot property value used in another property key

In my spring boot project, I want to use a property value in another property key:
server.mode=mock
server.protocol.mock=http
server.host.mock=my.host-mock.org
...
server.protocol.prod=https
server.host.prod=my.host-prod.org
...
I want to depending on "server.mode" value use the related property key server.protocol.{value}
How could I do this?
Thanks for help
You can use spring profiles, where you can setup different property configurations for different deployment environments.
Using property files, you can create a property file per profile and then have spring boot use the right property configuration depending on the active profile.
application-dev.properties
server.scheme=http
server.host=my.host-mock.org
application-prod.properties
server.scheme=http
server.host=my.host-mock.org
You would then have to tell spring boot which profile to use by setting it in the spring.profiles.active property. When deploying to the cloud with application manifests (like Cloud Foundry or Kubernetes), then it is convenient to set this via an environment variable SPRING_PROFILES_ACTIVE.
See the official spring-boot documentation for more information about profiles.
This can be achieved using the following format while fetching the value in code (or the correpsonding xml) where you are using it:
#Value("${server.protocol.${server.mode}}")
private String mode;

Setting spring.profiles.active from an environment variable in Spring Boot with Gradle

My company has a standard way to specifying the environment a web service should run in, specifically 'development' and 'production' by using an environment variable APP_ENV. Each of these environments have a separate config file: application-dev.yml and application-prod.yml.
In spring boot, we can use SPRING_PROFILES_ACTIVE or equivalents to do this, but what I really want to do is grab the VALUE from APP_ENV, and use that as the profile.
I've got some code
SpringApplication app = new SpringApplication(Application.class);
springApp.setAdditionalProfiles(System.getenv("APP_ENV"););
springApp.run(args);
Any tips on how to do this a bit cleaner? I'm getting
java.lang.IllegalStateException: Failed to load ApplicationContext errors on various gradle tasks that make this solution kind of flimsy.
you can use System.getProperty which will always return a String like below,
springApp.setAdditionalProfiles(System.getProperty("APP_ENV", "dev"));
In the above snippet, we use System.getProperty(“APP_ENV”) to extract the value of the property APP_ENV. We also are making use of the default value so if the property is not available in the system, getProperty returns dev.

How to access the credentials in Kubernetes for MongoDB with Java?

I am working on Java Springboot with MongoDB using Kubernetes. Currently I just hard coded the URI in application properties and I would like to know
how can I access to the MongoDB credentials on Kubernetes with Java?
The recommended way of passing credentials to Kubernetes pods is to use secrets and to expose them to the application either as environment variables, or as a volume. The link above describes in detail how each approach works.
If I properly understood the question, it is specifically about Java Spring Boot applications running on Kubernetes.
Few options come to my mind...some not that secure or exclusive to running on Kubernetes but still mentioned here:
Environment variables with values in the deployment/pod configuration. Everyone with access to the configuration will be able to see them.
Use ${<env-var>} / ${<end-var>:<default-value>} to access the environment variables in Spring Boot's application.properties/.yaml file. For example, if DB_USERNAME and DB_PASSWORD are two such environment variables:
spring.data.mongodb.username = ${DB_USERNAME}
spring.data.mongodb.password = ${DB_PASSWORD}
...or
spring.data.mongodb.uri = mongodb://${DB_USERNAME}:${DB_PASSWORD}#<host>:<port>/<dbname>
This will work regardless whether the application uses spring.data.mongodb.* properties or properties with custom names injected in a #Configuration class with #Value.
Based on how the Java application is started in the container, startup arguments can be defined in the deployment/pod configuration, similarly to the bullet point above.
Environment variables with values populated from secret(s). Access the environment variables from SpringBoot as above.
Secrets as files - the secrets will "appear" in a file dynamically added to the container at some location/directory; it would require you to define your own #Configuration class that loads the user name and password from the file using #PropertySource.
The whole application.properties could be put in a ConfigMap. Notice that the properties will be in clear text. Then populate a Volume with the ConfigMap so that application.properties will be added to the container at some location/directory. Point Spring Boot to that location using spring.config.location as env. var, system property, or program argument.
Spring Cloud Vault
Some other external vault-type of secure storage - an init container can fetch the db credentials and make them available to the Java application in a file on a shared volume in the same pod.
Spring Cloud Config...even though it is unlikely you'd want to put db credentials in its default implementation of the server storage backend - git.

Properties inheritance?

Is there anyway to do inheritance of properties?
This would be good in different regions specific property environment .
Like
Common.properties
Abc=myvalue
Developer specific environment
Local.properties
FileLocation=xyz1
Dev cloud environment
Dev.properties
FileLocation=xyz2
QA cloud environment
QA.properties
FileLocation=xyz2
Prod cloud environment
prod.properties
FileLocation=xyz3
Like above in common.properties will have all common properties others will region specific
I know in spring we can do using profile base but still..we have to include both properties in each region.
If there is such tool or configuration in spring, really it would be good know n use it.
A pattern I've used (and see other's use it too) for a long time while using spring is the following.
Have a PropertyPlaceholderConfigurer that loads files in the following order
common.properties
${env}.properties
The ${env}.properties will override any properties with the same key in common.properties.
when you launch your application, you'll need to tell it which on which environment you're running, and then spring will inject whatever properties you need in your beans.
I hope I'm answering your question, as this is a very common pattern (and has nothing to do with profiles).

Categories