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