2019-06-12 hystrix 入门

2019-06-12  本文已影响0人  zz云飞扬

1、pom 文件

<?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>

    <parent>

        <groupId>org.springframework.boot</groupId>

        <artifactId>spring-boot-starter-parent</artifactId>

        <version>2.1.5.RELEASE</version>

        <relativePath/> <!-- lookup parent from repository -->

    </parent>

    <groupId>com.zbiti.zz</groupId>

    <artifactId>order</artifactId>

    <version>0.0.1-SNAPSHOT</version>

    <name>order</name>

    <description>Demo project for Spring Boot</description>

    <properties>

        <java.version>1.8</java.version>

        <spring-cloud.version>Greenwich.SR1</spring-cloud.version>

    </properties>

    <dependencies>

        <dependency>

            <groupId>org.springframework.boot</groupId>

            <artifactId>spring-boot-starter-web</artifactId>

        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-netflix-hystrix -->

        <dependency>

            <groupId>org.springframework.cloud</groupId>

            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>

        </dependency>

        <dependency>

            <groupId>org.springframework.cloud</groupId>

            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>

        </dependency>

        <dependency>

            <groupId>org.springframework.cloud</groupId>

            <artifactId>spring-cloud-starter-feign</artifactId>

            <version>1.4.0.RELEASE</version>

        </dependency>

        <dependency>

            <groupId>org.springframework.boot</groupId>

            <artifactId>spring-boot-starter-test</artifactId>

            <scope>test</scope>

        </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>

2、application.yml

#feign 客户端调用, 下面这句配熔断时间

hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds: 6000

server:

  port: 1007

spring:

  application:

    name: order

eureka:

  client:

    service-url:

      defaultZone: http://localhost:1005/eureka/

    register-with-eureka: true

    fetch-registry: true

feign:

  hystrix:

    enabled: true

#配置ribbon 调用超时, 如果是网关,这个配置很重要

ribbon:

    ReadTimeout: 6000

    SocketTimeout: 6000

3、

3.1 feign 接口

//value 是被调用服务名称,fallback 是 实现本接口的 一个类的class 文件

@FeignClient(value ="member",fallback =             com.zbiti.zz.order.service.MemberApiFeignFallback.class)

public interface MemberApiFeign {

//服务中方法的映射路径

    @RequestMapping("/getMember")

    public String getMember();

}

3.2 fallback 类

@Component

public class MemberApiFeignFallback implements MemberApiFeign {

@Override

    public String getMember() {

        return "busy, please try again later";

    }

}

3.3 controller 调用

@RestController

public class OrderApiController {

@Autowired

    private MemberApiFeign  memberApiFeign;

    /*@HystrixCommand(commandProperties={@HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds", value="9000")} ) 有这个属性设置调用回调的时间,但是设置后测试了,没发现作用*/

    @RequestMapping("/feign/remote")

    public String feignRemoteGetMember(){

        return memberApiFeign.getMember();

    }

}

3.4 程序入口类

@SpringBootApplication

@EnableEurekaClient

@EnableFeignClients

@EnableHystrix

public class OrderApplication {

public static void main(String[] args) {

SpringApplication.run(OrderApplication.class, args);

}

}

问题总结:

1、 未设置hystrix 熔断时间, 我在member 服务中,getMember () 方法中睡1.5 秒,加了hystrix 会走 fallback, 怎么设置 程序在2 秒后 再走fallback? 

方式是: hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds: 6000 这句配熔断时间 (6000毫秒)。 注意,是在application.yml 中第一行放入这句配置, 格式确是和 appliation.properties 差不多,注意后面是 “:”, yml 中换成“= ”会报错。

yml中也可以这样写:(其中hystrix 和eureka的起始位置对齐,并没有放到feign下面)

hystrix:

    command:

        default:

            execution:

                isolation:

                    thread:

                        timeoutInMilliseconds: 2000

2、 feign 接口, 一个接口(jar 包里有实现类),一个fallback 实现类, 导致controller 中注入接口时 提示有多个 bean。

这个目前不能解决

用RestTemplate远程调用的时候, 配套的hystrix配置如下,如果用feign,hystrix 在feign 底下,不知道能不能单独这样配置hystrix

用resttemplate的时候这样配置的demo
上一篇下一篇

猜你喜欢

热点阅读