SpringCloud Hystrix服务容错保护模块

说明

在微服务架构中,我们将系统拆分成了很多服务单元,各单元的应用 间通过服务注册与订阅的方式相互依赖。由于每个单元都在不同的进程中运行,依赖通过远程调用的方式执行,这样就有可能因为网络原因或是依赖服务自身间题出现调用故障或延迟,而这些问题会直接导致调用方的对外服务也出现延迟,若此时调用方的请求不断增加,最后就会因等待出现故障的依赖方响应形成任务积压,最终导致自身服务的瘫痪。

在微服务架构中,存在着那么多的服务单元,若一个单元出现故障,就很容易因依赖关系而引发故障的蔓延,最终导致整个系统的瘫痪,这样的架构相较传统架构更加不稳定。为了解决这样的问题,Spring Cloud Hystrix使用断路器、线程隔离、服务降级、请求缓存等一系列的服务保护机制。

断路器:当某个服务单元发生故障,通过断路器的故障监控,直接返回调用方一个错误响应而不是长时间的等待和重试,这样就不会使线程因调用故障服务被长时间占用不释放,避免故障引发在分布式系统中的"雪崩效应"。

目标

使用SpringCloud Hystrix实现断路器、服务降级和仪表盘监控功能。

快速使用

首先给出项目结构(本项目改自上一篇Ribbon文章的服务消费者项目,服务提供者和Eureka注册中心沿用前面的项目即可)


1 更新pom.xml

<?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>yunlingfly</groupId>
	<artifactId>mavenspringcloudribbon</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>mavenspringcloudribbon</name>
	<description>Demo project for Spring Boot</description>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.5.10.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>

	<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>Dalston.SR2</spring-cloud.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-eureka</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-eureka-server</artifactId>
		</dependency>

		<!-- 断路器 -->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-hystrix</artifactId>
		</dependency>

		<!-- 断路器仪表盘 -->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-hystrix-dashboard</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>

2 更新application.yml

eureka:
  client:
    serviceUrl:
      defaultZone: http://xxx.xxx.xxx.xxx:8761/eureka/,http://xxx.xxx.xxx.xxx:8762/eureka/
server:
  port: 8764
spring:
  application:
    name: service-ribbon
info:
  app:
    name: 一个消费者
    version: 0.0.1

3 更新启动类

package yunlingfly.mavenspringcloudribbon;

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.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
// 向服务中心注册
@EnableDiscoveryClient
// 开启断路器
@EnableHystrix
// 开启仪表盘
@EnableHystrixDashboard
public class MavenspringcloudribbonApplication {

	public static void main(String[] args) {
		SpringApplication.run(MavenspringcloudribbonApplication.class, args);
	}
	@Bean
	// @LoadBalanced注解表明这个restRemplate开启负载均衡的功能
	@LoadBalanced
	RestTemplate restTemplate() {
		return new RestTemplate();
	}
}

4 编写Service层

package yunlingfly.mavenspringcloudribbon.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;

@Service
@Component
public class HelloService {
    @Autowired
    RestTemplate restTemplate;

    @HystrixCommand(fallbackMethod = "hiError")
    public String hiService(String name) {
        return restTemplate.getForObject("http://eureka-client/hi?name="+name,String.class);
    }

    public String hiError(String name){
        return "hi "+name+",sorry,error,断路器启动...";
    }
}
5 编写Controller层
package yunlingfly.mavenspringcloudribbon.controller;

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;
import yunlingfly.mavenspringcloudribbon.service.HelloService;

@RestController
public class HelloControler {
    @Autowired
    HelloService helloService;

    @RequestMapping(value = "/hi")
    public String hi(@RequestParam String name){
        return helloService.hiService(name);
    }
}
运行

首先启动Eureka注册中心,然后启动服务提供者,再启动服务消费者,打开浏览器输入http://localhost:8764/hi?name=芸灵fly可以正常看到上一篇文章的截图后,访问http://localhost:8764/hystrix可以进入仪表盘监控界面:


输入http://localhost:8764/hystrix.stream点击Monitor Stream进入页面,关闭服务提供者项目,然后再次访问http://localhost:8764/hi?name=芸灵fly即可看到


切换回刚刚的hystrix页面,可以查看监控


注意事项:使用@HystrixCommand来开启断路器,fallbackMethod属性执行服务降级的处理方法


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值