Liquibase running when contextLoads test in spring boot - java

When i run my spring boot contextLoads test, the code broken becouse liquibase is trying running
Connection to localhost:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP
my pom just has the property
<dependency>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-core</artifactId>
</dependency>
how should i do to my liquibase stop running in the test context?
Thanks.

You can disable liquibase with property spring.liquibase.enabled=false.
If you are using #SpringBootTest you can add properties to it.
#SpringBootTest(properties = { "spring.liquibase.enabled=false" })

Related

Jenkins build and deployment of spring boot client microservice fails because of config-server at https://localhost:8888 not present on maven build

My goal is to deploy 1 spring-config-server application AND 1 spring-boot-client application to the same Linux server machine.
spring-boot-client application port is 8081
spring-config-server application port is 8888
In Jenkins I created 2 jobs. First job is to build and deploy the spring-config-server application and start the jar. Second job is is to build and deploy the spring-boot-client application and start the jar.
I was able to build and deploy the spring-config-server application to the Linux machine. I can see it running at localhost:8888
Problem : Build task/job for spring-boot-client application in Jenkins is failing because when maven clean install is run, I believe that it tries to locate presence of a config server running at localhost:8888. I don't know if it's because of actuator dependency I added.
In Jenkins build console, this is what I get
Caused by: org.springframework.web.client.ResourceAccessException: I/O
error on GET request for "http://localhost:8888/application/dev":
Connection refused (Connection refused); nested exception is
java.net.ConnectException: Connection refused (Connection refused) at
org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:785)
at
org.springframework.web.client.RestTemplate.execute(RestTemplate.java:711)
at
org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:602)
at
org.springframework.cloud.config.client.ConfigServerConfigDataLoader.getRemoteEnvironment(ConfigServerConfigDataLoader.java:303)
at
org.springframework.cloud.config.client.ConfigServerConfigDataLoader.doLoad(ConfigServerConfigDataLoader.java:118)
... 94 common frames omitted Caused by: java.net.ConnectException:
Connection refused (Connection refused)
In the spring-boot-client app's application.properties, the only setting I have are these :
# ANT_PATH_MATCHER is added to avoid NULL error in SwaggerConfig
spring.mvc.pathmatch.matching-strategy = ANT_PATH_MATCHER
server.port=8081
# Spring Config Server URL of Config Server Microservice :
spring.config.import=configserver:http://localhost:8888
# Environment configuration which you want to load :
spring.profiles.active=dev
In spring-boot-client's jenkins build job, this is what I have :
Now, theoretically, I believe that deploying 1 config-server and 1 client-app in the same linux machine should be possible. I just don't know how to properly configure it in Jenkins.
I have this spring cloud config client in pom.xml
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-client</artifactId>
<version>3.1.3</version>
</dependency>
I have a copy of the spring-config-server app and spring-boot-client app in my local machine. I just start/run both and it works.
I am a beginner in Jenkins and I do everything in UI. This is just a build task with scp and ssh commands executed in shell to deploy.
I'd appreciate any help or comment.
Thank you.

DB driver is not found when running in docker swarm

I have a Spring boot application running with maven. I can successfully run my app locally, but when I run an image in the local docker swarm: docker stack deploy --compose-file docker-compose.yml compose I get the following error: Caused by: java.lang.IllegalStateException: Cannot load driver class: org.postgresql.Driver
I've checked env.getPropertySources():
compose_service#debian| spring.datasource.driver-class-name=org.postgresql.Driver
compose_service#debian| spring.datasource.url=jdbc:postgresql://localhost:5432/service
compose_service#debian| spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
These props work fine with local running.
I've checked, the built jar contains the Postgres lib; maven dependency in my project:
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.3.1</version>
</dependency>
I recently ran the app with docker-compose up and it also worked, so it seems like a problem with running in swarm. Any ideas?
I shouldn't add secrets to docker swarm by echo,
which adds \n to each string (that's why my driver name wasn't valid).
Instead, I should use printf:
printf "org.postgresql.Driver" | docker secret create db-driver -
Hope it will save time for someone

FINE logging org.postgresql.jdbc.PgConnection setAutoCommit = false

