Should I use only native profile if I want to use local config files in spring boot? - spring-boot

I'm setting local development environment to test using spring boot ver 2.7.1, and I want to use local file hierarchy as config server.
So, I use spring.cloud.config.server.native.searchLocations: ~~~.
I want to boot this project using profile named something other except 'native' (application-native.yml). But it is failed to load the server.
My Question:
When I want to use local files as config files, is it only possible to use 'native' profile?
I tried :
spring:
profiles:
active: local
cloud:
config:
server:
native:
searchLocations:
~~~~
still failed.

Related

Spring cloud config server git-uri local file resolves to master

In my app's bootstrap.yml file i have placed this configurations
spring:
application:
name: arun-test
cloud:
config:
server:
bootstrap: ${LOCAL_CLOUD_CONFIG_BOOTSTRAP:false}
git:
uri: file:${LOCAL_CLOUD_CONFIG_DIR}
I clone my properties folder from Github and then before starting the app, i turn my spring-boot app in to config server with below commands
export LOCAL_CLOUD_CONFIG_BOOTSTRAP=true
export LOCAL_CLOUD_CONFIG_DIR=/Users/arun/arunLabs/app-properties
./gradlew bootrun // to start
This works exactly fine.. But when i create a new branch and change the properties there to test something, the app always resolves to master-branch only and then it is still using my old configurations. Not going in to my branch
Not only that, it also replaces my file in the new branch with the one that is in master.
How to test it against my branch ?
Actually what you're looking for is a client side request. Cloud config will serve all branches in your repository.
You can read in the documentation.
You can either set the label value on your spring cloud Config Client
spring.cloud.config.label=newBranch
Or you can request directly to cloud config service
localhost:8080/{application}/{profile}/newBranch

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

Why Spring Boot Cloud Config Server properties not picked from GIT

I have the following issue. Just with the basic Spring Boot Config Server, I would like to read property file from my Github (i tried also Gitlab). Everything works localy, the property file is nicely loaded from Github, but after deploying application on any other environment, for example on Heroku or on dedicated Linux server, the property file is not loaded. I am checking http://localhost:8101/employee/service1
Loading the property file from local files (classpath:config/local) works very well both locally and on other environments. What I am doing wrong? I have created simple project on Github (https://github.com/troger19/config-server), if anybody can check. Also app is deployed on Heroku: https://protected-savannah-48323.herokuapp.com/employee/service1
The application.yml looks like this
server:
port: 8101
spring:
application:
name: config-server
cloud:
config:
server:
git:
uri: https://github.com/troger19/config-server
search-paths: src\main\resources\config\dev
And the employee-service1.yml is in resources/config/dev
I havent find anything in the logs so far. Thank you for any suggestion.
Please change use "/" for linux environment as "\" will only work on window environment.
Please change your search-paths: src\main\resources\config\dev to /src/main/resources/configdev
Good idea to use different-2 profile in yml file for different environment so such can be avoided.

Spring Config Server properties ordering when directories

We are running Spring Cloud Config Server v1.3.1.
We have Bitbucket hosting Git. The Git environment has a configuration folder where we hold our files. There are a number of subdirectories under the configuration folder. eg
environment/configuration
application-dev.yml
my-service.yml
my-service-ci.yml
my-service.dev.yml
...
environment/configuration/datasources
application-ci.yml
application-dev.yml
...
In the Spring Config Server the application and config server are configured as below:
spring:
application:
name: "#project.name#"
cloud:
config:
server:
git:
uri: https://xxxxx/scm/dep/environment
basedir: ${baseDirectory}/work
searchPaths: configuration, configuration/*
When loading the my-service Spring Boot app with dev profile I would expect the app specific config files (my-service) to load first. ie
environment/configuration/my-service-dev.yml
environment/configuration/my-service.yml
environment/configuration/configuration/application-dev.yml
environment/configuration/application-dev.yml
In reality it loads the folders in reverse order so configuration/* comes first with its subdirectories loaded in alphabetical order. Next comes the files
sitting directly under configuration. This gives below order
environment/configuration/application-dev.yml
environment/configuration/configuration/application-dev.yml
environment/configuration/my-service-dev.yml
environment/configuration/my-service.yml
Spring Boot loads these as a Map containing a list of PropertySources. It iterates from the start and returns the first match. In this case a property in application-dev.yml
would trump the same property in my-service-dev.yml.
Is this intended behaviour or is there a bug when directories are involved?

How to specify an external application.yml in spring with profiles

From the spring documentation http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html#boot-features-external-config-yaml i see that an external YAML file is possible.
I was able to use a PropertyPlaceholderConfig FileSystem resourse to load in yaml, but it did not honor the active profile.
I saw for application.properties you can use #PropertySource, but that does not work for YAML according to the docs.
So bottom line question: How do a specify an application.yml in a profile aware fashion in Spring4/spring boot.
Note: It works in src/main/resources/application.yml
In order to specify an external profile aware .yml file the SPRING_CONFIG_LOCATION and SPRING_PROFILES_ACTIVE system variables can be used.
JAVA_OPTS example
-Dspring.profiles.active=dev -Dspring.config.location=file:C:/application.yml
This will allow you to have provide multiple profiles inside of a YML file and let spring do the heavy lifting of evaluating the correct properties:
spring:
profiles: dev
someprop: devprop
---
spring:
profiles: test
someprop: testprop

Resources