SpringCloud使用Fegin进行HTTP接口调用

0

Fegin简介

Fegin是声明式、模块化的Http客户端,可以帮助我们快捷优雅的调用HTTP接口。
在SpringCloud中可以很方便的创建一个Feign客户端,只需声明一个接口,并加上对应的注解就能完成对HTTP接口的调用。

本文不集成注册中心也就不使用Fegin的负载均衡,所以可以理解为一个更简便,高可复用的Http客户端。

以示例讲解

SpringBoot版本:2.1.1.RELEASE
SpringCloud版本:Finchley.SR2

必要POM引入

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.1.RELEASE</version>
    </parent>
    <dependencyManagement>
        <dependencies>
            <!--spring-cloud -->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <dependencies>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

Fegin客户端定义

1、@FeignClient 定义接口为Http客户端,调用指定方法,并将接口注入Spring上下文中
参数url:所请求http服务的url、参数config:指定fegin的配置类
2、@PostMapping 为@RequestMapping的组合注解,默认为Post请求调用,注解不多介绍,
主要表示所需要调用的http接口的具体路径,可在url中拼接参数,也可以指定入参的ContentType
3、http接口的返回值格式如果和返回对象属性一致,会反序列化为对应对象。
package com.external.feign;

import java.util.List;
import java.util.Map;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.PostMapping;

import com.external.config.FormFeignConfig;
import com.external.dto.response.BaseBispDto;
import com.external.dto.response.NiftyRes;
import com.external.dto.response.ReportAnalyzesRes;
import com.external.dto.response.ReportSummaryRes;
import com.external.feign.rpc.BISPResponse;


@FeignClient(name="fegin-name", url="${http-url}" , configuration = FormFeignConfig.class)
public interface BispClient {
    
    /**
     * @Description 
     * @author zengzp
     */
    @PostMapping(value="/intf?method=XXXX", consumes = {"application/x-www-form-urlencoded"})
    public XXXXResponse<BaseBispDto> saveSampleInfo(Map<String, ?> params);

    /**
     * @Description 
     * @author zengzp
     */
    @PostMapping(value="/intf?method=XXXX", consumes = {"application/x-www-form-urlencoded"})
    public XXXXResponse<NiftyRes> getNiftyDetectionResultReplenish(Map<String, ?> params); 

Fegin配置代码

配置日志输出策略与指定对应ContentType的消息的编码与解码
package com.external.config;

import org.springframework.beans.factory.ObjectFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.cloud.openfeign.support.SpringDecoder;
import org.springframework.cloud.openfeign.support.SpringEncoder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;

import feign.Logger;
import feign.codec.Decoder;
import feign.codec.Encoder;
import feign.form.FormEncoder;

@Configuration
public class FormFeignConfig {
    @Autowired
    private ObjectFactory<HttpMessageConverters> messageConverters;

    @Bean
    Logger.Level feignLoggerLevel() {
        return Logger.Level.BASIC;
    }
    
    @Bean
    @Scope("prototype")
    Decoder decoder() {
        return new AllinpayDecoder(new SpringDecoder(messageConverters));
    }
    
    @Bean
    @Scope("prototype")
    Encoder encoder(){
        return new FormEncoder(new SpringEncoder(this.messageConverters));
    }
}

统一响应DTO

package com.external.feign.rpc;

import org.springframework.http.HttpStatus;

import com.external.common.dto.BaseDto;


public class XXXXResponse<T> extends BaseDto {
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private String code;
    private String msg;
    private Long total;
    private T rows;
}
启动服务时务必在配置类上增加@EnableFeignClients

你可能感兴趣的

载入中...