此处以前面第五节实验的为例:
当`电影服务`调用`用户服务`接口出现问题的时候,触发熔断,返回默认的值。
* 复制实验五中的项目`microservice-consumer-movie-ribbon`、`microservice-eureka-server`和`microservice-provider-user`三个服务。
* 在`microservice-consumer-movie-ribbon`的 pom.xml 中引入 Hystrix 依赖:
~~~xml
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
~~~
* 加注解:在启动类`App.java`上添加`@EnableCircuitBreaker`注解。
~~~java
package com.shiyanlou;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@SpringBootApplication
@EnableDiscoveryClient
@EnableCircuitBreaker
public class App {
@Bean
@LoadBalanced // 添加注解
public RestTemplate restTemplate() {
return new RestTemplate();
}
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
~~~
* 然后在需要熔断的接口方法上面通过注解的方式添加熔断:
~~~java
package com.shiyanlou;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import java.math.BigDecimal;
@RequestMapping("/movies")
@RestController
public class MovieController {
@Autowired
private RestTemplate restTemplate;
@HystrixCommand(fallbackMethod = "findByIdFallback")
@GetMapping("/users/{id}")
public User findById(@PathVariable Long id) {
// 这里用到了RestTemplate的占位符能力
User user = this.restTemplate.getForObject("http://microservice-provider-user/users/{id}", User.class, id);
// ...电影微服务的业务...
return user;
}
public User findByIdFallback(Long id) {
return new User(id, "default username", "default name", 0, new BigDecimal(1));
}
}
~~~
以上代码的作用:`@HystrixCommand(fallbackMethod = "findByIdFallback")`使用了一个`fallbackMethod`为`/users/{id}`接口添加了熔断机制,在该接口请求过程中出错时会调用`findByIdFallback`方法返回一个默认的用户值。
- 微服务开发框架 SpringCloud
- 单体应用
- 如何解决单体应用架构中存在的问题
- 如何实现微服务架构以及技术选型
- Spring Cloud 特点
- 开始使用 Spring Cloud 实战微服务
- 快速搭建开发脚手架
- 编写服务提供者-用户微服务
- 编写服务消费者【电影微服务】
- 整合 Spring Boot Actuator
- 开始整合
- 微服务注册与发现
- 编写服务发现服务
- 注册微服务至 Eureka Server
- 更新服务提供者 (用户微服务)
- 更新服务消费者 (电影微服务)
- 查看注册结果
- Ribbon 客户端负载均衡
- Ribbon 简介
- 引入 Ribbon
- Ribbon 入门
- Feign 声明式 REST 调用
- 改造项目
- Hystrix 容错处理
- 实现容错的手段
- Hystrix 简介
- 开始使用
- 测试
- Zuul 网关
- 网关是什么
- Spring Cloud Zuul 介绍
- Zuul 入门使用
- 网关测试
- Spring Cloud Config 配置管理
- 配置中心的作用
- Spring Cloud Config 简介
- Spring Cloud Config 使用
- Sleuth 与 Zipkin 结合图形化展示
- 分布式追踪相关基础概念
- Spring Cloud Sleuth 介绍及使用
- Zipin 简介
- Docker 入门
- 云原生概念
- Docker 容器介绍
- Docker 常用命令
- 微服务运行在 Docker 之上
- Dockerfile 及其常见指令介绍
- 改造 Eureka Server 微服务
- Docker Compose 编排微服务
- 安装 Compose
- Compose 快速入门
- Compose 编排 SpringCloud微服务
- 将 Eureka 等微服务运行在 Docker 容器中
- Docker-Compose 编排文件的编写
- 通过 Docker Compose 启动、停止
- Compose编排Spring Cloud微服务2
- Docker-Compose 来部署一个双节点的 Eureka 集群
