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.
Related
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.
I'm trying to use a spring profile argument to run a jar file, the profile isn't working:
java -jar -Dspring.profiles.active="test" build/libs/moley2-0.0.1-SNAPSHOT.jar
Here is my application.yml file:
spring:
profiles: dev
server:
port:8000
---
spring:
profiles: test
server:
port:9000
The jar was created using gradle build in the projects root directory. Running the jar with the command provided starts on port 8080 so it seems like the profile isn't loading. I'm sure I'm missing something simple here as I'm new to gradle / spring. Thanks in advance for any suggestions.
The property server should be:
server:
port: 9000
Instead of:
server:
port:9000
A space is missing between the colon and the value.
Also notice that configuring the profile as:
spring:
profiles: dev
is deprecated since 2.4.0 version and should be replaced with:
spring:
config:
activate:
on-profile: dev
More info in the doc. Or a tutorial here.
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.
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.
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