欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

springcloud系列之feign服务间远程调用

程序员文章站 2022-05-28 09:49:54
...

1.场景还原

    在微服务架构中,服务间调用是家常便饭;如何进行服务间远程调用呢?今天笔者就springcloud中feign远程调用给大伙讲解一番,希望能够有所帮助

2.实现方案

①加入pom依赖

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-feign</artifactId>
</dependency>

②启动类的注解配置

@SpringBootApplication
@ComponentScan("com.yivi")
@MapperScan({"com.yivi.yiviproj.microservicepayment.dao","com.yivi.yivisender.dispatchdata.dao"})
@EnableTransactionManagement
@EnableCaching
@EnableEurekaClient
@EnableCircuitBreaker
@EnableFeignClients
public class MicroServicePaymentApplication{

   public static void main(String[] args) {
      SpringApplication.run(MicroServicePaymentApplication.class, args);
   }
}

③feign远程调用接口申明

@FeignClient(name = "micro-service-dispatch",fallback = PaymentDispatchHystrixClientFallback.class)
public interface PaymentDispatchFeignClient {

    @RequestMapping(value = "/v1.0/dispatch/selectByPrimaryKey/{dispatchId}", method = RequestMethod.GET)
    YiViDispatchOrder selectByPrimaryKey(@PathVariable("dispatchId")String dispatchId);

    @RequestMapping(value = "/v1.0/dispatch/updateByPrimaryKeySelective", method = RequestMethod.POST)
    int updateByPrimaryKeySelective(@RequestBody YiViDispatchOrder record);

    @RequestMapping(value = "/v1.0/dispatch/insert", method = RequestMethod.POST)
    int insert(@RequestBody YiViDispatchOrderStatusLog record);

    @RequestMapping(value = "/v1.0/dispatch/insert/{dispatchOrderCode}", method = RequestMethod.GET)
    YiViDispatchOrder findDispatchOrderByOrderCode(@PathVariable("dispatchOrderCode")String dispatchOrderCode);

}

这里得注意,请求方式不能简写,如@GetMapping或者@PostMapping,必须以@RequestMapping(method = RequestMethod.Get)替代

④请求失败回调类

@Component
@Slf4j
public class PaymentDispatchHystrixClientFallback implements PaymentDispatchFeignClient {


  @Override
  public YiViDispatchOrder selectByPrimaryKey(String dispatchId) {
    return null;
  }

  @Override
  public int updateByPrimaryKeySelective(YiViDispatchOrder record) {
    return 0;
  }

  @Override
  public int insert(YiViDispatchOrderStatusLog record) {
    return 0;
  }

  @Override
  public YiViDispatchOrder findDispatchOrderByOrderCode(String dispatchOrderCode) {
    return null;
  }

}

feign自身支持hystrix熔断回调,防止一个服务挂掉,殃及另一个服务,俗称雪崩效应。

3.feign配置

①测试用例中你可能会遇到测试用例运行成功了却抛出如下一串错:

org.springframework.beans.factory.BeanCreationNotAllowedException: Error creating bean with name 'eurekaAutoServiceRegistration': Singleton bean creation not allowed while singletons of this factory are in destruction (Do not request a bean from a BeanFactory in a destroy method implementation!)

②添加feign配置

/**
 * @auther zx
 * @date 2018/8/11 14:26
 * @discribution 处理eurekaAutoServiceRegistration异常
 */
@Component
public class FeignBeanFactoryPostProcessor implements BeanFactoryPostProcessor {

    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
        if (containsBeanDefinition(beanFactory, "feignContext", "eurekaAutoServiceRegistration")) {
            BeanDefinition bd = beanFactory.getBeanDefinition("feignContext");
            bd.setDependsOn("eurekaAutoServiceRegistration");
        }
    }

    private boolean containsBeanDefinition(ConfigurableListableBeanFactory beanFactory, String... beans) {
        return Arrays.stream(beans).allMatch(b -> beanFactory.containsBeanDefinition(b));
    }
}

好了,这里笔者就不测试了;我是张星,欢迎加入博主技术交流群,群号:526601468

 

相关标签: 远程调用