加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-16 23:46:59 所属栏目:百科 来源:网络整理
导读: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.spri

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>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读