博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
5.Spring Cloud初相识-------Hystrix熔断器
阅读量:6907 次
发布时间:2019-06-27

本文共 8233 字,大约阅读时间需要 27 分钟。

前言:

1.介绍Hystrix

在一个分布式系统里,许多依赖不可避免的会调用失败,比如超时、异常等,如何能够保证在一个依赖出问题的情况下,不会导致整体服务失败,这个就是Hystrix需要做的事情。Hystrix提供了熔断、隔离、Fallback、cache、监控等功能,能够在一个、或多个依赖同时出现问题时保证系统依然可用。

2.为什么使用Hystrix

在分布式系统架构中多个系统之间通常是通过远程RPC调用进行通信,也就是 A 系统调用 B 系统服务,B 系统调用 C 系统的服务。当尾部应用 C 发生故障而系统 B 没有服务降级时候可能会导致 B,甚至系统 A 瘫痪,这种现象被称为雪崩现象。所以在系统设计时候要使用一定的降级策略,来保证当服务提供方服务不可用时候,服务调用方可以切换到降级后的策略进行执行。

实战:

1.新建服务消费者:

(1)添加依赖

4.0.0
com.xm.cloud
cl_hello_consumer_hy
0.0.1-SNAPSHOT
jar
cl_hello_consumer_hy
This is a Web about springcloud
org.springframework.boot
spring-boot-starter-parent
2.0.6.RELEASE
UTF-8
UTF-8
1.8
Finchley.SR2
org.springframework.boot
spring-boot-starter-web
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
org.springframework.cloud
spring-cloud-starter-netflix-hystrix
org.springframework.cloud
spring-cloud-starter-openfeign
org.springframework.boot
spring-boot-starter-test
test
org.springframework.boot
spring-boot-starter-actuator
org.springframework.cloud
spring-cloud-dependencies
${spring-cloud.version}
pom
import
org.springframework.boot
spring-boot-maven-plugin

(2)修改配置

eureka.client.service-url.defaultZone=http://127.0.0.1:7001/eureka/eureka.client.register-with-eureka=falsefeign.hystrix.enabled=true

(3)开启注解

package com.xm.cloud;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;import org.springframework.cloud.client.discovery.EnableDiscoveryClient;import org.springframework.cloud.openfeign.EnableFeignClients;@EnableDiscoveryClient@EnableFeignClients@SpringBootApplicationpublic class ClHelloConsumerHyApplication {    public static void main(String[] args) {        SpringApplication.run(ClHelloConsumerHyApplication.class, args);    }}

(4)配置熔断回调工厂

package com.xm.cloud.fallback;import org.springframework.stereotype.Component;import com.xm.cloud.service.HelloService;import feign.hystrix.FallbackFactory;@Componentpublic class HelloServiceFallbackFactory implements FallbackFactory
{ @Override public HelloService create(Throwable cause) { return new HelloService() { @Override public String sayHello() { return "HelloService 异常!"; } }; }}

(5)新建Service

package com.xm.cloud.service;import org.springframework.cloud.openfeign.FeignClient;import org.springframework.web.bind.annotation.GetMapping;import com.xm.cloud.fallback.HelloServiceFallbackFactory;@FeignClient(value="CL-HELLO-PRODUCER",fallbackFactory=HelloServiceFallbackFactory.class)public interface HelloService {        @GetMapping("/hello")    public String sayHello();}

(6)新建Controller

package com.xm.cloud.controller;import java.util.ArrayList;import java.util.List;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RestController;import com.xm.cloud.service.HelloService;@RestControllerpublic class HelloController {        @Autowired    private HelloService helloService;        @GetMapping("/hello")    public List
sayHello() { List
list = new ArrayList
(); for(int i=0;i<10;i++) { list.add(helloService.sayHello()); } return list; }}

2.新建服务提供者:

(1)添加依赖

4.0.0
com.xm.cloud
cl_hello_producer_hy
0.0.1-SNAPSHOT
jar
cl_hello_producer_hy
This is a Web about springcloud
org.springframework.boot
spring-boot-starter-parent
2.0.6.RELEASE
UTF-8
UTF-8
1.8
Finchley.SR2
org.springframework.boot
spring-boot-starter-web
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
org.springframework.cloud
spring-cloud-starter-netflix-hystrix
org.springframework.boot
spring-boot-starter-test
test
org.springframework.cloud
spring-cloud-dependencies
${spring-cloud.version}
pom
import
org.springframework.boot
spring-boot-maven-plugin

(2)修改配置

server.port=8001spring.application.name=cl-hello-producereureka.client.service-url.defaultZone=http://127.0.0.1:7001/eureka/

(3)开启注解

package com.xm.cloud;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;import org.springframework.cloud.client.discovery.EnableDiscoveryClient;@EnableDiscoveryClient@EnableCircuitBreaker@SpringBootApplicationpublic class ClHelloProducerHyApplication {    public static void main(String[] args) {        SpringApplication.run(ClHelloProducerHyApplication.class, args);    }}

(4)新建Controller

package com.xm.cloud.controller;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RestController;import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;@RestControllerpublic class HelloController {        @GetMapping("/hello")    @HystrixCommand    public String sayHello() {                if(Math.random()>0.5) {            throw new RuntimeException();        } else {            return "Hello spring cloud!";        }    }}

3.测试

运行:localhost:8080/hello

0 "HelloService 异常!"
1 "Hello spring cloud!"
2 "HelloService 异常!"
3 "Hello spring cloud!"
4 "Hello spring cloud!"
5 "Hello spring cloud!"
6 "Hello spring cloud!"
7 "Hello spring cloud!"
8 "HelloService 异常!"
9 "Hello spring cloud!"

转载于:https://www.cnblogs.com/TimerHotel/p/springcloud_05.html

你可能感兴趣的文章
代码编辑器Sublime_Text3的使用
查看>>
Docker Stack 部署web集群
查看>>
thinkphp源码分析(一)—开门篇
查看>>
猫叔产品读记 | 如何更好的玩转补贴、阿里入股B站商业化变现、儿童口腔市场怎么样?(3期)...
查看>>
Worse Is Better 思想的发展史
查看>>
力软移动框架 ionic cordova插件jpush-phonegap-plugin 极光推送配置方法 vs2017
查看>>
Will it finally: 关于 try/catch 的一些细节
查看>>
浅谈RPC
查看>>
翻译: Spring Cloud Feign使用文档
查看>>
vue-cli2 构建速度优化
查看>>
力扣(LeetCode)310
查看>>
Vue CLI 3 更改已经使用vue create 创建的项目的css pre-processor,添加stylus
查看>>
详解利用clear清除浮动的一些问题解决
查看>>
css浮动及其危害和解决方法
查看>>
【译】Go和WebAssembly:在浏览器中运行Go程序
查看>>
解决Linux下使用pm2运行nuxt2.0,IP+端口访问不了
查看>>
snabbdom源码解析(四) patch 方法
查看>>
Spring Boot项目使用maven-assembly-plugin根据不同环境打包成tar.gz或者zip
查看>>
《Redis入门指南(第2版)》摘要
查看>>
【C++】 2_C 到 C++ 的升级
查看>>