Disable Spring cloud server in spring boot 2.0.0 - spring

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.

Related

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

How to create Spring Cloud Config Client with env specific configuration?

I have facing an issue with Spring Cloud Config Server and Eureka Server Profiling.
Let's say I have 3 services with their name ("spring.application.name") as :
myapp-svc
myapp-spring-cloud-config-svc
myapp-spring-eureka-svc
I want to deploy each service in 2 regions ( dev and prod ). In Dev region, each service will run on localhost and in prod it will have some different url. 'myapp-spring-cloud-config-svc' in dev region will point to local git repo, while in prod region it will point to remote git repo.I can have 2 configurations:
1) When I start 'myapp-svc' service in local, it should connect to 'myapp-spring-cloud-config-svc' in dev.
I can do this by setting spring.cloud.config.uri = .
But the issue with this set up is that the property needs to be defined in bootstrap.properties.
So, If deploy 'myapp-svc' to prod, I will have to change config uri there to point it to prod config service which in turn would need another build creation.
This doesn't seem like a good solution, what if I have 50 app related services, I can't change this property in each one of them before prod deployment.
I tried setting spring.cloud.config.uri in application-dev.properties of 'myapp-svc' but it doesn't work. As per docs, it must be changed in bootstrap.
So, how do I implement this without having to create new build for prod ?
2) I can first call eureka and then using eureka I can call config service here.
The problem here is also same.
If I use eureka to look up config then "eureka.client.serviceUrl.defaultZone" must be defined in "bootstrap.yml".
See this:https://cloud.spring.io/spring-cloud-config/multi/multi__spring_cloud_config_client.html
So, in this case too, I need to change eureka url before deploying this service to prod.
Please help me on this...!!
Here is how, the properties, yml looks like for each of the above mentioned services:
1) myapp-svc:
1.1)bootstrap.yml
spring:
application:
name: myapp-svc
cloud:
config:
discovery:
enabled: true
serviceId: myapp-spring-cloud-config-svc
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8762/eureka/
server:
port: 8082
2) myapp-spring-cloud-config-svc:
2.1)application-dev.properties:
spring.cloud.config.server.git.uri=file:///C:/config-repo
eureka.client.serviceUrl.defaultZone=http://localhost:8762/eureka
2.2)application-prod.properties:
spring.cloud.config.server.git.uri=https://github.com/<mygit Repo>
2.3)bootstrap.proerties:
spring.application.name=myapp-spring-cloud-config-svc
server.port=8888
3) myapp-spring-eureka-svc
3.1)bootstrap.proerties
spring.application.name=myapp-spring-eureka-svc
server.port=8762
1) You can have profile specific bootstrap-<profile>.properties (like for application-<profile>.properties) for each supported profile to avoid rebuilding your application for each env. Then just pass application profile using to your application during start-up. Spring will load correct bootstrap-<profile>.properties and will connect to proper configuration server (or eureka, etc). Example:
java -jar your-app.jar --spring.profiles.active=dev
2) You can pass your URLs externally as custom properties (same as with profile above) and have smth like this in bootstrap.properties. Example:
spring.cloud.config.uri=${config.server.url}
then pass --config.server.url= ... during start-up.
3) You can pass Spring properties in the same way during start-up. Example:
java -jar your-app.jar --spring.cloud.config.uri= ...
4) You can use system env variables. Example:
spring.cloud.config.uri=${SYSTEM_ENV_CLOUD_CONFIG_URI}

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

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?

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