前言
记录一次学习SpringCloud的过程,不断积累经验,手撸方能熟能生巧。
新手上路难免有误,人非圣贤,欢迎各位指出不足之处,虚心听取各位的建议与意见。
项目源码:https://github.com/Ahua0918/hwa
一、简介
在微服务架构中,根据业务来拆分成一个个的服务,服务与服务之间可以相互调用(RPC),在SpringCloud可以用RestTemplate+Ribbon和Feign来调用。为了保证其高可用,单个服务通常会集群部署,由于网络原因或者自身的原因,服务并不能保证100%可用,如果单个服务出现问题,调用这个服务就会出现线程阻塞,此时若有大量的请求涌入,Servlet容器的线程资源会被消耗完毕,导致服务瘫痪。服务与服务之间的依赖性,故障会传播,会对整个微服务系统造成灾难性的严重后果,这就是服务故障的“雪崩”效应。为了解决这个问题,我们就会引入断路器的概念。
二、断路器
所谓的断路器,就是当被访问的微服务无法使用的时候,当前服务能够感知这个现象,并且提供一个备用的方案出来
举个例子,数据微服务无法使用了,如果有了断路器,那么视图微服务就能够知道此事,并且展示给用户相关信息,而不会报错或者一直卡在那里。
三、在Feign中使用断路器
3.1 修改pom文件添加依赖
<?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>
<parent>
<artifactId>hwa</artifactId>
<groupId>com.lee</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>hwa-eureka-feign</artifactId>
<packaging>jar</packaging>
<description>SuperFeign</description>
<dependencies>
<!-- Feign客户端依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<!-- Config客户端依赖 用于访问配置服务器 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<!-- 用于访问路径:/actuator/bus-refresh -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- RabbitMQ依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
<!-- Hystrix断路器依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
</dependencies>
</project>
3.2 配置文件
Feign是自带断路器的,但是没有默认打开,需要在配置文件中配置打开它
spring:
cloud:
config:
label: master
profile: dev
discovery:
enabled: true
service-id: hwa-config-server
#bus消息总线
bus:
enabled: true
trace:
enabled: true
#RabbitMQ配置
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest
#主机名
eureka:
client:
#eureka服务地址
#注明自己的服务注册中心的地址,与Eureka Server中的配置application.yml相对应
service-url:
defaultZone: http://localhost:1116/eureka/
feign:
hystrix:
enabled: true
3.3 配置熔断处理类
只需要在FeignClient的HelloFeignClient接口的注解中加上fallback的指定类就完成,表示前面不可用时,就调用后面来反馈信息。
package com.lee.feign.client;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
@FeignClient(name = "hwa-eureka-client",fallback = HelloFeignClientHystrix.class)
public interface HelloFeignClient {
@RequestMapping(value = "/hi",method = RequestMethod.GET)
String sayHiFromFeign(@RequestParam(value = "name") String name);
}
3.4 编写熔断处理类
package com.lee.feign.client;
import org.springframework.stereotype.Component;
@Component
public class HelloFeignClientHystrix implements HelloFeignClient {
@Override
public String sayHiFromFeign(String name) {
return "sorry" +name;
}
}
3.5 修改HwaFeignApplication
添加@EnableHystrix //通过注解@EnableHystrix开启Hystrix
3.6 启动并访问
依次启动HwaEurekaApplication,HwaConfigServerApplication,HwaFeignApplication,访问http://localhost:8884/hi?name=hwa,此时HwaClientApplication工程没有启动,浏览器显示
此时打开HwaClientApplication工程,再次访问时,浏览器显示,证明断路器起到作用了