被SpringBoot整合OpenFeign坑惨了!!

开发 架构
最近,在使用SpringBoot+K8S开发微服务系统,既然使用了K8S,我就不想使用SpringCloud了。为啥,因为K8S本身的就提供了非常6的服务注册与发现、限流、熔断、负载均衡等等微服务需要使用的技术,那我为啥还要接入SpringCloud呢?

[[411635]]

大家好,我是冰河~~

最近,在使用SpringBoot+K8S开发微服务系统,既然使用了K8S,我就不想使用SpringCloud了。为啥,因为K8S本身的就提供了非常6的服务注册与发现、限流、熔断、负载均衡等等微服务需要使用的技术,那我为啥还要接入SpringCloud呢?额,说了这么多,在真正使用SpringBoot+K8S这一套技术栈的时候,也会遇到一些问题,比如我不需要使用SpringCloud时,调用其他服务时,我使用的是原生的OpenFegin,在使用OpenFegin调用其他服务的时候,就遇到了一个大坑。通过OpenFeign请求返回值LocalDateTime发生了异常,今天,我们就来说说这个坑!

好了,我们开始吧~~

项目集成OpenFegin

集成OpenFegin依赖

首先,我先跟大家说下项目的配置,整体项目使用的SpringBoot版本为2.2.6,原生的OpenFegin使用的是11.0,我们通过如下方式在pom.xml中引入OpenFegin。

  1. <properties> 
  2.     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 
  3.     <skip_maven_deploy>false</skip_maven_deploy> 
  4.     <java.version>1.8</java.version> 
  5.     <openfegin.version>11.0</openfegin.version> 
  6. </properties> 
  7. <dependencies> 
  8.     <dependency> 
  9.         <groupId>io.github.openfeign</groupId> 
  10.         <artifactId>feign-core</artifactId> 
  11.         <version>${openfegin.version}</version> 
  12.     </dependency> 
  13.  
  14.     <dependency> 
  15.         <groupId>io.github.openfeign</groupId> 
  16.         <artifactId>feign-jackson</artifactId> 
  17.         <version>${openfegin.version}</version> 
  18.     </dependency> 
  19. </dependencies> 

 

 

这里,我省略了一些其他的配置项。

接下来,我就开始在我的项目中使用OpenFegin调用远程服务了。具体步骤如下。

实现远程调用

