spring.cloud.inetutils.ignoredInterfaces ignored in profiles - java

I'm running a Spring Boot application and using the Netflix OSS Spring Cloud framework. We are running a Eureka instance and have a service that is trying to register. When our service registers to Eureka it uses IP of the wrong port name. To fix this we have added:
spring.cloud.inetutils.ignoredInterfaces=eth0
This works great when we pass this from the command line, but when we move this into a profile configuration it doesn't work but all other configuration of the profile is picked up.
So for example this will work:
java -jar service.jar --spring.cloud.inetutils.ignoredInterfaces=eth0
and this will NOT work:
java -jar service.jar --spring.profiles.active=localvm
where application-localvm.properites contains:
spring.cloud.inetutils.ignoredInterfaces=eth0

Look you have to add -D argument before the main class or jar archive.
So try this:
java -jar -Dspring.profiles.active=localvm service.jar
For more details check this doc about how to set the active Spring profiles.

Related

Is there a way to change micronaut server port from CLI?

As the title suggested, i'm having trouble configuring the port for a micronaut server. I would like the port to be configured from the CLI or from a properties file, not from inside the java application (i don't want to rebuild the project each time i want to change the port). I have seen that using spring the following command can be used:
java -jar server.jar --server.port=8888
Is there any similar CLI command for micronaut? Or can someone help me out configure a properties file to change the port?
As mentioned in Run Micronaut service on port provided from commandline there are these two opions.
$ java -Dmicronaut.server.port=7070 -jar foo-0.1-all.jar
or
$ MICRONAUT_SERVER_PORT=7070 java -jar foo-0.1-all.jar
This is the solution i was searching for. However, only the first option worked for me. Thank you PeterMmm!

Override Spring boot properties in docker container

I have a spring boot application that is dockerized. By default the application has spring.cloud.config.enabled=false hence the application doesn't pick up the application.properties from configserver. However while deploying in a separate env we need to integrate the application with configserver so we need to override and set the spring.cloud.config.enabled property to true.
To achieve this I am running the docker image using the following docker-compose file :
version: '3'
services:
my-app-new:
container_name: my-app
image: my-app:1.0.0-SNAPSHOT
ports:
- "8070:8070"
environment:
- SPRING_CLOUD_CONFIG_ENABLED=true
- SPRING_CLOUD_CONFIG_URI=http://localhost:14180
However, it just doesn't work. If I hard code the values in the property file then it integrates fine.
I also tried the following command but it still didn't work :
docker run -p 8070:8070 -e SPRING_CLOUD_CONFIG_ENABLED=true -e SPRING_CLOUD_CONFIG_URI=http://localhost:14180 my-app:1.0.0-SNAPSHOT
The spring boot version is 2.2.6.
Let me know what the problem is.
Update :
I cannot use profiles as there too many env in our company and even the VMs keep getting changed so cannot have hardcoded profiles. We want a solution where we can just pass certain variables from the outside.
As someone pointed out in the comments the above compose yml is not working as the environment variables need to read by the spring boot application. So did some research on the internet and instead we are now passing the JAVA_OPTS tomcat variable while running the image. Like so :
docker run --env JAVA_OPTS="-Dspring.cloud.config.uri=http://localhost:14180 -Dspring.cloud.config.enabled=true" -p 8080:8080 my-app-image
And in the docker file we have used the JAVA_OPTS while starting the jar like so
ENTRYPOINT [ "sh", "-c", "java $JAVA_OPTS -Djava.security.egd=file:/dev/./urandom -jar app.jar" ]
And this still doesnt work. Not sure what is going wrong.
I found the problem with my setup. I made a silly error. The config server is not in my docker network and I used localhost to communicate with the config server. Localhost would of course mean that I am referring to the app containers IP which only has the app running. Instead when I used the ip address or the hostname of my machine my application container could connect to the config server successfully.
Why you not run container --> go inside --> change configuration and commit to new images.
After that deploy to new env.

Pass environment variable on AWS Elastic Beanstalk for Spring Boot App using Jasypt

I was deploying my Spring Boot Application on AWS Elastic Beanstalk. My web app is using Jasypt, so in the application.properties file, I wrote down jasypt.encryptor.password=, currently the password in this file is empty, I want to pass the password as a variable on AWS Beanstalk's configuration.
When testing locally, I used java -jar myapp.jar --jasypt.encryptor.password=1234 in command line, and it ran successfully. However, when deploying on AWS, I added jasypt.encryptor.password in environment properties and set its value to 1234, the app failed to run. The log said I cannot set the password as empty. So, at this point Beanstalk did not read the environment property I just set. But it can really read the properties later because I tested it after setting another property and used GET API to print it.
My question is: how to make Elastic Beanstalk run/read the environment properties at the beginning? In other words, how to make EB run java -jar myapp.jar with --jasypt.encryptor.password=1234attached?
Thank you so much in advance!
Procfile can't use environment variables. Use a shell script to start the application instead of invoking java -jar directly in Procfile.
Procfile:
web: /bin/sh start_server.sh
start_server.sh
#!/bin/bash
JAVA_OPTS='-Djasypt.encryptor.password=1234'
exec java $JAVA_OPTS -jar myapp.jar
references: https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/platforms-linux-extend.html

How to connect to online MongoDB database from Spring Boot?

I want to connect to an online MongoDB database hosted at Mlab, from inside a Spring Boot application.
I have configured the application.properties with the URI:
spring.data.mongodb.uri = mongodb://myuser:mypassword#ds129532.mlab.com:29532/consilium-dev
However, the application still connects to the local MongoDB database. How can I make it connect to the Mlab database?
SOLUTION:
The resources folder was not situated in the right folder. It should be in src/java/resources
In my case, spring boot was connecting to the uri specified in application.properties file while testing in my PC but once deployed to heroku, it always connected to the localhost irrespective of what was specified in application.properties. Solution was to pass the database uri as command line arguments while deploying the jar to the server because this will take precedence over the properties. To do so, create a Procfile like:
web: java -Dserver.port=$PORT -Dspring.data.mongodb.uri=mongodb://<user>:<pass>#<host>:<port>/<db> -jar my-app.jar
And using heroku toolkit, run following command.
heroku deploy:jar -j my-app.jar -i Procfile --app <host-name>
Using database values in the application.properties didn't work for me for online mongodb.
It works fine for local db. But Once I google and found an example online where they added it in this way below and it worked for me.
spring.data.mongodb.uri=mongodb://<USERNAME>:<PASSWORD>#ds261828.mlab.com:61828/springdb.

starting a Spring Boot application

I've generated a Spring Boot web application using Spring Initializr, using embedded Tomcat and package as an executable JAR file.
I started my application on Ubuntu 14.04 LTSwith
java -DAPPKEY=oracle -Dspring.profiles.active=oracle -jar licence-0.0.1-SNAPSHOT.jar
But after some hours the application was down, the process was not running anymore. Was this a proper way to start the application ?
I have no clue why the process shutdown
if you need to run your application as a service take a look at the installation guide in the spring boot doc:
https://docs.spring.io/spring-boot/docs/current/reference/html/deployment-install.html
As you can see if you are using init.d services you can link the jar inside the init.d directory.
The fat jar contains the init scripts:
sudo ln -s /var/myapp/myapp.jar /etc/init.d/myapp

Categories

Resources