how to use environment variables in tomcat without restarting Tomcat - spring-boot

I have a tomcat server with several applications. Then, I gonna deploy spring boot application on tomcat. But before deploy I set an environment variable in my server. Because of This application should use the environment variable. So I wish not to restart my tomcat after set new environment variable in my server.
Have you any solution? Help me, please?

You cannot change environment variables from Java without resorting to dirty tricks.
You can, however, change the values of system properties. Consider using system properties instead of environment variables to adjust the behavior of your application.
Better yet, don't use globally-visible/mutatable configuration and instead configure components individually through some other mechanism, such as a configuration file.

Related

Spring-boot Configuring properties with dash/hyphen using environment variables

One of my favorite spring-boot feature is the ability to configure most things using the unix environment variables.
Now I'm struggling with ActiveMQ broker config.
To set the broker url I have to specify spring.activemq.broker-url.
Rewritten in envrionment terms this would be SPRING_ACTIVEMQ_BROKER-URL which is working from eclipse, but not possible to be defined as environment variable in any unix shell.
using env is due to other restrictions not an option
Is there something I missed or do I have to remap those "dashed" properties in an own #PropertiesConfiguration
Any hints ?
Take a look at the documentation on relaxed binding.
The key in your application.properties or application.yml file will be spring.activemq.broker-url and your env var will be SPRING_ACTIVEMQ_BROKER_URL.

spring boot application properties based on spring profiles

Hi I want my spring boot web project to be deployed both on development and production environment and it should be run on specific profile based setting.
I googled on how to do that, and first of all that I have searched is defining application-{profile name}.properties properly in the src/main/resources classpath.
Now the problem is how to set profiles.
Since I am working on tomcat 8 in linux, there should be some configuration but I don't know how to do that.
and I am also curious that when my project is packaged as war file, java -jar {filename} -Dspring.active.profile=blahblah will not be work, but I think there is an alternative way.
plus, is there an way to set profile on tomcat 8 in Windows 10 ?
Thanks you
First:
I will recommend get rid of dedicated tomcat server and use embedded tomcat, jetty etc. Build your web apps as jar files and just run them. (of course if you don't have any limitations)
Second: You can do this either system property or env variable.
If you go with system property (order is important)
java -Dspring.profiles.active=blahblah -jar {filename}
If you go with env variable you need specify
SPRING_PROFILES_ACTIVE=blahblah

Switch between Prod and Dev environment

I've built a REST Service with Spring Boot. The setup in the development environment is different from the setup in the production environment. What is the best approach to switching setup between development environment and production environment? By setup I mean for instance the path to the database that is different in development vs. production. I can think of three approaches, use environment variables, use a properties file or use config file. Other suggestions are welcome and what I should think about when choosing.
You should have a look at Spring Profiles - see here. Using spring profile, u can easily switch configurations for the different environments.
Just name your configuration for "dev" as "application-dev.(properties|yaml) and provide -Dspring.profiles.active=dev when running the App from command line.

Environment specific properties from user home in springboot

I am working on a spring boot application in which i have to set Environment specific properties from user home folder.
i dig Google for the same & found we can put different properties file (dev, test, production) under resources and then we have to tell spring boot which environment we want to use using spring.profiles.active=dev OR prod.
however, my requirement is quite different. i will put a file in user home in my system & want to read properties form that file. how can i do that, need guidance.
Helping hands will be highly appreciated.
From the Spring Boot docs:
You can also refer to an explicit location using the spring.config.location environment property (comma-separated list of directory locations, or file paths).
As the docs go on to state, this must be specified on the command line or as an environment variable.
$ java -jar myproject.jar --spring.config.location=classpath:/default.properties,classpath:/override.properties
We explain that use case in a Devoxx presentation using EnvironmentPostProcessor, please refer to this section of the presentation for more details. You can also find the code sample online.
Well, it seems in your case you dont need environment variable. For production server your property file will be staying in and in staging machine it is also staying at same place. So where ever you deploy it will pick from . IMO you don't need to set environment, you just have to point property file to
Now to define this path you have 2 ways..
- You can put static path in your code
- You can set environment variable like Property_Path and read it in spring boot application..
However If you want to go one step ahead, you can use spring cloud configuration manager, by passing application+profile name to it, CM can fetch property file from directly from git or file system for you ...

WAS Liberty: DEV and UAT Configuration

I am wondering what is the best way to configure an WAS Liberty installation, allowing to to switch from a DEV environment configuration to an UAT(testing) environment configuration dynamically.
To elaborate, we have a similar setup with our glassfish servers, we simply configure system properties for both in the Glassfish console. For example
hostname.uat="some uat value"
hostname.dev="some dev value"
Dropping the ".uat" or ".dev" in the system property configuration in Glassfish makes that property active. In Glassfish, this can be done dynamically and while the application is running (no need to reboot).
Is there or can someone elaborate how I could achieve a similar setup in WAS Liberty?
Thank-you kindly
You can create a server.env file in two possible places:
${wlp.install.dir}/etc/server.env (properties are applied to all servers) or
${server.config.dir}/server.env (properties applied only to one server)
and specify any environment variables in that file.
For example:
# Specify properties and values
admin.email=dev.admin#domain.com
admin.email.uat=uat.admin#domain.com
To access these properties in an application environment (such as a Servlet) do the following:
System.getenv("admin.email"); // returns "dev.admin#domain.com"
Other useful properties can be specified in the server.env file as well such as JAVA_HOME, WLP_USER_DIR, WLP_OUTPUT_DIR, and WLP_DEBUG_ADDRESS.
For IBM's full doc on this, see: Customizing the Liberty Environment.
What we do is generate the Liberty server with Ansible, where the variables can be added to an ansible inventory based on environment.
So, our deployments essentially drop and recreate the Liberty server by using ansible templates and roles to stamp it out as needed.
Lastly, we make use of Hasicorp Vault (you can also use ansible-vault) for credentials or secrets at deploy time to fetch credentials. This is then injected into Ansible as JSON and used to stamp out the server.xml and other related configuration files.

Resources