spring-cloud之FeignClient使用总结

前言:

在微服务项目中会存在多个微服务之间互相调用的情况,如何高效便捷的进行远程过程调用便成为新的议论话题。spring-cloud中提供的feign方式可以有效解决该问题。

Feign是一种声明式、模板化的HTTP客户端。在Spring Cloud中使用Feign, 我们可以做到使用HTTP请求远程服务时能与调用本地方法一样的编码体验,开发者完全感知不到这是远程方法,更感知不到这是个HTTP请求。如下testMicroServiceAccess方法便是feignClient调用,但是外部看起来和普通方法无二。

业务场景:

假设同一个注册中心上有两个微服务:A和B,为完成某个业务功能,B微服务需要调用A微服务中的某个方法逻辑获取返回结果,并且二者是不能作为jar互相依赖的,按照传统方式只能如下处理:

请求——>外部网关——>应用层——>内部网关——>B微服务——>外部网关——>应用层——>内部网关——>A微服务——>结束

即便是拆掉外部网关和应用层,也需要通过内部网关中转才能访问到A微服务,况且还要考虑超时熔断、路由负载等问题。

引入feign后简化流程,并且其结合ribbon实现路由负载超时熔断等措施,具体流程如下:

请求——>外部网关——>应用层——>内部网关——>B微服务——>feignClient——>A微服务——>结束

架构说明:

按照上述业务假设进行架构说明:

1、为保持系统良好的可扩展性,降低模块耦合度,新建A-FeignClient模块,用于提供feign接口和相关实体类。后期作为jar文件推送到Maven私服中,供A、B微服务各自引用。

2、A微服务中仅需要在启动类上增加开启feign的注解,可以考虑增加A-FeignClient依赖,这样三个模块使用同一份实体类,保证一致性。

3、B微服务中增加A-FeignClient模块的依赖,编写客户端调用代码。

 调用逻辑:

 1、A-FeignClient:

a、在A-FeignClient中增加接口,通过spring注解标识其提供服务的真正接口路径。

b、@FeignClient注解中name值目标微服务名,contextId用于标识FeignClient名。

c、方法名上的postmapping用于标识该方法需要转发的接口路径。下图代码示例中意思即:调用该方法后会代理到A微服务的/demoServer/testMicroServiceAccess接口中。

d、通过实现该接口的方式新建一个类,可以用于feign调用失败后的容错处理。

package com.demo.DemoFeignClient;

/**
 * 微服务之间调用的核心接口     Aikes
 * 注解@FeignClient中contextID参数用于标识client,防止多个接口共用同一个目标FeignClient冲突
 */
@FeignClient(name = "A-server" ,contextId = "DemoFeignClient", fallback = DemoFeignClientFallback.class )
public interface DemoFeignClient{

    @PostMapping("/demoServer/testMicroServiceAccess")
    public ApiResponse<String> testMicroServiceAccess(@RequestBody ServerAccessRequest<DemoPo> cServerAccessRequest);
}

/**
 * 增加访问失败时的处理逻辑
 */
@Component
class DemoFeignClientFallback implements DemoFeignClient{

    @Override
    public ApiResponse<String> testMicroServiceAccess(ServerAccessRequest<DemoPo> cServerAccessRequest) {
        ApiResponse<String> tApiResponse = new ApiResponse<>();
        tApiResponse.setStatus(ApiResponse.BUSY);
        return tApiResponse;
    }
}

 2、A微服务:

作为服务提供方,只需要正常编写业务逻辑即可,重点需要考虑请求入参的实体类把控,建议引用A-FeignClient中,保证一致性。

package com.demo.DemoFeignServerController;

/**
 * 服务提供方    Aikes
 */
@Controller
@RequestMapping("demoServer")
@RestController
@Slf4j
public class DemoFeignServerController {

    @PostMapping("/testMicroServiceAccess")
    public ApiResponse<String> testMicroServiceAccess(@RequestBody ServerAccessRequest<DemoPo> cServerAccessRequest) {
        log.info(cServerAccessRequest.toString());
        log.info("This is FeignServerController");
        ApiResponse<String> tApiResponse = new ApiResponse<String>();
        tApiResponse.setStatus(ApiResponse.SUCCESS);
        tApiResponse.setStatusText(ApiResponse.SUCCESS_TEXT);
        tApiResponse.setData(cServerAccessRequest.getData().getDemoName());
        return tApiResponse;
    }
}

3、B微服务:

作为服务消费方,在核心逻辑处理中,通过调用引入的A-FeignClient模块的接口,由feign负责代理转发到A微服务,实现调用A微服务的相关逻辑。

package com.demo.DemoFeignClientController;

/**
 * 服务消费方    Aikes
 */
public class DemoFeignClientController {

    @Autowired
    private DemoFeignClient mDemoFeignClient;

    public void doService() {
        //TODO 业务处理
        ApiResponse<String> tApiResponse = this.testFeign();//调用 A微服务 处理逻辑
        //TODO 处理返回结果
    }

    public ApiResponse<String> testFeign() {
        ServerAccessRequest<DemoPo> tServerAccessRequest = new ServerAccessRequest<DemoPo>();
        tServerAccessRequest.setBusinessNo("TEST001");
        return mDemoFeignClient.testMicroServiceAccess(tServerAccessRequest);
    }
}

小结:

通过feign的方式可以降低服务间调用的复杂度,从而提升系统性能。但同时带来的问题也需要重点考量:

1、服务间调用失败后的事务一致性处理,需要结合各自业务场景分析。

2、错综复杂的服务间调用开启后,相当于给系统开了后门,需要考虑增加服务间调用日志记录的功能,推荐使用自定义注解+AOP统一处理。

  • 8
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Aikes902

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值