被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。

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

 

 

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

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

实现远程调用

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

@Configuration 
public class OpenFeignConfig { 
 @Bean 
 public Contract useFeignAnnotations() { 
  return new Contract.Default(); 
 } 

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.

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

public class FeginClientFactory { 
  
 /** 
  * 缓存所有的Fegin客户端 
  */ 
 private volatile static Map<String, Object> feginClientCache = new HashMap<>(); 
  
 /** 
  * 从Map中获取数据 
  * @return  
  */ 
 @SuppressWarnings("unchecked"
 public static <T> T getFeginClient(Class<T> clazz, String baseUrl){ 
  if(!feginClientCache.containsKey(baseUrl)) { 
   synchronized (FeginClientFactory.class) { 
    if(!feginClientCache.containsKey(baseUrl)) { 
     T feginClient = Feign.builder().decoder(new JacksonDecoder()).encoder(new JacksonEncoder()).target(clazz, baseUrl); 
     feginClientCache.put(baseUrl, feginClient); 
    } 
   } 
  } 
  return (T)feginClientCache.get(baseUrl); 
 } 

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.

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

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

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.

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

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

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.

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

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

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

解决问题

问题描述

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

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'
  • 1.

问题分析

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

问题解决

在项目中增加依赖。

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

 

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

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

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

添加后的效果如下所示。

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

 

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

 

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

2024-08-21 08:22:33

2021-09-29 09:07:22

Docker 日志容器

2020-03-20 08:00:32

代码程序员追求

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

推荐系统业务

2025-03-21 08:55:36

SpringOpenFeignAPI

2015-08-25 15:58:33

编程集锦

2016-11-16 15:04:56

大数据中国足球

2020-12-24 11:40:04

微信更新移动应用

2023-11-09 09:08:38

RibbonSpring

2021-05-20 09:06:20

KafkaZookeeper分布式

2023-11-21 17:36:04

OpenFeignSentinel

2020-10-21 12:10:30

订单号Java代码

2023-08-09 08:01:00

WebSockett服务器web

2020-10-26 08:55:52

Redis单线程模型

2024-07-04 11:44:02

2021-07-26 05:03:44

OpenFeign系统组件
点赞
收藏

51CTO技术栈公众号