Spring rest controller in management port? - spring-boot

When using spring-boot, how to you expose a controller at management port, the port defined by management.port?

In Spring Boot 2.x it is done using #RestControllerEndpoint annotation. For example if you define following properties in application.yml:
management:
server:
port: 8081
endpoints:
web:
base-path: "/actuator" # default value
and some controller as #RestControllerEndpoint(id="debug"), it will be mapped as
http://localhost:8081/actuator/debug/...
More info:
https://www.javadevjournal.com/spring-boot/spring-boot-actuator-custom-endpoint/

Okay, I ended up implementing
org.springframework.boot.actuate.endpoint.mvc.MvcEndpoint
This exposes the Controller into management port rather than application port.

Related

Actuator endpoints not exposed via HTTP in Spring boot 1.5.2 version in same port as application is running

I am using springboot version 1.5.2.RELEASE and i tried to implement spring boot actuator in my application. I am running my application in port 8081 . I just want to expose two end points i.e health and info. When i try to access end points in port 8081 same as my application, it does not gives the result and shows 404 not found status in Postman. I can access the health end point in port 8089 as http://localhost:8089/admin/info. Same thing i could not achieve in port 8081. I have searched for the answer and could not find proper answer for spring boot version 1.5.2.
My application.yml file looks like this:
management:
context-path : /admin
endpoints:
web:
exposure:
include: health,info
jmx:
exposure:
exclude: '*'
security:
enabled: true
port: 8081
If you have your management.port set to 8089, then you will only be able to access the management APIs at that port. You will not be able to reach them at the application port 8081
If you would like to reach the management APIs at the application port, do not set the managment.port value. It defaults to the same port as the application port.

How to test application yml resource file settings that uses Spring Boot?

For example, I have settings like code below which Spring Boot Actuator uses.
management:
server:
port: 60001
security:
enabled: false
And I found out that if I will write this setting with mistakes. For example (code below):
management:
server:
port: 60001
security:
enabled: fase # wrong typed a `false` word
Spring Boot will not do anything, and the application will have build successfully.
Spring boot will validate the data as long as #ConfigurationProperties exist in core. The problem is that your #ConfigurationProperties may not exist before or already deprecated in your spring boot version.
Below is the comparison of management.server class with #ConfigurationProperties.
Try to change the port with string and spring boot with validate the data.
Look also at ManagementContextAutoConfiguration.class if your using spring boot 2 you will see that it loads the class ManagementServerProperties and WebEndpointProperties
If you still needed to validate your yaml you can refer to SnakeYAML.

Spring boot admin with Eureka client and custom context path fails on health status

I have configured my spring boot application monitor-client to register to Eureka. I have a separate spring boot admin (SBA) application monitor that monitors all applications registered to Eureka.
If the context-path is not set in my application, everything is working fine. However if the context-path is set, SBA does not show correct information anymore. From the documentation it seems that I need to update the metadata properties of Eureka which I have done.
My monitor application is configured as follows:
application.properties:
spring.application.name=monitor-client
server.servlet.context-path=/monitor-client
server.port=9876
# Monitoring config
management.endpoints.web.exposure.include=*
eureka.instance.metadataMap.management.context-path=/monitor-client/actuator
My application is showing as 'offline' but I can browse all the details in the 'insight' tab. I suppose SBA correctly access the actuator endpoints but incorrectly uses the /health endpoint and I do not understand how that is possible?
If I go to the administration interface of SBA, I notice that the wrench endpoint and the health endpoint are not the same:
localhost:9876/monitor-client/actuator (wrench)
localhost:9876/actuator/health (health)
I've fiddled around with some settings but between the managment settings, the actuator settings and the eureka settings, I'm not sure which one is to be configured for this to work.
I've tried:
management.endpoints.web.base-path
eureka.instance.health-check-url-path
I'm currently using spring boot 2.1.2.RELEASE and matching version of SBA
Can you try with these configuration
server:
servlet:
context-path: /monitor-client
#management config
eureka:
instance:
health-check-url-path: /actuator/health
metadata-map:
management.context-path: /monitor-client/actuator
client:
serviceUrl:
defaultZone: ${EUREKA_SERVICE_URL:http://localhost:8761}/eureka/
management:
endpoint:
health:
show-details: always
endpoints:
web:
base-path: /actuator
exposure:
include: "*"

Auto-configure routes with Zuul and Eureka

Through reading various books / tutorials, it appears that it is possible to auto-configure routes in Zuul when using it in combination with Eureka service discovery. That means that I don't have to explicitly add routes to Zuul's application.properties.
So I understand this correctly? Or do I still need to add routes explicitly to Zuul in order it to work as a gateway?
I would like it to automatically create routes from the application name's that are registered with Eureka. Is this possible?
(Note: I have actually tried this, but when I go to http://localhost:8762/routes I just get an error page.)
Sure. In most microservices implementations, internal microservices endpoints are not exposed outside. A set of public services will be exposed to the clients using an API gateway.
The zuul proxy internally uses the Eureka Server for service discovery.
I would like it to automatically create routes from the application name's that are registered with Eureka. Is this possible?
Sure. I will show you gateway example.
1. Create your service project (user-service)
create application.properties file
# --- Spring Config
spring:
application:
name: OVND-USER-SERVICE
# Eureka client
eureka:
client:
serviceUrl:
defaultZone: ${EUREKA_URL:http://localhost:8761/eureka/}
2. Setting up Zuul project (Gateway-service)
1.#EnableZuulproxy to tell Spring Boot that this is a Zuul proxy
#SpringBootApplication
#EnableZuulProxy
#EnableDiscoveryClient
public class GatewayServiceApplication {
2.create an application.properties file
# =======================================
# Gateway-service Server Configuration
# =======================================
# --- Spring Config
spring:
application:
name: gateway-service
server:
port: ${PORT:8080}
# Eureka client
eureka:
client:
serviceUrl:
defaultZone: ${EUREKA_URL:http://localhost:8761/eureka/}
zuul:
host:
routes:
## By default, all requests to user service for example will start with: "/user/"
## What will be sent to the user service is what comes after the path defined,
## So, if request is "/user/v1/user/tedkim", user service will get "/v1/user/tedkim".
user-service:
path: /user/**
service-id: OVND-USER-SERVICE
another-service:
path: /another/**
service-id: OVND-ANOTHER-SERVICE
Eureka website ( localhost:8761 )
Yes. You can integrate Zuul with Eureka and configure the routes based on application names registered in Eureka. Just add the following configuration to Zuul application:
zuul:
ignoredServices: "*"
routes:
a-service: /a-service/**
b-service: /b-service/**
c-service: /c-service/**
d-service: /d-service/**

Can Spring set datasource from service discovery?

Before I start trying to programmatically set datasource configuration in my app using service discovery, I would like to be sure this functionality is not already managed by Spring.
In my case I am using Consul as the discovery service. So I read http://cloud.spring.io/spring-cloud-consul/1.0.x/index.html but don't really find something to answer my question (prolly because of my flawed knowledge of Spring configuration itself).
Basically I would configure the discovery system in a bootstrap.yml file
spring:
cloud:
consul:
host: localhost
port: 8500
then in the application.yml I would set something like:
spring:
datasource:
url: jdbc:${consul.service.datasource}
Is this possible "natively". If not, what do I miss in the concepts involved?

Resources