Springboot Hystrix属性不起作用

时间:2017-09-19 16:56:03

标签: java spring-boot hystrix

我正在将Hystrix与Springboot应用程序集成,但不知何故hystrix属性无效,即每当我调用getSchoolDetails(http://localhost:9003//getSchoolDetails/dummyschool)时,它都会直接进入我想基于少数Hystrix控制的fallBack方法属性,例如 - requestVolumeThreshold,sleepWindowInMilliseconds等。 但没有一个hystrix属性生效。 请让我知道还需要做些什么来使Hystrix属性在springboot应用程序中运行。

以下是我的示例代码。

    @EnableCircuitBreaker
    @EnableHystrix
    @SpringBootApplication
    public class Application {
        private static final Logger log = LoggerFactory.getLogger(Application.class);
        public static void main(String[] args) {
            System.setProperty("security.basic.enabled", "false");
            ApplicationContext ctx = SpringApplication.run(Application.class, args);
            log.debug("Spring context loaded");
        }

        @Bean
        public EmbeddedServletContainerFactory servletContainer() {
            TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory();
            /*LogbackValve logbackValve = new LogbackValve();
            // point to logback-access.xml
            logbackValve.setFilename("logback-access.xml");
            tomcat.addContextValves(logbackValve);*/
            return tomcat;
        }
    }


    @RestController
    public class SchoolServiceController {
        @Autowired
        StudentServiceDelegate studentServiceDelegate;

        @RequestMapping(value = "/getSchoolDetails/{schoolname}", method = RequestMethod.GET)
        public String getStudents(@PathVariable String schoolname) {
            System.out.println("Going to call student service to get data!");
            return studentServiceDelegate.callStudentServiceAndGetData(schoolname);
        }
    }


@Service
public class StudentServiceDelegate {
    @Autowired
    RestTemplate restTemplate;

   @HystrixCommand(fallbackMethod = "callStudentServiceAndGetData_Fallback", commandKey = "StudentServiceDelegate")
public String callStudentServiceAndGetData(String schoolname){

    System.out.println("Getting School details for " + schoolname);
    //"http://localhost:8098/getStudentDetailsForSchool/{schoolname}"
    HttpHeaders headers = new HttpHeaders();
    headers.add("Accept","application/json");
    HttpEntity<String> postEntity = new HttpEntity<>(null, headers);
    String response = restTemplate
            .exchange("http://localhost:9001/v1/browse/catalog"
                    , HttpMethod.GET
                    , postEntity
                    , new ParameterizedTypeReference<String>() {
                    }, schoolname).getBody();

    System.out.println("Response Received as " + response + " -  " + new Date());


    return "NORMAL FLOW !!! - School Name -  " + schoolname + " :::  " +
            " Student Details " + response + " -  " + new Date();
}

    private String callStudentServiceAndGetData_Fallback(String schoolname, Throwable t) {

        System.out.println("Student Service is down!!! fallback route enabled...");

        return "CIRCUIT BREAKER ENABLED!!! No Response From Student Service at this moment. " +
                " Service will be back shortly - " + new Date();
    }

    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }
}


application.properties
server.port=9003

hystrix.command.StudentServiceDelegate.circuitBreaker.requestVolumeThreshold=5
hystrix.command.StudentServiceDelegate.circuitBreaker.sleepWindowInMilliseconds=60000
hystrix.command.StudentServiceDelegate.metrics.rollingStats.timeInMilliseconds=60000
hystrix.command.StudentServiceDelegate.circuitBreaker.errorThresholdPercentage=50



    <?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.kohls</groupId>
        <artifactId>msp-hystrix-sample</artifactId>
        <version>1.0-SNAPSHOT</version>
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>1.5.1.RELEASE</version>
        </parent>
        <properties>
            <java.version>1.8</java.version>
        </properties>

        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-hystrix</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>

            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter</artifactId>
            </dependency>

        </dependencies>
        <dependencyManagement>
            <dependencies>
                <dependency>
                    <groupId>org.springframework.cloud</groupId>
                    <artifactId>spring-cloud-dependencies</artifactId>
                    <version>Camden.SR5</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>

在9001端口新建的服务方法,响应时间超过5秒:

@RequestMapping(value = "/v1/browse/catalog", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public @ResponseBody ResponseEntity<String> getCatalog(HttpServletRequest httpRequest) {
        long startTime = System.currentTimeMillis();
        MSPResponse response = null;
        try {
            Thread.sleep(5000);
            response = catalogService.getCatalog(httpRequest);
        } catch (Exception e) {
            System.out.println("Error "+e);
        }
        long endTime = System.currentTimeMillis() - startTime;
        System.out.println("API Time taken "+endTime);
        return browseUtil.forwardResponse(response);
    }

0 个答案:

没有答案