1. 程式人生 > >Feign的使用方法(個人理解)

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

pil eth builder 理解 eve pre stl 1.8 ann

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的使用方法(個人理解)