熔断和降级概念区分:
服务熔断一般是某个服务(下游服务)故障引起(调用的微服务出现故障,预期定义一个方法来进行回应,而不是进行超时等待,针对的可能只是另外一个微服务某个接口),而服务降级一般是从整体负荷考虑(可以理解为直接把另外一个服务停掉,任何请求都无法到达另外一个微服务,预期定义一个返回方法);
如果错误,欢迎及时纠正!!!
1. 导入依赖
2. 编写配置文件
3. 主启动类加上注解支持
4. 业务处理
Spring Cloud2020之后,需要引入 spring-cloud-starter-netflix-hystrix 依赖。
依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
<version>2.2.6.RELEASE</version>
</dependency>
配置文件
新版本需要配置下面这个。eign.hystrix.enabled=true,如果配置这个不会生效
feign:
circuitbreaker:
enabled: true
主启动了加上注解支持@EnableHystrix
package com.cz;
import lombok.extern.slf4j.Slf4j;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.cloud.openfeign.EnableFeignClients;
@SpringBootApplication
@Slf4j
@EnableFeignClients
@EnableHystrix
public class HospitalApplication {
public static void main(String[] args) {
log.info("程序起飞啦!!!");
SpringApplication.run(HospitalApplication.class, args);
}
}
业务处理
服务熔断:
调用的另外一个微服务,超过三秒,会执行预先定义的函数。(也可以通过配置文件进行配置)
调用接口,预期返回的就是service_Business
@RequestMapping(value = "/fegin",method = RequestMethod.POST)
@HystrixCommand(fallbackMethod ="Fallbackhystrix",
commandProperties = {
//开启超时进行服务降级
@HystrixProperty(name="execution.timeout.enabled", value="true"),
//响应超过3秒服务进行降级
@HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds", value="3000")
}
)
public String test1(){
try {
String test = doctorService.test1();
}catch (Exception e){
log.info("远程服务调用异常{}",e);
}
return "test";
}
public String Fallbackhystrix(){
return "service_Business";
}
hystrix:
command:
default:
execution:
isolation:
thread:
timeoutInMilliseconds: 3000 # 服务调用响应超过3s 出发服务降级
调用的微服务类
@RequestMapping(value = "/test",method = RequestMethod.POST)
public String test(){
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return "fegin调用成功";
}
服务降级:
服务降级是在客户端完成的,跟服务端是没有关系的。
feign客户端
package com.cz.service;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
/**
* @Date 2022/2/8 18:52
* @Description
* @Param
*/
@FeignClient(name = "hospatial-doctor",fallback = FallbackDoctor.class)
public interface DoctorService {
@RequestMapping(value = "/test",method = RequestMethod.POST)
public String test1();
}
实现了DoctorService,对每个方法进行了降级处理
package com.cz.service;
import feign.hystrix.FallbackFactory;
import org.springframework.stereotype.Component;
/**
* @Date 2022/2/9 16:45
* @Description
* @Param
*/
@Component
public class FallbackDoctor implements DoctorService {
@Override
public String test1() {
return "service_offline";
}
}
关闭另外一个微服务,访问回调service_offline。