【微服务架构 - 08 - Spring Cloud】06 使用熔断器防止服务雪崩
Ribbon 中使用熔断器
在 pom.xml
中增加依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
在 Application 中增加 @EnableHystrix
注解
package com.yuu.hello.spring.cloud.web.admin.ribbon;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
/**
* @Classname WebAdminRibbonApplication
* @Date 2019/1/29 0:04
* @Created by Yuu
*/
@SpringBootApplication
@EnableDiscoveryClient
@EnableHystrix
public class WebAdminRibbonApplication {
public static void main(String[] args) {
SpringApplication.run(WebAdminRibbonApplication.class, args);
}
}
在 Service 中增加 @HystrixCommand
注解
在 Ribbon 调用方法上增加 @HystrixCommand
注解并指定 fallbackMethod
熔断方法
package com.yuu.hello.spring.cloud.web.admin.ribbon.service;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
/**
* @Classname AdminService
* @Date 2019/1/29 0:09
* @Created by Yuu
*/
@Service
public class AdminService {
@Autowired
private RestTemplate restTemplate;
@HystrixCommand(fallbackMethod = "hiError")
public String sayHi(String message) {
return restTemplate.getForObject("http://HELLO-SPRING-CLOUD-SERVICE-ADMIN/hi?message=" + message, String.class);
}
public String hiError(String message) {
return "Hi, your message is :\"" + message + "\" but request error.";
}
}
测试熔断器
此时我们关闭服务提供者,再次请求 http://localhost:8764/hi?message=HelloRibbon 浏览器会显示:
Feign 中使用熔断器
Feign 是自带熔断器的,但默认是关闭的。需要在配置文件中配置打开它,在配置文件增加以下代码:
feign:
hystrix:
enabled: true
在 Service 中增加 fallback
指定类
package com.yuu.hello.spring.cloud.web.admin.feign.service;
import com.yuu.hello.spring.cloud.web.admin.feign.service.hystrix.AdminServiceHystrix;
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;
/**
* @Classname AdminService
* @Date 2019/1/29 13:09
* @Created by Yuu
*/
@FeignClient(value = "hello-spring-cloud-service-admin", fallback = AdminServiceHystrix.class)
public interface AdminService {
@RequestMapping(value = "hi", method = RequestMethod.GET)
public String sayHi(@RequestParam(value = "message") String message);
}
创建熔断器类并实现对应的 Feign 接口
package com.yuu.hello.spring.cloud.web.admin.feign.service.hystrix;
import com.yuu.hello.spring.cloud.web.admin.feign.service.AdminService;
import org.springframework.stereotype.Component;
/**
* @Classname AdminServiceHystrix
* @Date 2019/1/29 14:09
* @Created by Yuu
*/
@Component
public class AdminServiceHystrix implements AdminService {
@Override
public String sayHi(String message) {
return "Hi, your message is :\"" + message + "\" but request error";
}
}
测试熔断器
此时我们关闭服务提供者,再次请求 http://localhost:8765/hi?message=HelloFeign 浏览器会显示:
还没有评论,来说两句吧...