Spring-cloud(六) Hystrix入门
前提
- 一个可用的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
,如图
Spring-cloud(六) Hystrix入门的更多相关文章
- Spring Cloud中Hystrix、Ribbon及Feign的熔断关系是什么?
导读 今天和大家聊一聊在Spring Cloud微服务框架实践中,比较核心但是又很容易把人搞得稀里糊涂的一个问题,那就是在Spring Cloud中Hystrix.Ribbon以及Feign它们三者之 ...
- 《Spring Cloud微服务 入门 实战与进阶》
很少在周末发文,还是由于昨晚刚收到实体书,还是耐不住性子马上发文了. 一年前,耗时半年多的时间,写出了我的第一本书<Spring Cloud微服务-全栈技术与案例解析>. 时至今日,一年的 ...
- Spring Cloud中Hystrix 线程隔离导致ThreadLocal数据丢失问题分析
最近spring boot项目中由于使用了spring cloud 的hystrix 导致了threadLocal中数据丢失,其实具体也没有使用hystrix,但是显示的把他打开了,导致了此问题. 导 ...
- 笔记:Spring Cloud Zuul 快速入门
Spring Cloud Zuul 实现了路由规则与实例的维护问题,通过 Spring Cloud Eureka 进行整合,将自身注册为 Eureka 服务治理下的应用,同时从 Eureka 中获取了 ...
- 架构师系列文:通过Spring Cloud组件Hystrix合并请求
在前文里,我们讲述了通过Hystrix进行容错处理的方式,这里我们将讲述通过Hystrix合并请求的方式 哪怕一个URL请求调用的功能再简单,Web应用服务都至少会开启一个线程来提供服务,换句话说,有 ...
- Spring Cloud Zuul 快速入门
Spring Cloud Zuul 实现了路由规则与实例的维护问题,通过 Spring Cloud Eureka 进行整合,将自身注册为 Eureka 服务治理下的应用,同时从 Eureka 中获取了 ...
- 笔记:Spring Cloud Feign Hystrix 配置
在 Spring Cloud Feign 中,除了引入了用户客户端负载均衡的 Spring Cloud Ribbon 之外,还引入了服务保护与容错的工具 Hystrix,默认情况下,Spring Cl ...
- Spring Cloud微服务笔记(三)服务治理:Spring Cloud Eureka快速入门
服务治理:Spring Cloud Eureka 一.服务治理 服务治理是微服务架构中最为核心和基础的模块,它主要用来实现各个微服务实例的自动化注册与发现. 1.服务注册: 在服务治理框架中,通常会构 ...
- 从零开始学spring cloud(十一) -------- hystrix监控
一.官方文档阅读 服务启动后,可以通过/health和hystrix.stream查看效果,实际上,访问上述两个地址,会出现404,这是因为spring boot版本的问题, 我在这里使用的sprin ...
- Spring Cloud断路器Hystrix
在微服务架构中,存在着那么多的服务单元,若一个单元出现故障,就会因依赖关系形成故障蔓延,最终导致整个系统的瘫痪,这样的架构相较传统架构就更加的不稳定.为了解决这样的问题,因此产生了断路器模式. 什么是 ...
随机推荐
- 使用jQuery获取session中存储的list集合
在网上查找了很多关于jQuery获取session都不可得,如果大家有更好的方式,欢迎留言 这里是使用jQuery发送Ajax请求到后台获取session jsp中没有代码 js代码 <scri ...
- Krajee插件的用法
第一步: <!-- 必须引入 --> link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/boots ...
- webpack打包性能优化
1. 使用 gzip 压缩打包后的 js 文件 这个方法优化浏览器下载时的文件大小(打包后的文件大小没有改变) webpack.config.prod.js 中 var CompressionWebp ...
- Http协议消息报头
哎.不知道怎么写Http协议... 超文本传输协议(HTTP,HyperText Transfer Protocol)是互联网上应用最为广泛的一种网络协议. HTTP基于TCP/IP通信协议来传递数据 ...
- php Redis函数使用总结(string,hash,list, set , sort set )
对于:string, set , sort set , hash 的增,改操作,是同一个命令,但是把它当改操作时,及时成功返回值依旧为0 对于:list结构来说,增删改查自有一套方法. <? ...
- 远程连接服务器jupyter notebook、浏览器以及深度学习可视化方法
h1 { counter-reset: h2counter; } h2 { counter-reset: h3counter; } h3 { counter-reset: h4counter; } h ...
- TNS-12560: Message 12560 not found; No message file for product=network, facility=TNS报错
[oracle@localhost bin]$ ./lsnrctl startLSNRCTL for Linux: Version 12.2.0.1.0 - Production on 17-APR- ...
- 0316-关于js 注意事项
js:脚本语言,控制网页的行为(特效,表单验证) /* 这是多行注释 *///这是单行注释 js问题及注意事项: JavaScript 对大小写是敏感的 多写注释.提示性文字要说明白 定义变量时最好加 ...
- Go/Python/Erlang编程语言对比分析及示例
本文主要是介绍Go,从语言对比分析的角度切入.之所以选择与Python.Erlang对比,是因为做为高级语言,它们语言特性上有较大的相似性,不过最主要的原因是这几个我比较熟悉. Go的很多语言特性借鉴 ...
- C++11的value category(值类别)以及move semantics(移动语义)
转载请保留以下声明 作者:赵宗晟 出处:http://www.cnblogs.com/zhao-zongsheng/p/value_categories_and_move_semantics.html ...