断路器:就是对服务访问不到的情况做出自己的错误,也就是故障转移(将当前出现故障的请求重新返回特定消息)

改造消费者项目(RibbonDemo)

1、在pom.xml中引入hystrix的jar包

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>

2、在RibbonApp类开启断路器(@EnableHystrix)

package com.cppdy;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate; @SpringBootApplication
@EnableDiscoveryClient
//开启断路器
@EnableHystrix
public class RibbonApp { public static void main(String[] args) {
SpringApplication.run(RibbonApp.class, args);
} @Bean
@LoadBalanced
RestTemplate template() { return new RestTemplate();
} }

3、在HelloService类注入断路器(@HystrixCommand(fallbackMethod = "helloError"))

package com.cppdy.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate; import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand; @Service
public class HelloService { @Autowired
private RestTemplate template; @HystrixCommand(fallbackMethod = "helloError")
public String hellService(String name) { return template.getForObject("http://CPPDY-HELLO/hello?name" + name, String.class);
} // 断路时的回调方法
public String helloError(String name) { return "Service Error:" + name;
}
}

4、先启动EurekaDemo(注册中心项目),再启动RibbonDemo(消费者项目)端口号设置为9003,访问http://localhost:9003/hello,会调用断路器设置的回调方法

断路器Ribbon的更多相关文章

  1. 玩转SpringCloud(F版本) 三.断路器(Hystrix)RestTemplate+Ribbon和Feign两种方式

    此文章基于: 玩转SpringCloud 一.服务的注册与发现(Eureka) 玩转SpringCloud 二.服务消费者(1)ribbon+restTemplate 转SpringCloud 二.服 ...

  2. spring cloud ribbon 断路器

    @EnableDiscoveryClient @SpringBootApplication @EnableCircuitBreaker //开启断路器 public class ConsumerMov ...

  3. SpringCloud2.0 Hystrix Ribbon 基于Ribbon实现断路器

    原文:https://www.cnblogs.com/songlu/p/9949203.html 1.启动[服务中心]集群,工程名:springcloud-eureka-server 参考 Sprin ...

  4. SpringCloud2.0 Hystrix Ribbon 基于Ribbon实现断路器 基础教程(六)

    1.启动[服务中心]集群,工程名:springcloud-eureka-server 参考 SpringCloud2.0 Eureka Server 服务中心 基础教程(二) 2.启动[服务提供者]集 ...

  5. 断路器Hystrix(Ribbon)

    微服务架构中,根据业务划分成若干个服务,各单元应用间通过服务注册与订阅的方式互相依赖,依赖通过远程调用的方式执行,该方式难以避免因网络或自身原因而出现故障或者延迟,从而并不能保证服务的100%可用,此 ...

  6. Spring Cloud Ribbon 整合 Hystrix

    在前面随笔 Spring Cloud 之 Ribbon 的ribbon工程基础上进行改造 1.pom.xml 加入依赖 <dependency> <groupId>org.sp ...

  7. spring cloud ribbon和fegin

    一开始接触spring cloud的时候,还没有听说过微服务这个概念,对于服务直接的沟通是什么个情况,怎么组成微服务的完全懵逼,看到网上的教程都是用ribbong和fegin来调用接口,然后官网也给的 ...

  8. spring cloud 入门系列四:使用Hystrix 实现断路器进行服务容错保护

    在微服务中,我们将系统拆分为很多个服务单元,各单元之间通过服务注册和订阅消费的方式进行相互依赖.但是如果有一些服务出现问题了会怎么样? 比如说有三个服务(ABC),A调用B,B调用C.由于网络延迟或C ...

  9. Spring Cloud入门教程-Ribbon实现客户端负载均衡

    简介 我们继续以之前博客的代码为基础,增加Ribbon组件来提供客户端负载均衡.负载均衡是实现高并发.高性能.可伸缩服务的重要组成部分,它可以把请求分散到一个集群中不同的服务器中,以减轻每个服务器的负 ...

随机推荐

  1. 添加 node mocha 测试模块

    1.mocha  支持TDD 和 BDD两种测试风格 2.引用assert模块  此模块是node的原生模块,实现断言的功能,作用是声明预期的结果必须满足 3.mocha测试用例中可以使用第三方测试库 ...

  2. 如何在 JS 中嵌入大量 HTML 代码 --更新2017-10-10 16:37:09

    一.一般方式 方式1:\ 要写在标签后面,看例子 var longString = "\ ------------------------\ ------------------------ ...

  3. (3)java数据结构--有枚举 属性

    java中的数据结构 - 南风顾 - 博客园http://www.cnblogs.com/tingxuelou/p/6686143.html 线性表,链表,哈希表是常用的数据结构,在进行Java开发时 ...

  4. 20165221 JAVA第五周学习心得

    课本知识点 内部类与异常类 内部类:在一个类中定义另一个类 特点:外嵌类的成员在内部类仍然有效,内部类也可调用外嵌类的方法,内部类的类体不能声明类变量及类方法 非内部类不能是static类 匿名类:创 ...

  5. 【python网络爬虫】之requests相关模块

    python网络爬虫的学习第一步 [python网络爬虫]之0 爬虫与反扒 [python网络爬虫]之一 简单介绍 [python网络爬虫]之二 python uillib库 [python网络爬虫] ...

  6. 【ARTS】01_15_左耳听风-20190218~20190224

    ARTS: Algrothm: leetcode算法题目 Review: 阅读并且点评一篇英文技术文章 Tip/Techni: 学习一个技术技巧 Share: 分享一篇有观点和思考的技术文章 Algo ...

  7. MFC调用libyara遇到的问题

    测试结果 如果调用yara非要变成共享DLL的形式,加那么多的DLL,不如直接调用EXE文件.反而依赖vcruntime运行库的DLL会少很多... 调用libyara 调用libraya和C++调用 ...

  8. python下载夏目友人帳

    python下载夏目友人帐 一般情况下我们使用爬虫更多的应该是爬数据或者图片吧,今天在这里和大家分享一下关于使用爬虫技术来进行视频下载的方法,不仅可以方便的下载一些体积小的视频,针对大容量的视频下载同 ...

  9. requests库入门07-patch请求

    使用data参数提交 设置邮件能见度,这个接口用来设置邮件是公共可见,还是私有的 import requests test_url = 'https://api.github.com' def get ...

  10. 题解-poj3682King Arthur's Birthday Celebration

    Problem poj-3682 题目大意:抛一次硬币有\(p\)的概率得到正面,当有\(n\)次正面时停止,抛第\(i\)次的花费为\(2i-1\),求抛的期望次数和期望花费 Solution 本来 ...