13.Dubbo服务降级与整合Hystrix实现断路器

一、服务降级

什么是服务降级?

当服务器压力剧增的情况下,根据实际业务情况及流量,对一些服务和页面有策略的不处理或换种简单的方式处理,从而释放服务器资源以保证核心交易正常运作或高效运作。

通过Dubbo-Admin控制台进行操作

可以通过服务降级功能临时屏蔽某个出错的非关键服务,并定义降级后的返回策略。

向注册中心写入动态配置覆盖规则:

RegistryFactory registryFactory = ExtensionLoader.getExtensionLoader(RegistryFactory.class).getAdaptiveExtension();

Registry registry = registryFactory.getRegistry(URL.valueOf("zookeeper://10.20.153.10:2181"));

registry.register(URL.valueOf("override://0.0.0.0/com.foo.BarService?category=configurators&dynamic=false&application=foo&mock=force:return+null"));

 

其中:

  1. mock=force:return+null 表示消费方对该服务的方法调用都直接返回 null 值,不发起远程调用。用来屏蔽不重要服务不可用时对调用方的影响。
  2. 还可以改为 mock=fail:return+null 表示消费方对该服务的方法调用在失败后,再返回 null 值,不抛异常。用来容忍不重要服务不稳定时对调用方的影响。

二、整合hystrix

Hystrix 旨在通过控制那些访问远程系统、服务和第三方库的节点,从而对延迟和故障提供更强大的容错能力。Hystrix具备拥有回退机制和断路器功能的线程和信号隔离,请求缓存和请求打包,以及监控和配置等功能。

演示代码,只显示重要部分,全部代码文章末尾有代码下载地址

2.1新建Maven项目

2.2dubbo-spring-boot-hystrix 

2.2.1pom.xml

<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">
    <modelVersion>4.0.0</modelVersion>
    <groupId>org.niugang</groupId>
    <artifactId>dubbo-spring-boot-hystrix</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>pom</packaging>
    <!--dependencyManagement父类工程管理包 -->
    <dependencyManagement>
        <dependencies>
            <!--引入springboot -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>2.0.3.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <!--引入springcloud  -->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Finchley.SR1</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <modules>
        <module>dubbo-spring-boot-hystrix-api</module>
        <module>dubbo-spring-boot-hystrix-provider</module>
        <module>dubbo-spring-boot-hystrix-consumer</module>
    </modules>
</project>

 

2.3dubbo-spring-boot-hystrix-provider

2.3.1pom.xml

<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">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.niugang</groupId>
        <artifactId>dubbo-spring-boot-hystrix</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <artifactId>dubbo-spring-boot-hystrix-provider</artifactId>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--引入dubbo 集成springboot starter -->
        <dependency>
            <groupId>com.alibaba.boot</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>0.2.0</version>
        </dependency>

        <dependency>
            <groupId>org.niugang</groupId>
            <artifactId>dubbo-spring-boot-hystrix-api</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>

        <!--redis -->
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
        </dependency>
        <!--服务降级服务容错 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
        </dependency>
    </dependencies>
</project>

 

2.3.2 对外暴露RPCs实现类

package org.niugang.service;

import java.util.Random;

import org.springframework.stereotype.Component;
import com.alibaba.dubbo.config.annotation.Service;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;

/**
 * 
 * @ClassName: DefaultDemoService
 * @Description:对外暴露接口实现类
 * @author: niugang
 * @date: 2018年8月17日 下午7:50:47
 * @Copyright: 863263957@qq.com. All rights reserved.
 *
 */
// demo.service.version 在application.properties中配置过了
@Service // dubbo注解
@Component
@Service // dubbo注解
@Component
public class DefaultServiceImpl implements DefaultApiService {

    @HystrixCommand(commandProperties = {
            @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold", value = "10"),
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "2000") })
    public String defaultMethod(String str) {
        /*
         * Hystrix超时配置的为2s,当实现类睡眠超过2s,服务调用者将执行服务降级函数
         */
        int nextInt = new Random().nextInt(4000);
        System.out.println("sleep " + nextInt + "ms");
        try {
            Thread.sleep(nextInt);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "HELLO  " + str + " from Dubbo Spring Boot";
    }

}

 

2.4 dubbo-spring-boot-hystrix-consumer

2.4.1 pom.xml

<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">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.niugang</groupId>
        <artifactId>dubbo-spring-boot-hystrix</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <artifactId>dubbo-spring-boot-hystrix-consumer</artifactId>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba.boot</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>0.2.0</version>
        </dependency>
        <!--redis -->
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
        </dependency>
        <!--引入对外暴露的rpc -->
        <dependency>
            <groupId>org.niugang</groupId>
            <artifactId>dubbo-spring-boot-hystrix-api</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        <!--服务降级服务容错 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
        </dependency>
    </dependencies>
</project>

 

2.4.2调用服务提供者

/**
     * 引入服务提供者
     */
    // com.alibaba.dubbo.config.annotation.Reference
    @Reference
    private DefaultApiService demoService;

    @RequestMapping("/sayHello")
    @HystrixCommand(fallbackMethod = "failBackMethod")
    public String sayHello(@RequestParam String name) {
        return demoService.defaultMethod(name);
    }

    /**
     * 服务降级
     * @param name
     * @return
     */
    public String failBackMethod(String name) {
        return "service request fail";
    }

5.结果

 

5.1成功调用

5.2失败调用

6.源码

https://gitee.com/niugangxy/dubbo

  

微信公众号

                          
posted @ 2020-01-13 14:55  盲目的拾荒者  阅读(1731)  评论(0编辑  收藏  举报