Spring Boot with spring-cloud: gradle build fails - spring

./gradlew build fails with the error given at the bottom while running :test task. The code just checks if the context is loaded properly.
#RunWith(SpringRunner.class)
#SpringBootTest
#ContextConfiguration
public class RegistryApplicationTests {
#Test
public void contextLoads() {
}
}
The bootstrap.yml file is given below(pretty standard), I'm not sure why it is trying to load the property file from the cloud-config service, how do I work around it??
spring:
application:
name: registry
profiles:
active: default
cloud:
config:
uri: http://localhost:8888
fail-fast: true
eureka:
instance:
prefer-ip-address: true
client:
registerWithEureka: false
fetchRegistry: false
server:
waitTimeInMsWhenSyncEmpty: 0
Stack Trace
Caused by: java.lang.IllegalStateException: Could not locate PropertySource and the fail fast property is set, failing
at org.springframework.cloud.config.client.ConfigServicePropertySourceLocator.locate(ConfigServicePropertySourceLocator.java:130)
at org.springframework.cloud.bootstrap.config.PropertySourceBootstrapConfiguration.initialize(PropertySourceBootstrapConfiguration.java:89)
at
....
....
Caused by: org.springframework.web.client.ResourceAccessException: I/O error on GET request for "http://localhost:8888/registry/default": Connection refused: connect; nested exception is java.net.ConnectException: Connection refused: connect
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:666)
at
....
....
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)
at
UPDATE
As suggested by #dzatorsky tried adding #Profile("test") and #ActiveProfiles("test"),DID NOT WORK.
Tried manually adding the property file for the test using #TestPropertySource(locations = "file:src/test/resources/application-test.yml") DID NOT WORK
Finally overrode using #TestPropertySource(properties = {"spring.cloud.config.fail-fast=false"}) which worked, but it looks like a very ugly work around
The Inference is bootstrap.yml in src/main/resources overrides the properties specified anywhere else, tried renaming application-test.yml to bootstrap.yml in src/test/resources WORKED.
Is this the cleaner way getting this done?

Error on GET request for "http://localhost:8888/registry/default": Connection refused: connect; nested exception is java.net.ConnectException: Connection refused
Seems that your Spring Cloud Config server is down.
UPDATE: If you want to run your tests without running Config Server (which is a right thing to do in the most cases) then I would suggest doing the following:
Add application-test.yml with the following content:
cloud:
config:
fail-fast: false
Annotate your test class with:
#Profile("test")
In this case, whenever you run your tests they use a default parameters defined in application.yml plus those that you override in application.test.yml.

Related

Spring application.yaml and boolean values through env file

In my Spring Boot project, I have to pass to application.yaml file the SHOW_SQL boolean value from an environment file.
Here is application.yaml
spring:
...
show-sql: ${SHOW_SQL}
...
server:
port: 8080
Here is my env file (used to start the spring app)
SHOW_SQL=true
During the mvn test process I receive this error
Failed to bind properties under 'spring.jpa.show-sql' to boolean:
Property: spring.jpa.show-sql
Value: "${SHOW_SQL}"
Origin: class path resource [application.yaml] - 13:15
Reason: failed to convert java.lang.String to boolean (caused by java.lang.IllegalArgumentException: Invalid boolean value '${SHOW_SQL}')
Action:
Update your application's configuration
Is there a way to use boolean values from an .env file and pass them to the application yaml?

Setting mapped port of TestContainer in DynamicPropertySource

