微服务架构之spring cloud hystrix&hystrix dashboard

在前面介绍spring cloud feign中我们已经使用过hystrix,只是没有介绍,spring cloud hystrix在spring cloud中起到保护微服务的作用,不会让发生的异常无边界的蔓延下去,很像我们电路中的保险设置,有超压或者线路有问题就即时的断开,保护用电设备不被损坏,这篇文章就来介绍spring cloud hystrix及其hystrix dashboard。

(一) 版本说明

a) Spring boot 2.0.6.RELEASE

b) Spring cloud Finchley.SR2

c) Java version 1.8

d) spring-cloud-starter-netflix-hystrix 2.0.2.RELEASE

e) spring-cloud-starter-netflix-hystrix-dashboard 2.0.2.RELEASE

(二) 项目配置

1. 项目设置

a) POM设置

<dependency>

<groupId>org.springframework.cloud</groupId>

<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>

</dependency>

<dependency>

<groupId>org.springframework.cloud</groupId>

<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>

</dependency>

b) application.yml配置文件

eureka:

datacenter: ctm

environment: dev

instance:

hostname: 192.168.1.78

prefer-ip-address: true

ip-address: 192.168.1.129

lease-renewal-interval-in-seconds: 10

lease-expiration-duration-in-seconds: 30

instance-id: ${eureka.instance.ip-address}:${server.port}

client:

service-url:

defaultZone: http://${eureka.instance.hostname}:1001/eureka/,http://${eureka.instance.hostname}:1002/eureka/,http://${eureka.instance.hostname}:1003/eureka/

fetch-registry: true

register-with-eureka: true

healthcheck:

enabled: true

feign:

hystrix:

enabled: true

c) 主要参数说明

i. eureka.instance.prefer-ip-address 使用IP显示注册信息

ii. eureka.instance.ip-address 实例IP地址,

iii. eureka.instance.instance-id 自定义实例id,服务之间调用就是使用该配置,多个实例必须保证唯一性

iv. eureka.client.service-url.defaultZone 注册中心地址

v. feign.Hystrix.enabled 在feign中其中熔断器

d) 服务提供者API

@Value("${server.port}")

private String getPort;

@GetMapping(name = "index", value = "/index")

public String Index() {

return getPort;

}

2. feign消费端项目配置

a) POM设置

<dependency>

<groupId>org.springframework.cloud</groupId>

<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>

</dependency>

<dependency>

<groupId>org.springframework.cloud</groupId>

<artifactId>spring-cloud-starter-feign</artifactId>

</dependency>

i. 这里把服务消费端也注册到了服务治理中心,消费者同时也是其它服务的提供者。

b) application.yml配置文件

eureka:

datacenter: ctm

environment: dev

instance:

hostname: 192.168.1.78

prefer-ip-address: true

ip-address: 192.168.1.129

lease-renewal-interval-in-seconds: 10

lease-expiration-duration-in-seconds: 30

instance-id: ${eureka.instance.ip-address}:${server.port}

client:

service-url:

defaultZone: http://${eureka.instance.hostname}:1001/eureka/,http://${eureka.instance.hostname}:1002/eureka/,http://${eureka.instance.hostname}:1003/eureka/

fetch-registry: true

register-with-eureka: true

healthcheck:

enabled: true

c) feign服务消费者API

@Configuration

public class FeignConfig {

@Bean

public Retryer feignRetryer(){

return new Retryer.Default(100, TimeUnit.SECONDS.toMillis(1),3);

}

}

@Component

public class FallBackService implements FeignService {

@Override

public String Index() {

return "hi,feign,error!";

}

}

@Component

@FeignClient(name = "DEMOSERVICEIMPL",configuration = FeignConfig.class,fallback = FallBackService.class)

public interface FeignService {

@GetMapping(value = "/index")

String Index();

}

i. FeignClient feign客户端配置,这里配置了服务名称、重试规则、失败回调,fallback 属性就是熔断器提供的功能,这点在后面的熔断器看板中可以更直观的看到。

3. 项目运行

a) 运行服务提供者

i. 运行3个服务者实例后,会在服务中心看到如下效果,服务提供者已经注册成功

b) feign运行服务消费者

i. 运行消费者,如下图所示

c) 在浏览器中输入消费者地址服务地址,即可看到如下效果

在看板地址中输入http://192.168.1.129:1201/actuator/hystrix.stream,然后点击Monitor Stream 按钮,即可看到熔断器监控,如果没有数据显示,可以用任何客户端调用几次API,即可看到类似效果。

这样spring cloud hystrix&hystrix dashboard就介绍完了,如果在开发中遇到问题,也可以留言共同探讨共同进步。

原文地址:https://www.cnblogs.com/lzh-boy/p/10381443.html

时间: 2024-08-26 22:19:43

微服务架构之spring cloud hystrix&hystrix dashboard的相关文章

【微服务架构】Spring Cloud之Hystrix(三)

