Feign的使用方法(个人理解)

Posted pthwang

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Feign的使用方法(个人理解)相关的知识,希望对你有一定的参考价值。

Feign代码如下

package com.tydic.vc.adepter.nethall.service;

import feign.*;
import feign.gson.GsonDecoder;
import feign.gson.GsonEncoder;
import feign.slf4j.Slf4jLogger;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Map;

public interface VcServiceFeignClient {
  @RequestLine("GET /user/get1?id={id}&username={username}" )
  public User get1(@Param("id") Long id, @Param("username") String username);

  @RequestLine("GET /user/get1")
  public User get2(@QueryMap Map<String, Object> map);

// @RequestLine("GET /user/get1")
// public User get3(@QueryMap User user);

  //注解@RequestBody可以是map,也可以是pojo,
  //当需要发送Post方法时,参数解析可以用@RequestBody或者@RequestParam,两种效果一样,都支持Map和POJO
  @RequestLine("POST /post")
  @Headers("Content-Type: application/json")
  public User post0(@RequestParam User user);

  @RequestLine("POST /post")
  @Headers("Content-Type: application/json")
  public User post1(@RequestParam Map<String, Object> map);

  static VcServiceFeignClient connect(){
    return Feign.builder()
            .encoder(new GsonEncoder())
            .decoder(new GsonDecoder())
            .logger(new Slf4jLogger())
            .logLevel(feign.Logger.Level.FULL)
            .options(new Request.Options(1000, 3500))
            .retryer(new Retryer.Default(5000, 5000, 3))
            //.target(VcServiceFeignClient.class, "http://localhost:8010/");
            .target(VcServiceFeignClient.class, "http://localhost:8000/");
  }
}

pom.xml

    <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <org.springframework.version>4.3.15.RELEASE</org.springframework.version>
        <io.github.openfeign.version>9.3.1</io.github.openfeign.version>
    </properties>

      <dependency>
            <groupId>io.github.openfeign</groupId>
            <artifactId>feign-core</artifactId>
            <version>${io.github.openfeign.version}</version>
        </dependency>
        <dependency>
            <groupId>io.github.openfeign</groupId>
            <artifactId>feign-gson</artifactId>
            <version>${io.github.openfeign.version}</version>
        </dependency>
        <dependency>
            <groupId>io.github.openfeign</groupId>
            <artifactId>feign-slf4j</artifactId>
            <version>${io.github.openfeign.version}</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.7.25</version>
        </dependency>

以上是关于Feign的使用方法(个人理解)的主要内容,如果未能解决你的问题,请参考以下文章

SpringCloud+Feign环境下文件上传与form-data同时存在的解决办法

eureka_feign学习_1

OpenFeign快速入门

json 个人的vscode的代码片段

几个关于js数组方法reduce的经典片段

声明式调用---Feign