Configure spring cloud config server for each profile group - java

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.

Related

Run jar with spring profile environment variable

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.

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.

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 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

Spring activated profile is being ignored - without Spring Boot

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?

Categories

Resources