sentinel整合ribbon+openFeign+fallback,加强版的服务熔断功能

sentinel整合ribbon

Ribbon系列

【SpringCloudAlibaba】Sentinel整合ribbon+openFeign+fallback,加强版的服务熔断功能_第1张图片

  • 创建工程:
    【SpringCloudAlibaba】Sentinel整合ribbon+openFeign+fallback,加强版的服务熔断功能_第2张图片
    【SpringCloudAlibaba】Sentinel整合ribbon+openFeign+fallback,加强版的服务熔断功能_第3张图片
    【SpringCloudAlibaba】Sentinel整合ribbon+openFeign+fallback,加强版的服务熔断功能_第4张图片
  • yml
server:
  port: 84

spring:
  application:
    name: nacos-order-consumer
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848
    sentinel:
      transport:
        dashboard: localhost:8080
        #默认8719端口,假如被占用会自动从8719开始依次+1扫描,直至找到未被占用的端口
        port: 8719

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



  • pom 文件:
<?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>com.zcw.springcloud2020508</artifactId>
        <groupId>com.zcw</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>cloudalibaba-consumer-nacos-order84</artifactId>
    <dependencies>
     <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>com.zcw</groupId>
            <artifactId>cloud-api-commons</artifactId>
            <version>${project.version}</version>
        </dependency>
        <!--SpringBoot 整合web组件+actuator-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--日常通用jar包配置-->
        <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>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

</project>

  • 启动类
package com.zcw.springcloud.alibaba;

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


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


  • 配置类:
package com.zcw.springcloud.alibaba.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;


@Configuration
public class ApplicationContextConfig {
    @Bean
    @LoadBalanced
    public RestTemplate getRestTemplate(){
        return new RestTemplate();
    }
}


  • 业务类
    9003与9004只是端口不一样其他配置是一样的:
  • pom

<?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>com.zcw.springcloud2020508</artifactId>
        <groupId>com.zcw</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>cloudalibaba-provider-payment9003</artifactId>
    <dependencies>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
        <dependency>
            <groupId>com.zcw</groupId>
            <artifactId>cloud-api-commons</artifactId>
            <version>${project.version}</version>
        </dependency>
        <!--SpringBoot 整合web组件+actuator-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--日常通用jar包配置-->
        <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>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

</project>
  • yml
server:
  port: 9003

spring:
  application:
    name: nacos-payment-provider
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848
management:
  endpoints:
    web:
      exposure:
        include: '*'


  • 启动类
package com.zcw.springcloud.alibaba;

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


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



  • 业务类

package com.zcw.springcloud.alibaba.controller;

import com.zcw.springcloud.entities.CommonResult;
import com.zcw.springcloud.entities.Payment;
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.RestController;

import java.util.HashMap;


@RestController
public class PaymentController {
    @Value("${server.port}")
    private String serverPort;
    public static HashMap<Long, Payment> hashMap = new HashMap<>();
    static{
        hashMap.put(1L,new Payment(1L,"24242344525252352355423424"));
        hashMap.put(2L,new Payment(2L,"4324234234234234234234234234"));
        hashMap.put(3L,new Payment(3L,"23452342342323424fgdsggdgfgdgf"));
    }
    @GetMapping(value="/paymentSql/{id}")
    public CommonResult<Payment> paymentSQL(@PathVariable("id")Long id){
        Payment payment = hashMap.get(id);
        CommonResult<Payment> result = new CommonResult(200,"from mysql,severPort:"+serverPort,payment);
        return result;
    }
}


  • 测试
    【SpringCloudAlibaba】Sentinel整合ribbon+openFeign+fallback,加强版的服务熔断功能_第5张图片
    【SpringCloudAlibaba】Sentinel整合ribbon+openFeign+fallback,加强版的服务熔断功能_第6张图片
    【SpringCloudAlibaba】Sentinel整合ribbon+openFeign+fallback,加强版的服务熔断功能_第7张图片
    【SpringCloudAlibaba】Sentinel整合ribbon+openFeign+fallback,加强版的服务熔断功能_第8张图片
  • 【SpringCloudAlibaba】Sentinel整合ribbon+openFeign+fallback,加强版的服务熔断功能_第9张图片
    【SpringCloudAlibaba】Sentinel整合ribbon+openFeign+fallback,加强版的服务熔断功能_第10张图片
    解决上面报错问题,添加我们的兜底方法:
    【SpringCloudAlibaba】Sentinel整合ribbon+openFeign+fallback,加强版的服务熔断功能_第11张图片

@SentinelResource(value = “fallbakc”,fallback =“handlerFallback”)// fallback只负责业务异常

 //本案例是fallback
    public CommonResult handlerFallback(@PathVariable Long id,Throwable e){
        Payment payment = new Payment(id,"null");
        return new CommonResult<>(444,"兜底异常hadlerFallback,exception内容"+e.getMessage(),payment);
    }

