• 16.Hystrix 实例(springcloud)


    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配置文件

    1. server:
    2. port: 8080
    3. spring:
    4. application:
    5. name: register-service
    6. eureka:
    7. client:
    8. service-url:
    9. defaultZone: http://192.168.174.133:8761/eureka
    10. instance:
    11. hostname: localhost
    12. instance-id: ${eureka.instance.hostname}:${spring.application.name}:${server.port}

     3.创建一个controller类

    1. package com.it.controller;
    2. import org.springframework.web.bind.annotation.GetMapping;
    3. import org.springframework.web.bind.annotation.RestController;
    4. @RestController
    5. public class RegisterController {
    6. @GetMapping("register")
    7. public String register(){
    8. return "注册成功";
    9. }
    10. }

     4.启动主函数类

    1. package com.it;
    2. import org.springframework.boot.SpringApplication;
    3. import org.springframework.boot.autoconfigure.SpringBootApplication;
    4. import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
    5. @SpringBootApplication
    6. @EnableEurekaClient
    7. public class RegisterService02Application {
    8. public static void main(String[] args) {
    9. SpringApplication.run(RegisterService02Application.class, args);
    10. }
    11. }
    5.访问测试
    创建consumer-service项目
    1.导入依赖,由于springboot没有hystirx选项,所以该依赖需要手动导入

    1. <dependency>
    2. <groupId>org.springframework.cloudgroupId>
    3. <artifactId>spring-cloud-starter-netflix-hystrixartifactId>
    4. dependency>
    pom.xml文件
    1. "1.0" encoding="UTF-8"?>
    2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    4. <modelVersion>4.0.0modelVersion>
    5. <parent>
    6. <groupId>org.springframework.bootgroupId>
    7. <artifactId>spring-boot-starter-parentartifactId>
    8. <version>2.3.12.RELEASEversion>
    9. <relativePath/>
    10. parent>
    11. <groupId>com.itgroupId>
    12. <artifactId>customer-service-1artifactId>
    13. <version>0.0.1-SNAPSHOTversion>
    14. <name>customer-service-1name>
    15. <description>Demo project for Spring Bootdescription>
    16. <properties>
    17. <java.version>1.8java.version>
    18. <spring-cloud.version>Hoxton.SR12spring-cloud.version>
    19. properties>
    20. <dependencies>
    21. <dependency>
    22. <groupId>org.springframework.bootgroupId>
    23. <artifactId>spring-boot-starter-webartifactId>
    24. dependency>
    25. <dependency>
    26. <groupId>org.springframework.cloudgroupId>
    27. <artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
    28. dependency>
    29. <dependency>
    30. <groupId>org.springframework.cloudgroupId>
    31. <artifactId>spring-cloud-starter-openfeignartifactId>
    32. dependency>
    33. <dependency>
    34. <groupId>org.springframework.cloudgroupId>
    35. <artifactId>spring-cloud-starter-netflix-hystrixartifactId>
    36. dependency>
    37. <dependency>
    38. <groupId>org.springframework.bootgroupId>
    39. <artifactId>spring-boot-starter-testartifactId>
    40. <scope>testscope>
    41. dependency>
    42. dependencies>
    43. <dependencyManagement>
    44. <dependencies>
    45. <dependency>
    46. <groupId>org.springframework.cloudgroupId>
    47. <artifactId>spring-cloud-dependenciesartifactId>
    48. <version>${spring-cloud.version}version>
    49. <type>pomtype>
    50. <scope>importscope>
    51. dependency>
    52. dependencies>
    53. dependencyManagement>
    54. <build>
    55. <plugins>
    56. <plugin>
    57. <groupId>org.springframework.bootgroupId>
    58. <artifactId>spring-boot-maven-pluginartifactId>
    59. plugin>
    60. plugins>
    61. build>
    62. project>

    2.application.yml

    这里不同的配置是需要开启hystrix

    1. server:
    2. port: 8081
    3. spring:
    4. application:
    5. name: customer-service
    6. eureka:
    7. client:
    8. service-url:
    9. defaultZone: http://192.168.174.133:8761/eureka
    10. instance:
    11. hostname: localhost
    12. instance-id: ${eureka.instance.hostname}:${spring.application.name}:${server.port}
    13. feign:
    14. hystrix:
    15. enabled: true #在springcloud的F版本以前是默认开启的

    3.创建CustomerRegisterFeign接口

    @FeignClient需要指定访问失败的路径,即熔断路径

    1. package com.it.feign;
    2. import com.it.feign.hystrix.CustomerRegisterFeignHystrix;
    3. import org.springframework.cloud.openfeign.FeignClient;
    4. import org.springframework.web.bind.annotation.GetMapping;
    5. /**
    6. * 这里需要指定熔断类
    7. */
    8. @FeignClient(value = "register-service",fallback = CustomerRegisterFeignHystrix.class)
    9. public interface CustomerRegisterFeign {
    10. @GetMapping("register")
    11. public String register();
    12. }

    4.CustomerRegisterFeignHystrix熔断类,实现CustomerRegisterFeign接口

    1. package com.it.feign.hystrix;
    2. import com.it.feign.CustomerRegisterFeign;
    3. import org.springframework.stereotype.Component;
    4. //这里需要加入ioc容器的注解
    5. @Component
    6. public class CustomerRegisterFeignHystrix implements CustomerRegisterFeign {
    7. @Override
    8. public String register() {
    9. return "我是备选方案!";
    10. }
    11. }

    5.CustomerController类

    进行远程调用

    1. package com.it.controller;
    2. import com.it.feign.CustomerRegisterFeign;
    3. import org.springframework.beans.factory.annotation.Autowired;
    4. import org.springframework.web.bind.annotation.GetMapping;
    5. import org.springframework.web.bind.annotation.RestController;
    6. import javax.annotation.Resource;
    7. @RestController
    8. public class CustomerController {
    9. @Resource
    10. private CustomerRegisterFeign customerRegisterFeign;
    11. @GetMapping("customer")
    12. public String customer(){
    13. System.out.println("用户进行账号注册");
    14. //RPC
    15. String register = customerRegisterFeign.register();
    16. System.out.println(register);
    17. return register;
    18. }
    19. }

    6.主函数启动类

    1. package com.it;
    2. import org.springframework.boot.SpringApplication;
    3. import org.springframework.boot.autoconfigure.SpringBootApplication;
    4. import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
    5. import org.springframework.cloud.openfeign.EnableFeignClients;
    6. @SpringBootApplication
    7. @EnableEurekaClient
    8. @EnableFeignClients
    9. public class CustomerService1Application {
    10. public static void main(String[] args) {
    11. SpringApplication.run(CustomerService1Application.class, args);
    12. }
    13. }

    7.测试

    7.1关闭register-service,只启动该项目查看是否会发生熔断,进入熔断类中的方法

    结论:成功进入

    7.2打开register-service,查看该项目是否会正常的发起远程调用

    结论:正常发起远程调用

  • 相关阅读:
    【智慧燃气】智慧燃气解决方案总体概述--终端层、网络层
    【Java】使用HttpClient进行简单的post请求
    torchversion.transforms的使用
    MATLAB算法实战应用案例精讲-【深度学习】BP神经网络
    我的算法笔记 | leetCode easy题感受
    C++入门必备基础知识(下篇)
    安装下载Anaconda注意事项以及卸载
    布隆过滤器(Bloom Filter)
    UnityVR一体机报错:GL_OUT_OF_MEMORY,[EGL] Unable to acquire context
    # 2-1:B/S 架构 和 C/S 架构的区别-什么是URL
  • 原文地址:https://blog.csdn.net/weixin_59334478/article/details/127815874