spring boot can't load environment variables - spring

I use spring boot 1.3.3.RELEASE at my ubuntu 15.10 and try to configure my app via env properties like this (guided by https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html):
> export SPRING_REDIS_HOST=somehost
> echo $SPRING_REDIS_HOST
somehost
My application.properties contains:
spring.redis.host=localhost
The problem is that I never get somehost in my app.
Any ideas? Did I miss something?
Thanks, George

The reason is that I set env in another terminal window so java process doesn't see it. Everything works fine now.

You must set the correct systemPropertiesMode
http://docs.spring.io/spring/docs/4.2.5.RELEASE/javadoc-api/org/springframework/beans/factory/config/PropertyPlaceholderConfigurer.html#setSystemPropertiesMode-int-
the default value is fallback.
"If not being able to resolve a placeholder with the specified properties, a system property will be tried."

Related

Spring Boot project not resolving placeholder variable when restarting with #reboot

I have this placeholder variable in the application-DEV.properties file:
vm.hostname=${hostname}
Apparently this is not being resolved when the app was auto-restarted after VM reboot via #reboot command. I got this well known error:
Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder "${hostname}"
But when I ran the app as a user on the VM, everything was OK. So I'm guessing at #reboot time, value of ${hostname} was not defined at the VM-level for the Spring Boot app to pick it up?
This jvm arg is required: -Dhostname=$HOSTNAME
Everything is working ok after adding this. Arg is required for #reboot as well as any crontab as well.

Where to specify a specific port number in spring boot service file in systemd?

Need to know where can I specify the spring boot port aa when I check my service status I get this:
"Serverkt.log started at server-name with process id"
But I don't get the port number.
Also wanted to know where I can find the spring logs in the server
spring boot has its configuration file. I guess that you have it included in your jar/war file. So basically unzip that file and look inside it (try to search for application.properties or application.yaml|yml).
property server.port defines the port on which application is running. It defaults to 8080.
If you are using spring-boot 2 then with property logging.path you can change the path where output file will be placed. However I don't know if this works when you have logback/log4j/... configuration.
if you run your application you can override those properties specified in appplication.properties|yaml by providing command line properties. For example you can change the port with command java -jar your-boot.jar --server.port=9090
Since you're starting spring boot from a service file, you can set the port using command line arguments e.g.port 8083 would look like this:
ExecStart=/usr/bin/java -jar /opt/corda/spring.jar --server.port=8083

How to pass environment variable to Tomcat running as Windows service

I have an application installed in Tomcat that is invoked by the wrapper which sets an environment variable
set FOO=c:\foo
and invokes the startup.bat, and I have a requirement to run Tomcat as windows service.
So far, I tried to set the system environment variable, which did not help.
I tried following the windows service HOWTO appending ++Environment in the registry, so that
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tomcat7\Parameters\ImagePath
looks like this
C:\Apache\Tomcat-7.0.64\bin\Tomcat7.exe //RS//Tomcat7 ++Environment=FOO=c:\foo
That did not help either.
Any suggestions, please
You were almost there. Replace the equal sign with a space in your tomcat7 command, and that should work.
I've just written this up on another question: Setting User Environment Variables for tomcat on Windows
I had to change --JvmOptions of the service.bat file then remove the service and re-install again.
--JvmOptions "-Dspring.profiles.active=pp;-Dcatalina.home=%CATALINA_HOME%;...."
I have a InstallService.bat file. In the file I am first creating the service without specifying the Environment variable and then I am updating the service with the Environment variable specified.This is how the bat file looks like.
tomcat8 //IS//CompassTomcat8_8080 --DisplayName="Compass Apache Tomcat 8:8080" --Install=%CATALINA_HOME%\bin\tomcat8.exe --Startup=auto
tomcat8 //US//CompassTomcat8_8080 --DisplayName="Compass Apache Tomcat 8:8080" --Install=%CATALINA_HOME%\bin\tomcat8.exe --Startup=auto --Environment=Key1=Value1;Key2=Value2
It has started working now.

Making spring boot boostrap behind proxy

I tried to use Spring Boot using proxy.
I get the following message
$ spring init -l
Failed to retrieve help from service at 'https://start.spring.io'
(start.spring.io: unknown error)
I tried following
export http_proxy
export https_proxy
JAVA_OPTS settings
We are using commons HttpClient and we build a default instance via HttpClientBuilder
There are a bunch of system properties that are taken into account but I assume the following is the ones you're looking for:
http.proxyHost
http.proxyPort
Can you try that? If that does not work, please raise an issue on the boot tracker
Behind corporate proxy using locally installed cntlm proxy:
JAVA_OPTS
-Dhttp.proxyHost=localhost -Dhttp.proxyPort=3128 -Dhttps.proxyHost=localhost -Dhttps.proxyPort=3128
Output:
$ spring init
Using service at https://start.spring.io
Content saved to 'demo.zip'
Use
-Dhttp.proxyHost=your.proxy.net -Dhttp.proxyPort=8080

Spring boot CLI settings proxy

I installed Spring boot CLI on Windows 7 but, I use a proxy. I would like to know how to configure proxy with Spring boot. I set the variable JAVA_OPTS with -Dhttp.proxyHost=proxyhostURL and
-Dhttp.proxyPort=proxyPortNumber but I have received the message :
"startup failed : General error during connection ..." after the command "spring run myscript.groovy".
I use jdk 1.7.0_51 and spring boot 1.0.0RC3.
Thanks in advance !
It looks like a known bug and there's a fix on github
Not sure if it's released yet tho...

Resources