Spring profiles group - spring

I decided to deal with the profiles and divide into groups
spring:
//Common settings
---
spring:
profiles:
group:
default:
- prod
- actuator
dev:
- dev
- actuatorDev
uat:
- uat
- actuatorUat
Why when I specify dev or uat.
The settings are loaded from the main block, then replaced from dev or uat. And on top is the actuator profile
And when I don't specify the profile at startup, in theory it's just default
This kind of magic doesn't happen
How to correctly implement general settings and then replace them depending on the default, dev, uat profile?

Please Test:
//...
spring:
profiles:
default: "prod,actuator" # when no profile*S* set [3.]
group: # ...according to [3.1] and [3.2]
dev:
- "dev"
- "actuatorDev"
uat:
- "uat"
- "actuatorUat"
Spring (current) Ref: Chap. 3 Profiles

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.

SpringBoot yml how can I run two profiles at the same time

In my application yml, I have two profiles which I need to both run
spring:
profiles:
active:
awss3, #env#
application:
name: CONFIG-SERVICE
I need awss3 to be run, in what ever the env is. Is this possible ?
In my opinion, your requirements fits Profile Groups. This approach is preferable as it allows you to visually inform the developer which profiles are required for which environments.
https://docs.spring.io/spring-boot/docs/current/reference/html/features.html#features.profiles.groups
Example:
spring:
profiles:
group:
production:
- "aws3"
- "prodmq"
Then you can start the application using:
--spring.profiles.active=production
to active the production, aws3 and prodmq profiles in one hit.
To clarify: The keyword production under group: is what you will use when you run your application as spring.profiles.active=production
Alternatively like you can simply add whatever profiles you want from the command line.
java -jar -Dspring.profiles.active=awss3,prodmq demo-0.0.1-SNAPSHOT.jar
yes, using properties:
// Use your dynamic profile
spring.profiles.active=#env#
// Include your default profile
spring.profiles.include=awss3
yaml:
spring:
profiles:
include: awss3
active:
#env#

How can I activate Profile in Spring Boot YAML?

I have below application YAML of my Spring Boot (2.4.5) application with two profiles.
spring:
profiles: dev
spring:
profiles: prod
Now when I pass the arguments -Dspring.profiles.active=dev, it actually doesn't pickup the profile. Is there any changes in profile has been made recently.
When I activate the profile, like below it works
spring:
profiles:
active: dev
But as per the env profile can be different. I don't want to hard code the profile
You should use the following instead in your application.yml:
spring
config:
activate:
on-profile: dev
---
spring
config:
activate:
on-profile: prod

How to configure profiling for sentry?

I need to read a different properties for Sentry.io in spring boot application based on environment profile (DEV, QA or PROD).
so using next properties for sentry in each bootstrap profile with different values
sentry:
enabled: true
environment: LOCAL
servername: localhost
dsn: MY_DNS_URL
stacktrace.app.packages:
- MY_PACKAGE
sample:
rate: 0.75
So how can i achieve that ??

Spring load prod profile

