Spring activated profile is being ignored - without Spring Boot - java

I am using spring to run my app. Here is my configuration.yaml
spring:
profiles:
default: default
db:
host: mysql:3306
schema: my_db
user: user
password: pass
---
spring:
profiles: dev
db:
host: localhost:3306
---
spring:
profiles: prod
db:
host: mysql:3306
When running the service via intellij I am both tried to set the VM options (-Dspring.profiles.active=dev) and environment variable to SPRING_PROFILES_ACTIVE=dev.
Both didn't work. It seems like it takes the last profile, in my example it takes the prod profile.
The configuration is loaded via an xml:
<bean id="yamlProperties" class="org.springframework.beans.factory.config.YamlPropertiesFactoryBean">
<property name="resources" value="classpath:config.yaml"/>
</bean>
<context:property-placeholder properties-ref="yamlProperties"/>
Here is how I boot my app:
fun main(args: Array<String>) {
val context = ClassPathXmlApplicationContext("/api-applicationContext-all.xml")
val router = context.getBean("router") as Router
router.start()
}
What did I do wrong?

Related

SpringBoot profiles with Gradle vs cloud

I have some problem with starting microservice with cloud config on local machine:
In application.yml:
spring:
application:
name: my_servie
config:
import: 'configserver:'
profiles:
group:
env-prod-pg: postgres,log
I create application-local.yml, where add:
spring:
application:
name: mc-service-logistics-v2
config:
import: 'optional:configserver:'
and some other config updates,
in build.gradle I add:
tasks.register("bootRunLocal") {
group = "application"
description = "Runs the Spring Boot application with the local profile"
doFirst {
tasks.bootRun.configure {
systemProperty("spring.profiles.active", "local")
}
}
finalizedBy("bootRun")
}
but when I try to start app with gradle bootRunLocal I caught error:
Caused by: org.springframework.web.client.ResourceAccessException: I/O error on GET request for "http://localhost:8888/my-service/local": Connection refused; nested exception is java.net.ConnectException: Connection refused
If I understand- application still want to use cloud config and can't start. If I comment
spring.config.import in application.yml- aplication started without any problem.
How to solve this problem? I don't want to push to repo my local config again)
Well, I create to application-.yml and application.yml ,where store current profile:
spring:
profiles:
active: prod

Spring boot profiles and #Value

Not sure what I'm doing wrong here, but I have the following value that points to the URL of a micro service.
#Value("${url.dispenseRoot}")
private String dispenseRoot;
And I am setting up my dev profile in my application.yml file such:
---
spring:
datasource:
url: jdbc:h2:mem:test
username: sa
password: sa
driverClassName: org.h2.Driver
config:
activate:
on-profile: dev
mpesa:
host: sandbox.safaricom.co.ke
port: 443
url:
dispenseRoot: https://localhost:8844/dispense
---
Then, in accordance with my answer here: How do I activate a Spring Boot profile when running from IntelliJ?, I set up the spring boot profile on the run configuration as such:
And then run the following test to test my configuration:
#SpringBootTest
public class PaymentApplicationTests {
#Test
public void contextLoads() {
}
}
And I get:
... 111 more
Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'url.dispenseRoot' in value "${url.dispenseRoot}"
Now, if I copy and paste the following lines:
url:
dispenseRoot: https://localhost:8844/dispense
Above my profile where I'm setting up the general settings that are true for everything, it works fine. Therefore, I'm certain it has something to do with profiles, but I can't figure out what it is.
Please advise...

How to use spring cloud config server with postgresql and jdbc as backend with multiple profiles?

I'm able to connect to postgres using spring cloud config server with only bootstrap.yml file.
But I have multiple environments like dev,test and prod. So I want to create separate profiles for each environment(like bootstap-dev.properties) and change the url datasource url accordingly.
Can anyone please suggest me regarding the same ?
bootstrap.yml:
server:
port: 8081
spring:
application:
name: myapp
profiles:
active: jdbc
datasource:
url: jdbc:postgresql://localhost:5432/config_db
username: XXXX
password: XXXX
driverClassName: org.postgresql.Driver
cloud:
config:
server:
jdbc:
sql: SELECT key, value FROM properties WHERE application=? AND profile=? AND label=?;
order: 0
default-label: default
bus:
trace:
enabled: true
security:
user:
name: XXX
password: XXX
management:
endpoints:
web:
exposure:
include: bus-refresh,health
endpoint:
health:
show-details: always
It is easy to use in the spring cloud.
First, create an application.properties that content only follow like this spring.profiles.active=dev
Second, to modify your dev application.properties name to application-dev.properties.
Ok, then put them into your resources directory together.
Others, you can create another file of environments with different suffixes, such as application-test.properties or application-pro.properties..., and only change the active value to the suffix name. like this spring.profiles.active=test
My best to you.

