springcloud——hystrix服务消费者方服务降级

1、主配置文件

server.port=81

spring.application.name=payment-consumer

eureka.client.fetch-registry=true

eureka.client.register-with-eureka=true

eureka.client.service-url.defaultZone=http://eureka7001.com:7001/eureka/


#开启feign对hystrix的支持
feign.hystrix.enabled=true

2、主启动类

package com.springcloud;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.cloud.openfeign.EnableFeignClients;

/**
 * @author dc
 * @date 2020/7/29 - 15:28
 */
@SpringBootApplication
@Slf4j
@EnableEurekaClient     //开启eureka客户端
@EnableFeignClients     //开启feign客户端
@EnableHystrix      //开启hystrix
public class OrderHystrixMain81 {

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

}

3、控制器

package com.springcloud.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.springcloud.service.PaymentHystrixService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author dc
 * @date 2020/7/29 - 15:33
 */
@RestController
@Slf4j
public class PaymentHystrixController {

    @Resource
    private PaymentHystrixService paymentHystrixService;

    @GetMapping("/consumer/payment/hystrix/ok")
    public String paymentInfo_OK(Integer id) {
        String result = paymentHystrixService.paymentInfo_OK(id);
        return result;
    }

    @GetMapping("/consumer/payment/hystrix/timeout")
    @HystrixCommand(fallbackMethod = "paymentTimeOutFallbackMethod",commandProperties =
            {@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",
            value="1500")})
    //fallbackMethod:服务降级方法
    //name:属性名      value:属性值(当控制器运行时间超过该值时,将会进入服务降级方法)
    public String paymentInfo_TimeOut(Integer id) {
        String result = paymentHystrixService.paymentInfo_TimeOut(id);
        return result;
    }

    public String paymentTimeOutFallbackMethod(Integer id) {
        return "我是消费者81,对方支付系统繁忙请10秒钟后再试或者自己运行出错,请检查自己!";
    }

}

猜你喜欢

转载自blog.csdn.net/weixin_43925059/article/details/107693329