I have a Spring application that I have to deploy in production. Unfortunately I am unable to turn the prod profile on. I have an application-dev.yml and application-prod.yml file as well as application.yml.
My application.yml is as follows:
# ===================================================================
# Spring Boot configuration.
#
# This configuration will be overridden by the Spring profile you use,
# for example application-dev.yml if you use the "dev" profile.
#
# More information on profiles: http://www.jhipster.tech/profiles/
# More information on configuration properties: http://www.jhipster.tech/common-application-properties/
# ===================================================================
# ===================================================================
# Standard Spring Boot properties.
# Full reference is available at:
# http://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html
# ===================================================================
eureka:
client:
enabled: true
healthcheck:
enabled: true
fetch-registry: true
register-with-eureka: true
instance-info-replication-interval-seconds: 10
registry-fetch-interval-seconds: 10
instance:
appname: majurca
instanceId: majurca:${spring.application.instance-id:${random.value}}
lease-renewal-interval-in-seconds: 5
lease-expiration-duration-in-seconds: 10
status-page-url-path: ${management.context-path}/info
health-check-url-path: ${management.context-path}/health
metadata-map:
zone: primary # This is needed for the load balancer
profile: ${spring.profiles.active}
version: ${info.project.version}
ribbon:
eureka:
enabled: true
management:
security:
roles: ADMIN
context-path: /management
info:
git:
mode: full
health:
mail:
enabled: false # When using the MailService, configure an SMTP server and set this to true
spring:
profiles:
default: prod
active: prod
application:
name: majurca
jackson:
serialization.write_dates_as_timestamps: false
jpa:
open-in-view: false
hibernate:
ddl-auto: none
naming:
physical-strategy: org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy
implicit-strategy: org.springframework.boot.orm.jpa.hibernate.SpringImplicitNamingStrategy
messages:
basename: i18n/messages
mvc:
favicon:
enabled: false
thymeleaf:
mode: XHTML
security:
basic:
enabled: false
server:
session:
cookie:
http-only: true
info:
project:
version: #project.version#
# ===================================================================
# JHipster specific properties
#
# Full reference is available at: http://www.jhipster.tech/common-application-properties/
# ===================================================================
jhipster:
async:
core-pool-size: 2
max-pool-size: 50
queue-capacity: 10000
# By default CORS is disabled. Uncomment to enable.
#cors:
#allowed-origins: "*"
#allowed-methods: "*"
#allowed-headers: "*"
#exposed-headers: "Authorization,Link,X-Total-Count"
#allow-credentials: true
#max-age: 1800
mail:
from: majurca#localhost
swagger:
default-include-pattern: /api/.*
title: majurca API
description: majurca API documentation
version: 0.0.1
terms-of-service-url:
contact-name:
contact-url:
contact-email:
license:
license-url:
ribbon:
display-on-active-profiles: dev
# ===================================================================
# Application specific properties
# Add your own application properties here, see the ApplicationProperties class
# to have type-safe configuration, like in the JHipsterProperties above
#
# More documentation is available at:
# http://www.jhipster.tech/common-application-properties/
# ===================================================================
application:
forms:
register:
autocomplete-cp-nbitems: 20
google:
recaptcha:
url: https://www.google.com/recaptcha/api/siteverify
secret: 6Lci63UUAAAAAIsluk6G3ueNiJ_ET0m4luMsC8O5
key: 6Lci63UUAAAAADFyeZFTkEaHshVK2LQgYV63PPD_
#key: 6LfUG3UUAAAAAMxScj7ZFY-OXedoWLRzl0wryrrF
#secret: 6LfUG3UUAAAAANsSIsCFOi3X9uYzS72De8EqKqNM
So you can see I have spring.profiles.default and spring.pofiles.active set to prod. I also tried to build the app with maven and the option -Dspring.profiles.active=prod but despite this each time I run the war generated it says The following profiles are active: swagger,dev.
Anyone has a idea why the prod profile is not loaded ? Thanks in advance.
I've verified on spring-boot 2.1.4 the following configurations:
application.yaml:
spring:
profiles:
active: dev
application-dev.yaml:
sample:
value: development
application-prod.yaml:
sample:
value: production
And the following (very simple) spring boot application:
package com.example.demo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.event.ApplicationStartedEvent;
import org.springframework.context.event.EventListener;
#SpringBootApplication
public class DemoApplication {
#Value("${sample.value}")
private String sampleValue;
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
#EventListener
public void onAppStarted(ApplicationStartedEvent evt) {
System.out.println("Sample Value: " + sampleValue);
}
}
This configuration works, even if I don't supply any flags: java -jar myapp.jar
So there must be something else here, related to your code.
Its hard to say whats wrong without seeing the application, however I did found one "suspicious" statement:
You say, that no matter what you try:
The following profiles are active: swagger,dev
Now where does the 'swagger' profile come from? I don't see any reference to it. If you run the application with java -jar myapp.jar one possibility I can see is that there is somewhere spring.factories file that defines an EnvironmentPostProcessor - its a hook where you can "fiddle" with profiles and, among other things add active profiles "manually" (in code).
So please check this possibility, but again - what you've done is correct in spring boot (well, technically you don't need spring.profiles.default entry but it doesn't harm)
Spring Boot can load the same properties from many locations , which you can refer its loading order in the docs at here.
The upper part in the list have a higher loading priority than the lower part.That means what you define in the application-xxx.yml can be overridden by the same properties that are also defined in the OS env variable or JVM properties or etc .....
As the command line arguments has a pretty high loading order , which means basically you can try to use it to set the profile by adding --spring.profiles.active=prod to the command that start the application such as :
$ java -jar myproject.jar --spring.profiles.active=prod

Resources