上一节我们使用了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;
    }
}
/**
 * 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.");
    }
}
@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 {
...
@FeignClient(value = "mscx-ad-sponsor", fallback = SponsorClientHystrix.class)
public interface ISponsorFeignClient {
...

SponsorClientHystrix中,我们要特别注意2点

  1. 该类必须添加@Component注解,以便可以加入Spring 容器中
  2. 该类需要实现ISponsorFeignClientFeign的客户端接口

通过上面的实现,我们的服务在调用过程中,如果发生错误,就会进行服务降级,调用到出错应该调用的默认处理类中的方法,也就实现了我们想要做的短路处理来保护我们的当前服务。