Spring-Actuator is an integrated module of Spring-boot for application monitoring. It provides support for our server monitoring, allowing us to more intuitively obtain application configuration, environment variables, automated configuration reports, etc. loaded in the application.
Using Spring-Actuator
1. Introduce Maven dependencies
<!-- Monitoring and Management Module--><dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId></dependency>
After introducing dependencies, start the project.
The url we need will be printed when the project starts
2018-03-26 23:19:00.169 INFO 6148 --- [ main] osbaemvc.EndpointHandlerMapping : Mapped "{[/env/{name:.*}],methods=[GET],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EnvironmentMvcEndpoint.value(java.lang.String)
2018-03-26 23:19:00.169 INFO 6148 --- [ main] osbaemvc.EndpointHandlerMapping : Mapped "{[/env || /env.json],methods=[GET],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
2018-03-26 23:19:00.170 INFO 6148 --- [ main] osbaemvc.EndpointHandlerMapping : Mapped "{[/trace || /trace.json],methods=[GET],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
2018-03-26 23:19:00.170 INFO 6148 --- [ main] osbaemvc.EndpointHandlerMapping : Mapped "{[/dump || /dump.json],methods=[GET],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
2018-03-26 23:19:00.171 INFO 6148 --- [ main] osbaemvc.EndpointHandlerMapping : Mapped "{[/auditevents || /auditevents.json],methods=[GET],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}" onto public org.springframework.http.ResponseEntity<?> org.springframework.boot.actuate.endpoint.mvc.AuditEventsMvcEndpoint.findByPrincipalAndAfterAndType(java.lang.String,java.util.Date,java.lang.String)
2018-03-26 23:19:00.171 INFO 6148 --- [ main] osbaemvc.EndpointHandlerMapping : Mapped "{[/mappings || /mappings.json],methods=[GET],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
2018-03-26 23:19:00.172 INFO 6148 --- [ main] osbaemvc.EndpointHandlerMapping : Mapped "{[/autoconfig || /autoconfig.json],methods=[GET],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
2018-03-26 23:19:00.173 INFO 6148 --- [ main] osbaemvc.EndpointHandlerMapping : Mapped "{[/metrics/{name:.*}],methods=[GET],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.MetricsMvcEndpoint.value(java.lang.String)
2018-03-26 23:19:00.173 INFO 6148 --- [ main] osbaemvc.EndpointHandlerMapping : Mapped "{[/metrics || /metrics.json],methods=[GET],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
2018-03-26 23:19:00.173 INFO 6148 --- [ main] osbaemvc.EndpointHandlerMapping : Mapped "{[/pause || /pause.json],methods=[POST]}" onto public java.lang.Object org.springframework.cloud.endpoint.GenericPostableMvcEndpoint.invoke()
2018-03-26 23:19:00.174 INFO 6148 --- [ main] osbaemvc.EndpointHandlerMapping : Mapped "{[/service-registry/instance-status],methods=[POST]}" onto public org.springframework.http.ResponseEntity<?> org.springframework.cloud.client.serviceregistry.endpoint.ServiceRegistryEndpoint.setStatus(java.lang.String)
2018-03-26 23:19:00.174 INFO 6148 --- [ main] osbaemvc.EndpointHandlerMapping : Mapped "{[/service-registry/instance-status],methods=[GET]}" onto public org.springframework.http.ResponseEntity org.springframework.cloud.client.serviceregistry.endpoint.ServiceRegistryEndpoint.getStatus()
2018-03-26 23:19:00.175 INFO 6148 --- [ main] osbaemvc.EndpointHandlerMapping : Mapped "{[/loggers/{name:.*}],methods=[GET],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.LoggersMvcEndpoint.get(java.lang.String)
2018-03-26 23:19:00.175 INFO 6148 --- [ main] osbaemvc.EndpointHandlerMapping : Mapped "{[/loggers/{name:.*}],methods=[POST],consumes=[application/vnd.spring-boot.actuator.v1+json || application/json],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.LoggersMvcEndpoint.set(java.lang.String,java.util.Map<java.lang.String, java.lang.String>)
2018-03-26 23:19:00.175 INFO 6148 --- [ main] osbaemvc.EndpointHandlerMapping : Mapped "{[/loggers || /loggers.json],methods=[GET],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
2018-03-26 23:19:00.176 INFO 6148 --- [ main] osbaemvc.EndpointHandlerMapping : Mapped "{[/heapdump || /heapdump.json],methods=[GET],produces=[application/octet-stream]}" onto public void org.springframework.boot.actuate.endpoint.mvc.HeapdumpMvcEndpoint.invoke(boolean,javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse) throws java.io.IOException,javax.servlet.ServletException
2018-03-26 23:19:00.176 INFO 6148 --- [ main] osbaemvc.EndpointHandlerMapping : Mapped "{[/refresh || /refresh.json],methods=[POST]}" onto public java.lang.Object org.springframework.cloud.endpoint.GenericPostableMvcEndpoint.invoke()
2018-03-26 23:19:00.177 INFO 6148 --- [ main] osbaemvc.EndpointHandlerMapping : Mapped "{[/resume || /resume.json],methods=[POST]}" onto public java.lang.Object org.springframework.cloud.endpoint.GenericPostableMvcEndpoint.invoke()
2018-03-26 23:19:00.178 INFO 6148 --- [ main] osbaemvc.EndpointHandlerMapping : Mapped "{[/configprops || /configprops.json],methods=[GET],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
2018-03-26 23:19:00.178 INFO 6148 --- [ main] osbaemvc.EndpointHandlerMapping : Mapped "{[/archaius || /archaius.json],methods=[GET],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
2018-03-26 23:19:00.179 INFO 6148 --- [ main] osbaemvc.EndpointHandlerMapping : Mapped "{[/health || /health.json],methods=[GET],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.HealthMvcEndpoint.invoke(javax.servlet.http.HttpServletRequest,java.security.Principal)
2018-03-26 23:19:00.179 INFO 6148 --- [ main] osbaemvc.EndpointHandlerMapping : Mapped "{[/env],methods=[POST]}" onto public java.lang.Object org.springframework.cloud.context.environment.EnvironmentManagerMvcEndpoint.value(java.util.Map<java.lang.String, java.lang.String>)
2018-03-26 23:19:00.179 INFO 6148 --- [ main] osbaemvc.EndpointHandlerMapping : Mapped "{[/env/reset],methods=[POST]}" onto public java.util.Map<java.lang.String, java.lang.Object> org.springframework.cloud.context.environment.EnvironmentManagerMvcEndpoint.reset()
2018-03-26 23:19:00.179 INFO 6148 --- [ main] osbaemvc.EndpointHandlerMapping : Mapped "{[/features || /features.json],methods=[GET],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
2018-03-26 23:19:00.180 INFO 6148 --- [ main] osbaemvc.EndpointHandlerMapping : Mapped "{[/restart || /restart.json],methods=[POST]}" onto public java.lang.Object org.springframework.cloud.context.restart.RestartMvcEndpoint.invoke()
2018-03-26 23:19:00.181 INFO 6148 --- [ main] osbaemvc.EndpointHandlerMapping : Mapped "{[/entity || /entity.json],methods=[GET],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
2018-03-26 23:19:00.181 INFO 6148 --- [ main] osbaemvc.EndpointHandlerMapping : Mapped "{[/info || /info.json],methods=[GET],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
2018-03-26 23:19:00.193 INFO 6148 --- [ main] swsmmaRequestMappingHandlerMapping : Mapped "{[/error]}" onto public java.util.Map<java.lang.String, java.lang.Object> org.springframework.boot.actuate.endpoint.mvc.ManagementErrorEndpoint.invoke()
2. Configuration of yml configuration file
(1) Configure the port number and do not enable a certain functional module
#Develop port number, if not, it is the same as the server server.port management: port: 4321# Remove a function that does not need to view content, if health is not required, health: mail: #false means that this function is not enabled: false
The above configuration indicates that the configured port is http port 4321 and the /health function is disabled.
(2) Modify the mapping path
#Modify the path of the mapping id of a configuration endpoints: beans: id: entity
The above configuration means changing the path to create beans by /beans to /entity
(3) Configure Actuator security configuration
Spring-security is required
Introduce dependencies
<!-- for registration center access account authentication--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency>
Configure account password:
security: basic: #represents to enable account password authentication enabled: true #Configure account and password user: name: root password: 123
After configuring the account password, you can access it through http://127.0.0.1:4321, and enter the configured account and password. It is valid when the browser is not closed once.
(4) Display the display message in json format
json format display has better readability and aesthetics
#Print the packet displayed by acyuator into json format spring: jackson: serialization: indent-output: true
Add the above configuration to the application.yml file
3. Startup class configuration
@SpringBootApplication@EnableEurekaServer//This line annotation represents a registered component for a service. This annotation is only applicable to eurekapublic class EurekaServerApplication { public static void main(String[] args) { SpringApplication.run(EurekaServerApplication.class, args); }}Run without configuration
4. Some major configuration modules
| HTTP request method | path | describe |
| GET | /autoconfig | Used to obtain automated configuration information for the application |
| GET | /beans | Used to get all beans created by the application context |
| GET | /configprops | Get the attribute information report configured in the application |
| GET | /env | Used to get all available environment variable attribute reports for the application |
| GET | /mappings | Controller mapping relationship report for obtaining all SpringMvc |
| GET | /info | Used to obtain application customization information |
(1) /autoconfig
There are two master nodes under this configuration.
positiveMatches: Returns automatic configuration information for successful condition matching
negativeMatches: Returns configuration information that the condition match is unsuccessful
(2) /beans
This configuration file is used to display bean objects loaded by the ApplicationContext application context, including the following nodes:
"context" : "bootstrap", "parent" : null, "beans" : [ { "bean" : "propertySourceBootstrapConfiguration", "aliases" : [ ], "scope" : "singleton", "type" : "org.springframework.cloud.bootstrap.config.PropertySourceBootstrapConfiguration$$EnhancerBySpringCGLIB$$82fdf928", "resource" : "null", "dependencies" : [ ] }]Beans is a collection of classes loaded in the application context, which includes the following tags:
1), bean: The name of the bean created by the application context
2) scope: The scope of the bean created
singleton: singleton, is the default scope of spring
prototype: Each request will generate a new instance, which is equivalent to new operation
request: Each http request will generate a new instance, and the current bean is only valid in the same HttpRequest
session: valid in the same HttpSession
3), type: The type of bean that is used for context management
Fully qualified class name for the class
4) Resource: The specific path to the class file
5) Dependencies: The name of the dependency bean, that is, the name of the associated bean in the bean
(3) /configprops: Get the attribute information report configured in the application
There are two child nodes below
prefix: Configure prefix for attributes
properties: Represents the names and values of each property
(4) /env is used to obtain the environment variable attribute report of the application context, such as environment variables, JVM attributes, application configuration information, parameters in the command line, etc.
1) Server.ports represents the port number configured by the application
2) SystemProperties obtains jvm and other information for the application
(5) /mappings SpringMvc controller mapping report,
{ "{[/error]}" : { "bean" : "requestMappingHandlerMapping", "method" : "public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)" }}1) /error represents the mapping path of springMvc
2) /bean represents the class used for processor mapper mapping
3) /method represents the method in the mapping class
(6) Info is used to obtain custom configuration information configured in application.yml
5. Configuration of metrics
The report content provided by the metrics is dynamically changed and provides some snapshot information; such as memory usage, Http request counting, external resource indicator statistics, etc.
(1) /metrics Returns various important metrics currently applied. There are the following contents
1) System information: processors --- Number of processors
instance.uptime ------ Run time
systemload.average --system load average, etc.
2) , men.* : System memory summary information
3) , heap.* : Heap memory usage
4) , nonheap.* : Non-heap memory usage
5) threads.*: thread usage
6) classs.*: Class loading and unloading situation
7) , gc.* : Details of the garbage collector
8) , httpsession.* : The painting status of containers such as tomcat
9) , magage.*: Returns a mapping value, such as delay time, etc.
10) , conter.* : Used as a counter, the increase and decrease amount are recorded
(2) /health: Various health indicators
Some information about the memory is shown below.
"diskSpace" : { "status" : "UP", "total" : 21475880960, "free" : 8916258816, "threshold" : 10485760 }(3) /dump: used to expose thread information in the program
threadName -- the exposed thread name
threadId --exposed thread id, etc.
(4) /trace: used to track basic http information
6. Operation control class configuration
(1) /shutdowm is used to close the remote operation of the application--for post request
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.