前提

  • 一个可用的Eureka注册中心(文中以之前博客中双节点注册中心,不重要)
  • 一个连接到这个注册中心的服务提供者

快速入门

项目搭建

搭建一个新maven项目,artifactid为Ribbon-consum-hystrix,依赖是在ribbon-customer项目上加入hystrix依赖,这里直接给出,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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>com.cnblogs.hellxz</groupId>
<artifactId>Ribbon-consum-hystrix</artifactId>
<version>1.0-SNAPSHOT</version> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
<relativePath/>
</parent> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
<version>RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-ribbon</artifactId>
<version>RELEASE</version>
</dependency>
<!--暴露各种指标-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency> <!--hystrix熔断器-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
<version>RELEASE</version>
</dependency>
</dependencies> <dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>RELEASE</version>
<scope>import</scope>
<type>pom</type>
</dependency>
</dependencies>
</dependencyManagement> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>

新建包com.cnblogs.hellxz,新建应用入口类RibbonApplication:

package com.cnblogs.hellxz;

import org.springframework.boot.SpringApplication;
import org.springframework.cloud.client.SpringCloudApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate; /**
* @Author : Hellxz
* @Description: Hystrix熔断器使用HelloWorld
* @Date : 2018/4/20 10:03
*/
@SpringCloudApplication
public class RibbonApplication { @Bean
@LoadBalanced
RestTemplate getRestTemplate(){
return new RestTemplate();
} public static void main(String[] args) {
SpringApplication.run(RibbonApplication.class, args);
}
}

resources目录下添加配置文件application.yml,其中defaultZone的url为实际注册中心defaultZone地址

server:
port: 8088 spring:
application:
name: ribbon-hystrix eureka:
client:
serviceUrl:
defaultZone: http://peer1:1111/eureka/,http://peer2:1112/eureka/

在com.cnblogs.hellxz包下分别新建controller包和service包,controller包中新建RibbonController,代码如下:

package com.cnblogs.hellxz.controller;

import com.cnblogs.hellxz.servcie.RibbonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController; /**
* @Author : Hellxz
* @Description: Ribbon消费者controller
* @Date : 2018/4/20 10:07
*/
@RestController
public class RibbonController { @Autowired
RibbonService service; @GetMapping("/hystrix/test")
public String helloHystrix(){
//调用服务层方法
return service.helloService();
} }

service包下新建RibbonService,代码如下:

package com.cnblogs.hellxz.servcie;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate; /**
* @Author : Hellxz
* @Description: Ribbon服务层
* @Date : 2018/4/20 10:08
*/
@Service
public class RibbonService { @Autowired
private RestTemplate restTemplate; @HystrixCommand(fallbackMethod = "hystrixFallback")
public String helloService(){
//调用服务提供者接口,正常则返回hello字符串
return restTemplate.getForEntity("http://hello-service/hello", String.class).getBody();
} /**
* 调用服务失败处理方法
* @return “error"
*/
public String hystrixFallback(){
return "error";
}
}

项目最后的结构如图

测试

分别启动 注册中心--->服务提供者--->刚新建的项目

访问http://localhost:8088/hystrix/test

因为现在服务是正常的,返回了hello,如图



此时关闭服务提供者项目,再次访问,返回了error,如图

