Spring Config uri - spring

I have configured bootstrap.yml for spring config
spring:
application:
name: cce-auth
cloud:
config:
uri: http://temp.com:8888
It works fine but I need URI value Dynamically, for example if I will publish this .war in test environment this URL must be http://test-temp.com:8888.
So for this I have solution create config.txt file in server and using I/O Stream reed/write this string to bootstrap.yml.
But problem is loading, spring loads http://localhost:8888 before I'm writing in bootstrap.yml.
So my reason is to create dynamically URI for config server.
DO you have any idea?

Define active_profile in bootstrap.yml file
spring:
profiles:
active: ${activatedProperties}
Then create bootstrap-${activatedProperties}.yml for each environment, etc..bootstrap-dev.yml, bootstrap-pre.yml, bootstrap-prod.yml
For example:
spring:
application:
name: servicename_prod
cloud:
config:
uri: https://admin:123456#test.com:8888
server:
port: 8443
add plugin to pom.xml file :
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
When run java, define environment on it: for example, run with prod environment.
java -Dserver.port=8443 -Dspring.profiles.active=prod -jar ....

Related

spring boot 3 profile property values getting overridden in boostrap.yaml

Recently trying to upgrade to Spring Boot 3, but facing property issue.
My bootstrap yml looks like below,
When I run with profile dev, the configserver url is always getting connected to qa, basically whichever is declared last.
This is working fine in 2.7.X version of spring, but not in spring boot 3
spring:
application:
name: appname
profiles:
active: ${spring.profiles.active}
include:
- global-defaults
group:
dev:
- config-server-dev
- cloud-config-dev
qa:
- config-server-qa
- cloud-config-qa
---
spring:
config:
activate:
on-profile: config-server-dev
cloud:
config:
enabled: true
uri: http://urltoconfigserverdev
label: master
---
spring:
config:
activate:
on-profile: config-server-qa
cloud:
config:
enabled: true
uri: http://urltoconfigserverqa
label: master
---
But the same is working fine if add the property migration pom dependency. which is not suggested for higher environments.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-properties-migrator</artifactId>
<scope>runtime</scope>
</dependency>
If adding the properties migrator fixes the problem, it's likely that your configuration file is using a configuration property name that's been removed after deprecation. I'm not seeing anything specific to Spring Boot in the changelog, maybe this is due to a change in spring cloud config? The properties migrator should print WARN logs during startup, looking at those should tell you what needs fixing.

Spring cloud not able to resolve vault secret into .yml

I'm working with microservice architecture and have spring cloud config service and another microservice.
profiles:
active: vault
cloud:
# Configuration for a vault server running in dev mode
vault:
scheme: http
host: 127.0.0.1
port: 8200
connection-timeout: 5000
read-timeout: 15000
authentication: TOKEN
token: s.E4gdoIYAKxMvCE56MP5Etmvy
kv:
enabled: true
backend: secret
backend-version: 2
profile-separator: /
generic:
enabled: false
application-name: myapp
Config server dependency
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-vault-config</artifactId>
<version>2.1.5.RELEASE</version>
</dependency>
this is into .yml into the config service. Then into the .yml for my microservice i have db.username property which I want to resolve from Vault but I can't. Do you have any ideas?
username: db.username
password: secret/apm-transaction-service/dev/db.user
#Value("${db.username}")
this value is resolved into the java code but not into the .yml
Now for each microservice which I have I want to resolve the secrets from the configuration service without making any changes into the microservices. Currently reading native .ymls from the config service and want to add one more source :)
ApplicationStartupRunner run method Started !!root
if you are using spring-boot, for the value in .yml file to be resolved it has to be a variable. you must use ${db.username} in the yaml file

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

How can I integrate Spring Cloud with logback?

I have two Spring Boot applications:
cloud-config
It has following file structure:
└───src
└───main
└───environment
└───default
├───application.yml
└───bootstrap.yml
...
application.yml:
server:
port: 8087
spring:
cloud:
config:
server:
git:
uri: ssh://path-to-repository.git
bootstrap.yml:
spring:
application:
name: cloud-config
main-application:
└───src
└───main
└───environment
└───default
├───bootstrap.yml
└───logback.xml
I have application.yml for my main-application in git repository.
Also in git repository I have file with properties: application-default.properties.
I want to move logback.xml to git repository to get it with spring-cloud. How can I do this?
To solve my task I have done following:
add logback.xml to my Spring Cloud repository;
remove logback.xml from main > environment > default directory;
add to bootstrap.yml of main-application module:
logging:
config: http://localhost:8087/main-application/default/master/logback.xml
Documentation: description of logging: config: path: Serving plain text
logging.config=${spring.cloud.config.uri}/guestservices/default/master/logback.xml
works for me.

Spring Cloud Config Server + BitBucket

I'm trying to get Spring Cloud's Config Server setup with a BitBucket private repository and haven't had any luck. No matter what configuration I use, I always seem to get a 404 returned when trying to load a configuration.
I've also tried setting breakpoints in JGitEnvironmentRepository but it never seems to get called outside of afterPropertiesSet. If I manually triggering the findOne(application,profile,label) while debugging, I get an error that Branch name <null> not allowed. If I specify "master" for label property, then I get the dreaded Ref master cannot be resolved error.
The app loads fine but no results. From all the documentation I've read, it seems like this should work out of the box. Any help would be appreciated.
bootstrap.yml
server:
port: 8888
spring:
application:
name: config-service
cloud:
bus.amqp.enabled: false
config:
enabled: false
failFast: true
server:
prefix: /configs
git :
uri: https://bitbucket.org/[team]/[repo].git
username: [user]
password: [pass]
Repo files
- demo.app.yml
Attempted URL
http://localhost:8888/configs/demo.app
you need to add a profile name to the url. The default profile is 'default'. http://localhost:8888/configs/demo.app/default
this only works with the 1.0.0.RELEASE version to me, but with the 1.0.1.RELEASE version I found a problem on Windows (there is a problem with the file separators so the config server didn't find the YAML files in the local cloned repository). Maybe this is the related issue: Spring Cloud Config | Git Based | Not working on windows machine
If you still have any issues, you can refer to the below client configuration. I have successfully configured the bitbucket repository.
Spring Boot Version: 1.4.1.RELEASE
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Camden.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
-----------------------------------------------------------------------
bootstrap.yml
spring:
application:
name: client-config
cloud:
config:
failFast: true
label: master
profile: default
username: <username>
password: <password>
server:
bootstrap: true
uri: http://localhost:8888

Resources