断路器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组件来提供客户端负载均衡.负载均衡是实现高并发.高性能.可伸缩服务的重要组成部分,它可以把请求分散到一个集群中不同的服务器中,以减轻每个服务器的负 ...
随机推荐
- luogu P2520 [HAOI2011]向量
传送门 一堆人说数论只会gcd,我连gcd都不会,菜死算了qwq Orzyyb 这题欺负我数学不好qwq 首先可以发现实际上有如下操作:x或y±2a,x或y±2b,x+a y+b,x+b y+a(后面 ...
- 利用xpath爬取招聘网的招聘信息
爬取招聘网的招聘信息: import json import random import time import pymongo import re import pandas as pd impor ...
- linux全部替换命令学习
:%s/准备替换内容/新内容/g 可以使用 # 作为分隔符,此时中间出现的 / 不会作为分隔符 :%s#vivian/#sky/#g 替换 vivian/ 为 sky/ :%s+/oradata/ap ...
- 帮助类-AD域操作
private static void GetAllUsersInAD() { DirectorySearcher searcher = new DirectorySearcher(); search ...
- chrome调试技巧和插件介绍
14 个你可能不知道的 JavaScript 调试技巧 五种前端开发必备的调试技术 日志的艺术 吐血推荐珍藏的Chrome插件 吐血推荐珍藏的 Chrome 插件(二)
- java 写 Excel(不生成实体文件,写为流的形式)
java 写 Excel(不生成实体文件,写为流的形式) public String exportReportExcel(String mediaCode, List<SimpleMediaRe ...
- 自动安装gulpfile中所有依赖的模块
在gulpfile.js中定义任务的时候,需要引用不同的模块,这些模块需要使用 npm install --save-dev 命令进行安装,非常的繁琐.可以使用一个名为gulpfile-install ...
- shell脚本 统计一段程序运行时间【转】
转自:https://bbs.csdn.net/topics/391943383#include <stdio.h> #include <stdlib.h> #include ...
- linux中bashrc与profile的区别
bashrc与profile的区别 要搞清bashrc与profile的区别,首先要弄明白什么是交互式shell和非交互式shell,什么是login shell 和non-login shell. ...
- 【转】C++标准转换运算符reinterpret_cast
reinterpret_cast<new_type> (expression) reinterpret_cast运算符是用来处理无关类型之间的转换:它会产生一个新的值,这个值会有与原始参数 ...