服务降级
mock只出现在非业务异常(比如超时,网络异常等等)时执行.
他的配置支持两种:
- 布尔值,默认false,如果配置为true,则默认使用mock类名,即类名+Mock后缀
- return null.可以很简单的忽略掉异常
设置成return null
消费者dubbo配置
dubbo:
application:
name: dubbo-springboot-consumer
scan:
base-packages: com.ql.controller
protocol:
name: dubbo
port: 20990
registry:
client: curator
address: zookeeper://127.0.0.1:2181?backup=127.0.0.1:2182,127.0.0.1:2183
consumer:
mock: return null
check: false #启动时不检查服务
controller
@RestController
@RequestMapping("hello")
public class HelloController {
@DubboReference(version = "1.0.0")
private HelloService helloService;
@RequestMapping(value = "sayHello")
public String sayHello(){
String s = helloService.sayHello();
System.out.println(s);
return s;
}
}
service实现
@DubboService(version = "1.0.0")
@Service
public class HelloServiceImpl implements HelloService {
@Override
public String sayHello() {
return "服务被成功调用了";
}
}
interface
public interface HelloService {
String sayHello();
}
访问对比
这里使用关闭服务提供者来模拟非业务异常,所以需要设置consumer.check=false
成功访问时
关闭服务提供者模拟非业务异常,未进行服务降级
进行mock设置return null后
mock设置成true
消费者
dubbo:
application:
name: dubbo-springboot-consumer
scan:
base-packages: com.ql.controller
protocol:
name: dubbo
port: 20990
registry:
client: curator
address: zookeeper://127.0.0.1:2181?backup=127.0.0.1:2182,127.0.0.1:2183
consumer:
mock: true
check: false
在公共接口中使用类名+Mock后缀进行自定义
创建HelloServiceMock实现HelloService
/**
* @author qinlei
* @description todo
* @date 2020/10/20 15:20
*/
public class HelloServiceMock implements HelloService{
@Override
public String sayHello() {
System.out.println("服务调用失败...");
return null;
}
}
其他配置不变
重启消费者,同样不启动提供者
使用springcloud的hystrix集成dubbo
pom文件添加依赖
这里注意引入的依赖版本需要和你得springboot版本一致,否则会报错
<!--springcloud hystrix 依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
<version>2.1.3.RELEASE</version>
</dependency>
提供者
暴露方法上增加@HystrixCommand,这样该方法就会经过Hystrix代理了
/**
* @author qinlei
* @description todo
* @date 2020/10/20 14:52
*/
@DubboService(version = "1.0.0")
@Service
public class HelloServiceImpl implements HelloService {
@Override
@HystrixCommand //表示该方法被hystrix代理了,出现异常就会熔断
public String sayHello() {
return "服务被成功调用了";
}
}
消费者
yml文件配置Hystrix
hystrix:
command:
default:
execution:
isolation:
thread:
timeoutInMilliseconds: 4000 #超时时间
启动类添加@EnableHystrix ,开启熔断器服务
/**
* @author qinlei
* @description todo
* @date 2020/10/20 15:08
*/
@SpringBootApplication
@EnableDubbo
@EnableHystrix //开启熔断器服务
public class ConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class,args);
}
}
调用该提供者的方法时,添加@HystrixCommand,并指定出错时的回调方法
/**
* @author qinlei
* @description todo
* @date 2020/10/20 15:01
*/
@RestController
@RequestMapping("hello")
public class HelloController {
@DubboReference(version = "1.0.0")
private HelloService helloService;
@HystrixCommand(fallbackMethod = "hello")
@RequestMapping(value = "sayHello")
public String sayHello(){
String s = helloService.sayHello();
System.out.println(s);
return s;
}
/**
* 服务熔断的回调方法
*/
public String hello(){
System.out.println("服务熔断了...");
return ("服务熔断了...");
}
}
同样不启动提供者,模拟超时,设置启动不检查service
demo地址:
https://github.com/qlanto224/dubbo-springboot.git
觉得有用的,感谢您一赞的支持