Article directory
-
- 1. Reason for relationship
- 2. Introduction and simple use of Actuator
- 3. The relationship between Endpoint and Actuator
- 4. The relationship between Endpoint and HasFeatures
- 5. Analysis of Endpoint and HasFeatures principles
-
- 5.1 Implementation principle of Endpoint
- 5.2 Implementation principle of HasFeatures
- 6. Personal chat
1. Reason for relationship
We can often see the @Endpoint
annotation in Springboot
. The class marked by this annotation usually has the @XXXOperation
annotation. It can also be found in When I see implementation classes with the words Endpoint
and implementation classes with the words Features
in Springcloud
, I always feel confused when I see them for the first time. , completely unaware of the role of these classes and related frameworks, but they are very common. This time we will take a look at the specific functions of various classes with Endpoint
, HasFeatures
and NamedFeature
and how they work with Actuator
relationship.
2. Introduction and simple use of Actuator
Actuator
is developed based on the Springboot
system. Its function is to monitor the program in real time and obtain the running data of the program, such as obtaining health check, indicator collection, program bean operation status and configuration. attributes and other information. Therefore, the role of Actuator
is to expose the HTTP interface for obtaining program information. Actuator
can be extended to achieve real-time monitoring. The following is an introduction to Actuator
:
It is very simple to introduce Actuator
. You only need to configure the following maven configuration:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> <version>${version corresponding to springboot}</version> </dependency>
After introduction, configure what monitoring information needs to be loaded:
management: endpoints: web: exposure: include: '*'
The above configuration means opening all HTTP interfaces. Call the /actuator
interface to see which interfaces are open. As for what interfaces Actuator
has, what its function is and the calling path What they are will not be introduced one by one here.
3. The relationship between Endpoint and Actuator
As mentioned earlier, the main function of Actuator
is to open the HTTP interface for developers to query the running status of the program. The supported HTTP interfaces are determined by Endpoint
. Endpoint
generally refers to the @Endpoint
annotation of Springboot
, and the class name marked by this annotation will generally have Endpoint
, the annotation source code is as follows:
@Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface Endpoint {<!-- --> /** The unique name of Endpoint. This attribute is the path name of the open HTTP interface */ String id() default ""; /** Whether to open by default, if set to false, you need to manually configure it to open */ boolean enableByDefault() default true; }
So we can roughly understand the function of @Endpoint
as the @RequestMapping
annotation in SpringMVC
, which is only used to annotate classes and define HTTP The path to the interface. So when we see a class marked with @Endpoint
, we can know that this is the HTTP interface opened by Actuator
to monitor program information.
Now there is another question. Looking at the code of @Endpoint
, we know that this annotation can only be marked on the class. So when the HTTP interface I opened is called, which method should be called specifically? This involves three other annotations @ReadOperation
, @WriteOperation
and @DeleteOperation
that must be used together.
@Endpoint
is generally used with one of the previous three annotations. @Endpoint
is used to define the path of the HTTP interface, and the @XXXOperation
annotation It is used to define the calling method for receiving requests. Used together, Actuator
opens up the ability of monitoring system-level HTTP interfaces to developers. The relationship diagram is as follows:
At this point, Actuator
and @Endpoint
, @ReadOperation
, @WriteOperation
, @DeleteOperation
This concludes the relationship between the four annotations.
4. The relationship between Endpoint and HasFeatures
If we want to talk about the relationship between Endpoint
and HasFeatures
, we need to first know the relationship between Springcloud
and Actuator
. Actuator
is developed based on Springboot
, while Springcloud
extends microservices based on Springboot
Framework and functions, so Springcloud
naturally supports the use of Actuator
.
Springcloud
is oriented to microservices, so the framework is destined to be very complex. It is unrealistic to accurately grasp the technology stack used by a certain microservice without carefully looking at the system. Therefore, Springcloud
needs a mechanism that allows developers to quickly understand the technology stack and its version used by the program, and the naturally supported Actuator
is the best choice.
Springcloud
uses Actuator
to manage the externally exposed HTTP interface. On this basis, it has developed a set of HasFeatures
and NamedFeature
The function registration mechanism composed of Feature
, the framework that needs to be connected only needs to register the HasFeatures
object in the Spring
container, Springcloud
You can use the interface exposed by Actuator
to return the specific framework used by the program. The schematic diagram is as follows:
Therefore, HasFeatures
, NamedFeature
and Features
are mechanisms that Springcloud
opens to other frameworks to register themselves. Then use the @Endpoint
annotation combined with Actuator
to expose the HTTP interface, ultimately allowing developers to quickly understand the framework used by the program.
5. Analysis of Endpoint and HasFeatures principles
We divide the analysis of the principles of @Endpoint
and HasFeatures
into two major stages:
- Analyze the implementation principle of
@Endpoint
. This part is based onSpringboot
; - Based on the implementation principle of
@Endpoint
, let’s analyze the principle ofHasFeatures
. This part is a new feature ofSpringcloud
.
5.1 Implementation principle of Endpoint
Let’s first analyze the original and most basic @Endpoint
annotation implementation principle of Actuator
. We also divide this implementation principle into 4 parts:
- Search and create bean objects annotated with
@Endpoint
; - Process the method annotated with
@XXXOperation
in the bean object and parse it into aWebOperation
object; - Register the obtained
Endpoint
object information as the HTTP interface of the Servlet; - When calling, the HTTP request is parsed and called into the
WebOperation
object to complete the calling and return of the method.
5.2 Implementation principle of HasFeatures
HasFeatures
is a new mechanism introduced by Springcloud
, and its implementation is also very simple. Each framework only needs to use HasFeatures
to encapsulate the core class of the framework and register it with Spring
Container is enough. Springcloud
will obtain all HasFeatures
classes from the Spring
container and pass them to FeaturesEndpoint
, which will be transferred when calling the HTTP interface Returns the registered features as a Features
object. Generally speaking, it can be summarized into three steps:
- The framework uses
HasFeatures
to encapsulate core classes and register them in theSpring
container; - Get all
HasFeatures
from theSpring
container and pass them to theFeaturesEndpoint
bean; - When calling the HTTP interface, convert
HasFeatures
into aFeatures
object and return it.
The following figure will take the Feign framework as an example:
6. Personal chat
It is indeed relatively simple to analyze this set of implementations separately. As long as you have a little understanding of the registration implementation mechanism of the Spring
container and Servlet, you can understand the whole ins and outs clearly, so we will not analyze the source code. Too much analysis.
The Springcloud
framework provides the HasFeatures
feature registration mechanism, which is indeed a very good idea. It allows developers to know the features introduced by the system by calling the HTTP interface once. functional characteristics. However, this mechanism based on Actuator
is not convenient enough, and not everyone needs to use Actuator
to monitor the program. Companies of a certain size have their own As for the monitoring system, Actuator
is a bit useless.
Therefore, I personally think that in addition to connecting HasFeatures
to the specifications and implementation methods of Actuator
, Springcloud
can also integrate HasFeatures
The mechanism is combined with Logger
to provide a switch to directly print the introduced functional features when the system starts. This may improve a certain degree of independence and convenience and make it more widely used.