Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of self-defined Endpoint for SpringBoot Annotation

2025-01-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces the example analysis of SpringBoot annotation mode custom Endpoint, which has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, let the editor take you to understand it.

Overview

When using Spring Boot, we often use actuator, health check, / refresh in bus, etc. Here is a record of how to customize Endpoint using annotations. Can be used to meet some service status monitoring, or elegant downtime and so on.

Prepare for

Spring Boot project, add to pom:

Org.springframework.boot spring-boot-starter-actuator write custom Endpoint@Configuration@Endpoint (id = "my-endpoint") public class MyEndpoint {@ ReadOperation public Map endpoint () {Map map = new HashMap (16); map.put ("message", "this is my endpoint"); return map;}} configure management.endpoints.web.exposure.include=my-endpoint startup & test

After startup, you can see the log:

Mapped "{[/ actuator/my-endpoint], methods= [GET], produces= [application/vnd.spring-boot.actuator.v2+json | | application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.web.servlet.AbstractWebMvcEndpointHandlerMapping$OperationHandler.handle (javax.servlet.http.HttpServletRequest,java.util.Map)

Be careful

The id in @ EndPoint cannot use the hump method and needs to be segmented by-

@ Spring Boot scans the @ ReadOperation, @ WriteOperation, @ DeleteOperation annotations under the @ EndPoint annotation, corresponding to the Mapping that generates the Get/Post/Delete. There is a produces parameter in the annotation, and you can specify media type, such as: application/json, etc.

The use of Actuator for common endpoint in Spring Boot

Purpose

Monitor and manage applications

Monitoring: let's find and understand the various indicators of the running status of the program.

Management: for example, to do a shutdown function through Actuator, by accessing a specific url to operate, it is not enabled by default, and the log can be adjusted during operation.

Access mode

HTTP

JMX

By default, more Actuator will be exposed to JMX.

Dependence

Spring-boot-starter-actuator

Some commonly used Endpoint

Only health and info are accessible through http by default

Shutdown is not enabled by default.

Threaddump went to see the thread situation.

Although Prometheus is enabled by default, you must add relevant dependencies to the pom file to provide support.

How to access Actuator Endpoint

HTTP access

/ actuator/

Port and path

Management.server.address=

Actuator Endpoint published address

Management.server.port=

Port number published by Actuator Endpoint

Management.endpoints.web.base-path=/actuator

Custom Port

Management.endpoints.web.path-mapping.= diameter

Modify the access path (mapping) of the endpoint, which uses its value by default

Turn on Endpoint

Management.endpoint..enabled=true

Open an Endpoint named id

Management.endpoints.enabled-by-default=false

Endpoint enablement is selective join rather than selective exclusion. Indicates that all endpoints are disabled

Expose Endpoint

Management.endpoints.jmx.exposure.exclude=

Jmx method excludes endpoints that need to be exposed

Management.endpoints.jmx.exposure.include=*

The jmx method contains endpoints that need to be exposed

Management.endpoints.web.exposure.exclude=

Http method excludes endpoints that need to be exposed

Management.endpoints.web.exposure.include=info, health

Thank you for reading this article carefully. I hope the article "sample Analysis of SpringBoot Annotation Custom Endpoint" shared by the editor will be helpful to you. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report