not able to set spring active profile in tomcat - spring-boot

Tomcat8 is set to run as a service on ubuntu
I have deployed an application in tomcat8, I want the app to load(dev,local,prod)environment specific propety file
Which config file should I set -Dspring.profiles.active=\"prod\" value so the correct property file is read.
I tried in catalina.sh as JAVA_OPTS="$JAVA_OPTS -Dspring.profiles.active=\"prod\""
and also in /etc/default/tomcat8

In setenv.sh
CATALINA_OPTS="-Dspring.profiles.active=prod ...a whole bunch of stuff might already be here"

Related

Spring Boot Log initialization with Spring Cloud Kubernetes

Spring Boot 2.2.6-RELEASE
In my application.yml, I have this line :
logging.config: classpath:my-logback-config.xml
This works well, spring get its config nicely
In my my-logback-config.xml I have this line :
<springProperty name="LOG_HOST" source="config.logHost" />
<springProperty name="LOG_PORT" source="config.logPort" />
Again, this works well, it get its value from an external config file, which is defined in a ENV variable
SPRING_CONFIG_ADDITIONAL_LOCATION=file:/my-env.properties
But when I enable Spring Cloud Kubernetes, this fails, with an UnknowHostException : "LOG_HOST_IS_UNDEFINED" cannot be resolved. (the value of LOG_HOST is used to setup a network appender)
Why does enabling Spring Cloud Kubernetes change the behavior of logging initialization?
It seem that values from SPRING_CONFIG_ADDITIONAL_LOCATION are not loaded yet.
I can't load this value from a configMap, since Spring Cloud Kubernetes has not been initialized at the moment logging is being initialized.
Finally, the problem was that the ENV var SPRING_CONFIG_ADDITIONAL_LOCATION was set to a folder, and not to a file on container.
Locally, on windows, setting 'SPRING_CONFIG_ADDITIONAL_LOCATION' to either a file or a folder seems to work, but in the Docker image (RH linux), it has an impact.
When SPRING_CONFIG_ADDITIONAL_LOCATION is set to a folder, values of the enclosed ".properties" files are ignored.
I can reproduce (fix) the issue by changing my docker file :
ENV SPRING_CONFIG_ADDITIONAL_LOCATION="/mnt/properties/application.properties"
to
ENV SPRING_CONFIG_ADDITIONAL_LOCATION="/mnt/properties/"

Spring Cloud Config Server Not picking changes

I am trying to setup spring config cloud using local file system.
Below is my config on cloud server.
server.port=8888
spring.profiles.active=native
spring.cloud.config.server.native.searchLocations=classpath:/
Bootstrap.properties on client app is as shown below
spring.application.name=hello-world
spring.cloud.config.uri=http://localhost:8888
management.endpoints.web.exposure.include=*
I have also created hello-world.yml on class path for the hello-world spring boot application with property, test: Hello World
Followed below steps to make use of config server.
Step 1: Update the config file and start cloud config server. Able to
see config http://localhost:8888/hello-world/default
Step 2: Start client app hello-world, client app able to read the
test property file from cloud config server.
Step 3: Make changes to config by updating test: Good Bye on
hello-world.yaml.
At this moment, if I check
http://localhost:8888/hello-world/default ,it still shows old value.
Step 4: Run /actuator/refresh on client app. But it won't detect
any change on config server.
The new changes are reflected only if I restart the cloud config server.
Is there any configuration issue causing the cloud config server to unable to listen to changes ?
I could see o.s.cloud.commons.util.InetUtils : Cannot determine local hostname INFO log on cloud config app.
First of all I followed the same exact steps you followed and got the same issue, after almost day of search and study on the matter found out the followings,
we shouldn't use classpath:/<whatever> for
spring.cloud.config.server.native.searchLocations
because when we use so and build the project and run the location refers to the directory inside the generated .jar file, so we will not be able to update it in runtime.
To confirm this you can stop config server, open you .jar archive and delete hello-world.yml file then try http://localhost:8888/hello-world/default you will get default null responses
So we have to use some other locations for spring.cloud.config.server.native.searchLocations either with full directory path or just directory from app running location
Examples
For full path in windows use file:///full-path
spring.cloud.config.server.native.searchLocations: file:///E:\configs
Just for a directory (which will search project root directory if you running from IDE, if running jar then target directory or jar location spring.cloud.config.server.native.searchLocations: configs
spring.cloud.config.server.native.searchLocations: configs\whatever
we can configure multiple locations too as follows, spring.cloud.config.server.native.searchLocations: file:///E:\configs, configs

multiple parameters not getting passed when I start my spring boot application in command line

I want to override certain properties during deployment of my spring boot application.
when I try the following it works
sudo /etc/init.d/myapp start --app.env=prod
I see the app.env is correctly set to prod (my /health just echoes this values)
however when I set more than one property it did not work,
sudo /etc/init.d/myapp start --app.env=prod --version=2.3.4
I see only app.env is correctly set. the version value is not overridden.
why is it so? what is the right way to pass multiple parameters.
NOTE: I want to pass username and password for datasources. but for testing purposes, I kept it simple to override these properties.
You would want to read the section around Customizing the startup script. Specifically that you can include a myapp.conf file beside the jar file. In that .conf file is a JAVA_OPTS variable. You would then use -Dapp.env=prod -Dversion=2.3.4

spring boot log file being overwritten on restart

I'm using https://docs.spring.io/spring-boot/docs/current/reference/html/deployment-install.html to start my spring-boot application on ubuntu 14.04 and it's working great except my log file gets overwritten whenever I restart the app. Probably it's something simple... but how can I configure that behaviour? I'm just using the default console logging to /var/log/app_name.log. If I pass in the logback "--logging.file=..." config to the init script, it logs both to the default as well as this file and this file doesn't get overwritten. Thanks!

Spring 3.1 Profile: how to set in Stackato Tomcat container

I'm using Spring 3.1.3 and the new profile feature. When I set the environment in my IDE for spring_profiles_active=NONPROD, it works fine. However, when I deploy to our aPaaS environment which is also using Tomcat, it isn't getting picked up.
Shouldn't I just be able to do the following:
env:
CATALINA_OPTS: -Dspring_profiles_active=NONPROD
If I ssh to the machine, I see this is getting set. Any ideas why Spring isn't picking this up?
Put the following lines to your manifest.yml file to get the Spring profile activated in Stackato:
env:
spring_profiles_active:
default: NONPROD
This will put spring_profiles_active into environment variable and Spring happily reads it from there. Note that you have to use underscores in the variable name, because Stackato doesn't like dots in those. The reason is that Linux environment variable names shouldn't contain dots for shell programs to work correctly with them.

Resources