Dubbo hystrix 熔断降级 详细示例 多服务 公共api

目录

介绍

demo-api

pom

目录

代码api

provider 服务提供者

目录

pom

服务实现代码

启动代码

配置

日志

consumer 消费者

目录

pom

调用service接口

调用serviceImpl类

ctr

配置

页面调用熔断效果

相关文章


介绍

因为网上和官网拷贝的文档发现有很多版本迭代出现的jar包冲突 和配置缺少 导致服务启动不起来问题,所有写这一篇 dubbo hystrix 熔断器 多模块模式 的全代码文档,便于使用的时候具体拷贝具体位置,亲测通过正常运行,省去繁琐踩坑分析调整,当中有设计到  sentinel 相关配置可关注可注释掉,相关文档放到下方 sentinel 启动的时候jvm会配置一些链接控制台的东西可看具体文章。

https://blog.csdn.net/weixin_42749765/category_8752962.html

Dubbo hystrix 熔断降级 简单示例 java.lang.NoSuchApplicationBuilder.<init>([Ljava/lang/Object;)V_宇神城主_蒋浩宇的博客-CSDN博客

dubbo Sentinet 限流 流控配置 高级 直连 关联 链路 预热冷启动 排队等待 单机 qps 并发 机器总体法制 单机均摊 集群俩种身份_宇神城主_蒋浩宇的博客-CSDN博客

Dubbo sentinel限流 代码示例 控制台 下载使用_宇神城主_蒋浩宇的博客-CSDN博客 

相关文档有需要看运行没啥问题制作参考即可

demo-api

pom

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"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-api</artifactId><version>0.0.1-SNAPSHOT</version><name>demo-api</name><properties><java.version>1.8</java.version><spring-boot.version>1.5.3.RELEASE</spring-boot.version> </properties><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>

目录

代码api

package com.example.demo.dubbo.api;public interface DemoService {String sayHello22(String name);}

provider 服务提供者

目录

pom

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"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><sentinel.version>1.8.6</sentinel.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>com.example</groupId><artifactId>demo-api</artifactId><version>0.0.1-SNAPSHOT</version></dependency><dependency><groupId>com.alibaba.csp</groupId><artifactId>sentinel-apache-dubbo3-adapter</artifactId><version>${sentinel.version}</version></dependency><dependency><groupId>com.alibaba.csp</groupId><artifactId>sentinel-transport-simple-http</artifactId><version>${sentinel.version}</version></dependency><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.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(version = "1.0.0")
public class DemoServiceImpl implements DemoService {@HystrixCommand(commandProperties = {@HystrixProperty(name = "circuitBreaker.requestVolumeThreshold", value = "10"),@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "1000")})@Overridepublic String sayHello22(String name) {throw new RuntimeException("Exception to show hystrix enabled.");}
}

启动代码

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

配置

dubbo.application.logger=slf4j

server:port: 8101
dubbo:application:name: dubbo-springBoot-demo-providerprotocol:name: dubboport: -1registry:address: zookeeper://localhost:2181management:endpoints:web:exposure:include: "*"metrics:tags:application: ${spring.application.name}

日志

###set log levels###
log4j.rootLogger=info, stdout
###output to the console###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=[%d{hh:mm:ss:sss}] %5p %c{2}: %m%n
log4j.logger.org.apache.zookeeper=warn
log4j.logger.org.apache.dubbo.registry.zookeeper=warn
log4j.logger.org.apache.dubbo.config.deploy=warn
log4j.logger.org.apache.dubbo.registry.client=warn
log4j.logger.org.apache.dubbo.rpc.model=warn

consumer 消费者

目录

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><sentinel.version>1.8.6</sentinel.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>com.example</groupId><artifactId>demo-api</artifactId><version>0.0.1-SNAPSHOT</version></dependency><dependency><groupId>com.alibaba.csp</groupId><artifactId>sentinel-apache-dubbo3-adapter</artifactId><version>${sentinel.version}</version></dependency><dependency><groupId>com.alibaba.csp</groupId><artifactId>sentinel-transport-simple-http</artifactId><version>${sentinel.version}</version></dependency><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>

调用service接口

package com.example.demo.dubbo.consumer;public interface ConsumerI {public String get(String name);
}

调用serviceImpl类

package com.example.demo.dubbo.consumer;import com.example.demo.dubbo.api.DemoService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Service;@DubboService
public class Consumer implements ConsumerI  {@DubboReference(version = "1.0.0")private DemoService demoService;@HystrixCommand(fallbackMethod = "reliable")@Overridepublic String get(String name){return demoService.sayHello22(name);}public String reliable(String name) {return "hystrix fallback value";}
}

ctr

package com.example.demo.dubbo.consumer;import com.example.demo.dubbo.api.DemoService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
public class ctr {@DubboReferenceprivate ConsumerI c;@RequestMapping("/api/v")public String v(){return c.get("ss");}
}

配置


