共计 4035 个字符,预计需要花费 11 分钟才能阅读完成。
上一节我们使用了 Ribbon(基于 Http/Tcp
) 进行微服务的调用,Ribbon 的调用比较简单,通过 Ribbon 组件对请求的服务进行拦截,通过 Eureka Server
获取到服务实例的IP:Port
, 然后再去调用 API。本节课我们使用更简单的方式来实现,使用声明式的Web
服务客户端 Feign
,我们只需要使用 Feign 来声明接口,利用 注解
来进行配置就可以使用了,是不是很简单?实际工作中,我们也只会用到 Feign 来进行服务之间的调用(大多数)。接下来,我们来实例操作一把。
为了代码的重用性,我们来创建一个新的 project mscx-ad-feign-sdk
作为 Feign 的服务调用工具。
- 创建项目
mscx-ad-feign-sdk
- 三部曲之 Step 1(加依赖)
<?xml version="1.0" encoding="UTF-8"?> | |
<project xmlns="http://maven.apache.org/POM/4.0.0" | |
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | |
<parent> | |
<artifactId>mscx-ad</artifactId> | |
<groupId>com.sxzhongf</groupId> | |
<version>1.0-SNAPSHOT</version> | |
</parent> | |
<modelVersion>4.0.0</modelVersion> | |
<packaging>jar</packaging> | |
<name>mscx-ad-feign-sdk</name> | |
<description> 只定义微服务 Feign 调用用到的请求对象和响应对象,而不涉及具体的实现类。</description> | |
<groupId>com.sxzhongf</groupId> | |
<artifactId>mscx-ad-feign-sdk</artifactId> | |
<version>1.0-SNAPSHOT</version> | |
<dependencies> | |
<dependency> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-starter-web</artifactId> | |
</dependency> | |
<!-- 引入服务调用的组件 feign 依赖 --> | |
<dependency> | |
<groupId>org.springframework.cloud</groupId> | |
<artifactId>spring-cloud-starter-openfeign</artifactId> | |
</dependency> | |
<dependency> | |
<groupId>com.sxzhongf</groupId> | |
<artifactId>mscx-ad-common</artifactId> | |
<version>1.0-SNAPSHOT</version> | |
</dependency> | |
<!-- 引入系统容错 hystrix 的依赖 --> | |
<dependency> | |
<groupId>org.springframework.cloud</groupId> | |
<artifactId>spring-cloud-starter-hystrix</artifactId> | |
<version>1.2.7.RELEASE</version> | |
</dependency> | |
</dependencies> | |
<build> | |
<plugins> | |
<plugin> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-maven-plugin</artifactId> | |
</plugin> | |
</plugins> | |
</build> | |
</project> |
- 三部曲之 Step 2(加注解
@EnableFeignClients
, 添加在具体的微服务中,使用我们自定义的 FeignClient)
/** | |
* ISponsorFeignClient for service using | |
* | |
* @author <a href="mailto:magicianisaac@gmail.com">Isaac.Zhang | 若初 </a> | |
*/ | |
@FeignClient(value = "mscx-ad-sponsor", fallback = SponsorClientHystrix.class) | |
public interface ISponsorFeignClient {@RequestMapping(value = "/ad-sponsor/plan/get", method = RequestMethod.POST) | |
CommonResponse<List<AdPlanVO>> getAdPlansUseFeign(@RequestBody AdPlanGetRequestVO requestVO); | |
@RequestMapping(value = "/ad-sponsor/user/get", method = RequestMethod.GET) | |
/** | |
* Feign 埋坑之 如果是 Get 请求,必须在所有参数前添加{@link RequestParam}, 不能使用{@link Param} | |
* 会被自动转发为 POST 请求。*/ | |
CommonResponse getUsers(@RequestParam(value = "username") String username); | |
} | |
--- | |
@RestController | |
@Slf4j | |
@RequestMapping(path = "/search-feign") | |
public class SearchFeignController { | |
/** | |
* 注入我们自定义的 FeignClient | |
*/ | |
private final ISponsorFeignClient sponsorFeignClient; | |
@Autowired | |
public SearchFeignController(ISponsorFeignClient sponsorFeignClient) {this.sponsorFeignClient = sponsorFeignClient;} | |
@GetMapping(path = "/user/get") | |
public CommonResponse getUsers(@Param(value = "username") String username) {log.info("ad-search::getUsersFeign -> {}", JSON.toJSONString(username)); | |
CommonResponse commonResponse = sponsorFeignClient.getUsers(username); | |
return commonResponse; | |
} | |
} |
- 三部曲之 Step 3(加配置,工具类库不需要,添加在具体的微服务中)
我们上面的实例中有一个问题,如果说我们的广告提供服务出现了问题,那么我们通过使用 FeignClient 调用的 APIsponsorFeignClient.getUsers(username);
就会报错,如果长时间报错,会引起大规模的服务错误问题,也就有是我们常说的服务雪崩效应,我们要怎样避免一个服务出错而拖垮整个系统的问题呢?这里我们需要引入一个组件 Hystrix
来处理服务错误。
- 三部曲之 Step1(加依赖)
从上图我们可以看到,我们引入 Feign 依赖的时候,它本身已经依赖了 Hystrix,根据 Maven 依赖的传递性,我们可以知道我们自己的服务已经包含了 Hystrix 的依赖支持,我们可以直接使用了~
- 三部曲之 Step2(加注解)
@EnableHystrix // 开启 hystrix 断路器
- 三部曲之 Step3(改配置)
feign: | |
hystrix: | |
enabled: true |
- 使用 Hystrix 来配置 Feign 实现调用容错
@Component | |
public class SponsorClientHystrix implements ISponsorFeignClient { | |
@Override | |
public CommonResponse<List<AdPlanVO>> getAdPlansUseFeign(AdPlanGetRequestVO requestVO) {return new CommonResponse<>(-1, "mscx-ad-sponsor feign & hystrix get plan error."); | |
} | |
@Override | |
public CommonResponse getUsers(String username) {return new CommonResponse<>(-1, "mscx-ad-sponsor feign & hystrix get user error."); | |
} | |
} |
在 ISponsorFeignClient
类中,添加出错处理类(fallback)
@FeignClient(value = "mscx-ad-sponsor", fallback = SponsorClientHystrix.class) | |
public interface ISponsorFeignClient {... |
在 SponsorClientHystrix
中,我们要特别注意 2 点
- 该类必须添加
@Component
注解,以便可以加入 Spring 容器中 - 该类需要实现
ISponsorFeignClient
Feign 的客户端接口
通过上面的实现,我们的服务在调用过程中,如果发生错误,就会进行服务降级,调用到出错应该调用的默认处理类中的方法,也就实现了我们想要做的短路处理来保护我们的当前服务。