本章将编写一个使用SpringBoot工程集成dubbo使用hystrix组件实现服务熔断示例。包含服务提供者工程和服务消费者工程。主要在实现整合springcloud hystrix过程步骤如下:
本章下面代码的pom中接口类工程做了独立工程,代码在Dubbo(三) 消费者、提供者工程搭建并实现远程调用 文章中查看。
1.1 pom.xml(注意springboot 和 hystrix 版本,hystrix过低时启动会报错)
添加hystrixpom依赖
4.0.0
com.xiaohui.bootuserserviceprovider
boot-user-service-provider
1.0-SNAPSHOT
1.8
3.1.1
2.7.3
org.springframework.boot
spring-boot-starter-parent
2.1.4.RELEASE
com.xiaohui.serviceinterface
service-interface
1.0
org.apache.dubbo
dubbo-spring-boot-starter
${dubbo.version}
org.apache.dubbo
dubbo-dependencies-zookeeper
${dubbo.version}
pom
org.slf4j
slf4j-log4j12
org.springframework.cloud
spring-cloud-starter-netflix-hystrix
2.1.6.RELEASE
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 address = new ArrayList();
static{
address.add(new UserAddress(1,"西安市未央区XXXX路","20881"));
address.add(new UserAddress(2,"郑州市中原区XXXX街","20881"));
}
@HystrixCommand
@Override
public List 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 相关依赖。
4.0.0
com.xiaohui.boot-order-service-consumer
boot-order-service-consumer
1.0-SNAPSHOT
1.8
3.1.1
2.7.3
org.springframework.boot
spring-boot-starter-parent
2.1.4.RELEASE
com.xiaohui.serviceinterface
service-interface
1.0
org.springframework.boot
spring-boot-starter-web
org.apache.dubbo
dubbo-spring-boot-starter
${dubbo.version}
org.apache.dubbo
dubbo-dependencies-zookeeper
${dubbo.version}
pom
org.slf4j
slf4j-log4j12
org.springframework.cloud
spring-cloud-starter-netflix-hystrix
2.1.6.RELEASE
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 initOrder(String userId) {
List userAddresses = userService.queryAllUserAddress(userId);
System.out.println(userAddresses);
return userAddresses;
}
public List 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 getUserAddress(String userId){
List userAddresses = orderService.initOrder(userId);
return userAddresses;
}
}
三,启动测试
分别启动服务提供者、消费者工程。通过访问本地web测试页面接口,可以看到返回了我们消费者工程中fallback中指定的本地方法。使接口进行了容错效果。