Feign【替换默认的feign client】
说明:
feign默认情况下使用的是JDK原始的URLConnection发送的HTTP请求,没有使用到连接池,但是对每个地址会保持长连接,即HTTP的persistence connection。我们可以利用Apache的HTTP client替换原始的HTTP client,通过设置连接池,超时时间等,对服务调用进行调优。spring cloud从Brixtion.SR5版本之后支持这种替换操作。
1、使用Apache的HTTP client替换feign默认的client
a、项目依赖:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
<relativePath/>
</parent> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
</properties> <dependencyManagement>
<dependencies>
<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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Spring Cloud OpenFeign的Starter的依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<!-- 使用Apache HttpClient替换Feign原生httpclient -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
</dependency> <dependency>
<groupId>com.netflix.feign</groupId>
<artifactId>feign-httpclient</artifactId>
<version>8.17.0</version>
</dependency>
</dependencies> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
b、项目启动类:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients; @SpringBootApplication
@EnableFeignClients
public class SpringCloudFeignApplication { public static void main(String[] args) {
SpringApplication.run(SpringCloudFeignApplication.class, args);
}
}
c、编写测试代码:
client接口:
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod; //https://api.caiyunapp.com/v2/TAkhjf8d1nlSlspN/121.6544,25.1552/forecast.json 彩云天气API
@FeignClient(name = "caiyunapp", url = "https://api.caiyunapp.com/v2/TAkhjf8d1nlSlspN/121.6544,25.1552")
public interface HelloFeignService { @RequestMapping(value = "/forecast.json", method = RequestMethod.GET)
ResponseEntity<byte[]> searchRepo(); }
controller类:
import cn.springcloud.book.feign.service.HelloFeignService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController; @RestController
public class HelloFeignController { @Autowired
private HelloFeignService helloFeignService; // 服务消费者对位提供的服务
@GetMapping(value = "/search")
public ResponseEntity<byte[]> searchGithubRepoByStr() {
return helloFeignService.searchRepo();
} }
d、工程配置文件:
server:
port: 8011
spring:
application:
name: httpclient-demo feign:
httpclient:
enabled: true
e、启动工程,浏览器访问接口:

