SpringClound05

服务降级

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
    @LoadBalanced
    public 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 {
    @Autowired
    private 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 {
    @Autowired
    private 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-consumer
  eureka:
    client:
      service-url:
        defaultZone: http://localhost:10086/eureka
      fetch-registry: true #拉取服务列表,缓存到本地
      registry-fetch-interval-seconds: 5 #拉取列表的间隔时间
service-provider: #服务提供方的服务id
  ribbon:
    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
    @LoadBalanced
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }

    public static void main(String[] args) {
        SpringApplication.run(ItcastServiceConsumerApplication.class, args);
    }

}

熔断状态
在这里插入图片描述
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值