1、概念:Hystrix 熔断机制
2、具体内容
所谓的熔断机制和日常生活中见到电路保险丝是非常相似的,当出现了问题之后,保险丝会自动烧断,以保护我们的电器, 那么如果换到了程序之中呢?
当现在服务的提供方出现了问题之后整个的程序将出现错误的信息显示,而这个时候如果不想出现这样的错误信息,而希望替换为一个错误时的内容。
一个服务挂了后续的服务跟着不能用了,这就是雪崩效应
对于熔断技术的实现需要考虑以下几种情况:
· 出现错误之后可以 fallback 错误的处理信息;
· 如果要结合 Feign 一起使用的时候还需要在 Feign(客户端)进行熔断的配置。
服务端 POM添加依赖:
org.springframework.cloud
spring-cloud-starter-hystrix
org.springframework.cloud
spring-cloud-starter-eureka
com.atguigu.springcloud
microservicecloud-api
${project.version}
org.springframework.cloud
spring-cloud-starter-config
org.springframework.boot
spring-boot-starter-actuator
junit
junit
mysql
mysql-connector-java
com.alibaba
druid
ch.qos.logback
logback-core
org.mybatis.spring.boot
mybatis-spring-boot-starter
org.springframework.boot
spring-boot-starter-jetty
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
org.springframework
springloaded
org.springframework.boot
spring-boot-devtools
application.yml和之前一样没有需要修改的:
server:
port: 8001
mybatis:
config-location: classpath:mybatis/mybatis.cfg.xml # mybatis配置文件所在路径
type-aliases-package: com.atguigu.springcloud.entities # 所有Entity别名类所在包
mapper-locations:
- classpath:mybatis/mapper/**/*.xml # mapper映射文件
spring:
application:
name: microservicecloud-dept
datasource:
type: com.alibaba.druid.pool.DruidDataSource # 当前数据源操作类型
driver-class-name: org.gjt.mm.mysql.Driver # mysql驱动包
url: jdbc:mysql://localhost:3306/cloudDB01 # 数据库名称
username: root
password: 123456
dbcp2:
min-idle: 5 # 数据库连接池的最小维持连接数
initial-size: 5 # 初始化连接数
max-total: 5 # 最大连接数
max-wait-millis: 200 # 等待连接获取的最大超时时间
eureka:
client:
service-url:
#defaultZone: http://localhost:7001/eureka
defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka,http://eureka7003.com:7003/eureka
instance:
instance-id: microservicecloud-dept8001-hystrix #注册中心显示名称
prefer-ip-address: true #访问路径显示ip地址
info:
app.name: atguigu-microservicecloud
company.name: www.atguigu.com
bulid.artifactId: $project.artifactId$
bulid.version: $project.version$
第一种使用方法,服务端 对单个方法进行Hyxtrix(服务熔断):
@RequestMapping(value = "/dept/get/{id}", method = RequestMethod.GET)
@HystrixCommand(fallbackMethod = "processHystrix_Get")
public Dept get(@PathVariable("id") Long id) {
Dept dept = service.get(id);
if (dept == null)
throw new RuntimeException("该ID:" + id + "没有对应的信息");
return dept;
}
public Dept processHystrix_Get(@PathVariable("id") Long id) {
// return new Dept().setDeptno(id).setDname("该ID:" + id +
// "没有没有对应的信息,null--@HystrixCommand").setDb_source("no this database in
// MySQL");
return new Dept(id, "该ID:" + id + "没有没有对应的信息,null--@HystrixCommand", "no this database in MySQL");
}
主要是@HystrixCommand(fallbackMethod = "processHystrix_Get") fallbackMethod 里面写对应的出现服务异常时候处理的方法
第二种使用方法,对整个接口类使用(服务降级):
API工程添加Hyxtrix fallBack类 :
@Component
public class DeptClientServiceFallbackFactory implements FallbackFactory {
@Override
public DeptClientService create(Throwable cause) {
return new DeptClientService() {
@Override
public Dept get(long id) {
return new Dept(id, "该ID:" + id + "没有没有对应的信息,Consumer客户端提供的降级信息,此刻服务Provider已经关闭", "no this database in MySQL");
}
@Override
public List list() {
return null;
}
@Override
public boolean add(Dept dept) {
return false;
}
};
}
}
注意这里的@Component注解是必须的
API项目的DeptClientService 添加 fallbackFactory:
@FeignClient(value = "MICROSERVICECLOUD-DEPT", fallbackFactory = DeptClientServiceFallbackFactory.class)
public interface DeptClientService {
@RequestMapping(value = "/dept/get/{id}", method = RequestMethod.GET)
public Dept get(@PathVariable("id") long id);
@RequestMapping(value = "/dept/list", method = RequestMethod.GET)
public List list();
@RequestMapping(value = "/dept/add", method = RequestMethod.POST)
public boolean add(Dept dept);
}
消费端application.yml开启 hystrix:
server:
port: 80
feign:
hystrix:
enabled: true
eureka:
client:
register-with-eureka: false
service-url:
defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
消费端启动类添加注解: @EnableFeignClients
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients(basePackages="com.atguigu.springcloud")
@ComponentScan("com.atguigu.springcloud")
public class DeptConsumer80_Feign_App {
public static void main(String[] args) {
SpringApplication.run(DeptConsumer80_Feign_App.class, args);
}
}
服务端启动类:
@SpringBootApplication
@EnableEurekaClient//自动注册服务
//@EnableDiscoveryClient//服务发现
@EnableCircuitBreaker//hystrix 熔断机制
public class DeptProvider8001_Hystrix_App {
public static void main(String[] args) {
SpringApplication.run(DeptProvider8001_Hystrix_App.class, args);
}
}