【SpringCloudAlibaba】Sentinel整合ribbon+openFeign+fallback,加强版的服务熔断功能

前言

本篇博客主要是分享Sentinel服务熔断功能,通过demo的演示,提高自己技术编写能力的同时也想通过博客分享,帮助需要帮助的人o( ̄︶ ̄)o


sentinel整合ribbon+openFeign+fallback

Ribbon系列

在这里插入图片描述

  • 创建工程:
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
  • 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;

/**
 * @ClassName : OrderNacosApplication
 * @Description :
 * @Author : Zhaocunwei
 * @Date: 2020-05-28 11:27
 */
@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;

/**
 * @ClassName : ApplicationContextConfig
 * @Description :
 * @Author : Zhaocunwei
 * @Date: 2020-05-28 14:28
 */
@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;

/**
 * @ClassName : PaymentApplication
 * @Description :
 * @Author : Zhaocunwei
 * @Date: 2020-05-28 11:39
 */
@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;

/**
 * @ClassName : PaymentController
 * @Description :
 * @Author : Zhaocunwei
 * @Date: 2020-05-28 11:30
 */
@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;
    }
}


  • 测试
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    解决上面报错问题,添加我们的兜底方法:
    在这里插入图片描述

@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);
    }

在这里插入图片描述
在这里插入图片描述
服务熔断只配置blockHandler
在这里插入图片描述


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

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
服务熔断fallback和blockHandler都配置
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
通过测试结论为:
若blockHandler和fallback都进行了配置,则被限流降级而抛出BlockException时只会进入blockhandler处理逻辑。

  • 异常忽略:
    在这里插入图片描述
    在这里插入图片描述

Feign系列

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

在这里插入图片描述

  • 配置文件修改
    在这里插入图片描述
#激活Sentinel对Feign的支持
feign:
  sentinel:
    enabled: true

  • 主启动类修改:
    在这里插入图片描述
  • 创建接口
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;

/**
 * @ClassName : PaymentService
 * @Description :
 * @Author : Zhaocunwei
 * @Date: 2020-05-28 16:29
 */
@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;

/**
 * @ClassName : PaymentServiceImpl
 * @Description :
 * @Author : Zhaocunwei
 * @Date: 2020-05-28 16:34
 */
@Service
public class PaymentServiceImpl implements PaymentService {
    @Override
    public CommonResult<Payment> paymentSql(Long id) {
        return new CommonResult<>(4444444,"服务降级返回-----PaymentServiceImpl",
                new Payment(id,"error"));
    }
}


  • 修改controller类:
    在这里插入图片描述
@Resource
    private PaymentService paymentService;
    @GetMapping(value = "/consumer/paymentSql/{id}")
    public CommonResult<Payment> paymentSql(@PathVariable("id") Long id){
        return paymentService.paymentSql(id);
    }

  • 测试
    在这里插入图片描述
    在这里插入图片描述
  • 关闭我们9003微服务,进行测试
    在这里插入图片描述
    在这里插入图片描述

熔断框架比较

在这里插入图片描述

相关推荐
©️2020 CSDN 皮肤主题: 书香水墨 设计师:CSDN官方博客 返回首页