一.ribbon中使用熔断器
在(4)springcloud创建服务消费者(Ribbon)
https://blog.csdn.net/yanyf2016/article/details/102895149
在pom.xml中增加依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
在WebAdminRibbonApplication中增加注解@EnableHystrix开启熔断器
在AdminService增加一个注解@HystrixCommand和一个方法
@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 String.format("Hi your message is : %s but request bad", message);
}
重启项目,将8762和8763服务关掉,再次访问8764看效果
二、在feign中使用熔断器 目录结构:
Feign自带熔断器,但默认是关闭的,需要再配置文件中打开
(5)springcloud创建服务消费者(feign)
https://blog.csdn.net/yanyf2016/article/details/102895844
application.yum中加
feign:
hystrix:
enabled: true
实现AdminFeignService
import com.itheima.hello.spring.cloud.web.admin.feign.service.AdminFeignService;
import org.springframework.stereotype.Component;
@Component
public class AdminServiceHystrix implements AdminFeignService {
@Override
public String sayHi(String message) {
return String.format("Hi your message is : %s, but request bad", message);
}
}
接口中加 fallback = AdminServiceHystrix.class
@FeignClient(value = "hello-spring-cloud-service-admin", fallback = AdminServiceHystrix.class)
public interface AdminFeignService {
@GetMapping("hi")
public String sayHi(@RequestParam(value="message") String message);
}