Spring Cloud 接口调用组件:Feign

在学习Feign之前我们调用微服务接口的方式主要是:RestTemplate + ribbon;

一、Feign的概念

Feign是一个声明式Web Service客户端。使用Feign能让编写Web Service客户端更加简单, 它的使用方法是定义一个接口,然后在上面添加注解,同时也支持JAX-RS标准的注解。Feign也支持可拔插式的编码器和解码器。Spring Cloud对Feign进行了封装,使其支持了Spring MVC标准注解和HttpMessageConverters。Feign可以与Eureka和Ribbon组合使用以支持负载均衡。

使用主要包括两个:Feign 和 OpenFeign

在这里插入图片描述

二、Feign的作用

Feign旨在使编写java Http客户端变得更容易。
在之前我们在使用RestTemplate + Ribbon 时,利用RestTemplate 对http 请求做封装处理,行程一套模板化的调用方法,但是在实际开发过程中,由于对服务依赖的调用可能不止一处,往往一个接口会被多处调用,所以通常会针对每个微服务自行封装一些客户端类来包装这些依赖服务的调用。

所以Feign在此基础上做了一步封装,由他来帮助我们定义和实现依赖服务的接口的定义。在Feign的实现下,我们只需要创建一个接口并使用注解的方式来配置它,即可完成对服务提供方的接口绑定,简化了使用springcloud Ribbon时,自动封装服务调用客户端的开发量。

此外,Feign本身集成了Ribbon,利用Ribbon维护了服务列表,并通过轮询的方式实现客户端的负载均衡,而与申明式Ribbon调用不同的是,通过Feign只需要定义服务绑定接口以声明式的方法,优雅而简单的实现服务调用。

三、OpenFeign的使用

本文讲述使用OpenFeign实现微服务的调用与负载均衡。

1.pom.xml 需要引入 OpenFeign的核心包

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

2.application.yml 配置文件

server:
  port: 80
debug: true
spring:
  application:
    name: cloud-consumer-order-feign
eureka:
  instance:
    hostname: localhost #eureka服务注册地址
  client:
    #是否从eureka Server 抓取已有的注册信息,默认true,单节点无所谓,集群必须为true才能配合ribbon使用负载均衡
    fetch-registry: true
    #表示是否将自己加入eureka service true代表加入
    register-with-eureka: true
    service-url:
      #      #设置 eureka server 交互的地址查询服务和注册服务需要依赖的地址
      defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka

这里与之前使用Ribbon调用时一模一样,不再多言,可见:ribbon实现负载均衡

3.主启动 需要使用@EnableFeignClients开启Feign

package com.cpown.springcloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;

@SpringBootApplication
@EnableFeignClients
public class OrderMainFeign80 {
    public static void main(String[] args) {
        SpringApplication.run(OrderMainFeign80.class,args);
    }
}

4.接口PaymentService 需要使用@FeignClient(value = “cloud-payment-service”)

注解标注对应的微服务名称

package com.cpown.springcloud.service;

import com.cpown.springcloud.dto.CommonResult;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

@FeignClient(value = "cloud-payment-service")
@Component
public interface PaymentService {

    @GetMapping("/get/{id}")
    CommonResult get(@PathVariable("id") Long id);
}

此处要注意,微服务名称与接口名称需要与8001,,8002微服务提供方保持一致。

在这里插入图片描述在这里插入图片描述

5.controller调用

package com.cpown.springcloud.controller;

import com.cpown.springcloud.dto.CommonResult;
import com.cpown.springcloud.service.PaymentService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
public class FeignController {

    @Resource
    private PaymentService paymentService;

    @GetMapping("/consumer/get/{id}")
    public CommonResult getInfo(@PathVariable("id") Long id){
        return paymentService.get(id);
    }
}

四、测试

分别启动微服务提供者:cloud-provider-payment8001,cloud-provider-payment8002,
服务注册与发现:cloud-eureka-server7001,cloud-eureka-server7002
Feign接口客户端:cloud-consumer-order-feign80

在这里插入图片描述
在这里插入图片描述
最后:OpenFeign默认等待服务一秒钟,超时的话直接报错,必要的时候可以使用配置对超时进行修改

ribbon:
 ReadTimeout:  5000
 ConnectTimeout: 5000
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值