How to monitor amqp/rabbitMQ connection with Spring? - spring

We have two different AMQP servers (RabbitMQ) and configured one binder for each one.
What is the best way to check the connection status to each of the amqp servers?
One way seems to be to use a AmqpTemplate with CachingConnectionFactory, where one can configure the host. But is there an easier way?
I also tried to go through List<HealthIndicator>. But it contains only one RabbitHealthIndicator when there should be two.
Our config:
spring:
cloud:
stream:
binders:
binder1:
type: rabbit
environment:
spring:
rabbitmq:
host: localhost
port: 5672
password: guest
username: guest
binder2:
type: rabbit
environment:
spring:
rabbitmq:
host: localhost
port: 56721
password: guest
username: guest

So, you have to declare one more RabbitHealthIndicator for the RabbitTemplate on the second ConnectionFactory.
http://docs.spring.io/spring-boot/docs/current/reference/html/production-ready-endpoints.html#_writing_custom_healthindicators

Related

SpringBoot Mongo config being ignore by application.yaml

Having a really annoying issue with my Spring Boot API.
So I have a MySQL db and a MongoDB which I connect to. Everything with the Mysql works fine i.e. the settings from the Application.yaml file but not Mongo. It ignores the Database name and creates its own one called "test". Even if I put in a random host and port it still connects locally. Anyone able to spot what i'm doing wrong?
server:
servlet:
contextPath: /api/v1/
port: 8080
spring:
data:
mongo.database: springboot-rest
mongo.host: localhost
mongo.port: 27017
output:
ansi:
enabled: ALWAYS
datasource:
url: jdbc:mysql://localhost:3306/spring_social?useSSL=false&serverTimezone=UTC&useLegacyDatetimeCode=false
username: test
password: test
servlet:
multipart:
max-file-size: -1
max-request-size: -1
jpa:
show-sql: true
hibernate:
ddl-auto: update
naming-strategy: org.hibernate.cfg.ImprovedNamingStrategy
properties:
hibernate:
dialect: org.hibernate.dialect.MySQL5InnoDBDialect
EDIT worth noting i also tied this way with no luck either:
spring:
data:
mongo:
database: springboot-rest
host: localhost
port: 27017
output:
ansi:
enabled: ALWAYS
datasource:
url: jdbc:mysql://localhost:3306/spring_social?useSSL=false&serverTimezone=UTC&useLegacyDatetimeCode=false
username: test
password: test
mongodb should be there instead of mongo
spring:
data:
mongodb:
database: springboot-rest

Not able to change Spring Actuator Port

My application Is running on port 8444 but I want my actuator metrics to be exposed to a different port, by default I can access them via https://localhost:8444/actuator/health but I want them on a different port like 8086
https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#production-ready-customizing-management-server-port
I followed this document and added management.server.port=8086 in my application.yml but it's just showing that site can't be reached
I tried other properties such as changing the endpoint base-path and that seems to be working fine.
I am using Spring boot 2.2.4.RELEASE and Wildfly 18,
Here is my application.yml
server:
port: 8443
servlet:
context-path: /appName
info:
app:
name: name
description: description
version: 1.0
management.endpoints.web.exposure.include: ["health", "info", "prometheus"]
management.endpoint.health.show-details: always
spring:
servlet:
multipart:
max-request-size: 100MB
datasource:
jndi-name: java:/datasource
main:
allow-bean-definition-overriding: true
management:
server:
port: 8086
endpoints:
web:
base-path: /manage

Spring Cloud Stream: Cannot connect to 2 rabbitmq clusters without allowOverride

