JAVA开发Java技术升华Java学习笔记

Spring Clouad(六):Hystrix

2018-04-08  本文已影响45人  聪明的奇瑞

雪崩效应

雪崩效应 雪崩效应

容错机制

容错机制

Hystrix

快速入门

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
@RestController
public class MovieController {

    @Autowired
    private RestTemplate restTemplate;

    @HystrixCommand(fallbackMethod = "findByIdFallback")
    @GetMapping("/user/{id}")
    public UserEntity findById(@PathVariable int id) {
        return this.restTemplate.getForObject("http://FILM-USER/" + id, UserEntity.class);
    }

    public UserEntity findByIdFallback(int id) {
        UserEntity user = new UserEntity();
        user.setId(-1);
        user.setName("默认用户");
        return user;
    }

}
@HystrixCommand(fallbackMethod = "findByIdFallback",commandProperties = {
    @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "5000"),
    @HystrixProperty(name = "metrics.rollingStats.timeInMilliseconds",value = "10000")
    },threadPoolProperties = {
    @HystrixProperty(name="coreSize",value = "1"),
    @HystrixProperty(name="maxQueueSize",value = "10")
})
@GetMapping("/user/{id}")
public User findById(@PathVariable int id){
       //...
}

Hystrix 断路器的状态

{"status":"UP","hystrix":{"status":"UP"}}
"hystrix":{"status":"CIRCUIT_OPEN"

Hystrix 线程隔离策略

@HystrixCommand(fallbackMethod = "findByIdFallback",commandProperties = {
    @HystrixProperty(name = "execution.isolation.strategy", value = "SEMAPHORE")
})

Feign 整合 Hystrix

@FeignClient(name = "FILM-USER",fallback = UserFeignClientFallBack.class)
public interface UserFeignClient {
    @GetMapping("/{id}")
    UserEntity findById(@PathVariable("id") int id);
}

/**
 * 回退类 FeignClientFallback 需实现 FeignClient 接口
 */
public class UserFeignClientFallBack implements UserFeignClient {
    @Override
    public UserEntity findById(int id) {
        UserEntity user = new UserEntity();
        user.setId(-1);
        user.setUsername("默认用户");
        return user;
    }
}

使用 FallbackFactory 捕获回退异常

public class UserFeignClientFallbackFactory implements FallbackFactory<UserFeignClient> {

    private static final Logger LOGGER = LoggerFactory.getLogger(UserFeignClientFallbackFactory.class);

    @Override
    public UserFeignClient create(Throwable throwable) {
        return new UserFeignClient() {
            @Override
            public UserEntity findById(int id) {
                UserFeignClientFallbackFactory.LOGGER.info("fallback;reason was:",throwable);
                UserEntity user = new UserEntity();
                user.setId(-1);
                user.setUsername("默认用户");
                return user;
            }
        };
    }
    
}

为 Feign 禁用 Hystrix

@Configuration
public class FeignDisableHystrixConfiguration {
    @Bean
    @Scope("prototype")
    public Feign.Builder feignBuilder(){
        return Feign.builder();
    }
}
@FeignClient(name = "FILM-USER",fallback = FeignDisableHystrixConfiguration.class)
public interface UserFeignClient {
    //...
}
feign.hystrix.enabled = false

Hystrix 监控

使用 Hystrix Dashboard 可视化监控数据

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-hystrix-dashboard</artifactId>
</dependency>
server:
    port:8030
Hystrix Dashboard Hystrix Dashboard

使用 Turbine 聚合监控数据

Turbine
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-turbine</artifactId>
</dependency>
server:
    port: 8031
spring:
    application:
        name: turbine
eureka:
    client:
        service-url:
            defaultZone: http://localhost:8761/eureka/
    instance:
        prefer-ip-address: true
turbine:
    appConfig: flim-consumer1,flim-consumer2
    clusterNameExpression: "'default'"
Turbine
上一篇 下一篇

猜你喜欢

热点阅读