[Spring cloud 一步步实现广告系统] 11. 使用Feign实现微服务调用

上一节咱们使用了Ribbon(基于Http/Tcp)进行微服务的调用,Ribbon的调用比较简单,经过Ribbon组件对请求的服务进行拦截,经过Eureka Server 获取到服务实例的IP:Port,而后再去调用API。本节课咱们使用更简单的方式来实现,使用声明式的Web服务客户端Feign,咱们只须要使用Feign来声明接口,利用注解来进行配置就能够使用了,是否是很简单?实际工做中,咱们也只会用到Feign来进行服务之间的调用(大多数)。接下来,咱们来实例操做一把。java

为了代码的重用性,咱们来建立一个新的project mscx-ad-feign-sdk做为Feign的服务调用工具。web

  • 建立项目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来处理服务错误。spring

  • 三部曲之Step1(加依赖) UTOOLS1564468068440.png

从上图咱们能够看到,咱们引入Feign依赖的时候,它自己已经依赖了Hystrix,根据Maven依赖的传递性,咱们能够知道咱们本身的服务已经包含了Hystrix的依赖支持,咱们能够直接使用了~apache

  • 三部曲之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)app

@FeignClient(value = "mscx-ad-sponsor", fallback = SponsorClientHystrix.class)
public interface ISponsorFeignClient {
...

SponsorClientHystrix中,咱们要特别注意2点maven

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

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

相关文章
相关标签/搜索