说明替换默认client成功,接口正常访问。
2、使用OKHTTP替换feign默认的client
OKHTTP是目前比较火的一个HTTP客户端,具有以下优点:
- 支持SPDY,可以合并多个到同一主机的请求。
- 使用连接池技术减少请求延迟。
- 使用GZIP压缩减少传输数据量。
- 缓存响应避免重复请求。
a、项目依赖:
跟上面的依赖类似,排除下面的依赖:
<!-- 使用OKHTTP无需这2个依赖 -->
<!-- 使用Apache HttpClient替换Feign原生httpclient -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
</dependency> <dependency>
<groupId>com.netflix.feign</groupId>
<artifactId>feign-httpclient</artifactId>
<version>8.17.0</version>
</dependency>
加上下面的配置:
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-okhttp</artifactId>
</dependency>
b、在上面的代码编写基础上,编写一个配置类,如下:
import feign.Feign;
import okhttp3.ConnectionPool;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.cloud.openfeign.FeignAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import java.util.concurrent.TimeUnit; @Configuration
@ConditionalOnClass(Feign.class)
@AutoConfigureBefore(FeignAutoConfiguration.class)
public class FeignOkHttpConfig {
@Bean
public okhttp3.OkHttpClient okHttpClient(){
return new okhttp3.OkHttpClient.Builder()
//设置连接超时
.connectTimeout(60, TimeUnit.SECONDS)
//设置读超时
.readTimeout(60, TimeUnit.SECONDS)
//设置写超时
.writeTimeout(60,TimeUnit.SECONDS)
//是否自动重连
.retryOnConnectionFailure(true)
.connectionPool(new ConnectionPool())
//构建OkHttpClient对象
.build();
} }
c、配置文件:
server:
port: 8011
spring:
application:
name: okhttp-demo feign:
httpclient:
enabled: false
okhttp:
enabled: true
d、启动项目,访问接口,即可正常获取到数据,表明替换成功。
Feign【替换默认的feign client】的更多相关文章
- feign三:覆写feign的默认配置及feign的日志
feign三:覆写feign的默认配置及feign的日志 默认配置复写 本项目地址:http://192.168.1.103:7601 本例是通过feign调用 eureka项目中的/eureka/a ...
- Feign二:复写Feign的默认配置
Feign二:复写Feign的默认配置 1.在启动文件加入feign注解:@EnableFeignClients FeignApp.java import org.springframework.bo ...
- 客户端负载均衡Feign之二:Feign 功能介绍
一.Ribboon配置 在Spring cloud Feign中客户端负载均衡是通过Spring cloud Ribbon实现的,所以我们可以直接通过配置Ribbon客户端的方式来自定义各个服务客户端 ...
- ubuntu设置开机启动图形应用程序,替换默认图形桌面
直接将启动程序放在rc.local即可.但是如果自动启动的程序奔溃后,会返回到ubuntu的unity桌面系统. 我遇到的问题是程序还有调用 xset 去定时关闭屏幕.在桌面启动后调用没问题.如果rc ...
- freeswitch 使用mysql替换默认的sqlite
转自 80000hz.com freeswitch 使用mysql替换默认的sqlite No Reply , Posted in 默认分类 on January 14, 2014 目标使用mysql ...
- springboot打包去除资源文件,启动时指定配置文件位置,使用log4j2替换默认logback
springboot打包时,去掉资源文件 <build> <resources> <resource> <directory>src/main/reso ...
- SpringCloud系列六:Feign接口转换调用服务(Feign 基本使用、Feign 相关配置)
1.概念:Feign 接口服务 2.具体内容 现在为止所进行的所有的 Rest 服务调用实际上都会出现一个非常尴尬的局面,例如:以如下代码为例: Dept dept = this.restTempla ...
- spring cloud微服务快速教程之(十四)spring cloud feign使用okhttp3--以及feign调用参数丢失的说明
0-前言 spring cloud feign 默认使用httpclient,需要okhttp3的可以进行切换 当然,其实两者性能目前差别不大,差别较大的是很早之前的版本,所以,喜欢哪个自己选择: 1 ...
- 关于Cygwin——包管理、替换默认终端、同MSYS的比较
(搬运自我在SegmentFault的博客) Cygwin 是一个用于 Windows 的类 UNIX shell 环境. 它由两个组件组成:一个 UNIX API 库,它模拟 UNIX 操作系统提供 ...
随机推荐
- 常用的os库笔记
1.创建文件 import os os.mkdir('d:/log') 2.重命名文件 import os os.rename('d:/log','d:/newlog') 3.删除文件 import ...
- sftp远程传输脚本
该脚本主要是是批量修改文件名,然后传输文件到服务器#!/bin/bash #脚本名:sftp.sh while true do num=`cat test.txt|wc -l` ;a<=$num ...
- Spring boot + mybatis 只读取到一个jar包中的mapper配置文件
采用spring boot 开发了一个多模块项目,有多个模块中都有mapper配置文件. 采用如下的方式配置,制度去到了一个模块jar包中配置文件: @Bean(name = "sqlSe ...
- meshing-simple_block
原视频下载地址:https://yunpan.cn/cqjeSzP7s93Pc 访问密码 aaff
- java面向对象-进度2
1.面向对象的五个基本原则 三个基本元素: 1. 封装: 封装是把过程和数据包围起来,对数据的访问只能通过已定义的界面.面向对象计算始于这个基本概念,即现实世界可以被描绘成一系列完全自治.封装的对象, ...
- Navicat Premium 12安装与激活(亲测已成功激活)
说明:博主所提供的激活文件理论支持Navicat Premium 12.0.16 - 12.0.24简体中文64位,但已测试的版本为Navicat Premium 12.0.22.12.0.23和12 ...
- Linux系统中rm删除命令
rm命令 1.可以删除一个目录中的一个或多个文件或目录 2.可以将某个目录及其下属的所有文件及其子目录均删除掉 3.对于链接文件,只是删除整个链接文件,而原有文件保持不变 语法 rm (选项)(参数) ...
- 简易的CRM系统案例SpringBoot + thymeleaf + MySQL + MyBatis版本
创建maven项目 pop.xml <?xml version="1.0" encoding="UTF-8"?> <project xmlns ...
- osg HUD 前景色
#ifdef _WIN32 #include <Windows.h> #endif // _WIN32 #include<iostream> #include <osgV ...
- 一百四十二:CMS系统之帖子详情页面布局
定义一个404页面 <!DOCTYPE html><html lang="en"><head> <meta charset="U ...