2.Spring Cloud Hystrix 简介
熔断器,也叫断路器!(正常情况下 断路器是关的 只有出了问题才打开)用来保护微服务不雪崩的方法。思想和我们上面画的拦截器一样。 Hystrix 是 Netflix 公司开源的一个项目,它提供了熔断器功能,能够阻止分布式系统中出现联动故障。Hystrix 是通过隔离服务的访问点阻止联动故障的,并提供了故障的解决方案,从 而提高了整个分布式系统的弹性。微博 弹性云扩容 Docker K8s 3.Hystrix 快速入门 当有服务调用的时候,才会出现服务雪崩,所以 Hystrix 常和 OpenFeign,Ribbon 一起出现 3.1 在 OpenFeign 中使用 Hystrix(重点) 1.创建register-service项目,选择依赖2.application.yml配置文件
server:port: 8080
spring:application:name: register-service
eureka:client:service-url:defaultZone: http://192.168.174.133:8761/eurekainstance:hostname: localhostinstance-id: ${eureka.instance.hostname}:${spring.application.name}:${server.port}
3.创建一个controller类
package com.it.controller;import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
public class RegisterController {@GetMapping("register")public String register(){return "注册成功";}}
4.启动主函数类
package com.it;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;@SpringBootApplication
@EnableEurekaClient
public class RegisterService02Application {public static void main(String[] args) {SpringApplication.run(RegisterService02Application.class, args);}}
5.访问测试
org.springframework.cloud spring-cloud-starter-netflix-hystrix
pom.xml文件
4.0.0 org.springframework.boot spring-boot-starter-parent 2.3.12.RELEASE com.it customer-service-1 0.0.1-SNAPSHOT customer-service-1 Demo project for Spring Boot 1.8 Hoxton.SR12 org.springframework.boot spring-boot-starter-web org.springframework.cloud spring-cloud-starter-netflix-eureka-client org.springframework.cloud spring-cloud-starter-openfeign org.springframework.cloud spring-cloud-starter-netflix-hystrix org.springframework.boot spring-boot-starter-test test org.springframework.cloud spring-cloud-dependencies ${spring-cloud.version} pom import org.springframework.boot spring-boot-maven-plugin
2.application.yml
这里不同的配置是需要开启hystrix
server:port: 8081
spring:application:name: customer-service
eureka:client:service-url:defaultZone: http://192.168.174.133:8761/eurekainstance:hostname: localhostinstance-id: ${eureka.instance.hostname}:${spring.application.name}:${server.port}
feign:hystrix:enabled: true #在springcloud的F版本以前是默认开启的
3.创建CustomerRegisterFeign接口
@FeignClient需要指定访问失败的路径,即熔断路径
package com.it.feign;import com.it.feign.hystrix.CustomerRegisterFeignHystrix;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;/*** 这里需要指定熔断类*/
@FeignClient(value = "register-service",fallback = CustomerRegisterFeignHystrix.class)
public interface CustomerRegisterFeign {@GetMapping("register")public String register();}
4.CustomerRegisterFeignHystrix熔断类,实现CustomerRegisterFeign接口
package com.it.feign.hystrix;import com.it.feign.CustomerRegisterFeign;
import org.springframework.stereotype.Component;
//这里需要加入ioc容器的注解
@Component
public class CustomerRegisterFeignHystrix implements CustomerRegisterFeign {@Overridepublic String register() {return "我是备选方案!";}
}
5.CustomerController类
进行远程调用
package com.it.controller;import com.it.feign.CustomerRegisterFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;import javax.annotation.Resource;@RestController
public class CustomerController {@Resourceprivate CustomerRegisterFeign customerRegisterFeign;@GetMapping("customer")public String customer(){System.out.println("用户进行账号注册");//RPCString register = customerRegisterFeign.register();System.out.println(register);return register;}}
6.主函数启动类
package com.it;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
public class CustomerService1Application {public static void main(String[] args) {SpringApplication.run(CustomerService1Application.class, args);}}
7.测试
7.1关闭register-service,只启动该项目查看是否会发生熔断,进入熔断类中的方法
结论:成功进入
7.2打开register-service,查看该项目是否会正常的发起远程调用
结论:正常发起远程调用