2019-06-12 hystrix 入门

1、pom 文件

        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

    4.0.0

   

        org.springframework.boot

        spring-boot-starter-parent

        2.1.5.RELEASE

       

   

    com.zbiti.zz

    order

    0.0.1-SNAPSHOT

    order

    Demo project for Spring Boot

   

        1.8

        Greenwich.SR1

   

   

       

            org.springframework.boot

            spring-boot-starter-web

       

       

       

            org.springframework.cloud

            spring-cloud-starter-netflix-hystrix

       

       

            org.springframework.cloud

            spring-cloud-starter-netflix-eureka-client

       

       

            org.springframework.cloud

            spring-cloud-starter-feign

            1.4.0.RELEASE

       

       

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

你可能感兴趣的:(2019-06-12 hystrix 入门)