Java之 Spring Cloud 微服务搭建 Hystrix (第二个阶段)

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.PathVariable;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;

import org.springframework.web.bind.annotation.RestController;

import org.springframework.web.client.RestTemplate;

@RestController

@RequestMapping(“/order”)

/**

  • @DefaultProperties : 指定此接口中公共的熔断设置

  •  如果过在@DefaultProperties指定了公共的降级方法
    
  •  在@HystrixCommand不需要单独指定了
    

*/

@DefaultProperties(defaultFallback = “defaultFallBack”)

public class OrderController {

@Autowired

private RestTemplate restTemplate;

/**

  • 使用注解配置熔断保护

  • fallbackmethod : 配置熔断之后的降级方法
    

*/

@HystrixCommand

@RequestMapping(value = “/buy/{id}”,method = RequestMethod.GET)

public Product findById(@PathVariable Long id) {

if(id != 1) {

throw new RuntimeException(“服务器异常”);

}

return restTemplate.getForObject(“http://service-product/product/1”,Product.class);

}

/*

指定统一的降级方法

参数:没有参数

*/

public Product defaultFallBack(){

Product product = new Product();

product.setProductName(“触发统一的降级方法”);

return product;

}

/**

  • 降级方法

  • 和需要收到保护的方法的返回值一致

  • 方法参数一致

*/

public Product orderFallBack(Long id) {

Product product = new Product();

product.setProductName(“触发降级方法”);

return product;

}

}

启动运行测试

在这里插入图片描述

访问http://localhost:9004/order/buy/1

在这里插入图片描述

4、Feign实现服务熔断


SpringCloud Fegin默认已为Feign整合了hystrix,所以添加Feign依赖后就不用在添加hystrix,那么怎么才能让Feign的熔断机制生效呢,只要按以下步骤开发:

(1)修改order_service_feign当中的application.yml在Fegin中开启hystrix

在Feign中已经内置了hystrix,但是默认是关闭的需要在工程的 application.yml 中开启对hystrix的支持

在这里插入图片描述

hystrix: #在feign中开启hystrix熔断

enabled: true

(2)配置FeignClient接口的实现类

基于Feign实现熔断降级,那么降级方法需要配置到FeignClient接口的实现类中

在order_service_feign当中创建ProductFeignClientCallBack

在这里插入图片描述

package cn.itbluebox.order.feign;

import cn.itbluebox.order.entity.Product;

import org.springframework.stereotype.Component;

@Component

public class ProductFeignClientCallBack implements ProductFeignClient {

/**

  • 熔断降级的方法

*/

public Product findById(Long id) {

Product product = new Product();

product.setProductName(“feign调用触发熔断降级方法”);

return product;

}

}

(3)启动运行测试

在这里插入图片描述

访问:http://localhost:9003/order/buy/1

在这里插入图片描述

5、Hystrix 的超时时间


在这里插入图片描述

#默认的连接的超时时间是1秒,若1秒没有返回数据,会自动触发降级逻辑

hystrix:

command:

default:

execution:

isolation:

thread:

timeoutInMilliseconds: 3000 #默认的连接的超时时间是1秒,若1秒没有返回数据,会自动触发降级逻辑,这里设置的是3秒

二、服务熔断Hystrix高级

==========================================================================

我们知道,当请求失败,被拒绝,超时的时候,都会进入到降级方法中。但进入降级方法并不意味着断路器已经被打开。那么如何才能了解断路器中的状态呢?

1、Hystrix的监控平台


除了实现容错功能,Hystrix还提供了近乎实时的监控HystrixCommandHystrixObservableCommand在执行时,会生成执行结果和运行指标。

比如每秒的请求数量,成功数量等。

这些状态会暴露在Actuator提供的/health端点中。

只需为项目添加 spring-boot-actuator 依赖,重启项目,

即可看到实时的监控数据。

1)导入依赖

在这里插入图片描述

org.springframework.boot

spring-boot-starter-actuator

org.springframework.cloud

spring-cloud-starter-netflix-hystrix

org.springframework.cloud

spring-cloud-starter-netflix-hystrix-dashboard

2)添加EnableCircuitBreaker注解(激活Hystrix)

在这里插入图片描述

package cn.itbluebox.order;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.boot.autoconfigure.domain.EntityScan;

import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;

import org.springframework.cloud.openfeign.EnableFeignClients;

@SpringBootApplication

@EntityScan(“cn.itbluebox.order.entity”)

//激活Feign

@EnableFeignClients

//激活Hystrix

@EnableCircuitBreaker

