1、服务雪崩效应
2、服务熔断服务降级
3、Hystrix默认超时时间设置
4、Hystrix服务监控Dashboard
服务雪崩效应
定义:
服务雪崩效应是一种因服务提供者的不可用导致服务调用者的不可用,并将不可用逐渐放大的过程.
如果所示:
A为服务提供者, B为A的服务调用者, C和D是B的服务调用者. 当A的不可用,引起B的不可用,并将不可用逐渐放大C和D时, 服务雪崩就形成了.
形成原因
服务雪崩的过程可以分为三个阶段:
- 服务提供者不可用;
- 重试加大请求流量;
- 服务调用者不可用;
服务雪崩的每个阶段都可能由不同的原因造成,总结如下:
Hystrix的引入,可以通过服务熔断和服务降级来解决这个问题
服务熔断服务降级
Hystrix断路器简介:
在一个分布式系统里,许多依赖不可避免的会调用失败,比如超时、异常等,如何能够保证在一个依赖出问题的情况下,不会导致整体服务失败,这个就是Hystrix需要做的事情。Hystrix提供了熔断、隔离、Fallback、cache、监控等功能,能够在一个、或多个依赖同时出现问题时保证系统依然可用。
Hystrix服务熔断服务降级@HystrixCommand fallbackMethod
熔断机制是应对雪崩效应的一种微服务链路保护机制。
当某个服务不可用或者响应时间超时,会进行服务降级,进而熔断该节点的服务调用,快速返回自定义的错误影响页面信息。
我们写一个新的带服务熔断的服务提供者项目 microservice-student-provider-hystrix-1004
把配置和代码都复制一份到这个项目里;
然后修改;
1,pom.xml加下 hystrix支持
<!--Hystrix相关依赖--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-hystrix</artifactId> </dependency>
2,application.yml修改下端口和实例名称
server: port: 1004 context-path: / spring: datasource: type: com.alibaba.druid.pool.DruidDataSource driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://localhost:3306/mybatis_ssm?useUnicode=true&characterEncoding=utf8 username: mybatis_ssm password: 123 jpa: hibernate: ddl-auto: update show-sql: true application: name: microservice-student profiles: provider-hystrix-1004 eureka: instance: hostname: localhost appname: microservice-student instance-id: microservice-student:1004 prefer-ip-address: true client: service-url: defaultZone: http://eureka2001.psy.com:2001/eureka/,http://eureka2002.psy.com:2002/eureka/,http://eureka2003.psy.com:2003/eureka/ info: groupId: com.psy.springcloud artifactId: microservice-student-provider-hystrix-1004 version: 1.0-SNAPSHOT userName: http://psy.com phone: 123456
3,启动类StudentProviderHystrixApplication_1004加下注解支持 @EnableCircuitBreaker
package com.psy.microservicestudentproviderhystrix1004; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @EnableCircuitBreaker @EntityScan("com.psy.*.*") @EnableEurekaClient @SpringBootApplication public class MicroserviceStudentProviderHystrix1004Application { public static void main(String[] args) { SpringApplication.run(MicroserviceStudentProviderHystrix1004Application.class, args); } }
4,服务提供者1004中controller新增
package com.psy.microservicestudentproviderhystrix1004.controller; import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand; import com.psy.microservicestudentproviderhystrix1004.service.StudentService; import cpm.psy.microservicecommon.entity.Student; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.*; import java.util.HashMap; import java.util.List; import java.util.Map; @RestController @RequestMapping("/student") public class StudentProviderController { @Autowired private StudentService studentService; @Value("${server.port}") private String port; @PostMapping(value="/save") public boolean save(Student student){ try{ studentService.save(student); return true; }catch(Exception e){ return false; } } @GetMapping(value="/list") public List<Student> list(){ return studentService.list(); } @GetMapping(value="/get/{id}") public Student get(@PathVariable("id") Integer id){ return studentService.findById(id); } @GetMapping(value="/delete/{id}") public boolean delete(@PathVariable("id") Integer id){ try{ studentService.delete(id); return true; }catch(Exception e){ return false; } } @RequestMapping("/ribbon") public String ribbon(){ return "工号【"+port+"】正在为您服务"; } /** * 测试Hystrix服务降级 * @return * @throws InterruptedException */ @ResponseBody @GetMapping(value="/hystrix") @HystrixCommand(fallbackMethod="hystrixFallback") public Map<String,Object> hystrix() throws InterruptedException{ // Thread.sleep(2000); Map<String,Object> map=new HashMap<String,Object>(); map.put("code", 200); map.put("info","工号【"+port+"】正在为您服务"); return map; } public Map<String,Object> hystrixFallback() throws InterruptedException{ Map<String,Object> map=new HashMap<String,Object>(); map.put("code", 500); map.put("info", "系统【"+port+"】繁忙,稍后重试"); return map; } }
microservice-student-consumer-80项目也要对应的加个方法
/** * 测试Hystrix服务降级 * @return */ @GetMapping(value="/hystrix") @ResponseBody public Map<String,Object> hystrix(){ return restTemplate.getForObject(SERVER_IP_PORT+"/student/hystrix/", Map.class); }
因为 Hystrix默认1算超时,所有 sleep了2秒 所以进入自定义fallback方法,防止服务雪崩;
我们这里改sleep修改成100毫秒;
Hystrix默认超时时间设置
Hystrix默认超时时间是1秒,我们可以通过hystrix源码看到,
找到 hystrix-core.jar com.netflix.hystrix包下的HystrixCommandProperties类
default_executionTimeoutInMilliseconds属性局势默认的超时时间
默认1000毫秒 1秒
我们系统里假如要自定义设置hystrix的默认时间的话;
application.yml配置文件加上
hystrix: command: default: execution: isolation: thread: timeoutInMilliseconds: 3000
Hystrix服务监控Dashboard
Hystrix服务监控Dashboard仪表盘
Hystrix提供了 准实时的服务调用监控项目Dashboard,能够实时记录通过Hystrix发起的请求执行情况,
可以通过图表的形式展现给用户看。
我们新建项目:microservice-student-consumer-hystrix-dashboard-90
加依赖:
<!--Hystrix服务监控Dashboard依赖--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-hystrix</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-hystrix-dashboard</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
application.yml配置
server: port: 90 context-path: /
新建启动类:StudentConsumerDashBoardApplication_90
加注解:@EnableHystrixDashboard
package com.psy.microservicestudentconsumerhystrixdashboard90; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @EnableHystrixDashboard @SpringBootApplication(exclude={DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class}) public class MicroserviceStudentConsumerHystrixDashboard90Application { public static void main(String[] args) { SpringApplication.run(MicroserviceStudentConsumerHystrixDashboard90Application.class, args); } }
启动
出现这个 就说明OK;
然后我们来测试下;
我们启动三个eureka,然后再启动microservice-student-provider-hystrix-1004
我们直接请求http://localhost:1004/student/hystrix
返回正常业务
我们监控的话,http://localhost:1004/hystrix.stream 这个路径即可;
一直是ping,然后data返回数据;
用图形化的话