重庆分公司,新征程启航

为企业提供网站建设、域名注册、服务器等服务

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

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

创新互联成立于2013年,先为大通等服务建站,大通等地企业,进行企业商务咨询服务。为大通企业网站制作PC+手机+微官网三网同步一站式服务解决您的所有建站问题。

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

  • 创建项目mscx-ad-feign-sdk
  • 三部曲之Step 1(加依赖)

    
    
    
        mscx-ad
        com.sxzhongf
        1.0-SNAPSHOT
    
    4.0.0
    jar
    mscx-ad-feign-sdk
    只定义微服务Feign调用用到的请求对象和响应对象,而不涉及具体的实现类。
    
    com.sxzhongf
    mscx-ad-feign-sdk
    1.0-SNAPSHOT
    
        
            org.springframework.boot
            spring-boot-starter-web
        
        
        
            org.springframework.cloud
            spring-cloud-starter-openfeign
        
        
            com.sxzhongf
            mscx-ad-common
            1.0-SNAPSHOT
        
        
        
            org.springframework.cloud
            spring-cloud-starter-hystrix
            1.2.7.RELEASE
        
    
    
    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    
    
  • 三部曲之Step 2(加注解@EnableFeignClients,添加在具体的微服务中,使用我们自定义的FeignClient)

    /**
    * ISponsorFeignClient for service using
    *
    * @author Isaac.Zhang | 若初
    */
    @FeignClient(value = "mscx-ad-sponsor", fallback = SponsorClientHystrix.class)
    public interface ISponsorFeignClient {
    @RequestMapping(value = "/ad-sponsor/plan/get", method = RequestMethod.POST)
    CommonResponse> 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
br/>@Slf4j
public class SearchFeignController {
/**

  • 注入我们自定义的FeignClient
    */
    private final ISponsorFeignClient sponsorFeignClient;@Autowired
    br/>@Autowired
    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(加依赖)
    [Spring cloud 一步步实现广告系统] 11. 使用Feign实现微服务调用

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

  • 三部曲之Step2(加注解) @EnableHystrix // 开启hystrix 断路器
  • 三部曲之Step3(改配置)

    feign:
    hystrix:
    enabled: true
  • 使用Hystrix来配置Feign实现调用容错

    @Component
    public class SponsorClientHystrix implements ISponsorFeignClient {
    @Override
    public CommonResponse> 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点

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

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


当前文章:[Springcloud一步步实现广告系统]11.使用Feign实现微服务调用
当前链接:http://cqcxhl.cn/article/jhgsei.html

其他资讯

在线咨询
服务热线
服务热线:028-86922220
TOP