断路器Ribbon
断路器:就是对服务访问不到的情况做出自己的错误,也就是故障转移(将当前出现故障的请求重新返回特定消息)
改造消费者项目(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的更多相关文章
- 玩转SpringCloud(F版本) 三.断路器(Hystrix)RestTemplate+Ribbon和Feign两种方式
此文章基于: 玩转SpringCloud 一.服务的注册与发现(Eureka) 玩转SpringCloud 二.服务消费者(1)ribbon+restTemplate 转SpringCloud 二.服 ...
- spring cloud ribbon 断路器
@EnableDiscoveryClient @SpringBootApplication @EnableCircuitBreaker //开启断路器 public class ConsumerMov ...
- SpringCloud2.0 Hystrix Ribbon 基于Ribbon实现断路器
原文:https://www.cnblogs.com/songlu/p/9949203.html 1.启动[服务中心]集群,工程名:springcloud-eureka-server 参考 Sprin ...
- SpringCloud2.0 Hystrix Ribbon 基于Ribbon实现断路器 基础教程(六)
1.启动[服务中心]集群,工程名:springcloud-eureka-server 参考 SpringCloud2.0 Eureka Server 服务中心 基础教程(二) 2.启动[服务提供者]集 ...
- 断路器Hystrix(Ribbon)
微服务架构中,根据业务划分成若干个服务,各单元应用间通过服务注册与订阅的方式互相依赖,依赖通过远程调用的方式执行,该方式难以避免因网络或自身原因而出现故障或者延迟,从而并不能保证服务的100%可用,此 ...
- Spring Cloud Ribbon 整合 Hystrix
在前面随笔 Spring Cloud 之 Ribbon 的ribbon工程基础上进行改造 1.pom.xml 加入依赖 <dependency> <groupId>org.sp ...
- spring cloud ribbon和fegin
一开始接触spring cloud的时候,还没有听说过微服务这个概念,对于服务直接的沟通是什么个情况,怎么组成微服务的完全懵逼,看到网上的教程都是用ribbong和fegin来调用接口,然后官网也给的 ...
- spring cloud 入门系列四:使用Hystrix 实现断路器进行服务容错保护
在微服务中,我们将系统拆分为很多个服务单元,各单元之间通过服务注册和订阅消费的方式进行相互依赖.但是如果有一些服务出现问题了会怎么样? 比如说有三个服务(ABC),A调用B,B调用C.由于网络延迟或C ...
- Spring Cloud入门教程-Ribbon实现客户端负载均衡
简介 我们继续以之前博客的代码为基础,增加Ribbon组件来提供客户端负载均衡.负载均衡是实现高并发.高性能.可伸缩服务的重要组成部分,它可以把请求分散到一个集群中不同的服务器中,以减轻每个服务器的负 ...
随机推荐
- LAS(Listener、Attender、Speller)端到端构架
基于注意力(Attention)机制的端到端系统,又被称为LAS端到端构架. [6] W. Chan, N. Jaitly, Q. Le, O. Vinyals. Listen, Attend and ...
- python 数据分析2
本节概要 Numpy详解 安装 Numpy的安装已经不想多说..在确保pip或pip3的路径被添加到系统环境变量里面之后,就可以直接用下面语句进行安装. pip install numpy or pi ...
- mysql 查询优化 ~ 多表查询改写思路
一 简介:在之前我们从基础可知,现在咱们聊一下改写的几种思路二 分类: 1 left join 2 inner join 3 right join三 具体改写思路:思路1 本身不包含子查询,将多 ...
- 20165237 学习基础和C语言基础调查
学习基础和C语言基础调查 一.技能学习与特长 你有什么技能比大多人(超过90%以上)更好? 我的爱好和技能说实话挺广泛的.如果要挑出来一个很擅长的话,我觉得应该是钢琴. 针对这个技能的获取你有什么成功 ...
- pythonの连接MySQL数据库
1.要确保开发环境中安装了pymsql,如果没有安装那么在控制台输入: pip3 install pymysql 安装完成后,打开编辑器: #!/usr/bin/env python import p ...
- [sklearn] 官方例程-Imputing missing values before building an estimator 随机填充缺失值
官方链接:http://scikit-learn.org/dev/auto_examples/plot_missing_values.html#sphx-glr-auto-examples-plot- ...
- WC2019 游记
Day 0 早上奇迹般的六点半起床平常这时候我还没睡呢 早餐在武汉站吃了一碗28的番茄牛肉米线,结果上菜后我把所有非米线的固体(包括番茄和牛肉)全挑出去了 高二大佬:一个愿宰一个愿挨 在高铁上待了四个 ...
- HAProxy详解(三):基于虚拟主机的HAProxy负载均衡系统配置实例【转】
一.基于虚拟主机的HAProxy负载均衡系统配置实例 1.通过HAProxy的ACL规则配置虚拟主机: 下面将通过HAProxy的ACL功能配置一套基于虚拟主机的负载均衡系统.这里操作系统环境为:Ce ...
- C# 基础之string[,] 和string[][]
昨天做项目时碰到一个问题,后台返回一张关系表,里面就两个字段,简化为A,B字段(1:N的关系),一个A对应多个B字段. 由于对于string[,] 和string[][] 的认识不到位,刚开始搞错了数 ...
- http 遇到中文表单 转码
#include <string> #include <vector> inline BYTE toHex(const BYTE x) { return x>9?x+55 ...