当服务端发生异常时告诉客户端目前服务端不可用。可以用注解`@FeignClient`下面的两个属性做到。
```java
public @interface FeignClient {
...
Class<?> fallback() default void.class;
Class<?> fallbackFactory() default void.class;
}
```
[TOC]
# 1. fallback演示
**1. 客户端引入 alibaba-sentinel,服务端不需要**
```xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
```
**2. 服务端抛出异常**
```java
@RestController
public class GoodsController implements GoogsClient {
@Override
public String getById(Integer id) {
if (id < 1) {
throw new RuntimeException("id不能小于1");
}
return "[getById]: OK";
}
}
```
**3. openfeign 接口指定 fallback 类**
```java
@FeignClient(value = "assemb-server-provider", fallback = GoodsClientFallback.class)
public interface GoogsClient {
@GetMapping("/goods/getById/{id}")
String getById(@PathVariable("id") Integer id);
}
```
**4. fallback 类实现 openfeign 接口**
```java
@Component
public class GoodsClientFallback implements GoogsClient {
@Override
public String getById(Integer id) {
return "[getById]: 内部异常,id=" + id;
}
}
```
**5. 客户端调用演示**
```java
@RestController
@RequiredArgsConstructor
public class ShopController {
final GoogsClient googsClient;
@GetMapping("/goods/getById/{id}")
public String getGoodsById(@PathVariable("id") Integer id) {
return googsClient.getById(id);
}
}
```
![](https://img.kancloud.cn/0b/a2/0ba22a57c5cb63f684105b61ccdd9fac_1554x214.png)
<br/>
# 2. fallbackFactory演示
**1. 客户端引入 alibaba-sentinel,服务端不需要**
```xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
```
**2. 服务端抛出异常**
```java
@RestController
public class StoreController implements StoreClient {
@Override
public String getById(Integer id) {
if (id < 1) {
throw new RuntimeException("id不能小于1");
}
return "[getById|OK]";
}
}
```
**3. openfeign 接口指定 fallbackFactory 类**
```java
@FeignClient(value = "assemb-server-provider", fallbackFactory = StoreClientFallback.class)
public interface StoreClient {
@GetMapping("/store/getById/{id}")
String getById(@PathVariable("id") Integer id);
}
```
**4. fallbackFactory 类实现 FallbackFactory 接口**
```java
@Slf4j
@Component
public class StoreClientFallback implements FallbackFactory<StoreClient> {
@Override
public StoreClient create(Throwable cause) {
return new StoreClient() {
@Override
public String getById(Integer id) {
log.error("[getById]: {}", cause.getMessage());
return "[getById]: " + cause.getMessage();
}
};
}
}
```
**5. 客户端调用演示**
```java
@RestController
@RequiredArgsConstructor
public class ShopController {
final StoreClient storeClient;
@GetMapping("/store/getById/{id}")
public String getStoreById(@PathVariable("id") Integer id) {
return storeClient.getById(id);
}
}
```
![](https://img.kancloud.cn/68/17/68173ea48237c70b0629533238731c62_2279x217.png)