Feign 第一个Feign程序 一

Feign 开源地址:https://github.com/OpenFeign/feign

 

1.编写接口服务

(1)导入jar包

<parent>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-parent</artifactId>
      <version>2.0.2.RELEASE</version>
  </parent>

  <dependencies>
      <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-web</artifactId>
      </dependency>
  </dependencies>

(2)编写接口

public class Person {
    private Integer id;
    private String name;
    private Integer age;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }
}
  @RequestMapping(value = "/hello", method = RequestMethod.GET)
    public String hello() {
        return "hello world";
    }

    @RequestMapping(value = "/person/{id}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public Person person(@PathVariable  Integer id) {
        Person person = new Person();
        person.setAge(18);
        person.setId(id);
        person.setName("aa");
        return person;
    }

 

2.Feign客户端 (此处通过cxf做一个对比)

(1)cxf客户端

<dependencies>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-core</artifactId>
            <version>3.1.10</version>
        </dependency>

        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-rs-client</artifactId>
            <version>3.1.10</version>
        </dependency>
    </dependencies>
public class CxfClient {

    public static void main(String[] args) throws Exception{
        //创建WebClient
        WebClient client = WebClient.create("http://localhost:8080/hello");

        //获取响应
        Response response = client.get();

        //获取响应内容
        InputStream inputStream = (InputStream) response.getEntity();
        String content =  IOUtils.readStringFromStream(inputStream);

        //输出结果
        System.out.println("返回结果为:" + content);
    }
}

(2)feign客户端

<dependencies>
    <dependency>
      <groupId>io.github.openfeign</groupId>
      <artifactId>feign-core</artifactId>
      <version>9.5.0</version>
    </dependency>

    <dependency>
      <groupId>io.github.openfeign</groupId>
      <artifactId>feign-gson</artifactId>
      <version>9.5.0</version>
    </dependency>
  </dependencies>
import com.idelan.cloud.model.Person;
import feign.Param;
import feign.RequestLine;

/**
 * Created with IDEA
 * User gyli
 * date 2018/12/6
 */
public interface ClientInterface {

    @RequestLine("GET /hello")
    public String hello();

    @RequestLine("GET /person/{id}")
    public Person getPerson(@Param("id") Integer id);
}
public static void main(String[] args) {
        ClientInterface helloClient = Feign.builder().target(ClientInterface.class, "http://localhost:8080");

        String hello = helloClient.hello();
        System.out.println(hello);

        ClientInterface clientInterface = Feign.builder()
                .decoder(new GsonDecoder())
                .target(ClientInterface.class, "http://localhost:8080");

        Person person = clientInterface.getPerson(2);
        System.out.println(person.getId());
        System.out.println(person.getName());
        System.out.println(person.getAge());
    }

 

posted @ 2018-12-06 16:24  谋知  阅读(229)  评论(0编辑  收藏  举报