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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM