欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringBoot整合OpenFeign的坑

 更新時(shí)間:2021年07月14日 10:41:41   作者:冰 河  
最近試用SpringBoot+K8S,遇到了個(gè)坑,通過OpenFeign請求返回值LocalDateTime發(fā)生了異常,本文就詳細(xì)的介紹一下解決方法,感興趣的可以了解一下

最近,在使用SpringBoot+K8S開發(fā)微服務(wù)系統(tǒng),既然使用了K8S,我就不想使用SpringCloud了。為啥,因?yàn)镵8S本身的就提供了非常6的服務(wù)注冊與發(fā)現(xiàn)、限流、熔斷、負(fù)載均衡等等微服務(wù)需要使用的技術(shù),那我為啥還要接入SpringCloud呢?額,說了這么多,在真正使用SpringBoot+K8S這一套技術(shù)棧的時(shí)候,也會(huì)遇到一些問題,比如我不需要使用SpringCloud時(shí),調(diào)用其他服務(wù)時(shí),我使用的是原生的OpenFegin,在使用OpenFegin調(diào)用其他服務(wù)的時(shí)候,就遇到了一個(gè)大坑。通過OpenFeign請求返回值LocalDateTime發(fā)生了異常,今天,我們就來說說這個(gè)坑!

項(xiàng)目集成OpenFegin

集成OpenFegin依賴

首先,我先跟大家說下項(xiàng)目的配置,整體項(xiàng)目使用的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>

這里,我省略了一些其他的配置項(xiàng)。

接下來,我就開始在我的項(xiàng)目中使用OpenFegin調(diào)用遠(yuǎn)程服務(wù)了。具體步驟如下。

實(shí)現(xiàn)遠(yuǎn)程調(diào)用

首先,創(chuàng)建OpenFeignConfig類,配置OpenFegin默認(rèn)使用的Contract。

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

接下來,我們寫一個(gè)通用的獲取OpenFeign客戶端的工廠類,這個(gè)類也比較簡單,本質(zhì)上就是以一個(gè)HashMap來緩存所有的FeginClient,這個(gè)的FeginClient本質(zhì)上就是我們自定義的Fegin接口,緩存中的Key為請求連接的基礎(chǔ)URL,緩存的Value就是我們定義的FeginClient接口。

public class FeginClientFactory {
 
 /**
  * 緩存所有的Fegin客戶端
  */
 private volatile static Map<String, Object> feginClientCache = new HashMap<>();
 
 /**
  * 從Map中獲取數(shù)據(jù)
  * @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);
 }
}

接下來,我們就定義一個(gè)FeginClient接口。

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

接下來,我們創(chuàng)建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));
 }
}

一切準(zhǔn)備就緒,運(yùn)行測試。麻蛋,出問題了。主要的問題就是通過OpenFeign請求返回值LocalDateTime字段會(huì)發(fā)生異常?。。?/p>

注:此時(shí)異常時(shí),我們在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;

解決問題

問題描述

SpringBoot通過原生OpenFeign客戶端調(diào)用HTTP接口,如果返回值中包含LocalDateTime類型(包括其他JSR-310中java.time包的時(shí)間類),在客戶端可能會(huì)出現(xiàn)反序列化失敗的錯(cuò)誤。錯(cuò)誤信息如下:

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

問題分析

從客戶端調(diào)用fegin,也是相當(dāng)于URL傳參就相當(dāng)于經(jīng)過一次JSON轉(zhuǎn)換,數(shù)據(jù)庫取出‘2020-10-07T11:04:32'數(shù)據(jù)這時(shí)是時(shí)間類型,進(jìn)過JSON之后就變成了String類型,T就變成了字符不再是一個(gè)特殊字符,因此String的字符串“2020-10-07T11:04:32”反序列化就會(huì)失敗。

問題解決

在項(xiàng)目中增加依賴。

<dependency>
    <groupId>com.fasterxml.jackson.datatype</groupId>
    <artifactId>jackson-datatype-jsr310</artifactId>
    <version>2.9.9</version>
</dependency>

注:如果是用的是SpringBoot,并且明確指定了SpringBoot版本,引入jackson-datatype-jsr310時(shí),可以不用指定版本號。

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

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

添加后的效果如下所示。

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;

此時(shí),再次調(diào)用遠(yuǎn)程接口,問題解決。

到此這篇關(guān)于SpringBoot整合OpenFeign的坑的文章就介紹到這了,更多相關(guān)SpringBoot整合OpenFeign 內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論