目录
介绍
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
持续更新