feign高級用法

feign爲我們帶來簡潔風格rest客戶端的同時也帶來了一些麻煩,因爲稍微對他不瞭解就可能走彎路。

依賴

<dependency>
            <groupId>com.netflix.feign</groupId>
            <artifactId>feign-core</artifactId>
            <version>8.18.0</version>
        </dependency>
        <dependency>
            <groupId>com.netflix.feign</groupId>
            <artifactId>feign-gson</artifactId>
            <version>8.18.0</version>
        </dependency>
        <dependency>
            <groupId>com.netflix.feign</groupId>
            <artifactId>feign-slf4j</artifactId>
            <version>8.4.1</version>
        </dependency>

demo展示了重寫url和自定義請求頭 

public interface XxxRestClient {
	@RequestLine("POST /api/xxx/list")
	@Headers("Content-Type: application/json")
	XxxResp uploadFile(@RequestBody XxxReq req);
}

@Configuration
@Slf4j
public class XxxRestConf {

@Bean
public XxxRestClient newXxxRestClient () {
    return Feign.builder().requestInterceptor(new RequestInterceptor() {
            @Override
            public void apply(RequestTemplate template) {
                String json = new String(template.body());
                if (StringUtils.isNotEmpty(json)) {
                    JSONObject jsonObject = JSONObject.parseObject(json);
                    String secret = jsonObject.getString("RSAstring");
                    if (template.url().equals(url)) {
                        try {
                            // 重寫請求uri,增加加密串
                            template.insert(url.length(), "?RSAString=" + URLEncoder.encode(secret, Charsets.UTF_8.toString()));
                        } catch (UnsupportedEncodingException e) {
                            log.error("", e);
                        }
                    } 
                        // 在請求發送之前重寫頭信息
                        template.header("token", token);
                    }
                }
            }
        })
                .encoder(new GsonEncoder())
                .decoder(new GsonDecoder())
                //關閉重試功能
                .retryer(Retryer.NEVER_RETRY)
                //打印完整的http請求響應日誌
                .logLevel(Logger.Level.FULL)
                .logger(new Slf4jLogger())
                .target(XxxRestClient.class, "http://127.0.0.1:8080");
    }
}

注意聲明feign日誌級別

如logback.xml新增

    <logger name="feign" level="DEBUG"/>

效果

2019-09-27 10:44:08.891 [http-apr-8080-exec-2] DEBUG
                feign.Logger - [PowerRestClient#BuyPowerStatus] ---> POST http://127.0.0.1/api/list HTTP/1.1
2019-09-27 10:44:08.891 [http-apr-8080-exec-2] DEBUG
                feign.Logger - [PowerRestClient#BuyPowerStatus] Content-Type: application/json
2019-09-27 10:44:08.891 [http-apr-8080-exec-2] DEBUG
                feign.Logger - [PowerRestClient#BuyPowerStatus] Content-Length: 177
2019-09-27 10:44:08.891 [http-apr-8080-exec-2] DEBUG
                feign.Logger - [PowerRestClient#BuyPowerStatus] 
2019-09-27 10:44:08.891 [http-apr-8080-exec-2] DEBUG
                feign.Logger - [PowerRestClient#BuyPowerStatus] {
  "id": 2941587,
  "systemId": 21,
  "signature": "5c168dd2891f85dfd764196373fc1d1b",
  "signtype": "md5",
}
2019-09-27 10:44:08.891 [http-apr-8080-exec-2] DEBUG
                feign.Logger - [PowerRestClient#BuyPowerStatus] ---> END HTTP (177-byte body)
2019-09-27 10:44:09.062 [http-apr-8080-exec-2] DEBUG
                feign.Logger - [PowerRestClient#BuyPowerStatus] <--- HTTP/1.1 200 OK (170ms)
2019-09-27 10:44:09.062 [http-apr-8080-exec-2] DEBUG
                feign.Logger - [PowerRestClient#BuyPowerStatus] cache-control: no-cache
2019-09-27 10:44:09.062 [http-apr-8080-exec-2] DEBUG
                feign.Logger - [PowerRestClient#BuyPowerStatus] content-length: 115
2019-09-27 10:44:09.062 [http-apr-8080-exec-2] DEBUG
                feign.Logger - [PowerRestClient#BuyPowerStatus] content-type: application/json; charset=utf-8
2019-09-27 10:44:09.062 [http-apr-8080-exec-2] DEBUG
                feign.Logger - [PowerRestClient#BuyPowerStatus] date: Fri, 27 Sep 2019 02:43:46 GMT
2019-09-27 10:44:09.062 [http-apr-8080-exec-2] DEBUG
                feign.Logger - [PowerRestClient#BuyPowerStatus] expires: -1
2019-09-27 10:44:09.062 [http-apr-8080-exec-2] DEBUG
                feign.Logger - [PowerRestClient#BuyPowerStatus] pragma: no-cache
2019-09-27 10:44:09.062 [http-apr-8080-exec-2] DEBUG
                feign.Logger - [PowerRestClient#BuyPowerStatus] server: Microsoft-IIS/7.5
2019-09-27 10:44:09.063 [http-apr-8080-exec-2] DEBUG
                feign.Logger - [PowerRestClient#BuyPowerStatus] x-aspnet-version: 4.0.30319
2019-09-27 10:44:09.063 [http-apr-8080-exec-2] DEBUG
                feign.Logger - [PowerRestClient#BuyPowerStatus] x-powered-by: ASP.NET
2019-09-27 10:44:09.063 [http-apr-8080-exec-2] DEBUG
                feign.Logger - [PowerRestClient#BuyPowerStatus] 
2019-09-27 10:44:09.063 [http-apr-8080-exec-2] DEBUG
                feign.Logger - [PowerRestClient#BuyPowerStatus] {"result":1,"message":"查詢成功","data":{"state":1,"date":"2019-09-25 17:39:04","cnt":1.00}}
2019-09-27 10:44:09.063 [http-apr-8080-exec-2] DEBUG
                feign.Logger - [PowerRestClient#BuyPowerStatus] <--- END HTTP (115-byte body)

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章