spring boot yaml profile names resolution - spring

I have multimodule maven project in which I have one common part which I want to share between applications.
In common module I'm defining the configuration for liquibase and db connection. For this I have 2 profiles common-prod and common-dev located in application.yaml.
spring:
profiles: common-prod
# POSTGRE
datasource:
url: ${DATABASE_URL:jdbc:postgresql://localhost:5432/dbname}
driver-class-name: org.postgresql.Driver
username: pguser
password: pguser
jpa:
database-platform: org.hibernate.dialect.PostgreSQLDialect
hibernate:
ddl-auto: none
properties:
hibernate.default_schema: public
#LIQUIBASE
liquibase:
enabled: true
change-log: /db/changelog/db-changelog-master.xml
drop-first: false
---
spring:
profiles: common-dev
# POSTGRE
datasource:
url: ${DATABASE_URL:jdbc:postgresql://localhost:5432/dbname-dev}
username: pguserdev
password: pguserdev
#LIQUIBASE
liquibase:
enabled: true
drop-first: true
now in web application I want to define some additional properties. But when I include them in application.yaml (in web module) I think it overrides the application.yaml from common module.
So I need to
rename the application.yaml from common to something like application-common.yaml
rename the application.yaml in application to something like application-web.yaml
if I rename the common module's yaml file am I able to reference the profiles from within this file? eg something like -Dspring.profiles.active=common-common-prod or I need to separate this profiles to separate yaml files?
If I rename web modules config file to application-web.yaml I must allways include the web profile. I can live with that, but it would be simpler to include the profiles in application.yaml and don't care about command line arguments.

Related

Spring Boot Liquibase doesn't appear to support XML formatted changelog

spring-boot-starter-parent:2.7.5
I have configured the following in my src/main/resources/application.yml file:
spring:
liquibase:
change-log: classpath:/db/changelog/db.changelog-master.xml
---
spring:
config:
activate:
on-profile: prod
datasource:
driver-class-name: org.postgresql.Driver
url: ${DB_URL}
username: ${DB_USERNAME}
password: ${DB_PASSWORD}
Additionally, the following resource exists in the project repository:
src/main/resources/db/changelog/db.changelog-master.xml
I also have the following configured in src/test/resources/application.yml:
spring:
config:
activate:
on-profile: test
datasource:
driver-class-name: org.h2.Driver
url: jdbc:h2:mem:db;DB_CLOSE_DELAY=-1
username: sa
password: sa
liquibase:
change-log: classpath:/db/changelog/db.changelog-master.xml
When I try to build this project the build fails with the following error message:
Liquibase failed to start because no changelog could be found at 'classpath:/db/changelog/db.changelog-master.yaml'
Is there some way to configure spring boot to allow an xml based liquibase changelog file? If so, what am I missing? Or does it only support the yaml format?
Your configurations seems valid, but some configuration still points to classpath:/db/changelog/db.changelog-master.yaml (based by on provided error message)
If there is no some typo, than may be you should look for this property in some other configuration (.properties files, command line arguments and other, see doc for all possible ways)

How do I setup JavaFX with SpringBoot desktop application to share database on a LAN

I have an inventory management application built with JavaFx and springboot that was originally built to run as standalone using hsqldb. Now I want to upgrade it so that two or more systems can share same database in a LAN environment. When I connect two systems via wifi, I could run the application from the other connected computer but after creating a record, it did not reflect on the other system. Please how do I make this work? Do I need to use mysql instead of hsqldb?
Below is my application.yml file configuration. The lines hashed out are the previous setting for the stand alone version and I have re-defined the hsql settings to use a network path as shown (\PC001\ims_compiled\shared_hsqldb\hsqldb)
Please any assistance ill be appreciated
app:
fixedDelay: 300000
fixedRate: 1200000
localLogin: true
debug: true
hsql:
parameters: hsqldb.lock_file=false;createDatabaseIfNotExist=true;DB_CLOSE_DELAY=-1;shutdown=true;hsqldb.write_delay=false;hsqldb.write_delay_millis=100
log4j:
logger:
org:
hibernate:
transaction: ERROR
springframework:
transaction: ERROR
logging:
config: logback.xml
level:
com:
zaxxer:
hikari:
HikariConfig: ERROR
org:
hibernate:
SQL: ERROR
type:
descriptor:
sql:
BasicBinder: ERROR
mysql-base:
datasource:
DB: ikooba_db
host: 34.244.38.76
jdbc-url: jdbc:mysql://${mysql-base.datasource.host}:${mysql-base.datasource.port}/${mysql-base.datasource.DB}?${mysql-base.datasource.parameters}
parameters: serverTimezone=UTC&useSSL=FALSE&autoReconnect=true&sessionVariables=sql_mode=''
password: 'gsjgsgssshj'
port: 3306
username: sfgsdsfdfs
sentry:
dsn: https://shfs6765676757shsvvs#errors.xxxxxxxx.com/6
enabled: true
stacktrace:
app:
packages: com.ikooba.desktop.ims
spring:
datasource:
hikari:
auto-commit: true
# jdbcUrl: jdbc:hsqldb:hsqldb/userDB;${hsql.parameters}
jdbcUrl: jdbc:hsqldb:\\PC001\ims_compiled\shared_hsqldb\hsqldb/userDB;${hsql.parameters}
password: ''
username: sa
jpa:
hibernate:
ddl-auto: update
use-new-id-generator-mappings: true
properties:
hibernate:
enable_lazy_load_no_trans: true
jdbc:
batch_size: 120
order_inserts: true
search:
default:
directory_provider: filesystem
# indexBase: hsqldb/lucence/indexes
indexBase: \\PC001\ims_compiled\shared_hsqldb\hsqldb/lucence/indexes
indexwriter:
infostream: true
generate_statistics: true
jmx_enabled: true
lucene_version: LATEST
javax:
persistence:
validation:
mode: none
main:
allow-bean-definition-overriding: true
lazy-initialization: true
Setup a HSQLDB server on PC001 and connect to it from different machines.
Chapter 15. http://hsqldb.org/doc/2.0/guide/listeners-chapt.html#lsc_hsql_server
For simplicity, create a server.properties file as described. Inside this file the property setting below should reflect the name, path, and the startup properties of your databse. For example:
server.database.0=file:\\PC001\ims_compiled\shared_hsqldb\hsqldb/userDB;hsqldb.write_delay_millis=100
The other parameters that you used in your hsql.parameters are unnecessary (e.g. hsqldb.lock_file, shutdown). See http://hsqldb.org/doc/2.0/guide/dbproperties-chapt.html for valid properties (but you probably don't need them).

Spring cloud config not apply global configuration

I'm having a problem on spring cloud configuration 2020.0.3, spring boot 2.4.5 for details:
Yaml configuration file is following Multi-profile YAML documents
I have a configuration yaml file on the config server.
my_cofig.yaml
spring:
datasource:
url: "jdbc:mariadb://localhost:3306/default_db"
driver-class-name: org.mariadb.jdbc.Driver
username: my_db
password: 12345
---
spring:
profiles: dev
datasource:
url: "jdbc:mariadb://localhost:3306/dev_db"
I have loaded the config from the config server by browser, it's correct.
But:
When I run Spring application with specific configuration (e.g. dev), Spring application must not apply global configuration variables defined on configuration file from configuration server. It only loads dev's configuration variables.
bootstrap.yaml
server:
port: 8081
spring:
application:
name: auth-service
cloud:
config:
enabled: true
fail-fast: true
allow-override: true
profile: dev
uri: http://localhost:5000
Error detail:
Caused by: org.hibernate.HibernateException: Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set
Please help me, many thanks!
spring:
profiles:
This does not exist (it was deprecated if I am not mistaken). Please check the reference documentation.
I believe that what you are looking for is the following (reference documentation):
spring:
datasource:
url: "jdbc:mariadb://localhost:3306/default_db"
driver-class-name: org.mariadb.jdbc.Driver
username: my_db
password: 12345
---
spring:
config:
activate:
on-profile: dev
datasource:
url: "jdbc:mariadb://localhost:3306/dev_db"
Finaly, I solved the problem by upgrading spring boot version to 2.5.1
Thanks all

Default value for profiles in Spring Boot

I have multiple profiles and there are lots of attributes(which will grow), I don't want to set each attribute every time in a different profile.
I have an application.yml file like this:
freemarker:
template-loader-path: classpath:/templates
datasource:
username: postgres
password: mypass
driver-class-name: org.postgresql.Driver
jpa:
show-sql: true
properties:
hibernate:
jdbc:
lob:
non_contextual_creation: true
dialect: org.hibernate.dialect.PostgreSQLDialect
hibernate:
ddl-auto: create
security:
secret: "jwt_secret_key_it_is_a_random_key_229"
loginTokenExpiration: 86400
confirmUserTokenExpiration: 86400
devTokenExpiration: 157680000
tokenPrefix: "Bearer"
headerString: "Authorization"
signUpUrl: "/token/login"
mysite:
apiTosUrl: "https://example.com/api-tos"
fromEmail: "alert#example.com"
firstFreeCredits: 10
junction:
port: 9080
hasBasicAuth: false
---
spring:
profiles:
active: dev
---
spring:
profiles: stage
jpa:
show-sql: true
hibernate:
ddl-auto: update
chargeBee:
site: "example.chargebee.com"
apiKey: "mykey"
---
spring:
profiles: prod
datasource:
url: jdbc:postgresql://localhost/myproddb
jpa:
show-sql: false
hibernate:
ddl-auto: update
chargeBee:
site: "example.chargebee.com"
apiKey: "myapikey"
Most of the settings are common between profiles, and which are not common, I have re-defined it in the corresponding section of the profile.
I am assuming that the first section in this YAML document fills the properties with default values and the corresponding sections overwrite it.
Is this approach correct? If not then how to have an inheritance of properties values such that I define the common value once and for the rest of the profiles, I only need to define the different values?
you can also achieve profile using create the different profile-specific yml file.
you can also be defined by using the following naming convention:
application-{profile}.yml
Create application.yml file. This file loaded first. Load all attribute of application.yml file.
Check any active profile found(yes stage) then load after successfully load default profile then load application-stage.yml file and override existing attribute and add the new attribute.
spring:
profiles:
active: stage
datasource:
url: jdbc:postgresql://localhost:5432/springbootdb
create application-stage.yml file same location of application.yml.
spring:
jpa:
show-sql: true
hibernate:
ddl-auto: update
chargeBee:
site: "example.chargebee.com"
apiKey: "mykey"
create application-prod.yml file same location of application.yml.
spring:
datasource:
url: jdbc:postgresql://localhost/myproddb
jpa:
show-sql: false
hibernate:
ddl-auto: update
chargeBee:
site: "example.chargebee.com"
apiKey: "myapikey"
Yes that correct approach to define multiple profiles
You can specify multiple profile-specific YAML documents in a single
file by using a spring.profiles key to indicate when the document
applies, as shown in the following example:
See official document 24.6.3 Multi-profile YAML Documents
In the below example, if the development profile is active, the server.address property is 127.0.0.1. Similarly, if the production profile is active, the server.address property is 192.168.1.120. If the development and production profiles are not enabled, then the value for the property is 192.168.1.100.
server:
address: 192.168.1.100
---
spring:
profiles: development
server:
address: 127.0.0.1
---
spring:
profiles: production
server:
address: 192.168.1.120

Database application.yml for Spring boot from applications.properties

I've got a working Spring Boot Application that connects to a Postgres database. I've got the project set up with an application.properties file, but would like to make the switch over to an application.yml file. However when I make the switch, my application errors out while attempting to connect to the db.
Original applications.properties file:
spring.jpa.database=POSTGRESQL
spring.datasource.platform=postgres
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=create-drop
spring.database.driverClassName=org.postgresql.Driver
spring.datasource.url=jdbc:postgresql://localhost:5432/mydb
spring.datasource.username=foo
spring.datasource.password=bar
And Here's what I've got so far in the application.yml file:
spring.jpa:
database: POSTGRESQL
hibernate.ddl-auto: create-drop
show-sql: true
spring.datasource:
platform: postgres
driverClassName: org.postgresql.Driver
url: jdbc:postgresql://localhost:5432/mydb
username: foo
password: bar
Am I missing something in the translation between file types?
You need to treat each . character in property names as levels in the yaml file:
spring:
jpa:
database: POSTGRESQL
show-sql: true
hibernate:
ddl-auto: create-drop
datasource:
platform: postgres
url: jdbc:postgresql://localhost:5432/mydb
username: foo
password: bar
driverClassName: org.postgresql.Driver
EDIT: edits have been suggested, thanks for that. The driverClassName property actually should be under spring.datasource. However, the purpose of this answer was to show how a properties file is converted into yaml format. So I have changed the driverClassName property to be at the right path, that is not part of the transformation from properties to yaml.
Please upvote the other answer (Z0lt#n's answer)
But pasting here for future readers... a sql server version.
spring:
jpa:
show-sql: true
hibernate:
ddl-auto: update
properties:
hibernate.jdbc.batch_size: 20
hibernate.cache.use_query_cache: false
hibernate.cache.use_second_level_cache: false
hibernate.cache.use_structured_entries: false
hibernate.cache.use_minimal_puts: false
datasource:
#SPRING_DATASOURCE_URL environment variable will be something like -> jdbc:sqlserver://MySqlServer\\MyInstance:1433;DatabaseName=MyDbName;
url: ${SPRING_DATASOURCE_URL}
username: ${SPRING_DATASOURCE_USERNAME}
password: ${SPRING_DATASOURCE_PASSWORD}
driverClassName: com.microsoft.sqlserver.jdbc.SQLServerDriver
and maven entry
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<version>7.0.0.jre8</version>
</dependency>
APPEND
This seems to be the "standard" name for the driverClassName.
SPRING_DATASOURCE_DRIVER-CLASS-NAME
And of course, in my example, you would use the value of:
com.microsoft.sqlserver.jdbc.SQLServerDriver
NOW, some spring, springboot, environment variable voodoo alert.
Sometimes, when specifying the environment variable...for some command lines items, I would have to change the hyphens and make them underscores.
(aka, "SPRING_DATASOURCE_DRIVER-CLASS-NAME" vs "SPRING_DATASOURCE_DRIVER_CLASS_NAME"
below the -e generically represents "passing environment variable values via the command line"
MyCommandLineProgram.exe -e SPRING_DATASOURCE_URL="jdbc:sqlserver://myServerName:1433;DatabaseName=MyDB;" -e SPRING_DATASOURCE_USERNAME="myUserName" -e SPRING_DATASOURCE_PASSWORD="myPassword" -e SPRING_DATASOURCE_DRIVER_CLASS_NAME="com.microsoft.sqlserver.jdbc.SQLServerDriver"
There's some voodoo for you.
Those interested in the logging (logback.xml) issue, maybe want to find my answer here as well:
Spring Boot Logback DB Appender Properties
application.yml file for postgresql
Spring DATASOURCE (DataSourceAutoConfiguration & DataSourceProperties)
spring:
datasource:
driver-class-name: org.postgresql.Driver
username: postgres
password: root
url: jdbc:postgresql://localhost/postgres
platform: postgres
initialization-mode: always
continue-on-error: true
jpa:
show-sql: true
generate-ddl: true
hibernate:
ddl-auto: create
database: postgresql
server:
port: 1111
spring:
application:
name: client-one
datasource:
password: postgres
url: jdbc:postgresql://localhost:5432/postgres?currentSchema=education
username: postgres
jpa:
hibernate:
ddl-auto: update
properties:
hibernate:
dialect: org.hibernate.dialect.PostgreSQLDialect
show-sql: true

Resources