【SpringCloudAlibaba】Sentinel整合ribbon+openFeign+fallback,加强版的服务熔断功能_第12张图片
【SpringCloudAlibaba】Sentinel整合ribbon+openFeign+fallback,加强版的服务熔断功能_第13张图片
服务熔断只配置blockHandler
【SpringCloudAlibaba】Sentinel整合ribbon+openFeign+fallback,加强版的服务熔断功能_第14张图片


public CommonResult blockHandler(@PathVariable Long id , BlockException blockException){
        Payment payment = new Payment(id,"null");
        return new CommonResult<>(445,
                "blockHandler-sentinel限流,无此流水:blockException"
                        +blockException.getMessage());
    }

【SpringCloudAlibaba】Sentinel整合ribbon+openFeign+fallback,加强版的服务熔断功能_第15张图片
【SpringCloudAlibaba】Sentinel整合ribbon+openFeign+fallback,加强版的服务熔断功能_第16张图片
【SpringCloudAlibaba】Sentinel整合ribbon+openFeign+fallback,加强版的服务熔断功能_第17张图片
【SpringCloudAlibaba】Sentinel整合ribbon+openFeign+fallback,加强版的服务熔断功能_第18张图片
服务熔断fallback和blockHandler都配置
【SpringCloudAlibaba】Sentinel整合ribbon+openFeign+fallback,加强版的服务熔断功能_第19张图片
【SpringCloudAlibaba】Sentinel整合ribbon+openFeign+fallback,加强版的服务熔断功能_第20张图片
【SpringCloudAlibaba】Sentinel整合ribbon+openFeign+fallback,加强版的服务熔断功能_第21张图片
【SpringCloudAlibaba】Sentinel整合ribbon+openFeign+fallback,加强版的服务熔断功能_第22张图片
【SpringCloudAlibaba】Sentinel整合ribbon+openFeign+fallback,加强版的服务熔断功能_第23张图片
【SpringCloudAlibaba】Sentinel整合ribbon+openFeign+fallback,加强版的服务熔断功能_第24张图片
【SpringCloudAlibaba】Sentinel整合ribbon+openFeign+fallback,加强版的服务熔断功能_第25张图片
通过测试结论为:
若blockHandler和fallback都进行了配置,则被限流降级而抛出BlockException时只会进入blockhandler处理逻辑。

  • 异常忽略:

    【SpringCloudAlibaba】Sentinel整合ribbon+openFeign+fallback,加强版的服务熔断功能_第26张图片

Feign系列

  • pom文件修改
 <!--SpringCloud openfeign-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>

 

  • 配置文件修改
    【SpringCloudAlibaba】Sentinel整合ribbon+openFeign+fallback,加强版的服务熔断功能_第28张图片
#激活Sentinel对Feign的支持
feign:
  sentinel:
    enabled: true

  • 主启动类修改:
    【SpringCloudAlibaba】Sentinel整合ribbon+openFeign+fallback,加强版的服务熔断功能_第29张图片
  • 创建接口
package com.zcw.springcloud.alibaba.service;

import com.zcw.springcloud.alibaba.service.impl.PaymentServiceImpl;
import com.zcw.springcloud.entities.CommonResult;
import com.zcw.springcloud.entities.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 = PaymentServiceImpl.class)
public interface PaymentService {
    @GetMapping(value = "/paymentSql/{id}")
    public CommonResult<Payment> paymentSql(@PathVariable("id") Long id);
}


  • 接口实现类:
package com.zcw.springcloud.alibaba.service.impl;

import com.zcw.springcloud.alibaba.service.PaymentService;
import com.zcw.springcloud.entities.CommonResult;
import com.zcw.springcloud.entities.Payment;
import org.springframework.stereotype.Service;


@Service
public class PaymentServiceImpl implements PaymentService {
    @Override
    public CommonResult<Payment> paymentSql(Long id) {
        return new CommonResult<>(4444444,"服务降级返回-----PaymentServiceImpl",
                new Payment(id,"error"));
    }
}


  • 修改controller类:
    【SpringCloudAlibaba】Sentinel整合ribbon+openFeign+fallback,加强版的服务熔断功能_第30张图片
@Resource
    private PaymentService paymentService;
    @GetMapping(value = "/consumer/paymentSql/{id}")
    public CommonResult<Payment> paymentSql(@PathVariable("id") Long id){
        return paymentService.paymentSql(id);
    }

  • 测试
    【SpringCloudAlibaba】Sentinel整合ribbon+openFeign+fallback,加强版的服务熔断功能_第31张图片
    【SpringCloudAlibaba】Sentinel整合ribbon+openFeign+fallback,加强版的服务熔断功能_第32张图片
  • 关闭我们9003微服务,进行测试
    【SpringCloudAlibaba】Sentinel整合ribbon+openFeign+fallback,加强版的服务熔断功能_第33张图片
    【SpringCloudAlibaba】Sentinel整合ribbon+openFeign+fallback,加强版的服务熔断功能_第34张图片

熔断框架比较


 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值