SpringCloud - 06熔断器Hystrix
2021-03-06 17:26
标签:假设 and pid col 条件 就是 names star pos 当服务端Provider已经down后, 由于做了服务降级处理,让客户端在服务端不可用时也会获取提示信息而不会挂起耗死服务器。 服务熔断: 当某个异常条件被触发,直接熔断整个服务,而不是一直等到此服务超时。 服务降级: 从整体负荷考虑,当某个服务熔断之后,服务端不能再被调用。此时客户端自己准备一个本地的fallback回调,返回一个缺省值。 2021-01-08 13:40:20.941 WARN 8136 --- [nio-9003-exec-3] ashboardConfiguration$ProxyStreamServlet : Origin parameter: http://localhost:8004/actuator/hystrix.stream is not in the allowed list of proxy host names. If it should be allowed add it to hystrix.dashboard.proxyStreamAllowList.
Delay: 用来控制服务器上轮询监控信息的延迟时间,默认为2000毫秒,通过配置该属性来降低客户端的网络和CPU消耗。 Title: 对应头部标题Hystrix Stream之后的内容,默认会使用具体监控实例的URL,通过配置该属性来展示更合适的标题。 实心圆: 有两种含义。 通过颜色的变化代表了实例的健康程度,健康度从绿色 实心圆大小也会根据实例的请求流量发生变化, 流量越大该实心圆就越大。通过实心圆可以在大量实例中快速发现故障实例和高压力实例。 SpringCloud - 06熔断器Hystrix 标签:假设 and pid col 条件 就是 names star pos 原文地址:https://www.cnblogs.com/kingdomer/p/14250772.htmlSpringCloud - 06熔断器Hystrix
(1)服务熔断介绍
(1.1)服务雪崩
(1.2)服务熔断
(2) 改造 microservicecloud-dept-provider
(2.1)修改POM文件
dependency>
groupId>org.springframework.cloudgroupId>
artifactId>spring-cloud-starter-netflix-hystrixartifactId>
dependency>
(2.2)修改application.yml
eureka:
client:
service-url:
defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka,http://eureka7003.com:7003/eureka
instance:
instance-id: microservice-dept-provider-hystrix
prefer-ip-address: true
(2.3)修改Controller
@RestController
@RequestMapping("/dept")
public class DeptController {
@RequestMapping(value = "/get/{id}", method = RequestMethod.GET)
@HystrixCommand(fallbackMethod = "processHystrix_Get")
public Dept get(@PathVariable("id") Long id){
Dept dept = deptService.get(id);
if(null == dept){
throw new RuntimeException("该ID:"+ id +"没有对应的信息");
}
return dept;
}
// 一旦调用服务方法失败并抛出了错误消息后,会自动调用@HystrixCommand标注好的fallbackMethod调用类中的指定方法
public Dept processHystrix_Get(@PathVariable("id") Long id){
Dept dept = new Dept();
dept.setDeptno(id);
dept.setDname("该ID: "+ id +"没有对应的信息,null-HystrixCommand");
dept.setDb_source("no this database in MySQL");
return dept;
}
}
(2.4)主启动类,新增@EnableCircuitBreaker
@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
@EnableCircuitBreaker
public class DeptProviderHystrixApplication {}
(2.5)测试: 启动Eureka集群, 再启动服务提供者Provider_Hystrix, 然后启动消费者
(3)服务降级
(4)部署构建服务降级
(4.1)修改microservicecloud-api,新增FallbackFactory类
@Component
public class DeptClientServiceFallbackFactory implements FallbackFactoryDeptClientService> {
@Override
public DeptClientService create(Throwable throwable) {return new DeptClientService() {
@Override
public Dept get(Long id) {
Dept dept = new Dept();
dept.setDeptno(id);
dept.setDname("该ID: "+ id +"没有对应的信息,Consumer 客户端提供的降级服务,此刻服务Provider已经关闭");
dept.setDb_source("no this database in MySQL");
return dept;
}
@Override
public List
(4.2)修改接口
//@FeignClient(value = "MICROSERVICECLOUD-DEPT-PROVIDER")
@FeignClient(value = "MICROSERVICECLOUD-DEPT-PROVIDER",fallbackFactory = DeptClientServiceFallbackFactory.class)
public interface DeptClientService { }
(4.3)重新打包api模块 mvn clean install
(4.4)修改消费者配置文件microservicecloud-dept-consumer-feign
feign: // 新增配置
hystrix:
enabled: true
(4.5)测试: 先启动Eureka集群,再启动服务提供者,然后启动服务消费者; 然后关闭服务提供者。
(4.6)总结
(5)调用监控
(6)构建Hystrix-Dashboard
(6.1)POM文件
dependency>
groupId>org.springframework.bootgroupId>
artifactId>spring-boot-starter-actuatorartifactId>
dependency>
dependency>
groupId>org.springframework.cloudgroupId>
artifactId>spring-cloud-starter-netflix-hystrixartifactId>
dependency>
dependency>
groupId>org.springframework.cloudgroupId>
artifactId>spring-cloud-starter-netflix-hystrix-dashboardartifactId>
dependency>
(6.2)application.yml配置文件
server:
port: 9003
hystrix:
dashboard:
proxy-stream-allow-list: localhost
(6.3)主启动类
@SpringBootApplication
@EnableHystrixDashboard
@EnableCircuitBreaker
public class DeptConsumerDashboardApplication { }
(6.4)服务提供者Provider 添加监控依赖
dependency>
groupId>org.springframework.bootgroupId>
artifactId>spring-boot-starter-actuatorartifactId>
dependency>
dependency>
groupId>org.springframework.cloudgroupId>
artifactId>spring-cloud-starter-netflix-hystrixartifactId>
dependency>
(6.5)服务提供者Provider修改application.yml配置文件
management:
endpoints:
web:
exposure:
include: ‘*‘
(6.6)测试
(6.7)监控数据解释
文章标题:SpringCloud - 06熔断器Hystrix
文章链接:http://soscw.com/index.php/essay/60949.html