I was facing the issue with Postgres Driver which was 9.1-901.jdbc4 and my database server was Postgres 10.I was getting issue in bulk update so I tried to change the driver to version 42.2.5.Following is the dependency:-
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.2.5</version>
</dependency>
Its working fine now but I am getting infinite logs and I also do have Schedulars in my code:-
2019-06-04 06:48:33,358 FINE [org.postgresql.jdbc.PgConnection] (DefaultQuartzScheduler_Worker-9) setAutoCommit = false
2019-06-04 06:48:33,359 FINE [org.postgresql.jdbc.PgConnection] (DefaultQuartzScheduler_Worker-9) setAutoCommit = true
How I disable these logs.I am using Wildfly 10 as application server.
It looks like you have debug logging turned on. The simplest way would be with the web console or CLI. An example CLI command would look like:
/subsystem=logging/logger=org.postgresql.jdbc.PgConnection:remove
Note you can use tab complete as you may not have that logger specifically added.

Sprint Boot Application Shutdown

I have Spring Boot Application which listens to JCAPS. The Connection is Durable.
When I shutdown the Application using
Curl -X POST ip:port//shutdown
Application is not shutting down completely. I can see the PID when I grep the processes. So I tried to kill using
Kill -15 PID
or
Kill -SIGTERM PID
The PID is gone, but the subscription to JCAPS topic is still Active. Hence, When I restart the Application, I am unable to connect to the same Topic using the same subscriber name.
Please help on How to properly shutdown the spring boot application.
Add the following dependency in your pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
Set following property in application.properties
management.endpoints.web.exposure.include=*
management.endpoint.shutdown.enabled=true
endpoints.shutdown.enabled=true
Then start spring-boot app
You want to shutdown app run following curl command
curl -X POST localhost:port/actuator/shutdown
Reference : https://www.baeldung.com/spring-boot-shutdown

How to one-off run #DataJpaTest against real database instead of in-memory with Spring Boot

I am using Spring Boot 1.4.3 and have a whole bunch of tests that are annotated with #DataJpaTest. By default, they run against an in-memory database. I would like to be able to run all of them against a local MySQL temporarily. How can I do this in an easy way?
I have found that I can make it work for one by adding #ActiveProfiles("local") where I have an application-local.properties that points to my local MySQL, but it is just too much work to add that everywhere, run the tests and then remove it again (since I only want to run this manually against MySQL, the CI environment will run against the in memory db).
I am using Maven if that would matter.
UPDATE:
So I have an application-local.properties which contains the db properties to connect to my local MySQL database (Which I use already to run my application against the local MySQL)
Then I right-click in IntelliJ on a package and select "Run all tests in package". In the settings of that run configuration, I add -Dspring.profiles.active=local to the "VM options" field.
I would have thought that this would activate the local profile during the tests, but it does not. If I stop the local MySQL, the tests still run fine.
In the docs it states that you are able to remove the autoconfigured h2 datasource with #AutoConfigureTestDatabase(replace= Replace.NONE) on the test class https://docs.spring.io/spring-boot/docs/1.4.2.RELEASE/reference/html/boot-features-testing.html#boot-features-testing-spring-boot-applications-testing-autoconfigured-jpa-test.
Also you then need to provide your db setup in properties, so that it does not use your app properties e.g.:
# Database
spring.datasource.url=jdbc:mariadb://localhost:3303/test
spring.datasource.username=test
spring.datasource.password=test
spring.datasource.driver-class-name=org.mariadb.jdbc.Driver
spring.jpa.hibernate.ddl-auto=none
spring.jpa.show-sql=true
i put this in application.properties in the test package
You can add the profile with the MySQL datasource properties in the same application.properties (or .yml) as:
application.yml
# Existing properties
---
spring:
profiles: h2
# More h2-related properties
---
spring:
profiles: postgres
database:
driverClassName: org.postgresql.Driver
datasource:
url: jdbc:postgresql://localhost:5432/db_dvdrental
username: user_dvdrental
password: changeit
jpa:
database: POSTGRESQL
generate-ddl: false
# More postgres-related properties
and either use #ActiveProfiles("postgres") in an integration test class or start teh container using VM argument as:
java -Dspring.profiles.active=h2 ...
Add application.yml(properties) with jdbc connection into src/test/resources
Run your JPA test with #AutoConfigureTestDatabase(replace= AutoConfigureTestDatabase.Replace.NONE) - it disables using embedded database (h2), otherwise :
org.springframework.beans.factory.BeanCreationException: Error
creating bean with name 'dataSource': Invocation of init method
failed; nested exception is java.lang.IllegalStateException: Failed to
replace DataSource with an embedded database for tests. If you want an
embedded database please put a supported one on the classpath or tune
the replace attribute of #AutoConfigureTestDatabase.

Categories