插播之SpringCloud的Feign

官方说明

  • Feign是一个声明式的Web客户端,一个Http请求调用的轻量级框架,来让Rest服务调用更简单
  • Feign英文表意为“假装,伪装”,所以其提供了Http的请求模板,通过编写简单的接口和注解,就可以定义好Http请求的参数、格式、地址等信息
  • Feign完全代理了Http请求,让我们看起来是调用方法实际是发送服务请求的相关处理
  • 同时Feign整合了Ribbon负载均衡和Hystrix熔断,可以不再需要显示的使用这两个组件
  • 同时SpringCloud为每个命名的Feign客户端创建了一个默认的FeignClientsConfiguration类的配置集,根据需要,我们可以自定义该类【有空再细讲】

理解一下常见的Java Http请求库

通常,java项目中调用接口会用到以下工具
1、HttpClient: Apache子项目,功能丰富、易用、灵活并高效
2、Okhttp:处理网络请求的开源框架、Okhttp拥有更简洁的API,高效的性能,并支持多种协议
3、Httpurlconnection:java的标准类,继承自URLConnection,发送get/post请求,使用比较复杂
4、RestTemplate:Spring提供的用于访问Rest服务的客户端,便捷高效

依次来看一下吧

HttpURLConnection

  • 优点:JDK自带的标准库,不需要额外引入
  • 缺点:缺乏连接池管理、域名机制控制等特性支持,在Java9下才支持HTTP/2

具体使用

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
 
 
public class HttpUrlConnectionDemo {
 
    public static void main(String[] args) throws Exception {
        String urlString = "https://httpbin.org/post";
        String bodyString = "password=e10adc3949ba59abbe56e057f20f883e&username=test3";
 
        URL url = new URL(urlString);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("POST");
        conn.setDoOutput(true);
 
        OutputStream os = conn.getOutputStream();
        os.write(bodyString.getBytes("utf-8"));
        os.flush();
        os.close();
 
        if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
            InputStream is = conn.getInputStream();
            BufferedReader reader = new BufferedReader(new InputStreamReader(is));
            StringBuilder sb = new StringBuilder();
            String line;
            while ((line = reader.readLine()) != null) {
                sb.append(line);
            }
            System.out.println("rsp:" + sb.toString());
        } else {
            System.out.println("rsp code:" + conn.getResponseCode());
        }
    }
 
}

HttpClient

依赖

<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
    <version>4.5.3</version>
</dependency>

GET请求(Delete请求同Get请求)

//Get请求不带参数
public class DoGET {
 
    public static void main(String[] args) throws Exception {
 
        // 创建Httpclient对象
        CloseableHttpClient httpclient = HttpClients.createDefault();
 
        // 创建http GET请求
        HttpGet httpGet = new HttpGet("http://www.baidu.com/");
 
        CloseableHttpResponse response = null;
        try {
            // 执行请求
            response = httpclient.execute(httpGet);
            // 判断返回状态是否为200
            if (response.getStatusLine().getStatusCode() == 200) {
                String content = EntityUtils.toString(response.getEntity(), "UTF-8");
                System.out.println("内容长度:" + content.length());
//                FileUtils.writeStringToFile(new File("C:\\baidu.html"), content);
            }
        } finally {
            if (response != null) {
                response.close();
            }
            httpclient.close();
        }
 
    }
 
}

//带参数
public class DoGETParam {
 
    public static void main(String[] args) throws Exception {
 
        // 创建Httpclient对象
        CloseableHttpClient httpclient = HttpClients.createDefault();
 
        // 定义请求的参数
        URI uri = new URIBuilder("http://www.baidu.com/s").setParameter("wd", "java").build();
 
        System.out.println(uri);
 
        // 创建http GET请求
        HttpGet httpGet = new HttpGet(uri);
        
 
        CloseableHttpResponse response = null;
        try {
            // 执行请求
            response = httpclient.execute(httpGet);
            // 判断返回状态是否为200
            if (response.getStatusLine().getStatusCode() == 200) {
                String content = EntityUtils.toString(response.getEntity(), "UTF-8");
                System.out.println(content);
            }
        } finally {
            if (response != null) {
                response.close();
            }
            httpclient.close();
        }
 
    }
 
}

