zoukankan      html  css  js  c++  java
  • SpringCloud四:hystrix-propagation

    注:pom.xml 及配置文件配置与上篇相同

    package com.itmuch.cloud.controller;

    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.RestController;
    import org.springframework.web.client.RestTemplate;
    import org.springframework.web.context.annotation.SessionScope;

    import com.itmuch.cloud.entity.User;
    import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
    import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
    /**
    * 短路器第二天
    * @author z
    *
    */
    @RestController
    public class MovieController {
    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("/movie/{id}")
    //表示@HystrixCommand与findById方法会在同一个线程中调用
    //如果不配合的话findById是一个线程,@HystrixCommand是一个隔离的线程相当于两个线程
    //正常情况下不需要配置,等抛异常了在配置
    @HystrixCommand(fallbackMethod = "findByIdFallback", commandProperties = @HystrixProperty(name = "execution.isolation.strategy", value = "SEMAPHORE"))
    public User findById(@PathVariable Long id) {
    return this.restTemplate.getForObject("http://microservice-provider-user/simple/" + id, User.class);
    }

    public User findByIdFallback(Long id) {
    User user = new User();
    user.setId(0L);
    return user;
    }
    }

  • 相关阅读:
    第六周 8.23-8.29
    Go-ethereum源码解析-Part I
    Go语言
    UVa Live 4725
    UVa 11134
    UVa 11100
    UVa 11627
    UVa Live 4794
    UVa LA 4254
    UVa 10905
  • 原文地址:https://www.cnblogs.com/a8457013/p/7736038.html
Copyright © 2011-2022 走看看