Fegin是聲明式、模塊化的Http客戶端,能夠幫助咱們快捷優雅的調用HTTP接口。
在SpringCloud中能夠很方便的建立一個Feign客戶端,只需聲明一個接口,並加上對應的註解就能完成對HTTP接口的調用。java
本文不集成註冊中心也就不使用Fegin的負載均衡,因此能夠理解爲一個更簡便,高可複用的Http客戶端。
SpringBoot版本:2.1.1.RELEASE
SpringCloud版本:Finchley.SR2
<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>
一、@FeignClient 定義接口爲Http客戶端,調用指定方法,並將接口注入Spring上下文中 參數url:所請求http服務的url、參數config:指定fegin的配置類 二、@PostMapping 爲@RequestMapping的組合註解,默認爲Post請求調用,註解很少介紹, 主要表示所須要調用的http接口的具體路徑,可在url中拼接參數,也能夠指定入參的ContentType 三、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);
配置日誌輸出策略與指定對應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)); } }
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