Hystrix的用法

package com.example.demo;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/app")
public class AppController {

    @RequestMapping("/get/{id}")
    @HystrixCommand(fallbackMethod = "getFallBack",  commandProperties = {
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "500")
    })
    public  String get(@PathVariable("id") long id) throws  Exception  {

     // throw   new Exception("error");
 Thread.sleep(id);
        return  "get";
    }

    public String getFallBack(@PathVariable("id") long id) {
        return "getFallBack";

    }
}

 

package com.example.demo;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/app")
public class AppController {

@RequestMapping("/get/{id}")
@HystrixCommand(fallbackMethod = "getFallBack", commandProperties = {
@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "500")
})
public String get(@PathVariable("id") long id) throws Exception {

// throw new Exception("error");
Thread.sleep(id);
return "get";
}

public String getFallBack(@PathVariable("id") long id) {
return "getFallBack";

}
}
posted @ 2018-09-03 14:47  甜菜波波  阅读(243)  评论(0编辑  收藏  举报