SpringBoot profiles with Gradle vs cloud - java

I have some problem with starting microservice with cloud config on local machine:
In application.yml:
spring:
application:
name: my_servie
config:
import: 'configserver:'
profiles:
group:
env-prod-pg: postgres,log
I create application-local.yml, where add:
spring:
application:
name: mc-service-logistics-v2
config:
import: 'optional:configserver:'
and some other config updates,
in build.gradle I add:
tasks.register("bootRunLocal") {
group = "application"
description = "Runs the Spring Boot application with the local profile"
doFirst {
tasks.bootRun.configure {
systemProperty("spring.profiles.active", "local")
}
}
finalizedBy("bootRun")
}
but when I try to start app with gradle bootRunLocal I caught error:
Caused by: org.springframework.web.client.ResourceAccessException: I/O error on GET request for "http://localhost:8888/my-service/local": Connection refused; nested exception is java.net.ConnectException: Connection refused
If I understand- application still want to use cloud config and can't start. If I comment
spring.config.import in application.yml- aplication started without any problem.
How to solve this problem? I don't want to push to repo my local config again)

Well, I create to application-.yml and application.yml ,where store current profile:
spring:
profiles:
active: prod

Related

Spring boot profiles and #Value

Not sure what I'm doing wrong here, but I have the following value that points to the URL of a micro service.
#Value("${url.dispenseRoot}")
private String dispenseRoot;
And I am setting up my dev profile in my application.yml file such:
---
spring:
datasource:
url: jdbc:h2:mem:test
username: sa
password: sa
driverClassName: org.h2.Driver
config:
activate:
on-profile: dev
mpesa:
host: sandbox.safaricom.co.ke
port: 443
url:
dispenseRoot: https://localhost:8844/dispense
---
Then, in accordance with my answer here: How do I activate a Spring Boot profile when running from IntelliJ?, I set up the spring boot profile on the run configuration as such:
And then run the following test to test my configuration:
#SpringBootTest
public class PaymentApplicationTests {
#Test
public void contextLoads() {
}
}
And I get:
... 111 more
Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'url.dispenseRoot' in value "${url.dispenseRoot}"
Now, if I copy and paste the following lines:
url:
dispenseRoot: https://localhost:8844/dispense
Above my profile where I'm setting up the general settings that are true for everything, it works fine. Therefore, I'm certain it has something to do with profiles, but I can't figure out what it is.
Please advise...

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.

Not Able to Connect JDBC-Hikari To my Micronaut App

Error I am Getting after running sudo ./gradlew run :
Task :run FAILED
12:03:13.440 [main] ERROR com.zaxxer.hikari.HikariConfig - Failed to load driver class com.mysql.jdbc.Driver from HikariConfig class classloader jdk.internal.loader.ClassLoaders$AppClassLoader#3d4eac69
12:03:13.445 [main] ERROR io.micronaut.runtime.Micronaut - Error starting Micronaut server: Bean definition [javax.sql.DataSource] could not be loaded: Error instantiating bean of type [javax.sql.DataSource]
How my Application.yml looks like :
micronaut:
application:
name: freshdb2
#datasources.default: {}
datasources:
default:
url: jdbc:mysql://localhost:3306/mydb;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE
username: root
password: ""
driverClassName: com.mysql.jdbc.Driver
You are missing driver, you should add dependency mysql-connector-java, for gradle add:
runtime group: 'mysql', name: 'mysql-connector-java', version: '8.0.13'
I was also getting the message:
io.micronaut.context.exceptions.BeanInstantiationException: Error instantiating bean of type [javax.sql.DataSource]
I'm not using MySQL, just trying to unpick how to use JPA from Micronaut Data Guide and others using H2.
It turns out I had incorrectly copied the datasource properties into application.yml. The above message is all you get to tell you this.
In my case the back quotes in the following had become something else.
datasources:
default:
url: ${JDBC_URL:`jdbc:h2:mem:devDb;MVCC=TRUE;LOCK_TIMEOUT=10000;DB_CLOSE_ON_EXIT=FALSE`}
username: ${JDBC_USER:sa}
password: ${JDBC_PASSWORD:""}
driverClassName: ${JDBC_DRIVER:org.h2.Driver}
dialect: ${JDBC_DIALECT:H2}

Configure spring cloud config server for each profile group

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.

spring.profiles.active=native is not working on app.yml

I keep getting the:
java.lang.IllegalStateException: You need to configure a uri for the git repository"
every time I try to run the app.
Already tried to set the spring.profiles.active=native, but it keeps not working.
spring:
profiles:
active: native
cloud:
config:
server:
native:
searchLocations: file:///${user.home}/config
Caused by: java.lang.IllegalStateException: You need to configure a uri for the git repository

Categories

Resources