Spring Boot does not resolve environment variable in external property file - java

I am starting spring boot project as follows
java -classpath "/home/madhur/github/fantasy_fury/target/fantasy_fury.jar:/home/madhur/github/fantasy_fury/target/dependency/*:/home/madhur/github/fantasy_fury/conf" com.games24x7.fantasy.fury.FantasyFuryInit
The conf folder has an application.properties with following contnents
zkHost=${ZK_CLUSTER_URLS}
Now, I am trying to read this variable as follows:
#Value("${zkHost:localhost}")
String zkHost;
However, it does not contain the actual value, but the default value localhost value every time.
Tried to set it as follows:
export ZK_CLUSTER_URLS=madhur1;java -classpath "/home/madhur/github/fantasy_fury/target/fantasy_fury.jar:/home/madhur/github/fantasy_fury/target/dependency/*:/home/madhur/github/fantasy_fury/conf" com.games24x7.fantasy.fury.FantasyFuryInit
Then also, the output is localhost. What I am missing?
Spring boot version is 1.4

Related

How to check what values are consumed by a spring boot java application from the Linux command line or in the docker container

How to check what values are consumed by a spring boot java application from the Linux command line or in the docker container?
I have a spring boot java application that has the below parameters for email
spring.mail.host=smtp.gmail.com
spring.mail.port=587
spring.mail.username=admin#qtech.com
spring.mail.password=${GPASSWORD}
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.ssl.trust=smtp.gmail.com
I'm passing GPASSWORD as an environment variable to the docker/k8s container.
How can I check what value/values are being consumed by the java application, especially spring.mail.password field.
I tried the below commands
printenv
env
But they list all environment variables being passed to all containers like the below environment variable.
GPASSWORD=bond007
So Is there a way to check what values are being consumed by the spring boot java application inside a pod or container?
You can use Slf4j annotation and log like below:
#Value("${spring.mail.host}")
private String mailHost;
log.info("spring.mail.host: "+mailHost);

How can I pass json file or object to start the application in cloud foundry?

I have a java application. Which needs json object to start the app. In my local environment I pass this through VM options but not able to figure out this in cloud foundry.
I tried passing in .profile file but it did not work. In this file I provide export variablename="{jsonObject}"
Any help will be appreciated.
Thank you
There are a few ways in which you can pass Environment Variables in a cloud foundry
Using the set-env variable command using cf commandline: The general syntax is cf set-env APP_NAME ENV_VAR_NAME ENV_VAR_VALUE
Using Manifest file: If you are using manifest file with cf push you can set the environamet valriables in it like so. Official documenttaion can be found here
env:
ENV_VARIABLE1: value1
ENV_VARIABLE1: value2
Using mtar descriptor: This might be specific to SAP Cloud Foundry, if you are using SAP Cloud platrom and are deploying applications as mtar, in the mtar deployment descriptor, you can set the PROPERTIES field in under the deployment section to add additional env varables. Additional documentation can be found here. e.g.
properties:
POPULATE_ALBUM_REPOSITORY: true

Wrapping Spring Boot application with Tanuki Service Wrapper

How to wrap Spring Boot application as a linux daemon and to set it to read from application.properties.
To start the jar with the parameters from the application.properties I am using this command:
java -Dspring.config.location=/application.properties -jar MyJar.jar
Where to set this in wrapper.conf?
I have tried like this but the jar is not starting with the parameters from the application.properties.
wrapper.java.command=java
wrapper.java.command.loglevel=INFO
wrapper.java.mainclass=org.tanukisoftware.wrapper.WrapperJarApp
wrapper.java.classpath.1=../lib/wrapper.jar
wrapper.java.classpath.2=/opt/MyService/lib/MyApp.jar
wrapper.java.library.path.1=../lib
wrapper.logfile=../logs/wrapper.log
wrapper.app.parameter.1=/opt/MyService/lib/MyApp.jar
wrapper.app.parameter.2=-c
wrapper.app.parameter.3=/opt/MyService/lib/conf
Try with this:
wrapper.java.additional.1=-Dspring.config.location=/opt/MyService/lib/conf/application.properties

server.port properties not working after buildin spring boot project

i'm working on spring boot project and all works fine , now i want to build and run the app.
in the application.properties file i set the property server.port = 8090
after building the project using maven i run the following command
java -jar jarfilename.jar but it says the port 8080 is already in use.
i try these commands:
java -jar -Dport=8090 jarfilename.jar
and
java -jar jarfilename.jar --port=8090
but also i got the same message the port 8080 is already in use.
I'm wondering why it takes the port number 8080 and ignore the port number 8090 that i set in the application.properties file.
Note : (I'm using tomcat embedded server) and when i check the folder target/classes.. application.properties i didn't find the property server.port=8090.
can anyone explain to me what' happen exacly?
thanks in advance.
Is the application.properties located at the right location?
Description from Spring.io:
SpringApplication will load properties from application.properties files in the following locations and add them to the Spring Environment:
A /config subdirectory of the current directory.
The current directory
A classpath /config package
The classpath root
The list is ordered by precedence (properties defined in locations higher in the list override those defined in lower locations).
Use java -jar -Dserver.port=8090 jarfilename.jar to set the port from command line.
Hint, from Spring.io: If you want to use the short term -Dport=8090, you can use server.port=${port:8080} in your application property file.
I encountered the same problem, in my case, I didn't pass the args to SpringApplication.
public static void main(String[] args) {
SpringApplication.run(MySpringConfiguration.class);
}
should be
public static void main(String[] args) {
SpringApplication.run(MySpringConfiguration.class, args);
}
when you create spring boot application using 'spring initializer' select jar file
I know its an old post, but I might know the answer(maybe it will help others): There is a hierarchy with the configuration settings.
The applicaton.properties file is at the bottom(OS env. variables, Java System properties etc. are all above), on the other hand, terminal parameters are at the top.
So when the server initialized, it used the port from a property setting, that is higher in the hierarchy ladder.

Spring Boot not obeying command line arguments

Put together a very basic Spring Boot RESTful backend with some CRUD endpoints. Trying to start it up on my Centos7 server. However I already have an application listening on its default port, 8080, so I have to configure that.
I have tried:
java -jar target/rest-1.0-SNAPSHOT.jar --server.port=8090
java -jar target/rest-1.0-SNAPSHOT.jar --Dserver.port=8090
java -jar target/rest-1.0-SNAPSHOT.jar --port=8090
java -jar target/rest-1.0-SNAPSHOT.jar --Dport=8090
My application.properties contains the line:
server.port=${port:8090}
Spring Boot still starts up the embedded Tomcat container on port 8080. It's not getting the message. What am I missing?
Edit: setting SERVER_PORT=8090 was effective - see below
Try with -Dserver.port=8090 note single -
or
server.port=xxxx in application.properties file
EDIT:-
Check your log, below line will echo in console 2015-10-16 23:13:23.082 INFO 4228 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8090 (http)
VM arg overrides application.properties
If you have a "application.yml" or "application.properties" in the same folder of jar, the jar will take preference of the external configuration file.
Doesnt make sense for me, but its true. The springboot has higher priority to external configuration file instead the internal one.
We have four different ways to change default port.
Using application.properties or application.yml
Implementing EmbeddedServletContainerCustomizer interface
Using SpringApplication class
Change port directly through command line
For more details, visit below
http://www.javainterviewpoint.com/spring-boot-change-embedded-tomcat-default-port/

Categories

Resources