· 在dubbo项目消费者进行远程调用时,遇到网络波动等非业务异常可以通过配置服务降级来进行异常的统一管理
-
直接在applicationContext.xml中直接配置
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:dubbo="http://dubbo.apache.org/schema/dubbo" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <!-- 声明应用的名称 --> <dubbo:application name="ego-user-service-consumer"/> <!--声明注册中心的地址--> <dubbo:registry address="zookeeper://127.0.0.1:2181"/> <!--<dubbo:reference interface="com.sdkj.service.UserService" timeout="10000" check="false" mock="return null"/>--> <!-- 原生方式 --> <!--生成远程调用对象--> <!--<dubbo:reference id="userService" interface="com.sdkj.service.UserService"/>--> <!--创建订单服务对象 指向远程通信对象 --> <!--<bean id="orderService" class="com.sdkj.service.impl.OrderServiceImpl"> <property name="userService" ref="userService"/> </bean>--> <!-- 注解方式,需要在类上面加入dubbo包的@Service 以及 @Reference引入接口 --> <dubbo:annotation package="com.sdkj.service.impl"/> <context:component-scan base-package="com.sdkj.service.impl"/> <!--配置服务降级:遇到网络波动等非业务异常会返回null--> <dubbo:consumer check="false" mock="return null"/> </beans>
-
通过编写异常类重写调用方法来进行管理
1 package com.sdkj.service; 2 3 import com.sdkj.pojo.UserAddress; 4 5 import java.util.List; 6 7 /** 8 * @Author wangshuo 9 * @Date 2022/5/17, 22:22 10 * <dubbo:consumer check="false" mock="return null"/> 11 * 将mock配置为true,遇到网络波动等非业务异常会调用本类中重写的方法 12 */ 13 public class UserServiceMock implements UserService{ 14 @Override 15 public List<UserAddress> queryAllAddress(String userid) { 16 System.out.println("服务调用失败"); 17 return null; 18 } 19 }
本文来自博客园,作者:荣慕平,转载请注明原文链接:https://www.cnblogs.com/rongmuping/articles/16282713.html