Why Spring Boot app doesn't consume application.yml file? - spring-boot

I have Intellij IDEA 2017.2.5 and few spring boot applications with gradle as a build tool.
For example in one them I have application.yml file with next content:
spring:
profiles:
active: native
application:
name: config-service
management.security.enabled: false
server:
port: 8888
But when I start it from the spring dashboard it doesn't consume the properties file and just uses default values:
Sometimes after rebuild it starts working, but it frustrates me.
Also when I start it from the terminal it works, so it's definitely a problem with Intellij IDEA or/and Gradle.
Is there any solution for this?

Related

Disable Spring cloud server in spring boot 2.0.0

For one of our customer, who is using Spring Boot version 2.0.0 Release, we have Spring cloud config server with native settings. For local development, we want to disable spring cloud config server so that other spring boot micro-services can use application-local.yml settings.
I tried below options but its not working
Setting spring.cloud.config.enabled=false in bootstrap.yml file
Setting -Dspring.profiles.active="local"
When I run the micro-services, it is still looking for config server. Any inputs.
Can not remove the dependency of config-starter reference in gradle file as a workaround
These are the configurations that worked for me. I'm using Eureka service to find where the config server is.
spring:
cloud:
config:
enabled: false
discovery:
enabled: false
eureka:
client:
enabled: false
to run the local service without loading props from any remote config server you need to disable the bootstrap file and config.
resources/bootstrap.yml --> resources/application.yml
with this springboot will load your application.yml by default.

Read values from consul while bootstrap spring boot

I have question is there any way to retrieve certain values and inject them to bootstrap.yml while application is coming up.
I have configuration file like this:
spring:
application:
name: myApp
cloud:
consul:
enabled: true
host: localhost
port: 8500
config:
enabled: true
datasource:
url: jdbc:oracle:thin:#localhost:1111:XXXX
username: ${nameOfVariable1}
password: ${nameOfVariable1}
driver-class-name: oracle.jdbc.OracleDriver
For example, I need to configure embedded tomcat port, or DB credentials, I don't want to put it hardcoded in .yml properties file, instead I want to put some variable name in .yml so Spring will go and bring value from Consul. Is it possible?
You can use Spring Cloud Consul Config project that helps to load configuration into the Spring Environment during the special "bootstrap" phase.
3 steps:
add pom dependency: spring-cloud-starter-consul-config
enable consul config: spring.cloud.consul.config.enabled=true
add some config in consul kv in specific folder, such as key: config/testConsulApp/server.port, value:8081
and then start the sample web app, it will listen 8081.
more detail at spring cloud consul doc.
and demo code here

Disabling config server for tests

I am using microservice architecture where the services are done with spring boot, to control them, I am using zuul as a proxy, eureka as register and config server as the configuration provider.
In the microservices I configure my bootstrap.yml file as follows:
spring:
application:
name: portal-gca-server-${MYENV:local}
cloud:
config:
uri: http://localhost:9090
As seen above, the config file name that I look for in the config server is according to my environment variable, otherwise I get the local profile.
This works fine, but when I run the tests on the development machine it will never work, because instead of searching for the test profile it searches for the location, because the dev machine has no environment variable.
I know I could register the environment variable to run the tests but that's not the intention or else I would have to do this all the time to run tests and to run the local application.
Is there any way to solve this? I already tried to use the annotations:
webEnvironment = SpringBootTest.WebEnvironment.MOCK
#ActiveProfiles("test")
#TestPropertySource(locations="classpath:application-test.properties")
None of them any good, because the first thing the application does when executed is to fetch the information in git according to bootstrap.yml.
Has anyone ever experienced this?
Go to your bootstrap.yml and then add :
spring:
cloud:
config:
enabled: false

Refreshing Spring Cloud Config from filesystem not working once my app is deployed on docker

I have a Spring Boot application for which I externalized the configuration with Spring Cloud Config so I can modify and refresh its properties on the fly.
Here is my bootstrap.yml :
spring:
application:
name: ${appName:tasky}
profiles:
include:
- native
cloud:
config:
failFast: true
server:
bootstrap: true
prefix: /config
native:
search-locations: classpath:config/{profile}
Updating then refreshing a property (for eg. app.api-key) on my local project works perfectly :
curl -X POST http://localhost:9190/management/refresh
["app.api-key"]
I dockerized my app and tried to achieve the same result from my container, so I tried the following :
sed -i -e 's/oldvalue/newvalue/g' tasky-prod.properties
This correctly changes my file, but when I try to refresh the values in my app (from within the container), my spring boot context is restarted but nothing is picked up from the actuator and the new value is also not used by my app :
curl -X POST http://localhost:9999/management/refresh
[]
What did I do wrong ?

Spring Boot + Spring Cloud Config - How to add more profiles from the Git external configuration

I have this configuration in a spring boot application:
spring:
application:
name: my-app
profiles:
active: ${ENVIRONMENT}
cloud:
config:
uri: http://localhost:8888
My config server reads the following files:
my-app-dev.yaml
prop: dev property
my-app-pro.yaml
prop: pro property.
I launch the spring boot app setting -DENVIRONMENT=dev, loading correctly the dev external Git properties.
When I inject the Environment in let's say a controller and do env.getActiveProfiles() I get "dev" as expected.
I would like to add more profiles from the git configuration. For instance:
my-app-dev.yaml
prop: dev property
spring:
active:
profiles: dev,business1
my-app-pro.yaml
prop: dev property
spring:
active:
profiles: pro,business2
So that env.getActiveProfiles() returns ["dev","business1"]. However what it returns is the initial "dev".
How could this be done?
UPDATE:
As suggested by Dave Syer I tried using spring.profiles.include in the Git files but the new profiles aren't added to the Environment:
my-app-dev.yaml
prop: dev property
spring:
profiles:
include: business1
my-app-pro.yaml
prop: dev property
spring:
profiles:
include: business2
environment.getActiveProfiles() ---> "dev"
Update your spring-boot to 1.5.4.
I tested the same case on my Mac, I found that spring with different version behaves different.
When I'm using Spring Boot 1.3.8.RELEASE with Spring Cloud Brixton.SR7, I got the [dev] profile as active profile(also with Spring Boot 1.4.5.RELEASE)
When I'm using Spring Boot 1.5.4.RELEASE with Spring Cloud Dalston.SR1, I got the
[business1, dev] profile as active profile
So I believe it is a bug in Spring Boot 1.3.x and 1.4.x

Resources