SpringCloud--服务降级--Hystrix整合支付模块

  1. Hystrix服务搭建:
    1.  

    2.  

       

    3.  

       

    4.  

       

    5. package com.model.service;
      
      import org.springframework.stereotype.Service;
      import org.springframework.web.bind.annotation.RequestMapping;
      import org.springframework.web.bind.annotation.RestController;
      
      import java.util.concurrent.TimeUnit;
      
      /**
       * @Description:测试类
       * @Author: 张紫韩
       * @Crete 2021/9/11 21:51
       */
      @Service
      public class PaymentService {
      
          public String paymentInfo_ok(Integer id){
              return "线程池:"+Thread.currentThread().getName()+"payment_ok,id:"+id;
          }
      
          public String paymentInfo_timeout(Integer id){
              int timeNumber=3;
              try {
                  TimeUnit.SECONDS.sleep(3);
              } catch (InterruptedException e) {
                  e.printStackTrace();
              }
              return "线程池:"+Thread.currentThread().getName()+"payment_timeout,id:"+id+"\t耗时:"+timeNumber+"秒钟";
          }
      
      }

       

       

    6. package com.model.controller;
      
      import com.model.service.PaymentService;
      import lombok.extern.slf4j.Slf4j;
      import org.springframework.beans.factory.annotation.Value;
      import org.springframework.web.bind.annotation.GetMapping;
      import org.springframework.web.bind.annotation.PathVariable;
      import org.springframework.web.bind.annotation.RequestMapping;
      import org.springframework.web.bind.annotation.RestController;
      
      import javax.annotation.Resource;
      
      /**
       * @Description:测试类
       * @Author: 张紫韩
       * @Crete 2021/9/11 21:57
       */
      @RestController
      @Slf4j
      @RequestMapping("/payment")
      public class PaymentController {
          @Resource
          private PaymentService paymentService;
      
          @Value("${server.port}")
          private String serverPort;
      
          @GetMapping("/hystrix/ok/{id}")
          public String paymentInfo_ok(@PathVariable("id")Integer id){
              return "端口号:"+serverPort+"\t"+paymentService.paymentInfo_ok(id);
          }
      
          @GetMapping("/hystrix/timeout/{id}")
          public String paymentInfo_timeout(@PathVariable("id")Integer id){
              return "端口号:"+serverPort+"\t"+paymentService.paymentInfo_timeout(id);
          }
      
      }
    7.  

posted @ 2021-09-11 22:31  张紫韩  阅读(25)  评论(0编辑  收藏  举报