feign 使用示例
这个例子是自己写的,当然也是参照官方的文档,没有spring, 也没有spring boot. 就是普通的main()函数执行。
既然feign是http client, 肯定要有一个server, server是普通的spring boot的 RestController:
server project 结构:
共三个文件: DemoApplication.java, DemoAPI.java, Demo.java
Demo.java
package ex.demo.domain; public class Demo { private int id; private String name; public Demo(){} public Demo(int id, String name) { this.id = id; this.name = name; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
DemoAPI.java
package ex.demo.api; import java.util.ArrayList; import java.util.List; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController; import ex.demo.domain.Demo; @RestController public class DemoAPI { @GetMapping("/demo/hello") public String hello() { return "hello, DEMO!"; } @GetMapping("/get/{id}") @ResponseBody public Demo getDemo(@PathVariable("id") int id) { return new Demo(id, "demo " + id); } @GetMapping("/demo/list") @ResponseBody public List<Demo> getDemoList() { List<Demo> list = new ArrayList<>(); Demo d1 = new Demo(1, "demo1"); Demo d2 = new Demo(2, "demo2"); list.add(d1); list.add(d2); return list; } }
DemoApplication.java
package ex.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }
pom.xml
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>1.5.10.RELEASE</version> </dependency>
demo-service是个很简单的restful服务, 这里只例出三个常用的情况:
1. 返回String
2. 返回Object
3. 返回对象列表
feign project 结构:
也是三个文件,都放一个包里了。 也就是个例子,不用那么讲究。
Demo.java
package ex.feign; public class Demo { private int id; private String name; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
RemoteService.java
package ex.feign; import java.util.List; import feign.Param; import feign.RequestLine; // 这个例子使用 demo-service 作为服务端 public interface RemoteService { @RequestLine("GET /demo/hello") String hello(); @RequestLine("GET /get/{id}") Demo getDemo(@Param("id") int id); @RequestLine("GET /demo/list") List<Demo> getList(); }
FeignApplication.java
package ex.feign; import java.util.List; import feign.Feign; import feign.jackson.JacksonDecoder; public class FeignApplication { private static String SERVER_ADDR = "http://127.0.0.1:8080"; public static void main(String[] args) { RemoteService helloService = Feign.builder().target(RemoteService.class, SERVER_ADDR); System.out.println(helloService.hello()); // 这个要将json转成Demo RemoteService demoService = Feign.builder() .decoder(new JacksonDecoder()) .target(RemoteService.class, SERVER_ADDR); Demo demo = demoService.getDemo(5); System.out.println("id=" + demo.getId() + ", name=" + demo.getName()); // Demo列表也用JscksonDecoder List<Demo> list = demoService.getList(); for(Demo d : list) { System.out.println("id=" + d.getId() + ", name=" + d.getName()); } } }
pom.xml
<dependency> <groupId>com.netflix.feign</groupId> <artifactId>feign-core</artifactId> <version>8.18.0</version> </dependency> <dependency> <groupId>com.netflix.feign</groupId> <artifactId>feign-jackson</artifactId> <version>8.18.0</version> </dependency>
运行: 先启动demo-service, 然后运行FeignApplication.java
输出结果:
hello, DEMO! id=5, name=demo 5 id=1, name=demo1 id=2, name=demo2