Given: TestContainer as an integration framework, Spring boot app, ibm mq as a broker I try to test it, cant connect with default settings, catch:
Caused by: com.ibm.mq.MQException: JMSCMQ0001: IBM MQ call failed with compcode '2' ('MQCC_FAILED') reason '2538' ('MQRC_HOST_NOT_AVAILABLE').
at com.ibm.msg.client.wmq.common.internal.Reason.createException(Reason.java:203)
... 118 common frames omitted
Caused by: com.ibm.mq.jmqi.JmqiException: CC=2;RC=2538;AMQ9204: Connection to host 'localhost(1414)' rejected. [1=com.ibm.mq.jmqi.JmqiException[CC=2;RC=2538;AMQ9204: Connection to host 'localhost/127.0.0.1:1414' rejected. [1=java.net.ConnectException[Connection refused (Connection refused)],3=localhost/127.0.0.1:1414,4=TCP,5=Socket.connect]],3=localhost(1414),4=,5=RemoteTCPConnection.bindAndConnectSocket]
Container declaration:
#Container
static GenericContainer<?> mqContainer = new GenericContainer<>(DockerImageName.parse("ibmcom/mq"))
.withEnv("LICENSE", "accept")
.withEnv("MQ_QMGR_NAME", "QM1")
.withEnv("MQ_USER_NAME","admin")
.withCommand("--volume q1data:/mnt/mqm")
.withExposedPorts(1414);```
All the settings in yml file for queue look unrelated, cause there is no any port specific stuff:
ibm:
mq:
channel: DEV.APP.SVRCONN
queue-manager: QM1
user: admin
password: passw0rd
use-i-b-m-cipher-mappings: false
user-authentication-m-q-c-s-p: false
#ssl-cipher-spec: ECDHE_RSA_AES_128_CBC_SHA256
ssl.enabled: false
I asked TC framework supporters and they advised to set host and port for spring in dynamic properties, which seems legit. The only problem I cant find correct settings to use. Any suggestion?
From the tutorial here: https://developer.ibm.com/tutorials/mq-jms-application-development-with-spring-boot/, these could be the following property:
ibm.mq.connName=localhost(1414)
So your #DynamicPropertySource could be something like:
#DynamicPropertySource
static void registerMqProperties(DynamicPropertyRegistry registry) {
registry.add("ibm.mq.connName",
() -> String.format("%s(%d)",
mqContainer.getHost(),
mqContainer.getFirstMappedPort()));
}

Spring API Gateway java.net.ConnectException: Connection timed out: no further information

I have a spring boot app with simple GET method "sayHello" i have configured API gateway to route calls to my microservice module when uri pattern matches /ms1/**
Both gateway and microservice registered to the Registry service and dashboard displays both registered
API-GATEAY YAML configuration
server:
port: '8010'
spring:
application:
name: MY-GATEWAY
cloud:
compatibility-verifier:
enabled: false
gateway:
routes:
- id: MICRO-SERVICE1
uri: lb://MICRO-SERVICE1
predicates:
- Path=/ms1/**
eureka:
client:
register-with-eureka: true
fetch-registry: true
service-uri: http://localhost:8761/eureka/
intance:
hostname: localhost
MICRO-SERVICE1 YAML configuration
server:
port: '8030'
spring:
application:
name: MICRO-SERVICE1
eureka:
client:
register-with-eureka: true
fetch-registry: true
service-uri: http://localhost:8761/eureka/
intance:
hostname: localhost
Controller's GET method
#RestController
#RequestMapping("/ms1")
public class UATController {
#GetMapping(value = "/sayHello")
public #ResponseBody ResponseEntity<String> sayHello() {
String message = "Hello World";
return new ResponseEntity<String>(message, HttpStatus.OK);
}
}
I am getting following error when i access via gateway, however when i access my microservice directly it works any idea where i am wrong here?
http://localhost:8010/ms1/sayHello <- Not working via gateway
http://localhost:8030/ms1/sayHello <- Working direct access
Wed Dec 07 23:32:31 EST 2021
[83b324d4-2] There was an unexpected error (type=Internal Server Error, status=500).
Connection timed out: no further information: DESKTOP-1.verizon.net/192.168.1.160:8030
io.netty.channel.AbstractChannel$AnnotatedConnectException: Connection timed out: no further information: DESKTOP-1.verizon.net/192.168.1.160:8030
Suppressed: The stacktrace has been enhanced by Reactor, refer to additional information below:
Error has been observed at the following site(s):
*__checkpoint ⇢ org.springframework.cloud.gateway.filter.WeightCalculatorWebFilter [DefaultWebFilterChain]
*__checkpoint ⇢ org.springframework.boot.actuate.metrics.web.reactive.server.MetricsWebFilter [DefaultWebFilterChain]
*__checkpoint ⇢ HTTP GET "/ms1/sayHello" [ExceptionHandlingWebHandler]
Original Stack Trace:
Caused by: java.net.ConnectException: Connection timed out: no further information
at java.base/sun.nio.ch.Net.pollConnect(Native Method)
at java.base/sun.nio.ch.Net.pollConnectNow(Net.java:672)
at java.base/sun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:946)
at io.netty.channel.socket.nio.NioSocketChannel.doFinishConnect(NioSocketChannel.java:330)
at io.netty.channel.nio.AbstractNioChannel$AbstractNioUnsafe.finishConnect(AbstractNioChannel.java:334)
at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:707)
at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:655)
at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:581)
at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:493)
at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:986)
at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74)
at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
at java.base/java.lang.Thread.run(Thread.java:833)
I have faced the same issue today as well when running the micro services, the gateway and the Eureka discovery all on localhost.
I was able to solve it by adding eureka.instance.preferIpAddress=true to my application.properties of the micro services. That property causes that the services will advertise their IP addresses instead of the host names. More info here under point 2.6: https://cloud.spring.io/spring-cloud-netflix/multi/multi_spring-cloud-eureka-server.html. My guess is that there was an issue with forwarding the requests to the microservices due to using localhost.
Edit: Here are my settings
There is nothing special in my setup. These are the values I am using in my application.properties
The microservice is using:
spring.cloud.discovery.enabled=true
spring.application.name=[The name of my app]
eureka.client.register.with-eureka=true
eureka.client.service-url.defaultZone=http://localhost:8999/eureka
eureka.instance.prefer-ip-address=true
The properties for the gateway are similar to the service one.
The load balancing part looks like this:
spring.cloud.gateway.routes[0].id=[Same name as my app]
spring.cloud.gateway.routes[0].uri=lb://[name of my app]
spring.cloud.gateway.routes[0].predicates=Path=/test/**
Where test/** is the part of the Rest URL of my application
Adding DESKTOP-1.verizon.net to hosts file worked, following is entry I have added to C:\Windows\System32\drivers\etc\hosts file. Unfortunately eureka.instance.prefer-ip-address=true didn't work for me
127.0.0.1 DESKTOP-1.verizon.net
have added this settings in yml file in all services and its work for me.
eureka:
instance:
prefer-ip-address: true
I just faced the same issue, and in my case, I had a problem with port number. I was using the code below to set up the port for me:
server:
port: ${random.int(8010,8013)}
I noticed when the service starts, even though the log shows "Tomcat started on port(s): 8010", on Eureka's page shows "host.docker.internal:my-service:8011"
This error happens randomly, because sometimes when you re-start the microservice, it is able to register on Eureka correctly.
You can do a simple test using a non-random port number.
I haven't figured out how to solve it for random port number.
UPDATING:
I did this and worked fine for me:
server:
port: 0
spring:
application:
name: user-service
eureka:
instance:
hostname: localhost
prefer-ip-address: true
client:
serviceUrl:
defaultZone: http://localhost:9000/eureka/

Configuring application using application.yaml instead of application.properties

I have a small Quarkus 1.1.0.Final Web application (using Java 1.8). I'm trying to use a YAML file to configure the application (instead of the usual application.properties) but there is no way the application comes up. I'm always getting this not-so-useful error message(s):
13:53:32,494 ERROR [io.qua.dev.DevModeMain] Failed to start Quarkus: java.lang.RuntimeException: io.quarkus.builder.ChainBuildException: No producers for required item class io.quarkus.deployment.builditem.BuildTimeRunTimeFixedConfigurationBuildItem
at io.quarkus.runner.RuntimeRunner.run(RuntimeRunner.java:180)
at io.quarkus.dev.DevModeMain.doStart(DevModeMain.java:177)
at io.quarkus.dev.DevModeMain.start(DevModeMain.java:95)
at io.quarkus.dev.DevModeMain.main(DevModeMain.java:66)
Caused by: io.quarkus.builder.ChainBuildException: No producers for required item class io.quarkus.deployment.builditem.BuildTimeRunTimeFixedConfigurationBuildItem
at io.quarkus.builder.BuildChainBuilder.build(BuildChainBuilder.java:240)
at io.quarkus.deployment.QuarkusAugmentor.run(QuarkusAugmentor.java:112)
at io.quarkus.runner.RuntimeRunner.run(RuntimeRunner.java:113)
... 3 more
13:53:32,519 INFO [io.qua.dev.DevModeMain] Attempting to start hot replacement endpoint to recover from previous Quarkus startup failure
13:53:32,532 ERROR [io.qua.dev.DevModeMain] Failed to start quarkus: java.lang.IllegalArgumentException: workerPoolSize must be > 0
at io.vertx.core.VertxOptions.setWorkerPoolSize(VertxOptions.java:275)
at io.quarkus.vertx.core.runtime.VertxCoreRecorder.convertToVertxOptions(VertxCoreRecorder.java:151)
at io.quarkus.vertx.core.runtime.VertxCoreRecorder.initializeWeb(VertxCoreRecorder.java:104)
at io.quarkus.vertx.http.runtime.VertxHttpRecorder.startServerAfterFailedStart(VertxHttpRecorder.java:115)
at io.quarkus.vertx.http.deployment.devmode.VertxHotReplacementSetup.handleFailedInitialStart(VertxHotReplacementSetup.java:30)
at io.quarkus.dev.RuntimeUpdatesProcessor.startupFailed(RuntimeUpdatesProcessor.java:449)
at io.quarkus.dev.DevModeMain.doStart(DevModeMain.java:191)
at io.quarkus.dev.DevModeMain.start(DevModeMain.java:95)
at io.quarkus.dev.DevModeMain.main(DevModeMain.java:66)
This is my YAML file:
#
# https://quarkus.io/guides/all-config
# https://quarkus.io/guides/config#overriding-properties-at-runtime
quarkus:
datasource:
driver: org.postgresql.Driver
flyway:
migrate-at-start: true
health:
extensions:
enabled: true
hibernate-orm:
dialect: org.hibernate.dialect.PostgreSQL10Dialect
http:
port: 8080
log: # ALL > FINEST > FINER > FINE > CONFIG > INFO > WARNING > SEVERE > OFF
console:
async: true
color: true
enable: true
format: "%d{yyyy-MM-dd HH:mm:ss,SSS} |- %-5p in %c:%L{3.} [%t] - %s%e%n"
level: WARNING
resteasy:
path: /api
smallrye-openapi:
path: /open-api
swagger-ui:
always-include: true
path: /swagger-ui
"%dev":
quarkus:
datasource:
password: postgres
url: jdbc:postgresql://localhost:5432/quarkus_web
username: postgres
flyway:
clean-at-start: true
hibernate-orm:
log:
sql: true
statistics: true
log:
category:
"io.quarkus.arc.processor":
level: OFF
"io.quarkus":
level: INFO
"org.acme":
level: CONFIG
"%prod":
quarkus:
datasource:
password: postgres
url: jdbc:postgresql://localhost:5432/quarkus_web
username: postgres
flyway:
clean-at-start: false
hibernate-orm:
database:
generation: none
sql-load-script: no-file
"%test":
quarkus:
datasource:
password: postgres
url: jdbc:postgresql://localhost:5432/quarkus_web
username: postgres
flyway:
clean-at-start: true
log:
category:
"io.quarkus":
level: WARNING
"org.acme":
level: WARNING
Anyone using this approach already and succeeded? I'm including io.quarkus:quarkus-config-yaml:1.1.0.Final.
One thing that I've noticed is that I can't separate the profiles using --- as I do with Spring Boot. I think I should file an issue for this :thinking_face:
It certainly looks like a bug, maybe even 2 as the second error message doesn't look like something I would expect considering your configuration file.
Could you create a bug in our tracker: https://github.com/quarkusio/quarkus/issues/new?assignees=&labels=bug&template=bug_report.md&title= ?
It would be nice if you could provide a reproducer. AFAICS, Quarkus doesn't start at all so probably your pom.xml with the various extensions you use and your configuration file should be enough to reproduce the issue.
Turns out this was not an issue. I just had an old Gradle config; some things changed with the Gradle plugin with version 1.1.0.Final and I just didn't have those.

Config server and eureka server in same application: tries to connect to localhost:8761

I have a spring-boot application which I use to setup a spring cloud config server and a eureka server in development and testing environments.
Strangely the application always tries to connect to localhost:8761, even though I have eureka.client.registerWithEureka set to false.
How can I deactivate this?
The error:
ERROR 3144 --- [et_localhost-12] c.n.e.cluster.ReplicationTaskProcessor : Network level connection to peer localhost; retrying after delay
com.sun.jersey.api.client.ClientHandlerException: org.apache.http.conn.ConnectTimeoutException: Connect to localhost:8761 timed out
at com.sun.jersey.client.apache4.ApacheHttpClient4Handler.handle(ApacheHttpClient4Handler.java:187) ~[jersey-apache-client4-1.19.1.jar:1.19.1]
at com.netflix.eureka.cluster.DynamicGZIPContentEncodingFilter.handle(DynamicGZIPContentEncodingFilter.java:48) ~[eureka-core-1.4.10.jar:1.4.10]
at com.netflix.discovery.EurekaIdentityHeaderFilter.handle(EurekaIdentityHeaderFilter.java:27) ~[eureka-client-1.4.10.jar:1.4.10]
at com.sun.jersey.api.client.Client.handle(Client.java:652) ~[jersey-client-1.19.1.jar:1.19.1]
at com.sun.jersey.api.client.WebResource.handle(WebResource.java:682) ~[jersey-client-1.19.1.jar:1.19.1]
at com.sun.jersey.api.client.WebResource.access$200(WebResource.java:74) ~[jersey-client-1.19.1.jar:1.19.1]
at com.sun.jersey.api.client.WebResource$Builder.post(WebResource.java:570) ~[jersey-client-1.19.1.jar:1.19.1]
at com.netflix.eureka.transport.JerseyReplicationClient.submitBatchUpdates(JerseyReplicationClient.java:116) ~[eureka-core-1.4.10.jar:1.4.10]
at com.netflix.eureka.cluster.ReplicationTaskProcessor.process(ReplicationTaskProcessor.java:71) ~[eureka-core-1.4.10.jar:1.4.10]
at com.netflix.eureka.util.batcher.TaskExecutors$BatchWorkerRunnable.run(TaskExecutors.java:187) [eureka-core-1.4.10.jar:1.4.10]
at java.lang.Thread.run(Thread.java:745) [na:1.8.0_92]
Caused by: org.apache.http.conn.ConnectTimeoutException: Connect to localhost:8761 timed out
My only class looks like this:
#EnableEurekaServer
#EnableConfigServer
#SpringBootApplication
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}
The application.yml:
server:
port: 8888
eureka:
client:
registerWithEureka: false
fetchRegistry: false
server:
waitTimeInMsWhenSyncEmpty: 0
renewal-percent-threshold: 0.49
security:
basic:
enabled: true
user:
password: mypassword
spring:
jmx:
default-domain: ${spring.application.name}
cloud:
config:
server:
git:
uri: https://example.com/myrepo.git
username: username
password: password
clone-on-start: true
search-paths: '{application},{application}/{profile}'
endpoints:
jmx:
domain: ${spring.application.name}
unique-names: true
In the bootstrap.yml I have only the application name set.
Versions:
spring-cloud-netflix-eureka: 1.1.6,
spring-cloud-config-server: 1.1.3
Could you try to change your configuration like below
eureka:
client:
registerWithEureka: false
fetchRegistry: false
service-url:
defaultZone: http://localhost:8888/eureka
You specify server.port: 8888. So your eureka is running on 8888 port. But you didn't specify any service-url for eureka. So I think that your eureka server is trying to replicate to localhost:8761 because it's default and you didn't specify service-url for eureka.
For me, below properties that worked for me.
eureka.client.registerWithEureka= false
eureka.client.fetchRegistry= false
eureka.server.maxThreadsForPeerReplication=0
Please Use 8761 port for your Eureka server as below.
server:
port: 8761
eureka:
client:
registerWithEureka: false
fetchRegistry: false
Here we’re configuring an application port – 8761 is the default one for Eureka servers. We are telling the built-in Eureka Client not to register with ‘itself’, because our application should be acting as a server.
It seems that eureka tries to connect to itself despite the below settings:
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
you have two options to fix that 1-Keep the default port of eureka 2-Add below to your application.properties of your eureka server eureka.server.maxThreadsForPeerReplication=0

Resources