Dubbo hystrix 熔断降级 示例

这篇具有很好参考价值的文章主要介绍了Dubbo hystrix 熔断降级 示例。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

目录

Pom

应用启动类

接口

服务提供者

消费者

总结

因为jar包冲突问题报错 java.lang.NoSuchMethodError: org.springframework.boot.builder.SpringApplicationBuilder.([Ljava/lang/Object;)V

解决方式修改低版本springboot

改成如下

版本对照

全代码

pom

启动

实现

yml






Pom

 <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
            <version>1.4.4.RELEASE</version>
        </dependency>

应用启动类

@SpringBootApplication
@EnableHystrix
public class ProviderApplication {

有需要加上这个

@EnableDubbo(scanBasePackages = {"org.apache.dubbo.spring.boot.provider.impl"})

接口

package org.apache.dubbo.spring.boot.api;

public interface HelloService {

    String sayHello(String name);

}

服务提供者

@Service(version = "1.0.0")
public class HelloServiceImpl implements HelloService {

    @HystrixCommand(commandProperties = {
                    @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold", value = "10"),
                    @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "2000") })
    @Override
    public String sayHello(String name) {
        // System.out.println("async provider received: " + name);
        // return "annotation: hello, " + name;
        throw new RuntimeException("Exception to show hystrix enabled.");
    }
}

消费者

有超时熔断跳转到 reliable 方法

   @Reference(version = "1.0.0")
private HelloService demoService;


    @HystrixCommand(fallbackMethod = "reliable")
    public String doSayHello(String name) {
        return demoService.sayHello(name);
}


    public String reliable(String name) {
        return "hystrix fallback value";
    }

总结

  • 对于dubbo provider的@Service是一个spring bean,直接在上面配置@HystrixCommand即可
  • 对于dubbo consumer的@Reference,可以通过加一层简单的spring method包装,配置@HystrixCommand即可
  • Hystrix本身提供HystrixCommandAspect来集成Spring AOP,配置了@HystrixCommand@HystrixCollapser的spring method都会被Hystrix处理

ok

持续更新文章来源地址https://www.toymoban.com/news/detail-520977.html

因为jar包冲突问题报错 java.lang.NoSuchMethodError: org.springframework.boot.builder.SpringApplicationBuilder.<init>([Ljava/lang/Object;)V

16:46:41.593 [main] DEBUG org.springframework.boot.diagnostics.LoggingFailureAnalysisReporter - Application failed to start due to an exception
java.lang.NoSuchMethodError: org.springframework.boot.builder.SpringApplicationBuilder.<init>([Ljava/lang/Object;)V
	at org.springframework.cloud.bootstrap.BootstrapApplicationListener.bootstrapServiceContext(BootstrapApplicationListener.java:161)
	at org.springframework.cloud.bootstrap.BootstrapApplicationListener.onApplicationEvent(BootstrapApplicationListener.java:102)
	at org.springframework.cloud.bootstrap.BootstrapApplicationListener.onApplicationEvent(BootstrapApplicationListener.java:68)
	at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:176)
	at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:169)
	at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:143)
	at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:131)
	at org.springframework.boot.context.event.EventPublishingRunListener.environmentPrepared(EventPublishingRunListener.java:85)
	at org.springframework.boot.SpringApplicationRunListeners.lambda$environmentPrepared$2(SpringApplicationRunListeners.java:66)
	at java.util.ArrayList.forEach(ArrayList.java:1249)
	at org.springframework.boot.SpringApplicationRunListeners.doWithListeners(SpringApplicationRunListeners.java:120)
	at org.springframework.boot.SpringApplicationRunListeners.doWithListeners(SpringApplicationRunListeners.java:114)
	at org.springframework.boot.SpringApplicationRunListeners.environmentPrepared(SpringApplicationRunListeners.java:65)
	at org.springframework.boot.SpringApplication.prepareEnvironment(SpringApplication.java:343)
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:301)
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1303)
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1292)
	at com.example.demo.DemoApplication.main(DemoApplication.java:15)
16:46:41.599 [main] ERROR org.springframework.boot.diagnostics.LoggingFailureAnalysisReporter - 

***************************
APPLICATION FAILED TO START
***************************

Description:

An attempt was made to call a method that does not exist. The attempt was made from the following location:

    org.springframework.cloud.bootstrap.BootstrapApplicationListener.bootstrapServiceContext(BootstrapApplicationListener.java:161)

The following method did not exist:

    org.springframework.boot.builder.SpringApplicationBuilder.<init>([Ljava/lang/Object;)V

The calling method's class, org.springframework.cloud.bootstrap.BootstrapApplicationListener, was loaded from the following location:

    jar:file:/C:/Users/ext.jianghaoyu1/.m2/repository/org/springframework/cloud/spring-cloud-context/1.3.6.RELEASE/spring-cloud-context-1.3.6.RELEASE.jar!/org/springframework/cloud/bootstrap/BootstrapApplicationListener.class

The called method's class, org.springframework.boot.builder.SpringApplicationBuilder, is available from the following locations:

    jar:file:/C:/Users/ext.jianghaoyu1/.m2/repository/org/springframework/boot/spring-boot/2.7.10/spring-boot-2.7.10.jar!/org/springframework/boot/builder/SpringApplicationBuilder.class

The called method's class hierarchy was loaded from the following locations:

    org.springframework.boot.builder.SpringApplicationBuilder: file:/C:/Users/ext.jianghaoyu1/.m2/repository/org/springframework/boot/spring-boot/2.7.10/spring-boot-2.7.10.jar


Action:

Correct the classpath of your application so that it contains compatible versions of the classes org.springframework.cloud.bootstrap.BootstrapApplicationListener and org.springframework.boot.builder.SpringApplicationBuilder


进程已结束,退出代码1

解决方式修改低版本springboot

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.0.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

改成如下

<parent>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-parent</artifactId>
       <version>1.5.3.RELEASE</version>
       <relativePath/> <!-- lookup parent from repository -->
</parent>

版本对照

Dubbo hystrix 熔断降级 示例,java,spring boot,dubbo prometheus grafana,dubbo,hystrix,java

ok

持续更新

全代码

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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo-p</name>
    <description>Demo project for Apache Dubbo</description>

    <properties>
        <java.version>1.8</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <dubbo.version>3.1.8</dubbo.version>
        <spring-boot.version>1.5.3.RELEASE</spring-boot.version>
        <hystrix-starter.version>1.4.7.RELEASE</hystrix-starter.version>

    </properties>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring-boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>org.apache.dubbo</groupId>
                <artifactId>dubbo-bom</artifactId>
                <version>${dubbo.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>org.apache.dubbo</groupId>
                <artifactId>dubbo-dependencies-zookeeper</artifactId>
                <version>${dubbo.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>

        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-dependencies-zookeeper</artifactId>
            <type>pom</type>
        </dependency>
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-hystrix</artifactId>
            <version>${hystrix-starter.version}</version>
        </dependency>


    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>${spring-boot.version}</version>
            </plugin>
        </plugins>
    </build>

</project>

启动

package com.example.demo;


import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;

@SpringBootApplication
@EnableDubbo
@EnableHystrix
public class DemoApplication {

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

}

实现

package com.example.demo.dubbo.service;

import com.example.demo.dubbo.api.DemoService;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.apache.dubbo.config.annotation.DubboService;

@DubboService
public class DemoServiceImpl implements DemoService {

    @HystrixCommand(commandProperties = {
            @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold", value = "10"),
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "2000")
    })
    @Override
    public String sayHello(String name) {
        return "Hello " + name;
    }
}

yml

server:
  port: 8101
dubbo:
  application:
    name: dubbo-springBoot-demo-provider
  protocol:
    name: dubbo
    port: -1
  registry:
    address: zookeeper://localhost:2181




  management:
    endpoints:
      web:
        exposure:
          include: "*"
    metrics:
      tags:
        application: ${spring.application.name}





ok

持续更新

到了这里,关于Dubbo hystrix 熔断降级 示例的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • Hystrix和Sentinel熔断降级设计理念

    Sentinel 和 Hystrix 的原则是一致的: 当检测到调用链路中某个资源出现不稳定的表现,例如请求响应时间长或异常比例升高的时候,则对这个资源的调用进行限制,让请求快速失败,避免影响到其它的资源而导致级联故障。 Sentinel 对这个问题采取了两种手段: 通过并发线程数进

    2024年02月09日
    浏览(33)
  • Hystrix入门使用 服务熔断 服务降级 服务雪崩

    hystrix停止更新,理念优秀。 分布式系统面临的问题: 对于复杂的分布式体系,有数十个依赖,依赖不可避免的错误。 服务会出现雪崩, 高可用受到破坏 。 Hystrix就是用于解决分布式系统延迟和容错的开源库。 保证在一个依赖出现问题,不会导致整体的服务失败,避免级联故

    2024年02月07日
    浏览(37)
  • 微服务:Springboot集成Hystrix实现熔断、降级、隔离

    在分布式微服务的项目中,常常会有多个服务复用,产生多个服务调用的情况。比如A服务调用B服务,B服务调用C服务。服务调用链路长了必然会增加服务超时的概率,服务的超时阻塞会一直占用线程资源,大量的阻塞会直接消耗完服务线程,严重情况下会导致服务直接宕机从

    2024年02月12日
    浏览(36)
  • 【从0到1设计一个网关】基于Hystrix实现熔断降级

    上文我们已经成功实现了请求重试与请求限流,接下来我们开始实现熔断与服务降级。 熔断与服务降级,在SpringCloud中设计到的就是我们的hystrix,这里我们也将会考虑配合hystrix来实现熔断与服务降级。 如果不了解hystix的可以先进行一下了解。 由于这里我是用的是基于hystr

    2024年02月05日
    浏览(38)
  • SpringCloud-Hystrix服务熔断与降级工作原理&源码

    在微服务架构中,根据业务来拆分成一个个的服务,服务与服务之间可以相互调用(RPC),在Spring Cloud可以用RestTemplate+Ribbon和Feign来调用。为了保证其高可用,单个服务通常会集群部署。由于网络原因或者自身的原因,服务并不能保证100%可用,如果单个服务出现问题,调用这

    2024年02月14日
    浏览(43)
  • 【OpenFeign】OpenFeign结合Hystrix和Sentinel实现熔断降级

    OpenFeign可以与Hystrix和Sentinel结合使用,实现降级和熔断。 使用OpenFeign需要引入OpenFeign的依赖: spring-cloud-starter-openfeign 引入的依赖如下: 默认已经自动引入了hystrix的依赖,不再需要单独再引入hystrix了。 降级方法的类需要实现FeignClient的接口,同时这个类需要注入到Spring容器

    2024年02月11日
    浏览(27)
  • Spring Boot Dubbo Zookeeper

    Common 公共依赖 定义接口(用户服务注册使用) Provider 首先需要依赖Common yml 实现定义的接口(Service是apache.dubbo) 启动类(@EnableDubbo) Consumer 首先需要依赖Common Controller(@Reference注解) 安装脚本

    2024年02月11日
    浏览(31)
  • 【微服务笔记10】微服务组件之Hystrix实现服务降级和服务熔断

    这篇文章,主要介绍微服务组件之Hystrix实现服务降级和服务熔断。 目录 一、服务降级 1.1、什么是服务降级 1.2、实现服务降级 (1)引入依赖 (2)编写Service层代码 (3)编写Controller层代码 (4)运行测试 (5)fallbackMethod属性 二、服务熔断 2.1、什么是服务熔断 2.2、实现服务

    2023年04月11日
    浏览(68)
  • SpringCloud-Hystrix服务熔断与降级工作原理&源码 | 京东物流技术团队

    在微服务架构中,根据业务来拆分成一个个的服务,服务与服务之间可以相互调用(RPC),在Spring Cloud可以用RestTemplate+Ribbon和Feign来调用。为了保证其高可用,单个服务通常会集群部署。由于网络原因或者自身的原因,服务并不能保证100%可用,如果单个服务出现问题,调用这

    2024年02月14日
    浏览(30)
  • Dubbo Spring Boot Starter 开发微服务应用

    系统:Windows、Linux、MacOS JDK 8 及以上(推荐使用 JDK17) Git IntelliJ IDEA(可选) Docker (可选) 在本任务中,将分为 3 个子模块进行独立开发,模拟生产环境下的部署架构。 如上所示,共有 3 个模块,其中  interface  模块被  consumer  和  provider  两个模块共同依赖,存储 RPC

    2024年02月12日
    浏览(27)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包