一.雪崩效应 在微服务架构中,由于服务和服务之间可以互相调用,一项工作的完成可能会依赖调用多个微服务模块,但由于网络原因或者自身的原因,服务并不能保证100%可用,如果单个服务出现问题,调用这个服务就会出现线程阻塞,此时若有大量的请求涌入,Servlet容器的线程资源会被消耗完毕,导致服务瘫痪:再加上服务和服务之间的依赖性,瘫痪会迅速传播,给整个微服务系统造成严重的后果,这就是服务故障的“血崩”效应. 服务雪崩效应形成的阶段 1.服务提供者不可用 硬件故障.硬件损坏导致服务器主机宕机,或网络硬件

微服务架构之spring cloud 介绍

在当前的软件开发行业中,尤其是互联网,微服务是非常炽热的一个词语,市面上已经有一些成型的微服务框架来帮助开发者简化开发工作量,但spring cloud 绝对占有一席之地,不管你是否为java开发,大部分都应该听说过,因为他实现了微服务所必备的功能. Spring cloud总体概览,这是我用了近3个小时的成果,也是实际项目的总结. a)         Spring cloud gateway 是网关,起到总管的作用,也是终端调用服务的第一道门槛,统一的入口. b)        Spring

微服务架构之spring cloud eureka

Spring Cloud Eureka是spring cloud的核心组件,负责服务治理功能,起到中心枢纽作用,其它组件都依赖eureka来获取服务,然后再根据项目需求实现自己的业务,eureka在整个微服务架构中的位置绝对是核心地位. (一)   版本说明 a)         Spring boot   2.0.6.RELEASE b)        Spring cloud  Finchley.SR2 c)         Java version   1.8 (二)   项目设置 Pom

微服务架构之spring cloud turbine

在前面介绍了spring cloud hystrix及其hystrix dashboard,但都是对单个项目的监控,对于一个为项目而言,必定有很多微服务,一个一个去看非常的不方便,如果有一个能集中熔断器监控的地方就完美了,spring cloud turbine 就实现了这样的功能,这篇文章就来介绍spring cloud turbine. (一) 版本说明 a) Spring boot 2.0.6.RELEASE b) Spring cloud Finchley.SR2 c) Java ver

微服务架构之spring cloud gateway

Spring Cloud Gateway是spring cloud中起着非常重要的作用,是终端调用服务的入口,同时也是项目中每个服务对外暴露的统一口径,我们可以在网关中实现路径映射.权限验证.负载均衡.服务聚合等业务功能. (一) 版本说明 a) Spring boot 2.0.6.RELEASE b) Spring cloud Finchley.SR2 c) Java version 1.8 d) spring-cloud-starter-gateway 2.0.2.RELEASE (二) 项

微服务架构之spring cloud feign

在spring cloud ribbon中我们用RestTemplate实现了服务调用,可以看到我们还是需要配置服务名称,调用的方法 等等,其实spring cloud提供了更优雅的服务调用方式,就是这篇文章要讲解的spring cloud feign,feign内部已经集成了ribbon,所以不用再单独引用,只需要引用spring cloud feign即可. (一) 版本说明 a) Spring boot 2.0.6.RELEASE b) Spring cloud Finchley.SR2

微服务为什么选Spring Cloud?

前言现如今微服务架构十分流行,而采用微服务构建系统也会带来更清晰的业务划分和可扩展性.同时,支持微服务的技术栈也是多种多样的,本系列文章主要介绍这些技术中的翘楚--Spring Cloud.这是序篇,主要讲述我们为什么选择Spring Cloud和它的技术概览. 1.为什么微服务架构需要Spring Cloud 简单来说,服务化的核心就是将传统的一站式应用根据业务拆分成一个一个的服务,而微服务在这个基础上要更彻底地去耦合(不再共享DB.KV,去掉重量级ESB),并且强调DevOps和快速演化.这

微服务:整合 Spring cloud Eureka - 服务治理机制

一.简介 在体验了Spring Cloud Eureka 通过简单的注解配置就能实现强大的服务治理功能之后,我们可以进一步了解一些Eureka基础架构中各个元素之间的通信行为,以此来更加深入的理解Eureka服务治理体系是如何运转起来的. 二.微服务基础架构拓扑图 1.“Eureka Server 服务注册中心-1” 和 “Eureka Server 服务注册中心-2” ,他们相互注册成为高可用集群. 2.服务提供者启动两个实例,一个注册到“Eureka Server 服务注册中心-1” 上,另

第二代微服务网关组件 - Spring Cloud Gateway

[TOC] 初识Spring Cloud Gateway 简介: Spring Cloud Gateway是Spring Cloud体系的第二代网关组件,基于Spring 5.0的新特性WebFlux进行开发,底层网络通信框架使用的是Netty,所以其吞吐量高.性能强劲,未来将会取代第一代的网关组件Zuul.Spring Cloud Gateway可以通过服务发现组件自动转发请求,默认集成了Ribbon做负载均衡,以及默认使用Hystrix对网关进行保护,当然也可以选择其他的容错组件,例如Sen