16.Hystrix 实例(springcloud)
创始人
2024-04-07 10:37:59
0
1.前言 1.1 什么是服务雪崩

服务雪崩的本质:线程没有及时回收。 不管是调用成功还是失败,只要线程可以及时回收,就可以解决服务雪崩 1.2 服务雪崩怎么解决 1.2.1 修改调用的超时时长(不推荐) 将服务间的调用超时时长改小,这样就可以让线程及时回收,保证服务可用 优点:非常简单,也可以有效的解决服务雪崩 缺点:不够灵活,有的服务需要更长的时间去处理(写库,整理数据) 1.2.2 设置拦截器

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.访问测试 创建consumer-service项目 1.导入依赖,由于springboot没有hystirx选项,所以该依赖需要手动导入

        org.springframework.cloudspring-cloud-starter-netflix-hystrix
pom.xml文件

4.0.0org.springframework.bootspring-boot-starter-parent2.3.12.RELEASE com.itcustomer-service-10.0.1-SNAPSHOTcustomer-service-1Demo project for Spring Boot1.8Hoxton.SR12org.springframework.bootspring-boot-starter-weborg.springframework.cloudspring-cloud-starter-netflix-eureka-clientorg.springframework.cloudspring-cloud-starter-openfeignorg.springframework.cloudspring-cloud-starter-netflix-hystrixorg.springframework.bootspring-boot-starter-testtestorg.springframework.cloudspring-cloud-dependencies${spring-cloud.version}pomimportorg.springframework.bootspring-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,查看该项目是否会正常的发起远程调用

结论:正常发起远程调用

相关内容

热门资讯

银河麒麟V10SP1高级服务器... 银河麒麟高级服务器操作系统简介: 银河麒麟高级服务器操作系统V10是针对企业级关键业务...
【NI Multisim 14...   目录 序言 一、工具栏 🍊1.“标准”工具栏 🍊 2.视图工具...
AWSECS:访问外部网络时出... 如果您在AWS ECS中部署了应用程序,并且该应用程序需要访问外部网络,但是无法正常访问,可能是因为...
不能访问光猫的的管理页面 光猫是现代家庭宽带网络的重要组成部分,它可以提供高速稳定的网络连接。但是,有时候我们会遇到不能访问光...
AWSElasticBeans... 在Dockerfile中手动配置nginx反向代理。例如,在Dockerfile中添加以下代码:FR...
Android|无法访问或保存... 这个问题可能是由于权限设置不正确导致的。您需要在应用程序清单文件中添加以下代码来请求适当的权限:此外...
月入8000+的steam搬砖... 大家好,我是阿阳 今天要给大家介绍的是 steam 游戏搬砖项目,目前...
​ToDesk 远程工具安装及... 目录 前言 ToDesk 优势 ToDesk 下载安装 ToDesk 功能展示 文件传输 设备链接 ...
北信源内网安全管理卸载 北信源内网安全管理是一款网络安全管理软件,主要用于保护内网安全。在日常使用过程中,卸载该软件是一种常...
AWS管理控制台菜单和权限 要在AWS管理控制台中创建菜单和权限,您可以使用AWS Identity and Access Ma...