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>