Spring Cloud Aibaba 服务熔断OpenFeign ,前面是Sentinel整合Ribbon来设置限流BlockHanlder以及运行时异常Fallbcak方法设置,下面是Sentinel整合OpenFeign来进行相关的设置。

1. 服务消费者85

重新新建cloud-alibaba-consumer-order85 服务作为服务消费者

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>live.yremp.springcloud</artifactId>
        <groupId>live.yremp</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>cloud-alibaba-consumer-order85</artifactId>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </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>live.yremp</groupId>
            <artifactId>cloud-api-commons</artifactId>
            <version>1.0-SNAPSHOT</version>
            <scope>compile</scope>
        </dependency>
    </dependencies>

</project>

1.2 配置文件

server:
  port: 85


spring:
  application:
    name: nacos-order-consumer
  cloud:
    nacos:
      discovery:
        server-addr: 39.105.173.178:8848
    sentinel:
      transport:
        dashboard: localhost:8080
        port: 8719


#消费者将要去访问的微服务名称(注册成功进nacos的微服务提供者)
service-url:
  nacos-user-service: http://nacos-payment-provider
feign:
  sentinel:
    enabled: true

1.3 主启动类

package live.yremp.cloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;

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

1.4 OpenFeign服务接口

package live.yremp.cloud.service;

import live.yremp.springcloud.entries.CommonResult;
import live.yremp.springcloud.entries.Payment;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

@FeignClient(value = "nacos-payment-provider",fallback = PaymentFallbackService.class )
public interface PaymentService {
    @GetMapping("/paymentSQL/{id}")
    public CommonResult<Payment> paymentSQL(@PathVariable("id")Long id);
}

1.5 Fallbcak处理类

上面服务接口指定的fallback方法对应的处理类

package live.yremp.cloud.service;

import live.yremp.springcloud.entries.CommonResult;
import live.yremp.springcloud.entries.Payment;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

@Component
public class PaymentFallbackService implements PaymentService {
    @Override
    public CommonResult<Payment> paymentSQL(Long id) {
        return new CommonResult<>(444,"服务降级返回------>PaymentFallbackService",new Payment(id,"ErrorSerial"));
    }
}

1.6 Controller

controller调用Service

package live.yremp.cloud.controller;

import live.yremp.cloud.service.PaymentService;
import live.yremp.springcloud.entries.CommonResult;
import live.yremp.springcloud.entries.Payment;
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
public class OrderNacosController
{
    @Resource
    private PaymentService paymentService;

    @GetMapping("/consumer/openfeign/{id}")
    public CommonResult<Payment> paymentInfo(@PathVariable("id") Long id)
    {
        return paymentService.paymentSQL(id);
    }

}

2.服务测试

启动cloud-alibaba-consumer-order85和服务提供者 cloud-alibaba-provider-payment9003 、 cloud-alibaba-provider-payment9004 项目

2.1 服务注册

先去Nacos查看三个微服务项目是否注册成功

2.2 消费者调用

访问 cloud-alibaba-consumer-order85 的接口测试是否能调用 cloud-alibaba-provider-payment9003 、 cloud-alibaba-provider-payment9004 两个微服务的接口

2.3服务降级测试

关闭9003、9004服务,再次去测试

触发fallback服务降级

标签云

ajax AOP Bootstrap cdn Chevereto CSS Docker Editormd GC Github Hexo IDEA JavaScript jsDeliver JS樱花特效 JVM Linux Live2D markdown Maven MyBatis MyBatis-plus MySQL Navicat Oracle Pictures QQ Sakura SEO Spring Boot Spring Cloud Spring Cloud Alibaba SpringMVC Thymeleaf Vue Web WebSocket Wechat Social WordPress Yoast SEO 代理 分页 图床 小幸运 通信原理

Spring Cloud Aibaba 服务熔断OpenFeign
Spring Cloud Aibaba 服务熔断OpenFeign