I have a spring boot application (let's call it example-service) with the following configuration to connect to 2 different rabbitmq clusters.
spring:
cloud:
stream:
defaultBinder: rabbitA
binders:
rabbitA:
inheritEnvironment: false
defaultCandidate: false
type: rabbit
environment:
spring:
rabbitmq:
addresses: rabbitmq-a:5672
username: user-a
password: password-a
rabbitB:
inheritEnvironment: false
defaultCandidate: false
type: rabbit
environment:
spring:
rabbitmq:
addresses: rabbitmq-b:5672
username: user-b
password: password-b
bindings:
dataFromA:
destination: exchange-1
group: queue-1
binder: rabbitA
dataFromB:
destination: exchange-2
group: queue-2
binder: rabbitB
That itself works fine, it connects to both clusters. The problem is that this service is deployed in an environment where there is a spring config server with the following files:
application.yml
spring.rabbitmq:
addresses: rabbitmq-a:5672
username: user-a
password: password-a
Then that seems to override the configuration set for each binder, located under the "environment" property. So I needed to add this extra config.
example-service.yml
spring.cloud:
config:
overrideSystemProperties: false
allowOverride: true
overrideNone: false
Now the example-service connects to both rabbitmq clusters again. But I have observed certain side effects, mainly not being able to override other properties in the config server example-service.yml anymore, which is a real need for me. So I have discarded using allowOverride and its related properties.
The question is... is it possible to make it work without using allowOverride, while keeping the spring.rabbitmq.addresses/username/password in the remote config server application.yml?
Thank you very much in advance.
Kind regards.
Which version are you using? I just tested it with 3.0.6 and it works fine:
spring:
cloud:
stream:
binders:
rabbitA:
type: rabbit
inherit-environment: false
environment:
spring:
rabbitmq:
virtual-host: A
rabbitB:
type: rabbit
inherit-environment: false
environment:
spring:
rabbitmq:
virtual-host: B
bindings:
input1:
binder: rabbitA
destination: input1
group: foo
input2:
binder: rabbitB
destination: input2
group: bar
rabbitmq:
virtual-host: /
Probably not related, but your group indentation is wrong.

Eureka registration with specifc hostname / ip

I would like to register my service with the Eureka server, using a specific Hostname or IP.
This is my configuration snippet:
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka
instance:
preferIpAddress: true
hostname: localhost
ipAddr: 127.0.0.1
I am using the 2.0.2.RELEASE version of client.
I would expect the service to register with 127.0.0.1; if I switch preferIpAddress to false, I would expect it to register with localhost.
Instead, I always get this:
APPLICATION/1H1DNQ2.mshome.net:application:8080 -
Re-registering apps/APPLICATION
1H1DNQ2 is my PC id. I also tried using eureka.hostname, same result.
I was able to get it to identify with the hostname of my choice using this snippet:
spring:
cloud:
client:
hostname: localhost
I found the inspiration on this article.
I think this is sort of a workaround though and I am still looking for a more specific solution.

Spring-Cloud-Config not getting properties

I cannot Create properties as I get this error:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'configClient': Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'user.role' in value "${user.role}"
I am following this tutorial:
http://www.baeldung.com/spring-cloud-configuration
I am using
#Value("${user.role}")
and
#Value("${user.role:}")
does not fetch any information.
I have the same issue that you when I tried the tutorial. It seems the client can't achieve the server to resolve the property 'user.role', in my case, the error was the wrong property:
spring.application.name: config-client
You can check the configurations of the server, If you can access your github repo with configs. With server running, like this:
curl http://root:s3cr3t#localhost:9090/config-client/development/master [13:26:43]
{"name":"config-client","profiles":["development"],"label":"master","version":"80d048de5faa3314429a1fce1645917786da28d6","state":null,"propertySources":[{"name":"https://gitlab.com/marcosnasp/spring-config-baeldung-tutorial.git/config-client-development.properties","source":{"user.role":"Developer"}}]}%
I used the the yml config for both, and a different port for the server 9090, the client port by default, since I have not configured in application.properties is the 8080, client and server, at looks like:
the client config:
bootstrap.yml
spring:
application:
name: config-client
profiles:
active: development
cloud:
config:
uri: http://localhost:9090
username: root
password: s3cr3t
fail-fast: true
the server config:
bootstrap.yml
spring:
application:
name: delivery-config-server
encrypt:
key-store:
location: classpath:/config-server.jks
password: my-s70r3-s3cr3t
alias: config-server-key
secret: my-k34-s3cr3t
application.yml:
server:
port: 9090
spring:
cloud:
config:
server:
git:
uri: https://gitlab.com/marcosnasp/spring-config-baeldung-tutorial.git
timeout: 10
clone-on-start: true
security:
user:
name: root
password: s3cr3t

Resources