Configure spring cloud config server for each profile group

I have few environments. There are:
local
dev
test
qa
lod
prod
Everything clear if config server connects to all of them.
In my case I need configuration server per group:
under dev control
under qa control
near devops control
Groups are connected to permissions and different environments.
so I need for each client something like:
bootstrap.yml
# default configs for local, dev, test profiles
spring:
application:
name: discovery-service
cloud:
config:
uri: http://local-dev-test-configuration-server:8888
---
# **bootstrap-qa.yml**
spring:
profiles: qa
application:
name: discovery-service
cloud:
config:
uri: http://qa-configuration-server:8888
---
# **bootstrap-prod.yml**
spring:
profiles: prod,lod
application:
name: discovery-service
cloud:
config:
uri: http://lod-prod-configuration-server:8888
Where
local-dev-test-configuration-server would have access to local, dev and test server configurations;
qa-configuration-server would have access to qa configuration;
lod-prod-configuration-server would have access to prod and lod configurations only.
Question:
I researched spring boot documentation but I have not faced with bootstrap.yml profiling.
Which way I should follow to cover my needs (manage 3 different config servers and correspond profiles)?
I've detected ability to configure different git resources for same config server. Is this approach the best for my case (I also have to manage few repositories to keep required configs)? I do not think so. I need to have few config servers for different envs because of different visibility. Thus I need configure on each consumer config hostname depending profile.
There are two possible solutions to configure clients for spring-cloud-configuration-servers:
spring-boot supports profiles in bootstrap.yml, so configuration provided in question can be used as solution
spring:
application:
name: discovery-service
cloud:
config:
uri: http://local-dev-test-configuration-server:8888
---
spring:
profiles: qa
application:
name: discovery-service
cloud:
config:
uri: http://qa-configuration-server:8888
---
spring:
profiles: prod,lod
application:
name: discovery-service
cloud:
config:
uri: http://lod-prod-configuration-server:8888
In case you want to keep bootstrap.yml configuration as simple as it possible:
spring:
application:
name: discovery-service
cloud:
config:
uri: http://local-dev-test-configuration-server:8888
in this case solution is to use -Dspring.cloud.config.uri=http://localhost:8888 parameter overrides required property for example:
java -Dspring.profiles.active=localhost -Dspring.cloud.config.uri=http://localhost:8888 -jar ./target/discovery-service-0.0.1-SNAPSHOT.jar
P.S.
Approaches can be mixed.

How can I create profiles with spring boot + .yaml?

I have spring boot server with 2 property files: application-local.properties and application-test.properties
In each file I have configs for dev machine and for test. Start it like this:
-Dspring.profiles.active=local
But in new spring boot project I use .yaml config file. And I do not understand how can I use profiles with .yaml. I tried read documentation but understood nothing. Can you explain what to do, step by step?
I need have two files?
application-local.yaml and application-test.yaml
Or I need write all in one application.yaml file? If in one file how can I separate configs? It is my config:
server:
path: ***
port: ***
cxf:
path: ***
spring.datasource:
type: com.zaxxer.hikari.HikariDataSource
driver-class-name: oracle.jdbc.OracleDriver
url: ***
username: ***
password: ***
hikari:
minimumIdle: 5
maximumPoolSize: 20
idleTimeout: 30000
poolName: SpringBootJPAHikariCP
maxLifetime: 2000000
connectionTimeout: 30000
connection-test-query: SELECT 1 FROM DUAL
spring.jpa:
show-sql: false
database-platform: org.hibernate.dialect.Oracle10gDialect
properties.hibernate.jdbc.batch_size: 30
properties.hibernate.cache.use_second_level_cache: false
hibernate:
ddl-auto: validate
spring.cache:
ehcache:
config: classpath:ehcache.xml
#app configs
my:
messages-max-count: 5
messages-delay: 100
schedulers-charge-delay: 100
client:
first-server-address: ***
second-server-address: ***
last-server-address: ***
enabled-client: FirstClient
I want create test profile and change database url (or change to postgreSql), change maximumPoolSize property
Create application.yaml and define all default properties there.
Create application-local.yaml and override properties needed for the local profile.
Create application-test.yaml and override properties needed for the test profile.
Set spring.profiles.active by either passing it as a system property (-D for java) or defining it within application.yaml.
When you are running an app with a {PROFILE}, Spring will parse application-{PROFILE}.yaml after application.yaml.
Yes, you can create multiple profiles even with single file
Profile are separated with 3 DASH (---)
logging:
level:
.: error
org.springframework: ERROR
spring:
profiles:
active: "dev"
main:
banner-mode: "off"
server:
port: 8085
---
spring:
profiles: dev
---
spring:
profiles: prod

Categories

Resources