SpringCould总结 | 第五篇 服务调用失败熔断器Hystrix
===========ribbon中使用熔断器解决服务调用失败===========
//工程结构
//pom文件
<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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.8.RELEASE</version>
<relativePath />
</parent>
<groupId>com.spc.hystrix.server</groupId>
<artifactId>spc-hystrix-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>spc-hystrix-server</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Edgware.RELEASE</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-ribbon</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
//服务配置
spring.application.name=spc-ribbon-server
server.port=2000
eureka.client.service-url.defaultZone=http://localhost:1000/eureka/
#eureka.instance.preferIpAddress=true
#eureka.instance.instance-id=${spring.cloud.client.ipAddress}:${server.port}
//服务类
package com.spc.hystrix.server.spc_hystrix_server;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
@Service
public class AppService {
@Autowired
RestTemplate restTemplate;
@HystrixCommand(fallbackMethod = "reqErrFallBack")
public String queryOrderStatusById(String orderId) {
return restTemplate.getForObject("http://SPC-SERVICE-ORDER/queryOrderStatusById?orderId="+orderId,String.class);
}
@HystrixCommand(fallbackMethod = "reqErrFallBack")
public String queryWlInfoByOrderId(String orderId) {
//return restTemplate.getForObject("http://xxx/xxx?xxx="+xxx,String.class);
return ":[订单-商品-已发货]";
}
public String reqErrFallBack(String orderId) {
//记录调用日志
return "记录熔断日志:Error:"+orderId+"调用失败调用IP:xxxx!!!";
}
}
package com.spc.hystrix.server.spc_hystrix_server;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class AppControler {
@Autowired
private AppService appService;
/**
* 根据订单号查询订单状态查询物流信息
* @param orderId
* @return
*/
@RequestMapping(value = "/queryOrderStatusAndWlInfoByOrderId")
public String queryOrderStatusAndWlInfoByOrderId(@RequestParam String orderId){
/**
* 查询订单状态
*/
String orderInfo = appService.queryOrderStatusById(orderId);
/**
* 查询物流信息
*/
String wlInfo = appService.queryWlInfoByOrderId(orderId);
return orderInfo+wlInfo;
}
}
package com.spc.hystrix.server.spc_hystrix_server;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@SpringBootApplication
@EnableDiscoveryClient
@EnableHystrix
public class RibbonServer {
public static void main(String[] args) {
SpringApplication.run(RibbonServer.class, args);
}
@Bean
@LoadBalanced
RestTemplate restTemplate() {
return new RestTemplate();
}
}
//运行结果
1.启动eureka注册中心
2.启动eureka服务spc-service-order开一个服务(端口)即可
3.启动ribbon服务
4.调用测试
5.关闭服务 测试熔断
//关闭order服务测试
========================feign使用熔断=================================
package com.spc.hystrix.server.feign.spc_hystrix_server;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
@FeignClient(value = "SPC-SERVICE-ORDER",fallback = AppServiceImpl.class)
public interface AppService {
@RequestMapping(value = "/queryOrderStatusById", method = RequestMethod.GET)
String queryOrderStatusById(@RequestParam(value = "orderId") String orderId);
}
package com.spc.hystrix.server.feign.spc_hystrix_server;
import org.springframework.stereotype.Component;
@Component
public class AppServiceImpl implements AppService{
@Override
public String queryOrderStatusById(String orderId) {
return "Feign_Error:"+orderId+"调用失败:IP:xxxx!!!";
}
}