Spring-cloud Hystrix入门

Posted Hellxz博客

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 和 JPA - LazyInitializationException

Spring-Cloud系列-Hystrix源码解析

Spring-Cloud系列-Hystrix源码解析

Spring-Cloud之Hystrix

Spring-cloud Hystrix 请求缓存的使用

spring-cloud hystrix CompletableFuture