How to enable actuator endpoints which are set in custom starter / library? - spring-boot

I have got custom starter application (library) which has actuator and prometheus dependency
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-prometheus</artifactId>
<version>1.5.2</version>
</dependency>
in application.properties
management.endpoints.web.exposure.include=health, info, metrics, prometheus
I am using this starter in my other applications and I want to pass also this exposition of the endpoints from the starter.
Actuator and Prometheus dependecies works in applications but not show selected endpoints in starter. Ofc I can add also line with management.endpoints.web.exposure.include=health, info, metrics, prometheus to my apps but with several apps using this starter I want to pass this once for all and change endpoints only in starter if needed.
Do you have an idea how to expose those endpoints in my app which are set in starter?
Spring Boot v2.3.2 /
Maven 3.6.3

You can pass this as command line args or as ENV vars to your jar, when you start your application.
This way you can pass it to required application, as and when needed w/o updating application.properties.
It will also safeguard against exposing the actuator endpoints when not required, as actuator endpoints reveal sensitive info about the application.
eg. java -Dmanagement.endpoints.web.exposure.include=health, info, metrics, prometheus -jar myapp.jar

Related

Spring boot actuator for commandline runner application

I want to enable actuator for health check in PCF for the non web or batch or commandLineRunner spring boot application.
Could some one please share some details to resolve this issue.
Please let me know if any other details required
I tried the below options
Adding actuator dependency
Adding http and jmx related properties
Since localhost:8081/actuator is not accessible could not view endpoints
How to enable url for non web app
add
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
application.properties
spring.jmx.enabled = true
management.endpoints.jmx.exposure.include=*
management.endpoints.jmx.domain=com.example.demomail
management.endpoints.jmx.unique-names=true
run jconsole find your application name then connect.
find Tab MBeans, find com.example.demomail in left tree.
As far as I understand you need to enable the Actuator endpoints.
You must add the Spring Boot Actuator dependency (as you have already done).
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
Then in your application.properties file enable HTTP and JMX support (as you have already done).
management.endpoints.web.exposure.include=*
management.endpoint.health.show-details=always
management.endpoint.jmx.exposure.include=*
Now you will have to push your app to PCF and create a binding to a route to create access for the Actuator endpoint/s.
The URL to access the actuator endpoint will be something like this http://<route-url>/actuator/health
In the command line, I would use something like this to return a list of routes and the URL route of the application.
cf curl /v2/apps/<app-guid>/routes

spring boot refresh the properties files without config server

I want to refresh the properties files in spring boot.
version - 2.1.4.RELEASE
In case of spring config server have to connect all my app to config server which i dont want as our application is in prod and we dont want a bigger change.
Is it possible in the same application i can refresh the prop file using config server if not using config server then using some spring code
can i do it.
NOT application.properties , Have a application-optional.properties outside of project want to refresh only this.
The RefreshEndpoint is provided by Spring Cloud. Adding the spring cloud starter config will get you the required beans and API to refresh.
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
Additionally you will need to expose the API via management.endpoints.web.exposure.include="health,info,refresh" or you can use "*" to expose every enabled actuator endpoint to the web.

Prometheus java/Spring API for fetching metrics

I need to query for collected metrics in my application and the only way I found to do it is via Prometheus rest api as described here: https://prometheus.io/docs/prometheus/latest/querying/api/ Does any Spring or java api for fetching metrics from Prometheus exist, so that I wouldn't have to implement it from scratch?
If you want to collect metrics from your spring application using Prometheus, this is what you have to do:
In your application's pom.xml add this dependency:
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-prometheus</artifactId>
<version>1.7.0</version>
</dependency>
In application.properties you need to add:
management.endpoints.web.exposure.include=*
management.endpoint.health.show-details=always
And this should be all, now when you start your Spring application you can check the collected metrics at /actuator/prometheus, you'll have all the metrics grouped here.
Good luck!

Spring Cloud Bus - Refresh a specific cluster of client

I have a spring cloud config server configured over spring cloud bus over kafka.
I am using Edgware.RELEASE with boot 1.5.9.
When I send a POST request to the endpoint /bus/refresh with destination=clientId:dev:** in the body via POSTMAN to config server, all the clients get their beans refreshed even though their clientId doesn't match the value in the destination field.
Here are additional configuration details:
spring cloud config server
pom.xml
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-monitor</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-kafka</artifactId>
</dependency>
application.properties
server.port=8888
management.security.enabled=false
spring.cloud.bus.enabled=true
spring.kafka.bootstrap-servers=localhost:9092
I have two config clients with id - config-client1 and config-client2
After changing an application property in the repository for config-client2, I submit a POST request to /bus/refresh endpoint on the config server, with destination=config-client2:dev:** in the body of the request. I was hoping this would refresh/re-initialize the beans only in config-client2 application. But I noticed beans in config-client application are also refreshed/re-initialized. I also noticed config-client application receiving refresh event along with config-client2.
I was hoping only config-client2 application receives the event and its beans are the only ones that are refreshed as a result of it.
Wondering if I am missing any configuration setting to enable granular refresh at specific client level.
I did go through the document posted at - http://cloud.spring.io/spring-cloud-static/Edgware.RELEASE/single/spring-cloud.html, but the results are not as explained.
Any help with a working example is appreciated.
You can use:
curl -X POST http://localhost:8001/actuator/bus-refresh/config-client2
You also need this in your application.properties or .yml.
spring.cloud.config.uri=http://localhost:8001
spring.cloud.config.uri should point to your Spring Config Server

How to add Spring Boot Actuator to a Spring application?

In the "Bootiful" Applications with Spring Boot Spring One 2014 section(https://www.youtube.com/watch?v=HCyYEVRZISk), Josh mentioned that it is easy to add the Boot Actuator to a Spring (non-bootiful) application. I assume that an Actuator configuration is needed somehow, but can't find how it shall be done.
Just add the starter to your pom.xml:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
... and the endpoints such as /health will be available.

Resources