服务降级

package cn.itcast.service;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;@SpringBootApplication
@EnableDiscoveryClient
@EnableCircuitBreaker //开启熔断
public class ItcastServiceConsumerApplication {@Bean@LoadBalancedpublic RestTemplate restTemplate(){return new RestTemplate();}public static void main(String[] args) {SpringApplication.run(ItcastServiceConsumerApplication.class, args);}}
package cn.itcast.service.controller;import cn.itcast.service.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;import java.util.List;@Controller
@RequestMapping("consumer/user")
public class UserController {@Autowiredprivate RestTemplate restTemplate;//    @Autowired
//    private DiscoveryClient discoveryClient; //包含了拉取的所有服务信息@GetMapping@ResponseBody@HystrixCommand(fallbackMethod = "qusetyUserByIdFallback")public String qusetyUserById(@RequestParam("id")Long id){//        List<ServiceInstance> instances = discoveryClient.getInstances("service-provider");
//        ServiceInstance instance = instances.get(0);return this.restTemplate.getForObject("http://service-provider/user/" + id, String.class);}//要和被熔断方法的返回值和参数列表一致public String qusetyUserByIdFallback(Long id){return "服务正忙,稍后再试";}
}

定义全局熔断方法

package cn.itcast.service.controller;import cn.itcast.service.pojo.User;
import javafx.beans.DefaultProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;import java.util.List;@Controller
@RequestMapping("consumer/user")
@DefaultProperties(defaultFallback = "qusetyUserByIdFallback") //定义全局的熔断方法
public class UserController {@Autowiredprivate RestTemplate restTemplate;//    @Autowired
//    private DiscoveryClient discoveryClient; //包含了拉取的所有服务信息@GetMapping@ResponseBody@HystrixCommand //声明熔断方法public String qusetyUserById(@RequestParam("id")Long id){//        List<ServiceInstance> instances = discoveryClient.getInstances("service-provider");
//        ServiceInstance instance = instances.get(0);return this.restTemplate.getForObject("http://service-provider/user/" + id, String.class);}//返回值类型要和被熔断方法一致,参数列表为空public String qusetyUserByIdFallback(){return "服务正忙,稍后再试";}
}
server:port: 80
spring:application:name: service-consumereureka:client:service-url:defaultZone: http://localhost:10086/eurekafetch-registry: true #拉取服务列表,缓存到本地registry-fetch-interval-seconds: 5 #拉取列表的间隔时间
service-provider: #服务提供方的服务idribbon:NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule #调用随机算法
hystrix:command:default:execution:isolation:thread:timeoutInMillisecond: 6000 # 设置hystrix的超时时间为6000ms

组合注解

package cn.itcast.service;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;//@SpringBootApplication
//@EnableDiscoveryClient
//@EnableCircuitBreaker //开启熔断
@SpringCloudApplication //组合注解,相当于@SpringBootApplication,@EnableDiscoveryClient,@EnableCircuitBreaker
public class ItcastServiceConsumerApplication {@Bean@LoadBalancedpublic RestTemplate restTemplate(){return new RestTemplate();}public static void main(String[] args) {SpringApplication.run(ItcastServiceConsumerApplication.class, args);}}

熔断状态

SpringClound05相关推荐

最新文章

  1. 分区表与分区索引(一):范围分区表的操作
  2. 关于Git中的一些常用的命令
  3. 软件需求工程与UML建模14组14周工作情况
  4. Info.plist中常用的key简介
  5. Log4j配置文件解读和模板页收藏
  6. 循环的各种形式(不拘一格编程序之四)
  7. 计算机组成原理平均cpi怎么算_【计算机考研】每日一题数网操组130
  8. 2019年博客之星评选活动之晋级TOP20博主名单
  9. 高等数学-【3.1-4】微分中值定理与导数的应用
  10. 获取随机数的n种方法,你知道几种
  11. Mac在已安装Python3.9的情况下利用miniconda配置【Python3.7+TensorFlow1.14环境】+ Sublime Text如何通过conda切换不同Python环境
  12. Django/Python发送HTML邮件 (包含图片)
  13. Linux 打印机移植说明
  14. minikube addons enable ingress 启动错误
  15. 2.数据类型--数值类型
  16. Hoj 1789 Electricity
  17. 基于springboot的校园二手交易系统-JAVA【毕业设计、论文、源码、开题报告】
  18. 《Mysql必知必会》-----笔记(2)
  19. 闭关修炼(二十五)基础web安全
  20. 微信朋友圈分享链接调用外部浏览器打开指定URL链接是如何实现的

热门文章

  1. Windows浏览器启动页被更改为www.hao123.com
  2. java多线程执行任务(工具升级版)
  3. Parcelable与Serializable
  4. Workfine新手入门:给图片加水印
  5. oracle 创建超大表空间文件,不用担心表空间占用满了
  6. 刚学玩原生JS,自己写了一个小游戏,希望在以后能不断地进步加以改善
  7. RRT*算法的原理简介以及Python实现代码
  8. matplotlib绘图形状、颜色汇总
  9. 图像转成PDF变形BUG调整完成
  10. layui数据表格table在选项卡tabs中分页条不显示的解决