如何通过HTTP优雅调用第三方-Feign

本文介绍了如何通过Feign实现Java优雅地调用第三方服务。讲解了HTTP请求响应的组成,提出面向接口而非面向方法的调用更优雅,并通过代理模式实现了基于HttpURLConnection的HTTP调用。此外,还探讨了Feign的设计理念,包括插件式设计、注解支持、负载均衡等,最后给出了对接GIO的实战例子。
摘要由CSDN通过智能技术生成

Java常用HTTP客户端

  • Java原生HttpURLConnection
  • Apache HttpClient
  • OkHttp
  • Spring RestTemplate

示例

public interface Client {
   
    /**
     * 
     * @param body
     * @return
     */
    Response post(Object body) throws Exception;
}
public class ApacheHttpClient implements Client {
   

    private static final String DEFAULT_CONTENT_TYPE_VALUE = "application/json";

    private static final String DEFAULT_CHARSET_NAME = "UTF-8";

    private String url;

    private final HttpClient client;

    private JsonSerializer jsonSerializer;

    /**
     * 
     * @param url
     */
    public ApacheHttpClient(String url) {
   
        this(url, HttpClientBuilder.create().build());
    }

    /**
     * 
     * @param url
     * @param client
     */
    public ApacheHttpClient(String url, HttpClient client) {
   
        this(url, client, new JacksonJsonSerializer());

    }

    /**
     * 
     * @param url
     * @param client
     * @param jsonSerializer
     */
    public ApacheHttpClient(String url, HttpClient client, JsonSerializer jsonSerializer) {
   
        if (StringUtils.isBlank(url)) {
   
            throw new IllegalArgumentException("Url can't be null!");
        }
        if (Objects.isNull(client)) {
   
            throw new IllegalArgumentException("client can't be null!");
        }
        if (Objects.isNull(jsonSerializer)) {
   
            throw new IllegalArgumentException("jsonSerializer can't be null!");
        }
        this.url = url;
        this.client = client;
        this.jsonSerializer = jsonSerializer;
    }

    @Override
    public Response post(Object body) throws Exception {
   
        if (Objects.isNull(body)) {
   
            return Response.NULL_BODY_RESPONSE;
        }
        if (body instanceof Collection && ((Collection<?>) body).size() <= 0) {
   
            return Response.EMPTY_BODY_RESPONSE;
        }
        HttpPost post = new HttpPost(url);
        String jsonString = jsonSerializer.serialize(body);
        StringEntity entity = new StringEntity(jsonString, DEFAULT_CHARSET_NAME);
        entity.setContentType(DEFAULT_CONTENT_TYPE_VALUE);
        post.setEntity(entity);
        HttpResponse httpResponse = client.execute(post);
        HttpEntity httpEntity = httpResponse.getEntity();
        String result = EntityUtils.toString(httpEntity, "UTF-8");
        Response response = new Response(httpResponse.getStatusLine().getStatusCode(),
                httpResponse.getStatusLine().getReasonPhrase(), result);
        httpResponse.getEntity().getContent().close();
        return response;
    }

}

存在问题

  • 面向方法
  • 耦合HTTP客户端
  • 手动处理请求响应
  • 无法通用,重复编码
  • 面向接口实现HTTP调用

HTTP请求和响应的组成

  • 请求:请求行、请求头、请求体
  • 响应:响应行、响应头、响应体

从访问百度主页开始

声明接口

package com.zby.service;

public interface BaiduClient {
   

    @RequestLine(url = "http://www.baidu.com", method = "GET")
    String index();

}

声明注解

package com.zby.annotation;

@Inherited
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值