首先,创建OpenFeignConfig类,配置OpenFegin默认使用的Contract。

  1. @Configuration 
  2. public class OpenFeignConfig { 
  3.  @Bean 
  4.  public Contract useFeignAnnotations() { 
  5.   return new Contract.Default(); 
  6.  } 

接下来,我们写一个通用的获取OpenFeign客户端的工厂类,这个类也比较简单,本质上就是以一个HashMap来缓存所有的FeginClient,这个的FeginClient本质上就是我们自定义的Fegin接口,缓存中的Key为请求连接的基础URL,缓存的Value就是我们定义的FeginClient接口。

  1. public class FeginClientFactory { 
  2.   
  3.  /** 
  4.   * 缓存所有的Fegin客户端 
  5.   */ 
  6.  private volatile static Map<String, Object> feginClientCache = new HashMap<>(); 
  7.   
  8.  /** 
  9.   * 从Map中获取数据 
  10.   * @return  
  11.   */ 
  12.  @SuppressWarnings("unchecked"
  13.  public static <T> T getFeginClient(Class<T> clazz, String baseUrl){ 
  14.   if(!feginClientCache.containsKey(baseUrl)) { 
  15.    synchronized (FeginClientFactory.class) { 
  16.     if(!feginClientCache.containsKey(baseUrl)) { 
  17.      T feginClient = Feign.builder().decoder(new JacksonDecoder()).encoder(new JacksonEncoder()).target(clazz, baseUrl); 
  18.      feginClientCache.put(baseUrl, feginClient); 
  19.     } 
  20.    } 
  21.   } 
  22.   return (T)feginClientCache.get(baseUrl); 
  23.  } 

接下来,我们就定义一个FeginClient接口。

  1. public interface FeginClientProxy { 
  2.  @Headers("Content-Type:application/json;charset=UTF-8"
  3.  @RequestLine("POST /user/login"
  4.  UserLoginVo login(UserLoginVo loginVo); 

接下来,我们创建SpringBoot的测试类。

  1. @RunWith(SpringRunner.class) 
  2. @SpringBootTest 
  3. public class IcpsWeightStarterTest { 
  4.  @Test 
  5.  public void testUserLogin() { 
  6.   ResponseMessage result = FeginClientFactory.getFeginClient(FeginClientProxy.class, "http://127.0.0.1").login(new UserLoginVo("zhangsan""123456", 1)); 
  7.   System.out.println(JsonUtils.bean2Json(result)); 
  8.  } 

一切准备就绪,运行测试。麻蛋,出问题了。主要的问题就是通过OpenFeign请求返回值LocalDateTime字段会发生异常!!!

注:此时异常时,我们在LocalDateTime字段上添加的注解如下所示。

  1. import java.time.LocalDateTime; 
  2. import com.baomidou.mybatisplus.annotation.FieldFill; 
  3. import com.baomidou.mybatisplus.annotation.TableField; 
  4. import com.fasterxml.jackson.annotation.JsonFormat; 
  5.  
  6.  
  7. @TableField(value = "CREATE_TIME", fill = FieldFill.INSERT
  8. @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", locale = "zh", timezone = "GMT+8"
  9. private LocalDateTime createTime; 

解决问题

问题描述

SpringBoot通过原生OpenFeign客户端调用HTTP接口,如果返回值中包含LocalDateTime类型(包括其他JSR-310中java.time包的时间类),在客户端可能会出现反序列化失败的错误。错误信息如下:

  1. Caused by:com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `java.time.LocalDateTime` (no Creators, like default construct, exist): no String-argument constructor/factory method to deserialize from String value ('2020-10-07T11:04:32'

问题分析

从客户端调用fegin,也是相当于URL传参就相当于经过一次JSON转换,数据库取出‘2020-10-07T11:04:32’数据这时是时间类型,进过JSON之后就变成了String类型,T就变成了字符不再是一个特殊字符,因此String的字符串“2020-10-07T11:04:32”反序列化就会失败。

问题解决

在项目中增加依赖。

  1. <dependency> 
  2.     <groupId>com.fasterxml.jackson.datatype</groupId> 
  3.     <artifactId>jackson-datatype-jsr310</artifactId> 
  4.     <version>2.9.9</version> 
  5. </dependency> 

 

注:如果是用的是SpringBoot,并且明确指定了SpringBoot版本,引入jackson-datatype-jsr310时,可以不用指定版本号。

接下来,在POJO类的LocalDateTime类型字段增加如下注解。

  1. import com.fasterxml.jackson.databind.annotation.JsonDeserialize; 
  2. import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer; 

添加后的效果如下所示。

  1. import java.time.LocalDateTime; 
  2. import com.baomidou.mybatisplus.annotation.FieldFill; 
  3. import com.baomidou.mybatisplus.annotation.TableField; 
  4. import com.fasterxml.jackson.annotation.JsonFormat; 
  5.  
  6. import com.fasterxml.jackson.databind.annotation.JsonDeserialize; 
  7. import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer; 
  8.  
  9.  
  10. @TableField(value = "CREATE_TIME", fill = FieldFill.INSERT
  11. @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", locale = "zh", timezone = "GMT+8"
  12. @JsonDeserialize(using = LocalDateTimeDeserializer.class) 
  13. private LocalDateTime createTime; 

 

此时,再次调用远程接口,问题解决。

 

责任编辑:武晓燕 来源: 冰河技术
相关推荐

2020-03-20 08:00:32

代码程序员追求

2021-09-29 09:07:22

Docker 日志容器

2024-03-14 10:30:05

缓存场景DEMO

2021-12-27 07:25:13

项目软件开发

2019-06-18 11:09:54

2022-12-09 15:19:48

新能源汽车智能汽车

2020-09-25 08:58:43

推荐系统业务

2016-11-16 15:04:56

大数据中国足球

2020-12-24 11:40:04

微信更新移动应用

2015-08-25 15:58:33

编程集锦

2023-11-09 09:08:38

RibbonSpring

2021-05-20 09:06:20

KafkaZookeeper分布式

2023-08-09 08:01:00

WebSockett服务器web

2023-11-21 17:36:04

OpenFeignSentinel

2020-10-21 12:10:30

订单号Java代码

2020-10-26 08:55:52

Redis单线程模型

2021-07-26 05:03:44

OpenFeign系统组件

2021-04-07 08:43:09

SpringBootRocketMQ开发技术

2021-09-08 20:43:27

腾讯王者荣耀人脸识别

2022-06-06 10:39:01

OpenFeignTCC模式
点赞
收藏

51CTO技术栈公众号