public class FeignOrderApplication {

public static void main(String[] args) {

SpringApplication.run(FeignOrderApplication.class,args);

}

}

3)设置暴露所有actuator监控的断点

在这里插入图片描述

management:

endpoints:

web:

exposure:

include: ‘*’

4)运行测试并访问

在这里插入图片描述

访问:http://localhost:9003/actuator/hystrix.stream

在这里插入图片描述

访问http://localhost:9003/order/buy/1

在这里插入图片描述

再次访问:http://localhost:9003/actuator/hystrix.stream

在这里插入图片描述

5)搭建Hystrix DashBoard监控

刚刚讨论了Hystrix的监控,但访问/hystrix.stream接口获取的都是已文字形式展示的信息。很难通过文字直观的展示系统的运行状态,所以Hystrix官方还提供了基于图形化的DashBoard(仪表板)监控平台。

Hystrix仪表板可以显示每个断路器(被@HystrixCommand注解的方法)的状态。

(1)添加EnableHystrixDashboard 注解

在启动类使用@EnableHystrixDashboard注解激活仪表盘项目

在这里插入图片描述

package cn.itbluebox.order;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.boot.autoconfigure.domain.EntityScan;

import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;

import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;

import org.springframework.cloud.openfeign.EnableFeignClients;

@SpringBootApplication

@EntityScan(“cn.itbluebox.order.entity”)

//激活Feign

@EnableFeignClients

//激活Hystrix

@EnableCircuitBreaker

//激活仪表盘项目

@EnableHystrixDashboard

public class FeignOrderApplication {

public static void main(String[] args) {

SpringApplication.run(FeignOrderApplication.class,args);

}

}

(2)运行测试

在这里插入图片描述

访问:http://localhost:9003/hystrix

在下列输入框当中输入:http://localhost:9003/actuator/hystrix.stream

点击:Monitor Stream

在这里插入图片描述

在这里插入图片描述

参数解释

在这里插入图片描述

2、断路器聚合监控Turbine


在微服务架构体系中,每个服务都需要配置Hystrix DashBoard监控。

如果每次只能查看单个实例的监控数据,就需要不断切换监控地址,这显然很不方便。要想看这个系统的Hystrix Dashboard数据就需要用到Hystrix Turbine。

Turbine是一个聚合所有Hystrix 监控数据的工具,他可以将所有相关微服务的Hystrix 监控数据聚合到一起,方便使用。引入Turbine后,整个监控系统架构如下:

在这里插入图片描述

(1) 搭建TurbineServer

1)创建工程 hystrix_turbine

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

2) 引入相关坐标

在这里插入图片描述

org.springframework.cloud

spring-cloud-starter-netflix-turbine

org.springframework.cloud

spring-cloud-starter-netflix-hystrix

org.springframework.cloud

spring-cloud-starter-netflix-hystrix-dashboard

3) 配置多个微服务的hystrix监控

在application.yml的配置文件中开启turbine并进行相关配置

在这里插入图片描述

server:

port: 8031

spring:

application:

name: hystrix-turbine

eureka:

client:

service-url:

defaultZone: http://localhost:9000/eureka/

instance:

prefer-ip-address: true

turbine:

要监控的微服务列表,多个用,分隔

appConfig: service-order

clusterNameExpression: “‘default’”

  • eureka相关配置 : 指定注册中心地址

  • turbine相关配置:指定需要监控的微服务列表

turbine会自动的从注册中心中获取需要监控的微服务,并聚合所有微服务中的/hystrix.stream 数据

4) 配置启动类

在这里插入图片描述

在这里插入图片描述

package cn.itbluebox;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;

import org.springframework.cloud.netflix.turbine.EnableTurbine;

@SpringBootApplication

@EnableTurbine //激活Turbine

@EnableHystrixDashboard //激活Hystrix页面监控平台

public class TurbineApplication {

public static void main(String[] args) {

SpringApplication.run(TurbineApplication.class, args);

}

}

作为一个独立的监控项目,需要配置启动类,开启HystrixDashboard监控平台,并激活Turbine

5) 测试

在这里插入图片描述

测试

访问其他微服务:http://localhost:9003/order/buy/1

在这里插入图片描述

浏览器访问 http://localhost:8031/hystrix 展示HystrixDashboard。

并在url位置输入 http://localhost:8031/turbine.stream,动态根据turbine.stream数据展示多个微服务的监控数据

在这里插入图片描述

在这里插入图片描述

hystrix可以对请求失败的请求,以及被拒绝,或者超时的请求进行统一的降级处理。

(2) 熔断器的状态

1)熔断器有三个状态

CLOSED 、