# Dubbo 应用名
dubbo.application.logger=slf4j
server:port: 8201
dubbo:application:name: dubbo-springBoot-demo-consumerprotocol:name: dubboport: -1registry:address: zookeeper://localhost:2181management:endpoints:web:exposure:include: "*"metrics:tags:application: ${spring.application.name}
###set log levels###
log4j.rootLogger=info, stdout
###output to the console###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=[%d{hh:mm:ss:sss}] %5p %c{2}: %m%n
log4j.logger.org.apache.zookeeper=warn
log4j.logger.org.apache.dubbo.registry.zookeeper=warn
log4j.logger.org.apache.dubbo.config.deploy=warn
log4j.logger.org.apache.dubbo.registry.client=warn
log4j.logger.org.apache.dubbo.rpc.model=warn

页面调用熔断效果

http://localhost:8201/api/v

相关文章

Dubbo hystrix 熔断降级 示例 java.lang.NoSuchMethod r.SpringApplicationBuilder.<init>([Ljava/lang/Object;)V_宇神城主_蒋浩宇的博客-CSDN博客

ok

持续更新

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/13275.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

PyQt如何查找帮助信息(不会写组件的代码,快看过来!)

1.可以在PyQt6官网中查找信息kReference Guide — PyQt Documentation v6.5.1 看不懂没有关系啦&#xff0c;可以使用网页翻译哒~ 找到或者直接搜索QLabel&#xff0c;寻找对应函数即可 2. https://zetcode.com/pyqt6/ 3.Qt Creator中寻找 例如&#xff0c;输入setText 就可以…

el-table 添加合计,合计某一列

效果图&#xff1a; 1. 使用elementui 官网上的方法 如果是只要是数值&#xff0c;就要合并&#xff0c;就只设置show-summary 即可。 2. html&#xff1a; <!--cell-style 改变某一列行的背景色 --><!-- tree-props 配置树形子表row-click: 单击事件highlight-cu…

SpringBoot2+Vue2实战(十二)springboot一对一,一对多查询

新建数据库表 Course Data TableName("t_course") public class Course implements Serializable {private static final long serialVersionUID 1L;/*** id*/TableId(value "id", type IdType.AUTO)private Integer id;/*** 课程名称*/private String…

开源数字名片生成器EnBizCard

什么是 EnBizCard &#xff1f; EnBizCard 可帮助您创建美观、响应灵敏的基于 HTML 的数字名片&#xff0c;并将其托管在您的网站上。 无需注册100% 免费和开源没有用户跟踪和数据收集离线工作 如果不想自己搭建&#xff0c;可以去试用官方的在线体验站点&#xff0c;地址&…

排序算法解析

常见的排序算法包括以下几种&#xff1a; 冒泡排序&#xff08;Bubble Sort&#xff09;&#xff1a;重复比较相邻的两个元素&#xff0c;将较大的元素逐渐向后移动&#xff0c;直到整个序列有序。 选择排序&#xff08;Selection Sort&#xff09;&#xff1a;从未排序部分选…

java面试题(24)

1、重写equals&#xff08;&#xff09;方法的原则 1、对称性&#xff1a; 如果x.equals&#xff08;y&#xff09;返回是“true”&#xff0c;那么y.equals&#xff08;x&#xff09;也应该返回是 “true”。 2、自反性&#xff1a; x.equals&#xff08;x&#xff09;必须…

mysql 数据库备份和还原

数据库备份 第一步&#xff1a;先在环境变量Path里面加上mysql bin目录的路径 第二部&#xff1a;执行对应的命令 备份数据库中的表&#xff1a; 命令&#xff1a; mysqldump -u root -p fenku_lianxi customer > d:\temp.sql root是 账号 username fenku_lianxi是数据…

transforms机制与数据标准化

文章和代码已经归档至【Github仓库&#xff1a;https://github.com/timerring/dive-into-AI 】或者公众号【AIShareLab】回复 pytorch教程 也可获取。 文章目录 图像预处理 transformstransforms运行机制数据标准化transforms.normalizetransforms.Normalize 图像预处理 transf…

javassist 02 implement interface

创建 interface package com.wsd;public interface AccountDao {int delete(); }利用 javassist 生产一个 类A, Class A implements AccountDao package com.wsd;import javassist.ClassPool; import javassist.CtClass; import javassist.CtMethod; import javassist.Modifi…

4. PS切图

4.1常见的图片格式 jpg图像格式: JPEG ( .JPG )对色彩的信息保留较好,高清,颜色较多,我们产品类的图片经常用jpg格式的gif图像格式 : GIF格式最多只能储存256色,所以通常用来显示简单图形及字体,但是可以保存透明背景和动画效果,实际经常用于一些图片小动画效果png图像格式&am…

RabbitMQ五种工作模式的简单应用

发布订阅模式(三种方式) 发布订阅模式上一篇我们已经介绍了,这里就不做介绍了,想要看的可以点此链接 在SpringBoot中对RabbitMQ三种使用方式_流殇꧂的博客-CSDN博客 Routing(路由模式)(基于注解的方式) 1.创建交换机和消息队列,类型要为direct(不设置也是默认的配置) //路由模式…

企业低成本万能架构

企业软件应用架构层出不穷&#xff08;这里的应用架构是指偏后端服务的软件架构&#xff09;每个企业由各自业务形态&#xff0c;技术栈&#xff0c;技术路线&#xff0c;技术实力不同&#xff0c;各自架构方案&#xff0c;技术选型各有各的不同&#xff0c;千姿百态&#xff0…