欢迎您访问365答案网,请分享给你的朋友!
生活常识 学习资料

插播之SpringCloud的Feign

时间:2023-07-04
文章目录

官方说明理解一下常见的Java Http请求库

HttpURLConnectionHttpClientOkHttpRestTemplate 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

依赖

org.apache.httpcomponents httpclient 4.5.3

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 parameters = new ArrayList(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】

依赖

com.squareup.okhttp3 okhttp 3.10.0

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);

表单数据提交

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数据提交

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请求

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服务的方法,能够大大提高客户端的编写效率

依赖

org.springframework.boot spring-boot-starter-web

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;@Configurationpublic 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 map = new HashMap();map.put("start","1");map.put("page","5");Notice notice = restTemplate.getForObject("http://fantj.top/notice/list/" , Notice.class,map);

Post请求

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


//方式二: ForEntityString url = "http://47.xxx.xxx.96/register/checkEmail";HttpHeaders headers = new HttpHeaders();headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);MultiValueMap map= new linkedMultiValueMap<>();map.add("email", "844072586@qq.com");HttpEntity> request = new HttpEntity<>(map, headers);ResponseEntity response = restTemplate.postForEntity( url, request , String.class );System.out.println(response.getBody());

Feign的原理

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

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

网络上有一张Feign的处理图可以便于理解

Copyright © 2016-2020 www.365daan.com All Rights Reserved. 365答案网 版权所有 备案号:

部分内容来自互联网,版权归原作者所有,如有冒犯请联系我们,我们将在三个工作时内妥善处理。