OPEN 、

HALF_OPEN

熔断器默认关闭状态,当触发熔断后状态变更为OPEN ,

在等待到指定的时间,Hystrix会放请求检测服务是否开启,

这期间熔断器会变为 HALF_OPEN 半开启状态,

熔断探测服务可用则继续变更为 CLOSED 关闭熔断器。

在这里插入图片描述

在这里插入图片描述

断路器:Closed(关闭)

  • 所有的请求都可以正常访问

  • 请求次数大于20次,且存在50%的失败概率open(开启)

  • 所有的请求会进入到降级方法中Half Open(半开)

  • 维持Open状态一段时间(5s),进入到半开状态(尝试释放一个请求到远程微服务发起调用)

  • 如果释放请求可以正常访问,就会关闭断路器

  • 如果释放的请求不能访问,继续保持开启的Open状态5s

断路器:Closed(关闭),Open(开启),Half Open(半开)

  • Closed:关闭状态(断路器关闭),所有请求都正常访问。代理类维护了最近调用失败的次数,如果某次调用失败,则使失败次数加1。

如果最近失败次数超过了在给定时间内允许失败的阈值,则代理类切换到断开(Open)状态。

此时代理开启了一个超时时钟,当该时钟超过了该时间,则切换到半断开(Half-Open)状态。该超时时间的设定是给了系统一次机会来修正导致调用失败的错误。

  • Open:打开状态(断路器打开),所有请求都会被降级。

Hystix会对请求情况计数,当一定时间内失败请求百分比达到阈值,则触发熔断,断路器会完全关闭。默认失败比例的阈值是50%,请求次数最少不低于20次。

  • Half Open:半开状态,open状态不是永久的,打开后会进入休眠时间(默认是5S)。随后断路器会自动进入半开状态。

此时会释放1次请求通过,若这个请求是健康的,则会关闭断路器,否则继续保持打开,再次进行5秒休眠计时。

2)为了能够精确控制请求的成功或失败,我们在 product_service :将之前的延时屏蔽掉

在这里插入图片描述

修改order_service_rest当中的OrderController

在这里插入图片描述

package cn.itbluebox.order.controller;

import cn.itbluebox.order.entity.Product;

import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.PathVariable;

小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数初中级Java工程师,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年最新Java开发全套学习资料》送给大家,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
img
img
img

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频

如果你觉得这些内容对你有帮助,可以添加下面V无偿领取!(备注Java)
img

总结

阿里伤透我心,疯狂复习刷题,终于喜提offer 哈哈~好啦,不闲扯了

image

1、JAVA面试核心知识整理(PDF):包含JVMJAVA集合JAVA多线程并发,JAVA基础,Spring原理微服务,Netty与RPC,网络,日志,ZookeeperKafkaRabbitMQ,Hbase,MongoDB,Cassandra,设计模式负载均衡数据库一致性哈希JAVA算法数据结构,加密算法,分布式缓存,Hadoop,Spark,Storm,YARN,机器学习,云计算共30个章节。

image

2、Redis学习笔记及学习思维脑图

image

3、数据面试必备20题+数据库性能优化的21个最佳实践

image

大家,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。**
[外链图片转存中…(img-8kj9xINF-1711052099004)]
[外链图片转存中…(img-kUop5Mgh-1711052099005)]
[外链图片转存中…(img-lVjCArjB-1711052099005)]

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频

如果你觉得这些内容对你有帮助,可以添加下面V无偿领取!(备注Java)
[外链图片转存中…(img-Tr49kWWI-1711052099006)]

总结

阿里伤透我心,疯狂复习刷题,终于喜提offer 哈哈~好啦,不闲扯了

[外链图片转存中…(img-QusyrhPP-1711052099006)]

1、JAVA面试核心知识整理(PDF):包含JVMJAVA集合JAVA多线程并发,JAVA基础,Spring原理微服务,Netty与RPC,网络,日志,ZookeeperKafkaRabbitMQ,Hbase,MongoDB,Cassandra,设计模式负载均衡数据库一致性哈希JAVA算法数据结构,加密算法,分布式缓存,Hadoop,Spark,Storm,YARN,机器学习,云计算共30个章节。

[外链图片转存中…(img-1OJ7GwTo-1711052099007)]

2、Redis学习笔记及学习思维脑图

[外链图片转存中…(img-Iyefh41R-1711052099007)]

3、数据面试必备20题+数据库性能优化的21个最佳实践

[外链图片转存中…(img-hpyLnbzJ-1711052099007)]

本文已被CODING开源项目:【一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码】收录

  • 11
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值