代码编织梦想

1.pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <artifactId>springcloudDemo</artifactId>
        <groupId>org.example</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <groupId>com.weitao</groupId>
    <artifactId>springcloud-hystrix</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springcloud-hystrix</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
            <version>2.2.6.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
            <version>2.2.6.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
            <version>2.2.6.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
            <version>2.2.6.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
            <version>2.2.6.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.example</groupId>
            <artifactId>springcloud-api</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>


    </dependencies>


    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
    <repositories>
        <repository>
            <id>spring-snapshots</id>
            <name>Spring Snapshots</name>
            <url>https://repo.spring.io/snapshot</url>
            <releases>
                <enabled>false</enabled>
            </releases>
        </repository>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>

</project>

2.application.yml

server:
  port: 8060
spring:
  application:
    name: hystrix
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/
  instance:
    prefer-ip-address: true
    instance-id: ${spring.application.name}:${server.port}
#断路器,起到降级作用,避免服务雪崩
feign:
  hystrix:
    enabled: true

management:
  endpoints:
    web:
      exposure:
        include: 'hystrix.stream'
hystrix:
  dashboard:
    proxy-stream-allow-list: localhost

3.代码结构

4.具体代码

package com.example.springcloud.controller;

import com.example.springcloud.fegin.FeginDept;
import com.example.springcloud.pojo.Dept;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/hystrix")
public class HystrixController {

    @Autowired
    private FeginDept feginDept;

    @GetMapping("/select")
    public List<Dept> get() {
        return feginDept.selectAll();
    }

    @GetMapping("/select/{id}")
    public Dept selectByID(@PathVariable(name = "id") Long id) {
        return feginDept.selectByID(id);
    }

    @PostMapping(value = "/save", produces = "application/json")
    public boolean saveDept(@RequestBody Dept dept) {
        return feginDept.saveDept(dept);
    }

    //假如服务提供者没启动,直接访问/handle/getInfo 那么容错机制就会调用FeginDept  fallback属性设置FeginError 里的方法
    @GetMapping("/getInfo")
    public String getInfo() {
        return feginDept.getInfo();
    }
}
package com.example.springcloud.fegin;

import com.example.springcloud.pojo.Dept;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.List;

/**
 * 接口注解方式
 * value="provider" 调用提供者的应用名称
 */
@FeignClient(value = "provider")
public interface FeginDept {

    @PostMapping(value = "/dept/save",produces = "application/json")
    boolean saveDept(@RequestBody  Dept dept);


    @GetMapping("/dept/select/{id}")
    Dept selectByID(@PathVariable("id") Long id);

    @GetMapping("/dept/select")
    List<Dept> selectAll();


    @GetMapping("/dept/getInfo")
    String getInfo();

}
package com.example.springcloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
import org.springframework.cloud.openfeign.EnableFeignClients;

@SpringBootApplication
@EnableFeignClients
@EnableHystrix
@EnableHystrixDashboard           //声明启用可视化数据监控
public class SpringcloudHystrixApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringcloudHystrixApplication.class, args);
    }

}

5.运行注册中心,提供者,和 Hystrix模块入口

6.  http://localhost:8060/actuator/hystrix.stream 

这个界面一直ping输出
 

7.访问接口

http://localhost:8060/hystrix/select

 

8.  http://localhost:8060/hystrix  

这个打开hystrix仪表盘 ,可以输入http://localhost:8060/actuator/hystrix.stream 

 

9.跳转到页面

 

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/SOMECT/article/details/117423810

spring cloud hystrix_一只勤奋的小猪的博客-爱代码爱编程

Spring Cloud Hystrix 创建spring-cloud-03-hystrix-server服务中心 端口为8088 创建spring-cloud-03-hystrix-client服务单元

spring cloud 使用hystrix实现微服务的容错处理_五百年前fhya的博客-爱代码爱编程

如何容错 容错机制: 1、为网络请求设置超时:必须为网络请求设置超时,一次远程调用对应着一个线程/进程,如果响应太慢,这个线程得不到释放,占用着系统资源,如果此类线程过多,就会耗尽资源,最终导致服务不可用。 2、使用断路器模式:断路器可理解为对容易导致错无的操作的代理,这种代理能够统计一段时间内调用失败的次数,并决定是正常请求依赖的服务还是直接返回。断路器

springcloud-hystrix 功能介绍+使用方式_hzq472583006的博客-爱代码爱编程

Hystrix官方文档:   https://github.com/Netflix/Hystrix/wiki#what 1、什么是Hystrix     在一个分布式系统中,必然会有部分系统的调用会失败。Hystrix是一个通过添加超时容错和失败容错逻辑来帮助你控制这些分布式系统的交互。Hystrix通过隔离服务之间的访问,阻止他们之间的级联故障以及提

springcloud引入容错管理机制_tinysakurac的博客-爱代码爱编程_导入的容错机制

为了避免服务单元出现故障导致的故障蔓延引起整个系统的瘫痪,需要在系统中加入容错保护机制。Hystrtix就是spring cloud下优秀的容错保护框架,下面记录一下引入过程 1 添加依赖 <!--

