服务降级的发生,其实是由于消费者调用服务超时引起的,即从发出调用请求到获取到提供者的响应结果这个时间超出了设定的时限,默认服务调用超时时限为1秒,可以在消费者端与提供者端设置超时时限;
1 创建提供者工程
(1) 创建工程
创建provider-timeout,并导入依赖;
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<!-- 自定义版本号 -->
<spring-version>4.3.16.RELEASE</spring-version>
</properties>
<dependencies>
<!-- dubbo依赖 -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
<version>2.7.0</version>
</dependency>
<!-- Spring依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>${spring-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>${spring-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring-version}</version>
</dependency>
<!-- zk客户端依赖:curator -->
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>2.13.0</version>
</dependency> <dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>2.13.0</version>
</dependency>
<!-- commons-logging依赖 -->
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring-version}</version>
</dependency>
</dependencies>
(3) 定义接口
在com.abc.service包中定义如下接口;
public interface UserService {
String getUsernameById(int id);
void addUser(String username);
}
(4) 定义接口实现类
在com.abc.service包中定义接口的实现类,该实现类中的业务方法添加一个2秒的Sleep,以延长向消费者返回结果的时间,模拟超时;
public class UserServiceImpl implements UserService{
@Override
public String getUsernameById(int id) {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//返回预定指定值
return "jamse";
}
@Override
public void addUser(String username) {
// 返回预定成功提示
System.out.println("添加用户成功");
}
}
(5) 配置文件
<!-- 添加 DUBBO SCHEMA -->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<!-- 指定当前工程在Monitor监控中心显示的名称,一般与工程名相同 -->
<dubbo:application name="provider-timeout" />
<!-- 指定注册中心-->
<dubbo:registry address="zookeeper://192.168.85.129:2181" />
<!-- 注册业务实现类,真正的服务提供者 -->
<bean id="userService" class="com.zxy.service.UserServiceImpl"/>
<!-- 暴露服务:接口即服务名称,指向真正的服务提供者 -->
<dubbo:service interface="com.zxy.service.UserService"
ref="userService"/>
</beans>
2 创建消费者工程
应用https://blog.csdn.net/zxylwj/article/details/108583068中的consumer-morkclass工程;
运行:抛出请求超时异常,返回了降级结果;
3 修改配置文件
可以通过修改提供者或消费者端的配置来设置超时时限,同样在消费者端设置权限高于在提供者端设置;
(1)提供者端设置超时时限
<!-- 暴露服务:接口即服务名称,指向真正的服务提供者 -->
<dubbo:service interface="com.zxy.service.UserService"
ref="userService" timeout="3000"/>
运行:请求成功,获取正确结果;
(2)消费者端与提供者端同时设置超时时限
<!-- 服务消费者-->
<dubbo:reference id="userService" mock="true" check="false"
interface="com.zxy.service.UserService" timeout="2000"/>
运行:消费者端设置为2秒(请求超时),提供者端设置为3秒(请求不超时),运行结果超时;
4 超时时限