actuator 是springboot 下的一个包,官方对actuator 的描述是这样的
Spring Boot includes a number of additional features to help you monitor and manage your application when you push it to production. You can choose to manage and monitor your application by using HTTP endpoints or with JMX. Auditing, health, and metrics gathering can also be automatically applied to your application.
SpringBoot Actuaor 可以使用 HTTP 或者 JMX endpoints来浏览操作信息。大多数应用程序都是用 HTTP,作为 endpoint 的标识以及使用 /actuator 前缀作为 URL路径。
这里有一些常用的内置 endpoints Actuator:
- auditevents:查看 audit 事件信息
- env:查看 环境变量
- health:查看应用程序健康信息
- httptrace:展示 HTTP 路径信息
- info:展示 arbitrary 应用信息
- metrics:展示 metrics 信息
- loggers:显示并修改应用程序中日志器的配置
- mappings:展示所有 @RequestMapping 路径信息
- scheduledtasks:展示应用程序中的定时任务信息
- threaddump:执行 Thread Dump
配置流程:
一,加入依赖
如果只是使用actuator 只需要使用actuator 包就可以了,但是后续我们还要使用hystrix 的dashboard,所以在这里一起引用了。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>
二,修改服务消费者yml配置文件
配置代码:
management:
endpoints:
web:
exposure:
include: '*'
整个yml 配置文件:
server:
port: 9013
tomcat:
max-threads: 10 # 设置线程数为最大10个
spring:
application:
name: service-order-hystrix
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/springclouddemo?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC
username: root
password: root
jpa:
database: MySQL
show-sql: true
open-in-view: true
eureka:
client:
service-url:
defaultZone: http://localhost:9003/eureka/,http://localhost:9004/eureka/
instance:
prefer-ip-address: true
instance-id: ${spring.cloud.client.ip-address}:${server.port} #向注册中心中展示注册服务id
lease-expiration-duration-in-seconds: 10 #eureka client 发送心跳给server端后,续约到期时间(默认为90秒)。
lease-renewal-interval-in-seconds: 5 # 发送心跳续约间隔(每一个心跳的间隔)
#修改ribbon的负载均衡策略 服务名-ribbon-NFLoadBalancer
service-product:
ribbon:
NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule
ConnectTimeout: 250 # Ribbon的连接超时时间
ReadTimeout: 3000 # Ribbon的数据读取超时时间
OkToRetryOnAllOperations: true # 是否对所有操作都进行重试
MaxAutoRetriesNextServer: 1 # 切换实例的重试次数
MaxAutoRetries: 1 # 对当前实例的重试次数
feign:
client:
config:
service-product: # 服务提供者的服务名称
loggerLevel: FULL
hystrix:
enabled: true
management:
endpoints:
web:
exposure:
include: '*'
logging:
level:
com.zjk.order.feign.productFeignHttpClient: debug #feign的自定义接口
三,服务消费者添加注解
添加 @EnableCircuitBreaker 注解
@SpringBootApplication
@EntityScan("com.zjk.feignHystrix.entity")
@EnableEurekaClient
//通过@EnableFeignClients 激活feign
@EnableFeignClients
//激活hystrix
@EnableCircuitBreaker
public class OrdFeignHystrixApplication {
@Bean
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
}
public static void main(String[] args) {
SpringApplication.run(OrdFeignHystrixApplication.class,args);
}
}
四,验证
重启服务消费者,访问http://ip:端口/actuator/hystrix.stream,可以看到以下页面
Hystrix的监控指标非常全面,例如HystrixCommand的名称、group名称、断路器状态、错误率、错误数等。