spring cloud hystrix 服务容错-爱代码爱编程

1. 背景 在复杂的分布式 架构 的应用程序有很多的依赖,都会不可避免地在某些时候失败。高并发的依赖失败时如果没有隔离措施,当前应用服务就有被拖垮的风险。服务雪崩效应是一种因 服务提供者 的不可用导致 服务调用者 的不可用,并将不可用 逐渐放大 的过程.如果所示: A服务调用B服务,B服务调用C服务,由于某种原因,B服务调用C服务不成功,B服务就会

springcloud篇- hystrix容错保护-爱代码爱编程

Hystrix容错保护 Hystrix是Netflix的一个开源项目,主要作用是通过控制那些访问远程系统、服务和第三方库的节点,从而对延迟和故障提供更强大的容错能力。 其可以看做是Netflix团队对分布式系统运维的各种理念

spring cloud hystrix服务容错--触发降级和设置超时时间_没有梦想何必远方。的博客-爱代码爱编程

1.为服务提供一系列的服务容错保护机制 2.Hystrix服务容错作用: 1.服务降级 2.依赖隔离 3.服务熔断 3.服务降级 4.加入依赖 <dependency> <gro

spring cloud hystrix原理简析_谈胖胖的博客-爱代码爱编程

Hystrix 中文含义是豪猪,因其背上长满棘刺,从而拥有了自我保护的能力。Hystrix是Netflix开源的一款容错框架。 Hystrix设计目标: 对来自依赖的延迟和故障进行防护和控制——这些依赖通常都是通过网络访问的阻止故障的连锁反应快速失败并迅速恢复回退并优雅降级提供近实时的监控与告警 Hystrix遵循的设计原则: 防止任何单独的依赖耗尽

springcloud容错机制(hystrix服务降级)_endure95的博客-爱代码爱编程_springcloud容错机制

SpringCloud容错机制 Hystrix服务降级 雪崩效应 服务雪崩效应是一种因“服务提供者的不可用”(原因)导致“服务调用者不可用”(结果),并将不可用逐渐放大的现象。如下图所示:        上图中,

springcloud hystrix入门简介(一)-爱代码爱编程

什么是Hystrix? Hystrix是Spring Cloud提供的一种带有熔断机制的框架,由于在微服务系统中同一个操作会由多个不同的微服务来共同完成,所以微服务与微服务之间会由很多相互的调用,由于在分布式环境中经常会出现某个微服务节点故障的情况,所以会由调用失败发生,而熔断器的作用就是当出现远程调用失败的时候提供一种机制来保证程序的正常运行而不会卡死

Springcloud之Hystrix容错机制-爱代码爱编程

1、什么是Hystrix 在一个分布式系统中,必然会有部分系统的调用会失败。所以就产生了Hystrix。Hystrix是一个 通过添加超时容错和失败容错逻辑来帮助你控制这些分布式系统的交互。Hystrix通过隔离服务之间的访问,阻止他们之间的级联故障以及提供后背选项来实现这些,所有新而这些都用来提高系统的整体弹性。 如不会搭建springcloud项目的

SpringCloud Hystrix 容错保护-爱代码爱编程

目录 提高系统容错的常见方法超时机制限流服务降级断路器hystrix简介hystrix的2种使用方式使用feign自带的hystrix直接使用hystrix   雪崩效应:也叫作级联故障,下游服务故障,拖跨上游服务,沿着服务调用链路逐级向上传播,造成上游服务大规模故障。 容错: 在分布式系统中,某些服务出现问题时,不会影响、拖垮其它服务(上

微服务学习笔记八 Spring Cloud Hystrix容错机制-爱代码爱编程

Hystrix 容错机制 在不改变各个服务器调用关系的前提下,针对错误情况进行预先处理。 设计原则: 1)服务隔离机制 2)服务降级机制 3)熔断机制 4)提供实时的监控和报警功能 5)提供实时的配置修改功能 Hystrix数据监控需要结合Spring Cloud Actuator来使用,Actuator提供了 对服务的健康监控、数据统计,可以通过hyst

springcloud-Hystrix初步配置-爱代码爱编程

Hystrix(服务熔断、监控) Hystrix是由Netflix开源的一个延迟和容错库,用于隔离访问远程系统、服务或者第三方库,防止级联失败,从而提升系统的可用性与容错性。Hystrix主要通过以下几点实现延迟和容错。 包裹请求:使用HystrixCommand包裹对依赖的调用逻辑,每个命令在独立线程中执行。这使用 了设计模式中的“命令模式”。跳闸机

Spring cloud 服务容错保护:Spring Cloud Hystrix-爱代码爱编程

        上一章,SrpingCloud 服务与消费 初识Eureka Consumer Ribbon_剑风偏冷的博客-CSDN博客   服务与消费 初识Eureka Consumer Ribbon。         在微服务架构中,存在着那么多的服务单元,若一个单元出现故障,就很容易因依赖关系而引发故障的蔓延,最终导致整个系统的瘫痪,这样的架构相