feign 入门实例

服务2代码:


@RestController
@RequestMapping("/cache")
public class CacheController {

    @LogRequired
    @GetMapping("/hello")
    public Map<String, String> hello() {
        HashMap<String, String> amap = new HashMap<String, String>();
        amap.put("data", "feign");
        return amap;
    }

    @LogRequired
    @GetMapping("/echo")
    public Map<String, String> echo(String data) {
        HashMap<String, String> amap = new HashMap<String, String>();
        amap.put("data", data);
        return amap;
    }
}

服务1代码:

public interface FeignTest {

    @RequestLine("GET /cache/hello")
    Map<String, String> hello();
    
    @RequestLine("GET /cache/echo?data={data}")
    Map<String, String> echo(@Param("data") String data);
}

@RestController
public class HelloworldDemoController {

    @RequestMapping("/echo")
    public String echo()  {
 
        FeignTest remote = Feign.builder()
                .encoder(new JacksonEncoder())
                .decoder(new JacksonDecoder())
                .target(FeignTest.class, "http://192.168.74.130:5000/" /*url of server 2*/);

        Map<String, String> amap = remote.echo("hello your head");
        return "feign:" + amap.get("data");
    }

    @RequestMapping("/hello")
    public String hello()  {
 
        FeignTest remote = Feign.builder()
                .encoder(new JacksonEncoder())
                .decoder(new JacksonDecoder())
                .target(FeignTest.class, "http://192.168.74.130:5000/" /*url of server 2*/);

        Map<String, String> amap = remote.hello();
        return "feign:" + amap.get("data");
    }
}

POM.XML 配置


        <dependency>
            <groupId>io.github.openfeign</groupId>
            <artifactId>feign-jackson</artifactId>
            <version>9.5.1</version>
        </dependency>

        <dependency>
            <groupId>com.netflix.feign</groupId>
            <artifactId>feign-core</artifactId>
            <version>8.18.0</version>
        </dependency>

 

相关文章:

  • 2021-06-06
  • 2021-12-04
  • 2021-09-11
  • 2021-08-05
  • 2021-11-30
  • 2021-12-09
  • 2021-11-20
  • 2022-01-07
猜你喜欢
  • 2022-01-03
  • 2022-12-23
  • 2022-12-23
  • 2021-04-21
  • 2021-07-13
  • 2021-12-29
  • 2022-12-23
相关资源
相似解决方案