0

I have a set of services that use spring-cloud, connect to a config server, and register to a eureka server.

I created a docker-compose.yml document in order to pull up all the containers in a simple way.

so far so good

There was a problem: the configuration server was not available when the other services were brought up.

So I added spring-retry to make several retries. And it worked fine and i want to keep it.

config:

spring.cloud.config.fail-fast=true
spring.cloud.config.retry.initial-interval=4500
spring.cloud.config.retry.max-attempts=12
spring.cloud.config.retry.max-interval=9000
spring.cloud.config.retry.multiplier=1.9

The problem now is that when building the .jar with "mvn package" the tests fail, since it retries to connect to the services and cannot find them:

Caused by: java.net.UnknownHostException: conf-server
        at java.base/java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:229)
        at java.base/java.net.Socket.connect(Socket.java:609)
        at java.base/sun.net.NetworkClient.doConnect(NetworkClient.java:177)
        at java.base/sun.net.www.http.HttpClient.openServer(HttpClient.java:508)
        at java.base/sun.net.www.http.HttpClient.openServer(HttpClient.java:603)
        at java.base/sun.net.www.http.HttpClient.<init>(HttpClient.java:276)
        at java.base/sun.net.www.http.HttpClient.New(HttpClient.java:375)
        at java.base/sun.net.www.http.HttpClient.New(HttpClient.java:396)
        at java.base/sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:1253)
        at java.base/sun.net.www.protocol.http.HttpURLConnection.plainConnect0(HttpURLConnection.java:1187)
        at java.base/sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:1081)
        at java.base/sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:1015)
        at org.springframework.http.client.SimpleBufferingClientHttpRequest.executeInternal(SimpleBufferingClientHttpRequest.java:76)
        at org.springframework.http.client.AbstractBufferingClientHttpRequest.executeInternal(AbstractBufferingClientHttpRequest.java:48)
        at org.springframework.http.client.AbstractClientHttpRequest.execute(AbstractClientHttpRequest.java:66)
        at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:776)
        ... 102 common frames omitted

[ERROR] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 170.46 s <<< FAILURE! - in com.px.labs.activityservice.ActivityServiceApplicationTests
[ERROR] contextLoads  Time elapsed: 0.002 s  <<< ERROR!
java.lang.IllegalStateException: Failed to load ApplicationContext
Caused by: org.springframework.cloud.config.client.ConfigClientFailFastException: Could not locate PropertySource and the fail fast property is set, failing
Caused by: org.springframework.web.client.ResourceAccessException: I/O error on GET request for "http://conf-server:8888/service-activities/default": conf-server; nested exception is java.net.UnknownHostException: conf-server
Caused by: java.net.UnknownHostException: conf-server

09:04:42,797 |-WARN in net.logstash.logback.appender.LogstashTcpSocketAppender[logstash] - Log destination logstash:5044: connection failed. java.net.UnknownHostException: logstash
        at java.net.UnknownHostException: logstash
        at      at java.base/java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:229)
        at      at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
        at      at java.base/java.net.Socket.connect(Socket.java:609)
        at      at net.logstash.logback.appender.AbstractLogstashTcpSocketAppender$TcpSendingEventHandler.openSocket(AbstractLogstashTcpSocketAppender.java:762)
        at      at net.logstash.logback.appender.AbstractLogstashTcpSocketAppender$TcpSendingEventHandler.onStart(AbstractLogstashTcpSocketAppender.java:681)
        at      at net.logstash.logback.appender.AsyncDisruptorAppender$EventClearingEventHandler.onStart(AsyncDisruptorAppender.java:382)
        at      at net.logstash.logback.encoder.com.lmax.disruptor.BatchEventProcessor.notifyStart(BatchEventProcessor.java:224)
        at      at net.logstash.logback.encoder.com.lmax.disruptor.BatchEventProcessor.run(BatchEventProcessor.java:120)
        at      at java.base/java.lang.Thread.run(Thread.java:829)
09:04:42,799 |-WARN in net.logstash.logback.appender.LogstashTcpSocketAppender[logstash] - Log destination logstash:5044: Waiting 29994ms before attempting reconnection.
[INFO] 
[INFO] Results:
[INFO] 
[ERROR] Errors: 
[ERROR]   ActivityServiceApplicationTests.contextLoads ยป IllegalState Failed to load App...
[INFO] 
[ERROR] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0
[INFO] 
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  03:27 min
[INFO] Finished at: 2022-09-02T09:04:56-03:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.22.2:test (default-test) on project activity-service: There are test failures.

I tried to skip the tests with "mvn package -Dmaven.test.skip" and everything works fine. But I don't think it's a good practice.

The question is: How can you use spring-retry and pass the tests?

Federremu
  • 84
  • 1
  • 7

0 Answers0