欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Kite的学习历程SpringCloud之Hystrix服务降级客户端通配服务降级

程序员文章站 2024-03-20 22:11:22
...

Kite学习历程的第二十三天

1. 客户端的通配服务降级

1.1 创建客户端cloud-consumer-feign-hystrix-order-81

1.1.1 修改pom.xml文件引入依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>demo01cloud</artifactId>
        <groupId>cn.kitey.spring</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>cloud-consumer-feign-hystrix-order-81</artifactId>


    <dependencies>
        <!--引入hystrix-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
        </dependency>

        <!--openfeign依赖的引入-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

        <!--引入自己创建的entities包-->
        <dependency>
            <groupId>cn.kitey.spring</groupId>
            <artifactId>cloud-api-commons</artifactId>
            <version>${project.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--监控-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

        <!--热部署-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId><!-- -->
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

1.1.2 创建application.yml配置文件

记住打开hytsrix支持


server:
  port: 81

eureka:
  client:
    register-with-eureka: true
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka
#让其支持hytrix
feign:
  hystrix:
    enabled: true

1.1.3 创建主启动类

package cn.kitey.springcloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.cloud.openfeign.EnableFeignClients;

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


1.1.4 创建业务类 service, contrller

  1. 创建service中的PaymentHystrixService接口
    注意添加标签:
    @FeignClient(value = “CLOUD-PROVIDER-HYSTRIX-PAYMENT”, fallback = PaymentFallbackService.class)
    后面的fallback 指明了降级通配的服务方法
package cn.kitey.springcloud.service;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

@Component
@FeignClient(value = "CLOUD-PROVIDER-HYSTRIX-PAYMENT", fallback = PaymentFallbackService.class)
public interface PaymentHystrixService {
    @GetMapping("/payment/hystrix/ok/{id}")
    String paymentInfo_OK(@PathVariable("id") Long id);


    @GetMapping("/payment/hystrix/timeout/{id}")
    String paymentInfo_TimeOut(@PathVariable("id") Long id);

}


  1. 创建通配服务类PaymentFallbackService 继承上面接口
package cn.kitey.springcloud.service;

import org.springframework.stereotype.Component;

@Component
public class PaymentFallbackService implements PaymentHystrixService {
    @Override
    public String paymentInfo_OK(Long id) {
        return "PaymentFallbackService--paymentInfo_OK 服务降级";
    }

    @Override
    public String paymentInfo_TimeOut(Long id) {
        return "PaymentFallbackService--paymentInfo_TimeOut 服务降级";
    }
}


  1. 创建controller包中的OrderHytrixController

package cn.kitey.springcloud.controller;


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

import javax.annotation.Resource;

@RestController
@Slf4j
public class OrderHytrixController {

    @Resource
    private PaymentHystrixService paymentHystrixService;

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


    @GetMapping("/consumer/payment/hystrix/timeout/{id}")
    public String paymentInfo_TimeOut(@PathVariable("id") Long id){
        return paymentHystrixService.paymentInfo_TimeOut(id);
    }

   
}

1.2 进行测试服务降级是否测试成功

  1. 首先打开8001, 7001 ,以及客户端81
    Kite的学习历程SpringCloud之Hystrix服务降级客户端通配服务降级
    这时访问:
    http://localhost:81/consumer/payment/hystrix/ok/1
    Kite的学习历程SpringCloud之Hystrix服务降级客户端通配服务降级
    访问成功
  2. 关闭8001服务端口,模拟服务器宕机
    3.Kite的学习历程SpringCloud之Hystrix服务降级客户端通配服务降级
    进行访问:
    http://localhost:81/consumer/payment/hystrix/ok/1
    Kite的学习历程SpringCloud之Hystrix服务降级客户端通配服务降级
    这时没有出现异常,而是调用了服务降级的方法。表明配置成功!