前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >SpringCloud 2.x学习笔记:3、Hystrix(Greenwich版本)

SpringCloud 2.x学习笔记:3、Hystrix(Greenwich版本)

作者头像
程裕强
发布2019-07-02 10:39:06
4870
发布2019-07-02 10:39:06
举报

版权声明:本文为博主原创文章,欢迎转载。 https://cloud.tencent.com/developer/article/1454224

1、Hystrix基本思想

在分布式系统当中,服务之间调用关系会随着业务的发展而变的复杂,一个服务可能依赖多个服务,服务之间层层依赖;一个服务的瘫痪可能导致整个系统的崩溃。

与电闸思想类似:每栋房子,每户都安装了电闸,电闸的作用是保证有一家电路出现短路时,电闸进行断电跳闸的操作,这样不至于导致整栋楼用电瘫痪。

2、新建hystrix模块

2.1 pom文件
代码语言:javascript
复制
<?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.cntaiping.tpa</groupId>
    <artifactId>hystrix</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
    <name>hystrix</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>com.cntaiping.tpa</groupId>
        <artifactId>cloud</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
        </dependency>
    </dependencies>

</project>
2.2 配置文件application.properties
代码语言:javascript
复制
#服务名称
spring.application.name=consumer-hystrix
#端口号
server.port=8505
#在注册中心中进行注册
eureka.client.serviceUrl.defaultZone=http://localhost:8800/eureka/
2.3 Application入口
代码语言:javascript
复制
package com.cntaiping.tpa.hystrix;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
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
@EnableHystrix
public class HystrixApplication {

    public static void main(String[] args) {
        SpringApplication.run(HystrixApplication.class, args);
    }

    @Bean
    @LoadBalanced
    RestTemplate restTemplate() {
        return new RestTemplate();
    }
}
2.4 服务消费类

在hello方法上加上@HystrixCommand注解。该注解对该方法创建了熔断器的功能,并指定了fallbackMethod熔断方法,熔断方法直接返回了一个字符串

代码语言:javascript
复制
package com.cntaiping.tpa.hystrix.service;

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;

@Service
public class HelloService {
    @Autowired
    RestTemplate restTemplate;

    @HystrixCommand(fallbackMethod = "error")
    public String hello(String name) {
        return restTemplate.getForObject("http://producer/get?name="+name, String.class);
    }

    public String error(String name) {
        return "hi,"+name+",sorry,error!";
    }
}
代码语言:javascript
复制
package com.cntaiping.tpa.hystrix.controller;

import com.cntaiping.tpa.hystrix.service.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {
    @Autowired
    HelloService helloService;

    @GetMapping(value = "/hello")
    public String hello(@RequestParam String name) {
        return helloService.hello( name );
    }
}

3、运行效果

(1)注册中心

(2)consumer-hystrix模块

http://localhost:8505/hello?name=chengyq

(3)测试熔断

将服务提供方producer的两个实例进程(8700和8701)关闭,再次运行http://localhost:8505/hello?name=chengyq

代码语言:javascript
复制
hi,chengyq,sorry,error!

这就说明当producer模块不可用的时候,consumer-hystrix调用producer的API接口时,会执行快速失败,直接返回一组字符串,而不是等待响应超时,这很好的控制了容器的线程阻塞。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019年05月31日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1、Hystrix基本思想
  • 2、新建hystrix模块
    • 2.1 pom文件
      • 2.2 配置文件application.properties
        • 2.3 Application入口
          • 2.4 服务消费类
          • 3、运行效果
          相关产品与服务
          微服务引擎 TSE
          微服务引擎(Tencent Cloud Service Engine)提供开箱即用的云上全场景微服务解决方案。支持开源增强的云原生注册配置中心(Zookeeper、Nacos 和 Apollo),北极星网格(腾讯自研并开源的 PolarisMesh)、云原生 API 网关(Kong)以及微服务应用托管的弹性微服务平台。微服务引擎完全兼容开源版本的使用方式,在功能、可用性和可运维性等多个方面进行增强。
          领券
          问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档