hystrix 例子

359 阅读2分钟

#################################################################################
restTemplate调用,feign调用使用fallback和fallbackfactory使用hystrix实例

---------------------------restTemplate-----------begin---------------------------------
        package com.example.user.controller;

import com.example.user.rest.RestTemplateWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 用户服务
 */
@RestController
public class UserRestController {

    @Autowired
    private RestTemplateWrapper restTemplateWrapper;

    /**
     * 正常请求
     * @return
     */
    @PostMapping("/rest/login")
    public String restLogin(){
        return "rest  " + restTemplateWrapper.loginActivity();
    }

    /**
     * 超时请求
     * @return
     */
    @PostMapping("/rest/timeout")
    public String restTimeOut(){
        return "rest  " + restTemplateWrapper.timeout();
    }

    /**
     * 异常请求
     * @return
     */
    @PostMapping("/rest/exception")
    public String restException(){
        return "rest  " + restTemplateWrapper.exception();
    }
}
-----------------------------------------------------------------------------------------
        package com.example.user.rest;

        import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
        import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
        import org.springframework.beans.factory.annotation.Autowired;
        import org.springframework.stereotype.Component;
        import org.springframework.web.client.RestTemplate;

@Component
public class RestTemplateWrapper {

    @Autowired
    private RestTemplate restTemplate;

    /**
     * 正常请求活动接口
     * @return
     */
    public String loginActivity(){
        return restTemplate.postForObject("http://ACTIVITY/activity/loginActivity",null, String.class);
    }

    /**
     * 超时请求
     * @return
     */
    public String timeout(){
        return restTemplate.postForObject("http://ACTIVITY/activity/timeout",null, String.class);
    }


    /**
     * 异常请求
     * @return
     */
    @HystrixCommand(
            fallbackMethod = "fallback",
            threadPoolKey = "exceptionPool",
            threadPoolProperties = {
                    @HystrixProperty(name = "coreSize", value = "3"),
                    @HystrixProperty(name = "maxQueueSize", value = "20")
            },
            commandProperties = {
                    @HystrixProperty(name = "metrics.rollingStats.timeInMilliseconds", value = "5000"),
                    @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold", value = "2"),
                    @HystrixProperty(name = "circuitBreaker.errorThresholdPercentage", value = "50"),
                    @HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds", value = "3000")
            }
    )
    public String exception(){
        return restTemplate.postForObject("http://ACTIVITY/activity/exception",null, String.class);
    }

    public String fallback(){
        return "出发熔断";
    }

}
---------------------------restTemplate------end-------------------------------------







        ---------------------------feign--fallback----begin----------------------------------
        package com.example.user.controller;

        import com.example.user.feign.UserFeign;
        import org.springframework.beans.factory.annotation.Autowired;
        import org.springframework.web.bind.annotation.PostMapping;
        import org.springframework.web.bind.annotation.RestController;

/**
 * 用户服务 feign调用
 */
@RestController
public class UserFeignController {

    @Autowired
    private UserFeign userFeign;

    /**
     * feign正常
     * @return
     */
    @PostMapping("/feign/login")
    public String feignLogin(){
        return "feign " + userFeign.loginActivity();
    }

    /**
     * feign超时
     * @return
     */
    @PostMapping("/feign/timeout")
    public String feignTimeOut(){
        return "feign " + userFeign.timeout();
    }

    /**
     * feign异常
     * @return
     */
    @PostMapping("/feign/exception")
    public String feignException(){
        return "feign " + userFeign.exception();
    }

}
-----------------------------------------------------------------------------
        package com.example.user.feign;

        import org.springframework.cloud.openfeign.FeignClient;
        import org.springframework.web.bind.annotation.PostMapping;

//@FeignClient(value = "ACTIVITY", fallback = UserFeignFallBack.class)
public interface UserFeign {

    @PostMapping("/activity/loginActivity")
    String loginActivity();


    @PostMapping("/activity/timeout")
    String timeout();


    @PostMapping("/activity/exception")
    String exception();

}
-----------------------------------------------------------------------------
        package com.example.user.feign;

        import org.springframework.stereotype.Component;

@Component
public class UserFeignFallBack implements UserFeign {

    @Override
    public String loginActivity() {
        return "hystrix 返回login";
    }

    @Override
    public String timeout() {
        return "hystrix 返回timeout";
    }

    @Override
    public String exception() {
        return "hystrix 返回exception";
    }

}
---------------------------feign--fallback----end-------------------------------------




---------------------------feign--fallbackFactory----begin----------------------------

        package com.example.user.controller;

        import com.example.user.feignfactory.UserFeign;
        import org.springframework.beans.factory.annotation.Autowired;
        import org.springframework.web.bind.annotation.PostMapping;
        import org.springframework.web.bind.annotation.RestController;

/**
 * 用户服务
 */
@RestController
public class UserFeignFactoryController {

    @Autowired
    private UserFeign userFeign;

    /**
     * feign正常
     * @return
     */
    @PostMapping("/feign/factory/login")
    public String feignLogin(){
        return "feign " + userFeign.loginActivity();
    }

    /**
     * feign超时
     * @return
     */
    @PostMapping("/feign/factory/timeout")
    public String feignTimeOut(){
        return "feign " + userFeign.timeout();
    }

    /**
     * feign异常
     * @return
     */
    @PostMapping("/feign/factory/exception")
    public String feignException(){
        return "feign " + userFeign.exception();
    }

}
-----------------------------------------------------------------------------
        package com.example.user.feignfactory;

        import org.springframework.cloud.openfeign.FeignClient;
        import org.springframework.web.bind.annotation.PostMapping;

@FeignClient(value = "ACTIVITY", fallbackFactory = UserFeignFallBackFactory.class)
public interface UserFeign {

    @PostMapping("/activity/loginActivity")
    String loginActivity();


    @PostMapping("/activity/timeout")
    String timeout();


    @PostMapping("/activity/exception")
    String exception();

}
-----------------------------------------------------------------------------
        package com.example.user.feignfactory;

        import feign.hystrix.FallbackFactory;
        import org.springframework.stereotype.Component;

@Component
public class UserFeignFallBackFactory implements FallbackFactory<UserFeign> {

    @Override
    public UserFeign create(Throwable throwable) {
        return new UserFeign() {
            @Override
            public String loginActivity() {
                return "fallbackFactory  loginActivity";
            }

            @Override
            public String timeout() {
                return "fallbackFactory  timeout";
            }

            @Override
            public String exception() {
                return "fallbackFactory  exception";
            }
        };
    }
}
---------------------------feign--fallbackFactory----end----------------------------------

#配置
server:
  port: 18080
  servlet:
    context-path: /user

eureka:
  client:
    service-url:
      defaultZone: http://127.0.0.1:18761/eureka
    enabled: true

spring:
  application:
    name: user

management:
  endpoints:
    web:
      base-path: /monitor
      exposure:
        include: "*"
  endpoint:
    health:
      show-details: always


feign:
  hystrix:
    enabled: true