SpringCloud(六) Hystrix入门的更多相关文章

  1. springcloud(六) Hystrix 熔断,限流

    Hystrix 熔断: 首先仍然启动Eureka,这里就不说了. OrderController.java: package com.tuling.cloud.study.user.controlle ...

  2. Hystrix入门教程

    Hystrix入门教程 一·什么是Hystrix?Hystrix有什么作用?使用Hystrix有哪些适用场景 Hystrix是springCloud的组件之一,Hystrix 可以让我们在分布式系统中 ...

  3. springcloud+eureka简单入门案例

    springcloud+eureka简单入门案例 一.服务提供者 直接提供服务,入门案例没有特别要设置的地方,注意下端口,由于要启动多个服务,可能会冲突 配置文件(src/main/resources ...

  4. 高并发场景-请求合并(一)SpringCloud中Hystrix请求合并

    背景 在互联网的高并发场景下,请求会非常多,但是数据库连接池比较少,或者说需要减少CPU压力,减少处理逻辑的,需要把单个查询,用某些手段,改为批量查询多个后返回. 如:支付宝中,查询"个人信 ...

  5. Spring-cloud(六) Hystrix入门

    前提 一个可用的Eureka注册中心(文中以之前博客中双节点注册中心,不重要) 一个连接到这个注册中心的服务提供者 快速入门 项目搭建 搭建一个新maven项目,artifactid为Ribbon-c ...

  6. SpringCloud无废话入门04:Hystrix熔断器及监控

    1.断路器(Circuit Breaker)模式 在上文中,我们人为停掉了一个provider,在实际的生产环境中,因为意外某个服务down掉,甚至某一层服务down掉也是会是有发生的.一旦发生这种情 ...

  7. 服务容错保护断路器Hystrix之一:入门示例介绍(springcloud引入Hystrix的两种方式)

    限流知识<高可用服务设计之二:Rate limiting 限流与降级> 在微服务架构中,我们将系统拆分成了一个个的服务单元,各单元间通过服务注册与订阅的方式互相依赖.由于每个单元都在不同的 ...

  8. SpringCloud之Hystrix服务降级入门全攻略

    理论知识 Hystrix是什么? Hystrix是由Netflix开源的一个服务隔离组件,通过服务隔离来避免由于依赖延迟.异常,引起资源耗尽导致系统不可用的解决方案.这说的有点儿太官方了,它的功能主要 ...

  9. 【微服务架构】SpringCloud之Hystrix断路器(六)

    一:什么是Hystrix 在分布式环境中,许多服务依赖项中的一些将不可避免地失败.Hystrix是一个库,通过添加延迟容差和容错逻辑来帮助您控制这些分布式服务之间的交互.Hystrix通过隔离服务之间 ...

随机推荐

  1. 08_Java基础语法_第8天(Eclipse)_讲义

    今日内容介绍 1.Eclipse开发工具 2.超市库存管理系统 01Eclipse的下载安装 * A: Eclipse的下载安装  * a: 下载 * http://www.eclipse.org ...

  2. Beta阶段——2

    一.提供当天站立式会议照片一张: 二. 每个人的工作 (有work item 的ID) (1) 昨天已完成的工作: 昨天主要是对beta阶段的任务做了总结 (2) 今天计划完成的工作: 今天主要是对管 ...

  3. 面试问题总结二(技术能力-PHP)----Ⅰ

    1.你都做过什么项目? 答:第一份实习工作接触的项目是CRM 销售管理系统,一款用JSP语言开发的进销存管理系统.第一份正式工作是一款主打高质量图片社交社区网站项目,“美啦周末”(后改型为”聊会儿”) ...

  4. CPU结合CS、IP寄存器进行执行程序

    上一篇介绍了CS.IP两个寄存器内容,当我们运行一个可执行文件时,我们需要另外一个程序来将这个可执行文件加载到内存当中,关于这个加载可执行文件的程序,我们在这里不管他,点一下即可,一般是通过操作系统的 ...

  5. 不再混淆,一次搞懂!图解flexbox十余个属性

    原文首发于个人博客:不再混淆,一次搞懂!图解flexbox十余个属性 flexbox的发明简直是csser的一大福音,终于可以不再需要为垂直居中一个元素而绞尽脑汁了.同时它还能够实现弹性布局,可以说没 ...

  6. bzoj2302-Problem c

    题意 有 \(n\) 个人,从 1 到 \(i\) 编号.给每个人一个值 \(a_i\) ,他们会按编号从小到大进行如下操作:查看 \(a_i\) 有没有人,若没有就坐进去,否则查看 \(a_i+1\ ...

  7. myeclipse8.6 注册码

    MyEclipse8.6 注册码 别处找的均是8.6版本,可以使用到2014年一:MyEclipse Standard Edition: zhucemLR7ZL-655954-695876566190 ...

  8. springmvc+mybatis 处理图片(一):上传图片

    一直觉得上传图片文件之类的很难,所以最后才处理图片,发现也并没有那么难,开始正文. 思路:将前台上传的file存到MutipartFile类型字段中,再将MulipartFile转换为pojo类中的b ...

  9. 【BZOJ1502】【NOI2005】月下柠檬树

    Portal 传送门 Solution 显然的是,每一个圆的影子,就是从树上的圆按光线方向平移至地面的圆.至于两个圆之间的连接部分,则是每两个在树上相邻的圆的,对应的影子圆的,公切线围起来的部分,如下 ...

  10. BZOJ4416 [Shoi2013]阶乘字符串 【序列自动机 + 状压dp】

    题目链接 BZOJ4416 题解 建立序列自动机,即预处理数组\(nxt[i][j]\)表示\(i\)位置之后下一个\(j\)出现的位置 设\(f[i]\)表示合法字符集合为\(i\)的最短前缀,枚举 ...