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

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

Related

How to connect to jhipster registry running in docker with microservice in intellj

I have an spring boot application which is dockerized, while it is dockerized I can connect without any problems to my jhipster-registry inside the same docker-compose. But when I am trying to start my application with intellj idea in order to test new implementations I am getting the following error:
com.netflix.discovery.shared.transport.TransportException: Cannot execute request on any known server
at com.netflix.discovery.shared.transport.decorator.RetryableEurekaHttpClient.execute(RetryableEurekaHttpClient.java:112)
at com.netflix.discovery.shared.transport.decorator.EurekaHttpClientDecorator.register(EurekaHttpClientDecorator.java:56)
at com.netflix.discovery.shared.transport.decorator.EurekaHttpClientDecorator$1.execute(EurekaHttpClientDecorator.java:59)
at com.netflix.discovery.shared.transport.decorator.SessionedEurekaHttpClient.execute(SessionedEurekaHttpClient.java:77)
at com.netflix.discovery.shared.transport.decorator.EurekaHttpClientDecorator.register(EurekaHttpClientDecorator.java:56)
at com.netflix.discovery.DiscoveryClient.register(DiscoveryClient.java:876)
at com.netflix.discovery.InstanceInfoReplicator.run(InstanceInfoReplicator.java:121)
at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515)
at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
at java.base/java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:304)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
at java.base/java.lang.Thread.run(Thread.java:829)
2022-07-07 14:48:02.062 WARN 18203 --- [freshExecutor-0] c.n.d.s.t.d.RetryableEurekaHttpClient : Request execution failed with message: I/O error on GET request for "http://admin:admin#jhipster-registry:8761/eureka/apps/": jhipster-registry; nested exception is java.net.UnknownHostException: jhipster-registry
2022-07-07 14:48:02.062 INFO 18203 --- [freshExecutor-0] com.netflix.discovery.DiscoveryClient : DiscoveryClient_PRODUCT/product:88e7f82cd95d241292a79dd4f76716a8 - was unable to refresh its cache! This periodic background refresh will be retried in 10 seconds. status = Cannot execute request on any known server stacktrace = com.netflix.discovery.shared.transport.TransportException: Cannot execute request on any known server
at com.netflix.discovery.shared.transport.decorator.RetryableEurekaHttpClient.execute(RetryableEurekaHttpClient.java:112)
at com.netflix.discovery.shared.transport.decorator.EurekaHttpClientDecorator.getApplications(EurekaHttpClientDecorator.java:134)
at com.netflix.discovery.shared.transport.decorator.EurekaHttpClientDecorator$6.execute(EurekaHttpClientDecorator.java:137)
at com.netflix.discovery.shared.transport.decorator.SessionedEurekaHttpClient.execute(SessionedEurekaHttpClient.java:77)
at com.netflix.discovery.shared.transport.decorator.EurekaHttpClientDecorator.getApplications(EurekaHttpClientDecorator.java:134)
at com.netflix.discovery.DiscoveryClient.getAndStoreFullRegistry(DiscoveryClient.java:1101)
at com.netflix.discovery.DiscoveryClient.fetchRegistry(DiscoveryClient.java:1014)
at com.netflix.discovery.DiscoveryClient.refreshRegistry(DiscoveryClient.java:1531)
at com.netflix.discovery.DiscoveryClient$CacheRefreshThread.run(DiscoveryClient.java:1498)
at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515)
at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
at java.base/java.lang.Thread.run(Thread.java:829)
This is how my docker jhipster registry is configured:
jhipster-registry:
image: jhipster/jhipster-registry:v7.3.0
volumes:
- ./central-server-config:/central-config
# By default the JHipster Registry runs with the "dev" and "native"
# Spring profiles.
# "native" profile means the filesystem is used to store data, see
# http://cloud.spring.io/spring-cloud-config/spring-cloud-config.html
environment:
- _JAVA_OPTIONS=-Xmx512m -Xms256m
- SPRING_PROFILES_ACTIVE=dev
- SPRING_SECURITY_USER_PASSWORD=admin
- JHIPSTER_REGISTRY_PASSWORD=admin
- SPRING_CLOUD_CONFIG_SERVER_COMPOSITE_0_TYPE=native
- SPRING_CLOUD_CONFIG_SERVER_COMPOSITE_0_SEARCH_LOCATIONS=file:./central-config
# - SPRING_CLOUD_CONFIG_SERVER_COMPOSITE_0_TYPE=git
# - SPRING_CLOUD_CONFIG_SERVER_COMPOSITE_0_URI=https://github.com/jhipster/jhipster-registry/
# - SPRING_CLOUD_CONFIG_SERVER_COMPOSITE_0_SEARCH_PATHS=central-config
- MANAGEMENT_METRICS_EXPORT_PROMETHEUS_ENABLED=true
ports:
- 8761:8761
central server config:
#common configuration shared between all applications
configserver:
name: Docker JHipster Registry
status: Connected to the JHipster Registry running in Docker
jhipster:
security:
authentication:
jwt:
base64-secret: MzEyMjE4N2FmMmMyNmFjMmQ4ODk0NzAzNjNhNmRkYTUxNzBkZjVkMTM4NDIwM2VmZjhkMTJjMTUyNjczZWUxNzIwZGI4ZWQ5YmZhNjk3MGU5ZmE4NTU2NWNkZGJkYTE3NTMyNWFhNmE4YzdjZTk5NGRhMGY5NTBkMWM2ODBkYmE=
management:
metrics:
export:
prometheus:
enabled: true
eureka:
client:
service-url:
defaultZone: http://admin:${jhipster.registry.password}#jhipster-registry:8761/eureka/
And this is the config of my spring boot application (dev)
logging:
level:
ROOT: DEBUG
tech.jhipster: DEBUG
org.hibernate.SQL: DEBUG
com.moniesta.product: DEBUG
eureka:
instance:
prefer-ip-address: true
client:
service-url:
defaultZone: http://admin:${jhipster.registry.password}#localhost:8761/eureka/
spring:
devtools:
restart:
enabled: true
additional-exclude: static/**,.h2.server.properties
livereload:
enabled: false # we use Webpack dev server + BrowserSync for livereload
jackson:
serialization:
indent-output: true
cloud:
config:
uri: http://admin:${jhipster.registry.password}#localhost:8761/config
# name of the config server's property source (file.yml) that we want to use
name: product
profile: dev
label: main # toggle to switch to a different version of the configuration as stored in git
# it can be set to any label, branch or commit of the configuration source Git repository
datasource:
type: com.zaxxer.hikari.HikariDataSource
url: jdbc:h2:mem:product;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE
username: product
password:
hikari:
poolName: Hikari
auto-commit: false
h2:
console:
# disable spring boot built-in h2-console since we start it manually with correct configuration
enabled: true
jpa:
database-platform: tech.jhipster.domain.util.FixedH2Dialect
generate-ddl: on
hibernate:
ddl-auto: update
liquibase:
# Remove 'faker' if you do not want the sample data to be loaded automatically
contexts: dev, faker
url: jdbc:h2:mem:product;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE
messages:
cache-duration: PT1S # 1 second, see the ISO 8601 standard
thymeleaf:
cache: false
sleuth:
# https://github.com/spring-projects/spring-framework/issues/27844#issuescomment-1002866885
# https://github.com/spring-cloud/spring-cloud-sleuth/issues/2094
async:
enabled: false
enabled: false
sampler:
probability: 1 # report 100% of traces
zipkin: # Use the "zipkin" Maven profile to have the Spring Cloud Zipkin dependencies
base-url: http://localhost:9411
enabled: false
locator:
discovery:
enabled: true
server:
port: 8081
# ===================================================================
# JHipster specific properties
#
# Full reference is available at: https://www.jhipster.tech/common-application-properties/
# ===================================================================
jhipster:
cache: # Cache configuration
hazelcast: # Hazelcast distributed cache
time-to-live-seconds: 3600
backup-count: 1
registry:
password: admin
# CORS is disabled by default on microservices, as you should access them through a gateway.
# If you want to enable it, please uncomment the configuration below.
# cors:
# allowed-origins: "http://localhost:9000,https://localhost:9000"
# allowed-methods: "*"
# allowed-headers: "*"
# exposed-headers: "Authorization,Link,X-Total-Count"
# allow-credentials: true
# max-age: 1800
security:
authentication:
jwt:
# This token must be encoded using Base64 and be at least 256 bits long (you can type `openssl rand -base64 64` on your command line to generate a 512 bits one)
base64-secret: NWRhMTc4MTY0MDBmNzc2YmY1NzgzNmU0ZmQ2MmUyZGFkNTdhMmM4MWQ0OTU2YjI5MzY4YjJiMjllMzRmYjVmNWM5NjNhNjdjOTcxMmM1M2Y3OGFkZDNhNTQyNDViZmFlZGY4ZTU5MTMwNjM1MTNmMjkzYjA4OTcyMWEyNTA5ZWQ=
# Token is valid 24 hours
token-validity-in-seconds: 86400
token-validity-in-seconds-for-remember-me: 2592000
logging:
use-json-format: false # By default, logs are not in Json format
logstash: # Forward logs to logstash over a socket, used by LoggingConfiguration
enabled: false
host: localhost
port: 5000
queue-size: 512
I am still an amateur in terms of docker and jhipster and so on. My guess is it is a small problem, maybe someone here can give me a pointer.

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.

Failed to configure a DataSource: 'url' attribute - although specified

I am trying to get run application.yml instead of application.properties. But it fails. Please help. The same thing works in application.properties.
My application.yml looks like this:
spring:
profiles: container
jpa:
show-sql: true
database: MYSQL
database-platform: org.hibernate.dialect.MySQL5InnoDBDialect
hibernate:
ddl-auto: update
datasource:
url: jdbc:mysql://mysql57:3308/hello_java
username: demo_java
password: 1234
driver-class-name: com.mysql.jdbc.Driver
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2020-04-03 10:40:26.671 ERROR 12600 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter :
APPLICATION FAILED TO START
Description:
Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.
Reason: Failed to determine a suitable driver class
Action:
Consider the following:
If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.
If you have database settings to be loaded from a particular profile you may need to activate it (no profiles are currently active).
Process finished with exit code 1

Spring cloud server returning empty configurations

I have a dead simple config server with following properties :
spring:
profiles:
active: native
cloud:
config:
server:
native:
searchLocations: classpath:/configs
server:
port: 8888
In the src/main/resources folder i have a configs folder with a customer-service.yml file inside it containing the following config :
spring:
application:
name: customer-service
h2:
console:
enabled: true
server:
port: 8080
eureka:
client:
serviceUrl:
defaultZone: ${EUREKA_URI:http://localhost:8761/eureka}
instance:
preferIpAddress: true
leaseRenewalIntervalInSeconds: 1
leaseExpirationDurationInSeconds: 2
logging:
level:
com.netflix: WARN
The config server starts with no issue but issuing the following URL in the browser - http://localhost:8888/customer-service/master - returns the following response :
{"name":"customer-service","profiles":["master"],"label":null,"version":null,"state":null,"propertySources":[]}
There doesn't seem to be many examples out there of using a folder on the classpath to store configs. What am I doing wrong?
I just tried it with Spring 2.1.3 and it works as you have laid it out. Since you mentioned you are using Spring 2.2, there might have been a change or potentially a bug.
Update
Just for kicks, I tried it with 2.2.0.BUILD-SNAPSHOT and it works as well. Not sure what to say at this point.

How can I provide different database configurations with Spring Boot?

As I currently see it I have 5 possible database profiles
CI testing -> h2 mem
developer environment (could be test or app run) -> h2 mem, or h2 file, or postgres
production -> postgres (ideally credentials not stored in the git/war)
currently I have postgres configured for running the application, and h2 configured for testing via having a different application.properties in java/resources vs test/resources
what's the simplest way to have the database connection information change for these scenarios?
As M. Deinum mentions in his comment, the simplest way to do this is to use profile specific configuration.
Spring Boot allows you to have one common configuration file (application.properties) and then multiple other files, each specific to a profile (application-${profile}.properties).
For instance:
application.properties - Common configuration
application-dev.properties - Configuration for dev profile
application-ci.properties - Configuration for ci profiles
If your application runs with "ci" profile for instance, the default configuration file as well as the ci configuration file (which would contain the datasource configuration properties for ci profile) will be loaded.
To switch profiles you can use one of the following options:
JVM property: -Dspring.profiles.active=ci
Command line switch: --spring.profiles.active=dev
For unit tests you can use #ActiveProfiles("test") annotation on your test classes to tell Spring that unit tests should be run with test profile.
Also if you don't want to store production database credentials along with your source code, you can specify external configuration file when you deploy your app in production:
Using command line switch: --spring.config.location=/srv/myapp/config.properties
Using a JVM property: -Dspring.config.location=/srv/myapp/config.properties
Compact answer for the above scenario would be by creating a single application.yml file and creating different profiles based on the requirement, in your case -dev, -ci and -prod and providing the DB information accordingly.
Sample example is:
spring:
profiles.active: development
---
spring:
profiles: development
datasource:
db-person:
url: jdbc:oracle:thin:#db_person_dev
username: username
password: pwd
driver-class-name: oracle.jdbc.OracleDriver
test-on-borrow: true
validation-query: SELECT 1 FROM dual
db-contract:
url: jdbc:oracle:thin:#db_contract_dev
username: username
password: pwd
driver-class-name: oracle.jdbc.OracleDriver
test-on-borrow: true
validation-query: SELECT 1 FROM dual
---
spring:
profiles: test
datasource:
db-person:
url: jdbc:oracle:thin:#db_person_test
username: username
password: pwd
driver-class-name: oracle.jdbc.OracleDriver
test-on-borrow: true
validation-query: SELECT 1 FROM dual
db-contract:
url: jdbc:oracle:thin:#db_contract_test
username: username
password: pwd
driver-class-name: oracle.jdbc.OracleDriver
test-on-borrow: true
validation-query: SELECT 1 FROM dual
---
spring:
profiles: production
datasource:
db-person:
url: jdbc:oracle:thin:#db_person_prod
username: username
password: pwd
driver-class-name: oracle.jdbc.OracleDriver
test-on-borrow: true
validation-query: SELECT 1 FROM dual
db-contract:
url: jdbc:oracle:thin:#db_contract_prod
username: username
password: pwd
driver-class-name: oracle.jdbc.OracleDriver
test-on-borrow: true
validation-query: SELECT 1 FROM dual
---
For further understanding and simple example you can refer this link.

Categories

Resources