Feign2.0用Apache的Httpclient发送请求并配置连接池

主要是针对Spring Cloud新出的版本(CloudFinchley.RC2与Springboot2.0.2.RELEASE),一些新的改动,与在使用中遇见的一些问题,踩过的坑,希望后面的人就不用踩了。

服务注入到Eureka需要的MAVEN配置

1<dependency> 2 <groupId>org.springframework.cloud</groupId> 3 <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> 4</dependency> 5<dependency> 6 <groupId>org.springframework.cloud</groupId> 7 <artifactId>spring-cloud-starter-openfeign</artifactId> 8</dependency>

如果找不到包加入下面:

1<dependencyManagement> 2 <dependencies> 3 <dependency> 4 <groupId>org.springframework.cloud</groupId> 5 <artifactId>spring-cloud-dependencies</artifactId> 6 <version>${spring-cloud.version}</version> 7 <type>pom</type> 8 <scope>import</scope> 9 </dependency> 10 </dependencies> 11</dependencyManagement>

如果还是不行,再加

1<repositories> 2 <repository> 3 <id>spring-snapshots</id> 4 <name>Spring Snapshots</name> 5 <url>https://repo.spring.io/snapshot</url> 6 <snapshots> 7 <enabled>true</enabled> 8 </snapshots> 9 </repository> 10 <repository> 11 <id>spring-milestones</id> 12 <name>Spring Milestones</name> 13 <url>https://repo.spring.io/milestone</url> 14 <snapshots> 15 <enabled>false</enabled> 16 </snapshots> 17 </repository> 18</repositories>

这个呢,是因为比较新,maven可能还么相关的包。

application.yml配置

1spring: 2 application: 3 name: deal-center 4server: 5 port: 8902 6eureka: 7 client: 8 serviceUrl: 9 defaultZone: http://admin:123123@127.0.0.1:8000/eureka/

启动类

1@SpringBootApplication 2@EnableEurekaClient 3@EnableFeignClients 4public class DealCentreApplication { 5 6 public static void main(String[] args) { 7 SpringApplication.run(DealCentreApplication.class, args); 8 } 9}

编写Feign接口类

1@FeignClient("user-center") 2public interface UserService { 3 @GetMapping("/user/getUserById") 4 public User getUserById(@RequestParam("id")Long id); 5 6 @GetMapping("/user/getById/{id}") 7 public User getById(@PathVariable("id")Long id); 8 9 @GetMapping("/user/queryUserList")/*这个是不对的*/ 10 public List<User> queryUserList(@RequestBody User user); 11}

Feign请求类中,第一个和第二个,都可以请求通。第三个不行。如果都换成POST请求,也可以请求正确。
在SpringCloud 1.5版本的时候,@GetMapping和@PostMapping不支持,现在已经可以了。但是复杂参数时还是不行,报错如下:

1UserService#queryUserList(User); content: 2{"timestamp":"2018-05-30T09:15:02.095+0000","status":405,"error":"Method Not Allowed","message":"Request method 'POST' not supported","path":"/user/queryUserList"}

意思是说不支持POST请求,可是明明发的是POST请求啊,跟代码走...发现一段代码如下:

1privatesynchronizedOutputStream getOutputStream0()throwsIOException { 2 try{ 3 if(!this.doOutput) { 4 thrownewProtocolException("cannot write to a URLConnection if doOutput=false - call setDoOutput(true)"); 5 }else{ 6 if(this.method.equals("GET")) { 7 this.method ="POST"; 8 } 9 } 10 } 11}

所以是不能接受requestbody方式传递参数的。

**问题来了:**feign通过jdk中的HttpURLConnection向下游服务发起http请求(源码详见feign.Client.Default)

1@Override 2 public Response execute(Request request, Options options) throws IOException { 3 HttpURLConnection connection = convertAndSend(request, options); 4 return convertResponse(connection).toBuilder().request(request).build(); 5 } 6 7 HttpURLConnection convertAndSend(Request request, Options options) throws IOException { 8 final HttpURLConnection 9 connection = 10 (HttpURLConnection) new URL(request.url()).openConnection(); 11 if (connection instanceof HttpsURLConnection) { 12 HttpsURLConnection sslCon = (HttpsURLConnection) connection; 13 if (sslContextFactory != null) { 14 sslCon.setSSLSocketFactory(sslContextFactory); 15 } 16.........

得出结论:缺乏连接池的支持,在达到一定流量的后服务肯定会出问题 ,我想用Apach的httpclient替换掉了原生的UrlConnection,网上找了下资料,发现有专门针对这个的一个包如下:

1<!-- https://mvnrepository.com/artifact/io.github.openfeign/feign-httpclient --> 2<dependency> 3 <groupId>io.github.openfeign</groupId> 4 <artifactId>feign-httpclient</artifactId> 5</dependency>

切要求这个包版本与feign-core的版本保持一致,并在application.yml中添加配置如下:

1feign: 2 httpclient: 3 enabled: true

新的CloudFinchley.RC2版中这不需要这个了,引入此包后,Spring Cloud会将LoadBalancerFeignClient中的Client注入为ApacheHttpClient。默认的连接池信息在FeignHttpClientProperties。

说说项目结构

其实还有一种,就是项目之间,全用map来传递。当生产者数据架构改变时候,只要消费者未使用到生产者改变的字段就不会受到影响。不过这样需要对数据结构比较清晰,或者文档比较完善。感觉都不完美...

微信群在QQ群中发,QQ技术群:

点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

皕杰报表之UUID

​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

java将前端的json数组字符串转换为列表

记录下在前端通过ajax提交了一个json数组的字符串,在后端如何转换为列表。前端数据转化与请求varcontracts{id:'1',name:'yanggb合同1'},{id:'2',name:'yanggb合同2'},{id:'3',name:'yang

Feign2.0用Apache的Httpclient发送请求并配置连接池 - HelloWorld