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

改造消费者项目(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. python 函数名 、闭包 装饰器 day13

    1,函数名的使用. 函数名是函数的名字,本质就是变量,特殊的变量.函数名()加括号就是执行此函数. 1,单独打印函数名就是此函数的内存地址. def func1(): print(555) print ...

  2. scss转css的过程中 转化问题

    如果  在转化过程中语法没有问题的话    测试一下是否是文件的bom头的问题

  3. zabbix系列 ~ 如何更好的利用mysql监控

    一 简介:今天来聊聊一些关于mysql 监控需要关注的问题二 实现的原理   1 global status   2 variables 三 我们需要关注的zabbix性能图  1 事务类型      ...

  4. Nginx系列6:对称加密与非对称加密各自的应用场景

    强推:推荐一篇通俗易懂的对称加密和非对称加密的文章:https://segmentfault.com/a/1190000004461428 推荐一篇文章:对称加密算法与非对称加密算法的优缺点:http ...

  5. drand48 等 随机数生成函数

    参考: http://www.man7.org/linux/man-pages/man3/drand48.3.html drand48 返回服从均匀分布的·[0.0, 1.0) 之间的 double ...

  6. SpringCloud概述

    ⒈官网说明 SpringCloud是基于SpringBoot提供了一套微服务解决方案,包括服务注册与发现.配置中心.全链路监控.服务网关.负载均衡.熔断器等组件,除了基于Netflix的开源组件做高度 ...

  7. 2017-2018-2 20165231实验二《Java面向对象程序设计》实验报告

    实验报告封面 课程:Java程序设计 班级:1652班 姓名:王杨鸿永 学号:20165231 指导教师:娄嘉鹏 实验日期:2018年4月16日 实验时间:13:45 - 15:25 实验序号:实验二 ...

  8. Node.js WEB服务器(1)——编写简单的HTTP服务器

    基于分而治之的思想,很多架构都会采用分层的模式来处理某类应用.Node.js在Web开发上也是采用这种模式,分别提供了如下的三个层: Node核心 社区模块 应用逻辑 下面是一个最简形式的web服务器 ...

  9. 利用Linux系统生成随机密码的10种方法【转】

    Linux操作系统的一大优点是对于同样一件事情,你可以使用高达数百种方法来实现它.例如,你可以通过数十种方法来生成随机密码.本文将介绍生成随机密码的十种方法. 1. 使用SHA算法来加密日期,并输出结 ...

  10. U盘文件系统格式

    u盘文件系统主要有FAT32.NTFS两种 ● FAT32文件系统 FAT32使用4个字节(也就是32位)的空间来表示每个扇区(Sector)配置文件的情形,故称之为FAT32.FAT16的分区容量上 ...