Post请求(put请求同Post)

//不带参数
public class DoPOST {
 
    public static void main(String[] args) throws Exception {
 
        // 创建Httpclient对象
        CloseableHttpClient httpclient = HttpClients.createDefault();
 
        // 创建http POST请求
        HttpPost httpPost = new HttpPost("http://www.oschina.net/");
 
        CloseableHttpResponse response = null;
        try {
            // 执行请求
            response = httpclient.execute(httpPost);
            // 判断返回状态是否为200
            if (response.getStatusLine().getStatusCode() == 200) {
                String content = EntityUtils.toString(response.getEntity(), "UTF-8");
                System.out.println(content);
            }
        } finally {
            if (response != null) {
                response.close();
            }
            httpclient.close();
        }
 
    }
 
}

//带参数
public class DoPOSTParam {
 
    public static void main(String[] args) throws Exception {
 
        // 创建Httpclient对象
        CloseableHttpClient httpclient = HttpClients.createDefault();
 
        // 创建http POST请求
        HttpPost httpPost = new HttpPost("http://www.oschina.net/search");
 
        // 设置2个post参数,一个是scope、一个是q
        List<NameValuePair> parameters = new ArrayList<NameValuePair>(0);
        parameters.add(new BasicNameValuePair("scope", "project"));
        parameters.add(new BasicNameValuePair("q", "java"));
        // 构造一个form表单式的实体
        UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(parameters);
        // 将请求实体设置到httpPost对象中
        httpPost.setEntity(formEntity);
 
        CloseableHttpResponse response = null;
        try {
            // 执行请求
            response = httpclient.execute(httpPost);
            // 判断返回状态是否为200
            if (response.getStatusLine().getStatusCode() == 200) {
                String content = EntityUtils.toString(response.getEntity(), "UTF-8");
                System.out.println(content);
            }
        } finally {
            if (response != null) {
                response.close();
            }
            httpclient.close();
        }
 
    }
 
}

OkHttp

  • 优点:接口设计良好,支持HTTP/2,并且在弱网和无网环境下有自动检测和恢复机制
    【当网络出现问题的时候,它会自动恢复一般的连接问题,如果你的服务有多个IP地址,当第一个IP请求失败时,OkHttp会交替尝试匹配其他的IP,OkHttp使用现代TLS技术(SNI,ALPN[我也不懂是啥])初始化新的连接,当握手失败时会回退到TLS1.0】

依赖

<dependency>
    <groupId>com.squareup.okhttp3</groupId>
    <artifactId>okhttp</artifactId>
    <version>3.10.0</version>
</dependency>

GET请求

/**
   * GET请求
   */
public static String get(String url) throws IOException {
    OkHttpClient client = new OkHttpClient();
    Request request = new Request.Builder()
        .get()
        .url(url)
        .build();
    Response response = client.newCall(request).execute();
    return response.body().string();
}

// 调用
String objStr = OkHttpUtil.get(url);

表单数据提交

/**
 * POST:表单数据提交
 */
public static void postFormBody(String url, FormBody formBody) throws IOException {
    OkHttpClient client = new OkHttpClient();
    final Request request = new Request.Builder()
        .url(url)
        .post(formBody)
        .build();
    Response response = client.newCall(request).execute();
    return response.body().string();
}

// 调用
FormBody formBody = new FormBody.Builder()
    .add("id", id.toString())
    .build();
OkHttpUtil.postFormBody(url, formBody);

Json数据提交

/**
 * POST:json数据提交
 */
