Fork me on GitHub

SpringCloud个人笔记-06-Hystrix初体验

 

  • sb_cloud_hystrix

 

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

 

<?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.huarui</groupId>
    <artifactId>sb_cloud_hystrix</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>sb_cloud_hystrix</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.19.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <spring-cloud.version>Edgware.SR3</spring-cloud.version>
    </properties>

    <dependencies>
        <!-- eureka client -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>

        <!-- config依赖 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>

        <!-- -hystrix依赖 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-hystrix</artifactId>
        </dependency>

    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>
pom.xml

 

spring:
  application:
    name: sb-cloud-hystrix

eureka:
  client:
    serviceUrl:
      defaultZone: http://39.108.85.204:8761/eureka/

server:
  port: 9006

 

/*
@SpringBootApplication
@EnableDiscoveryClient
@EnableCircuitBreaker 开启熔断器
*/
@SpringCloudApplication //相当于注释的三个
public class SbCloudHystrixApplication {

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

}

 

  • 服务降级
package com.huarui.controller;

import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.Arrays;

/**
* Created by Shinelon on 2019/3/15.
*/
@RestController
@DefaultProperties(defaultFallback = "defaultFallback")
public class HystrixController {


@HystrixCommand(fallbackMethod = "fallback")
@GetMapping("/getProductList")
public String getProductList(){

RestTemplate restTemplate = new RestTemplate();
return restTemplate.getForObject("http://localhost:9000/product/getMsg",
String.class);
}

@HystrixCommand //将会返回默认 defaultFallback
@GetMapping("/getProductList2")
public String getProductListDefa(){

RestTemplate restTemplate = new RestTemplate();
return restTemplate.getForObject("http://localhost:9000/product/getMsg",
String.class);
}

  //服务方不能提供服务 客户端发生异常 访问超时等
private String fallback(){
return "太拥挤了,请稍后再试";
}

private String defaultFallback(){
return "全局返回 稍后再试";
}

}

 

 

  •  超时设置

hystrix默认超时时间为1000毫秒

 

package com.huarui.controller;

import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import java.util.Arrays;

/**
 * Created by Shinelon on 2019/3/15.
 */
@RestController
@DefaultProperties(defaultFallback = "defaultFallback")
public class HystrixController {


    //将超时时间设置为3秒
    @HystrixCommand(fallbackMethod = "fallback",commandProperties = {
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "3000")
    })
    @GetMapping("/getProductList")
    public String getProductList(){

        RestTemplate restTemplate = new RestTemplate();
        return restTemplate.getForObject("http://localhost:9000/product/getMsg",
                String.class);
    }

    //默认超时间为1秒 【商品服务中休眠了两秒,所以触发服务降级】
    @HystrixCommand
    @GetMapping("/getProductList2")
    public String getProductListDefa(){

        RestTemplate restTemplate = new RestTemplate();
        return restTemplate.getForObject("http://localhost:9000/product/getMsg",
                String.class);
    }

    private String fallback(){
        return "太拥挤了,请稍后再试";
    }

    private String defaultFallback(){
        return "全局返回 稍后再试";
    }

}

 

 

  • 配置文件方式配置超时时间
#配置 yml格式配置hystrix 超时时间
hystrix:
  command:
    default:
      execution:
        isolation:
          thread:
            timeoutInMilliseconds: 3000
    getProductListDefa: # 与commonKey对应
      execution:
            isolation:
              thread:
                timeoutInMilliseconds: 3000

 

 在方法添加@HystrixCommand 注解

 

posted @ 2019-03-16 11:34  youxiu326  阅读(88)  评论(0编辑  收藏  举报