Dubbo(14)如何实现服务降级和熔断?

在分布式系统中,服务降级和熔断是确保系统高可用性的重要手段。服务降级是在服务不可用或响应时间过长时,提供备用的响应或简单的处理逻辑。熔断是当系统检测到某个服务的故障率过高时,主动停止对该服务的调用,以防止故障扩散。

Dubbo中的服务降级和熔断

Dubbo本身提供了服务降级的功能,但对于熔断机制,则通常需要借助其他工具,如Hystrix、Sentinel等。

1. 服务降级

Dubbo通过Mock机制实现服务降级。当服务调用失败时,可以返回预设的降级处理结果。

配置示例

在消费者配置中添加降级策略:

application.yml

dubbo:
  consumer:
    check: false
    references:
      - interfaceClass: com.example.DemoService
        mock: "return null"  # 当调用失败时,返回null
代码示例
服务定义

定义一个简单的服务接口DemoService

package com.example;

public interface DemoService {
    String sayHello(String name);
}
服务提供者

实现服务接口,并通过Dubbo注解将其暴露为远程服务。

服务实现:

package com.example;

import org.apache.dubbo.config.annotation.DubboService;

@DubboService
public class DemoServiceImpl implements DemoService {
    @Override
    public String sayHello(String name) {
        // 模拟一个长时间的处理,触发降级
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "Hello, " + name;
    }
}

Spring Boot配置文件(application.yml):

server:
  port: 8081

dubbo:
  application:
    name: dubbo-demo-provider
  registry:
    address: zookeeper://127.0.0.1:2181
  protocol:
    name: dubbo
    port: 20880
  scan:
    base-packages: com.example
服务消费者

通过Dubbo注解引用远程服务并进行调用。

服务引用:

package com.example;

import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Component;

@Component
public class DemoServiceConsumer {

    @DubboReference(mock = "com.example.DemoServiceMock")
    private DemoService demoService;

    public void execute() {
        String message = demoService.sayHello("World");
        System.out.println(message);
    }
}

降级实现:

package com.example;

public class DemoServiceMock implements DemoService {
    @Override
    public String sayHello(String name) {
        return "Fallback: Hello, " + name;
    }
}

Spring Boot配置文件(application.yml):

server:
  port: 8080

dubbo:
  application:
    name: dubbo-demo-consumer
  registry:
    address: zookeeper://127.0.0.1:2181
  consumer:
    check: false
  scan:
    base-packages: com.example

服务消费者启动类:

package com.example;

import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

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

    @Bean
    public CommandLineRunner demo(DemoServiceConsumer consumer) {
        return args -> consumer.execute();
    }
}

2. 熔断

熔断机制通常需要借助其他工具来实现。Hystrix和Sentinel是常用的熔断工具。

使用Sentinel实现熔断

Sentinel是阿里巴巴开源的流量防卫兵,提供了丰富的熔断降级功能。

添加依赖

pom.xml中添加Sentinel依赖:

<dependency>
    <groupId>com.alibaba.cspgroupId>
    <artifactId>sentinel-annotation-aspectjartifactId>
    <version>1.8.0version>
dependency>
<dependency>
    <groupId>com.alibaba.cspgroupId>
    <artifactId>sentinel-transport-simple-httpartifactId>
    <version>1.8.0version>
dependency>
Sentinel配置

application.yml

spring:
  application:
    name: dubbo-demo-consumer
  cloud:
    sentinel:
      transport:
        port: 8719
服务消费者

通过Sentinel注解实现熔断。

服务引用:

package com.example;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Component;

@Component
public class DemoServiceConsumer {

    @DubboReference
    private DemoService demoService;

    @SentinelResource(value = "sayHello", fallback = "fallbackSayHello")
    public String sayHello(String name) {
        return demoService.sayHello(name);
    }

    public String fallbackSayHello(String name, Throwable ex) {
        return "Fallback: Hello, " + name;
    }

    public void execute() {
        String message = sayHello("World");
        System.out.println(message);
    }
}

Spring Boot配置文件(application.yml):

server:
  port: 8080

dubbo:
  application:
    name: dubbo-demo-consumer
  registry:
    address: zookeeper://127.0.0.1:2181
  consumer:
    check: false
  scan:
    base-packages: com.example

服务消费者启动类:

package com.example;

import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

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

    @Bean
    public CommandLineRunner demo(DemoServiceConsumer consumer) {
        return args -> consumer.execute();
    }
}

运行示例

  1. 启动ZooKeeper。
  2. 启动服务提供者。
  3. 启动服务消费者。

在消费者的控制台中,你会看到调用结果,如果服务提供者响应超时或不可用,将触发降级逻辑,返回预设的降级响应。

总结

服务降级和熔断是确保分布式系统高可用性的重要手段。通过Dubbo的Mock机制和Sentinel的熔断功能,可以有效地应对服务不可用和故障传播的问题。

你可能感兴趣的:(微服务,dubbo,safari,前端)