public static String postJsonBody(String url, String jsonBody) throws IOException {
    OkHttpClient client = new OkHttpClient();
    MediaType mediaType = MediaType.parse("application/json");
    RequestBody body = RequestBody.create(mediaType, jsonBody);
    final Request request = new Request.Builder()
        .url(url)
        .post(body)
        .build();
    Response response = client.newCall(request).execute();
    return response.body().string();
}

// 调用
String config = "{\"id\": 123}";
String response = OkHttpUtil.postJsonBody(url, config);

Delete请求

/**
 * DELETE
 */
public static String delete(String url, String jsonBody) throws IOException {
    OkHttpClient client = new OkHttpClient();
    MediaType mediaType = MediaType.parse("application/json");
    RequestBody body = RequestBody.create(mediaType, jsonBody);
    Request request = new Request.Builder()
        .url(url)
        .method("DELETE", body)
        .addHeader("Content-Type", "application/json")
        .build();
    Response response = client.newCall(request).execute();
    return response.body().string();
}

// 调用
String response = OkHttpUtil.delete(url, "");

RestTemplate

  • Spring提供的用于访问Rest服务的客户端
  • 优点:提供了多种便捷访问远程 Http服务的方法,能够大大提高客户端的编写效率

依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

RestTemplate需要自己注入
三种方式

//方式一:如果要使用,可以编写配置文件自己注入

package com.itunion.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;

@Configuration
public class ApiConfig {
    @Bean
    public RestTemplate restTemplate(ClientHttpRequestFactory factory) {
        return new RestTemplate(factory);
    }

    @Bean
    public ClientHttpRequestFactory simpleClientHttpRequestFactory() {
        //默认的是JDK提供http连接,需要的话可以通过setRequestFactory方法替换为例如Apache HttpComponents、Netty或OkHttp等其它HTTP library。
        SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
        factory.setReadTimeout(5000);//单位为ms
        factory.setConnectTimeout(5000);//单位为ms
        return factory;
    }
}

//方式二:也可以通过自己新建对象直接使用

RestTemplate restTemplate = new RestTemplate();

//方式三:RestTemplateBuilder创建
@Autowired
    private RestTemplateBuilder templateBuilder;


    @Bean
    public RestTemplate restTemplate() {
        return templateBuilder.build();
    }

GET请求

//方式1:
Notice notice = restTemplate.getForObject("http://fantj.top/notice/list/{1}/{2}"
                , Notice.class,1,5);

//方式2:
Map<String,String> map = new HashMap();
map.put("start","1");
map.put("page","5");
Notice notice = restTemplate.getForObject("http://fantj.top/notice/list/"
                , Notice.class,map);

Post请求

//方式一: ForObject
String url = "http://demo/api/book/";
HttpHeaders headers = new HttpHeaders();
MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
headers.setContentType(type);
String requestJson = "{...}";
HttpEntity<String> entity = new HttpEntity<String>(requestJson,headers);
String result = restTemplate.postForObject(url, entity, String.class);
System.out.println(result);

//方式二: ForEntity
String url = "http://47.xxx.xxx.96/register/checkEmail";
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
MultiValueMap<String, String> map= new LinkedMultiValueMap<>();
map.add("email", "844072586@qq.com");

HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(map, headers);
ResponseEntity<String> response = restTemplate.postForEntity( url, request , String.class );
System.out.println(response.getBody());

Feign的原理

feign的核心就是一个代理,通过一系列的封装和处理,最终转换成Http的请求形式
大致原理如下:

  • 启动时,程序进行包扫描,扫描所有包下添加了@FeignClient注解的类,并将这些类注入到Spring的IOC容器中,当定义中的Feign接口被调用时,通过JDK动态代理来生成RequestTemplate
  • RequestTemplate中包含请求的所有信息,如请求参数,请求URL等
  • RequestTemplate生成一个Request对象,将Request交给Client处理,这个client默认是JDK的HttpUrlConnection,也可以是Okhttp,HttpClient等
  • 最后client封装成LoadBalanceClient,结合Ribbon负载均衡发起调用

网络上有一张Feign的处理图可以便于理解
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值