本章将编写一个使用SpringBoot工程集成dubbo使用hystrix组件实现服务熔断示例。包含服务提供者工程和服务消费者工程。主要在实现整合springcloud hystrix过程步骤如下:
- 提供者、消费者工程添加hystrix依赖。
- 提供者、消费者工程启动类添加@EnableHystrix注解
- 提供者工程接口方法添加@HystrixCommand注解
- 服务消费者工程调用类方法上添加@HystrixCommand注解,并配置failbackMethod 属性执行超时失败后执行本地函数
本章下面代码的pom中接口类工程做了独立工程,代码在Dubbo(三) 消费者、提供者工程搭建并实现远程调用 文章中查看。
一、提供者工程完整目录代码:
1.1 pom.xml(注意springboot 和 hystrix 版本,hystrix过低时启动会报错)
添加hystrixpom依赖
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.xiaohui.bootuserserviceprovider</groupId>
<artifactId>boot-user-service-provider</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<java.version>1.8</java.version>
<maven-jar-plugin.version>3.1.1</maven-jar-plugin.version>
<dubbo.version>2.7.3</dubbo.version>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.4.RELEASE</version>
</parent>
<dependencies>
<!-- 公共接口模块 -->
<dependency>
<groupId>com.xiaohui.serviceinterface</groupId>
<artifactId>service-interface</artifactId>
<version>1.0</version>
</dependency>
<!-- dubbo zookeeper 依赖 -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>${dubbo.version}</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-dependencies-zookeeper</artifactId>
<version>${dubbo.version}</version>
<type>pom</type>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
<version>2.1.6.RELEASE</version>
</dependency>
</dependencies>
</project>
1.2 springboot 配置文件 application.properties
dubbo.application.name=user-service-provider
dubbo.registry.address=zookeeper://192.168.0.126:2181
dubbo.protocol.name=dubbo
dubbo.protocol.port=20880
1.3 远程接口实现类UserServiceImpl.java
远程接口方法添加@HystrixCommand注解
package com.xiaohui.service.impl;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.xiaohui.domain.UserAddress;
import com.xiaohui.service.UserService;
import org.apache.dubbo.config.annotation.Service;
import java.util.ArrayList;
import java.util.List;
@Service(version = "1.0.0",retries = 2) //,loadbalance="consistenthash" apache dubbo service 注解
public class UserServiceImpl implements UserService {
public static List<UserAddress> address = new ArrayList<UserAddress>();
static{
address.add(new UserAddress(1,"西安市未央区XXXX路","20881"));
address.add(new UserAddress(2,"郑州市中原区XXXX街","20881"));
}
@HystrixCommand
@Override
public List<UserAddress> queryAllUserAddress(String userId) {
try { Thread.sleep(5000); } catch (Exception e) { e.printStackTrace(); }
return address;
}
}
1.4 启动类
启动类添加@EnableHystrix 注解启用 Hystrix。
package com.xiaohui;
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 BootProviderApplication {
public static void main(String[] args) {
SpringApplication.run(BootProviderApplication.class, args);
System.out.println("服务提供者启动....");
}
}
二、服务消费者工程目录及代码:
2.1 pom.xml
添加hystrix 相关依赖。
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.xiaohui.boot-order-service-consumer</groupId>
<artifactId>boot-order-service-consumer</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<java.version>1.8</java.version>
<maven-jar-plugin.version>3.1.1</maven-jar-plugin.version>
<dubbo.version>2.7.3</dubbo.version>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.4.RELEASE</version>
</parent>
<dependencies>
<!-- 公共接口模块 -->
<dependency>
<groupId>com.xiaohui.serviceinterface</groupId>
<artifactId>service-interface</artifactId>
<version>1.0</version>
</dependency>
<!-- web模块 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- dubbo 依赖-->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>${dubbo.version}</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-dependencies-zookeeper</artifactId>
<version>${dubbo.version}</version>
<type>pom</type>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
<version>2.1.6.RELEASE</version>
</dependency>
</dependencies>
</project>
2.2 springboot配置文件application.properties
server.port=8080
dubbo.application.name=order-service-consumer
dubbo.registry.address=zookeeper://192.168.0.126:2181
2.3 远程接口实现类OrderServiceImpl.java
在消费端调用类上添加@HystrixCommand注解,并使用fallback指定接口调用超时失败后的本地执行返回方法。
package com.xiaohui.service.impl;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.xiaohui.domain.UserAddress;
import com.xiaohui.service.OrderService;
import com.xiaohui.service.UserService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Service;
import java.util.Arrays;
import java.util.List;
@Service
public class OrderServiceImpl implements OrderService {
// @Reference(check = false,url = "127.0.0.1:20880",version = "1.0.0",retries = 3,stub = "com.xiaohui.service.impl.UserServiceStub")//,
@Reference(version = "1.0.0",timeout = 3000,cluster = "broadcast")
private UserService userService;
@HystrixCommand(fallbackMethod = "fallbackForInitOrder")
@Override
public List<UserAddress> initOrder(String userId) {
List<UserAddress> userAddresses = userService.queryAllUserAddress(userId);
System.out.println(userAddresses);
return userAddresses;
}
public List<UserAddress> fallbackForInitOrder(String userId) {
return Arrays.asList(new UserAddress(1,"springCloud Hystrix","20002"));
}
}
2.4 springboot启动类
服务消费者工程启动类添加@EnableHystrix注解启用Hystrix。
package com.xiaohui;
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 BootConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(BootConsumerApplication.class, args);
System.out.println("服务消费者启动....");
}
}
2.5 web Controller web调用类
package com.xiaohui.controller;
import com.xiaohui.domain.UserAddress;
import com.xiaohui.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequestMapping("order")
public class OrderController {
@Autowired
private OrderService orderService;
@GetMapping(value = "/getAddress")
public List<UserAddress> getUserAddress(String userId){
List<UserAddress> userAddresses = orderService.initOrder(userId);
return userAddresses;
}
}
三,启动测试
分别启动服务提供者、消费者工程。通过访问本地web测试页面接口,可以看到返回了我们消费者工程中fallback中指定的本地方法。使接口进行了容错效果。