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());
}

Feign 第一个Feign程序 一的更多相关文章

  1. 撸了一个 Feign 增强包 V2.0 升级版

    前言 大概在两年前我写过一篇 撸了一个 Feign 增强包,当时准备是利用 SpringBoot + K8s 构建应用,这个库可以类似于 SpringCloud 那样结合 SpringBoot 使用声 ...

  2. Exception:public class feign.codec.EncodeException feign.codec.EncodeException: 'Content-Type' cannot contain wildcard type '*'

    一.异常出现的场景  Spring Cloud 服务A通过feign调用服务B;之前是好好的,但今天突然就不好了,抛以下异常===> 出现原因补充,Spring Boot默认的JSON方式 Ja ...

  3. 客户端负载均衡Feign之四:Feign配置

    Ribbon配置 在Feign中配置Ribbon非常简单,直接在application.properties中配置即可,如: # 设置连接超时时间 ribbon.ConnectTimeout=500 ...

  4. SpringCloud全家桶学习之Feign负载均衡----Feign(四)

    一.Feign概述 (1)Feign是什么? 官网地址:https://projects.spring.io/spring-cloud/spring-cloud.html#spring-cloud-f ...

  5. DirectX游戏编程(一):创建一个Direct3D程序

    一.环境 Visual Studio 2012,DirectX SDK (June 2010) 二.准备 1.环境变量(如没有配置请添加) 变量名:DXSDK_DIR 变量值:D:\Software\ ...

  6. 第一个python程序

    一个python程序的两种执行方式: 1.第一种方式是通过python解释器: cmd->python->进入python解释器->编写python代码->回车. 2.第二种方 ...

  7. 编写第一个MapReduce程序—— 统计气温

    摘要:hadoop安装完成后,像学习其他语言一样,要开始写一个“hello world!” ,看了一些学习资料,模仿写了个程序.对于一个C#程序员来说,写个java程序,并调用hadoop的包,并跑在 ...

  8. 1.3 第一个C#程序

    几乎没一门编程语言的第一个程序都叫“你好,世界”,所以先在visual studio 中创建一个Helloworld程序. 各部分的详细内容: Main方法是程序运行的起点,最重要的代码就写在Main ...

  9. 一个.net程序员的安卓之旅-Eclipse设置代码智能提示功能

    一个.net程序员的安卓之旅-代码智能提示功能 过完年回来就决心开始学安卓开发,就网上买了个内存条加在笔记本上(因为笔记本原来2G内存太卡了,装了vs2010.SQL Server 2008.orac ...

随机推荐

  1. linux 上安装 java

    一.源码安装 1.本地下载 java, 并上传到 linux 上 2.解压文件  tar -zxvf jdk-7u72-linux-i586.gz 3.配置环境变量  vi /etc/profile ...

  2. 5-7 学生cpp成绩统计

    完成“学生cpp成绩计算”之后,修改Person和Student类,各自增加两个无参构造函数. 仍以Person类为基础,建立一个派生类Teacher,增加以下成员数据: int ID;//教师工号 ...

  3. Wanna go back home

    题目描述 Snuke lives on an infinite two-dimensional plane. He is going on an N-day trip. At the beginnin ...

  4. 多对多表创建、forms组件、cookie与session

    多对多表的三种创建方式 1.全自动(较为推荐) 优势:不需要你手动创建第三张表 不足:由于第三张表不是你手动创建的,所以表字段是固定的无法扩展 class Book(models.Model): ti ...

  5. MySQL的详细操作

    MySQL的详细操作 存储引擎 不同的数据应该有不同的处理机制 mysql存储引擎 Innodb:默认的存储引擎 查询速度较myisam慢 但是更安全,支持事务,行锁,外键由于以上的支持,数据更安全, ...

  6. Python:扫描目录下的所有文件

    扫描目录下的所有文件并返回文件的绝对路径 def fileListFunc(filePathList): fileList = [] for filePath in filePathList: for ...

  7. LINQ之路 7:子查询、创建策略和数据转换(要点笔记)

    匿名类型 上面我们自己定义了类型TempProjectionItem来存放查询的结果.通过使用匿名类型,我们可以省去这种中间类型的定义,而由编译器来帮我们完成: select item.Origina ...

  8. [洛谷P3386] [模板] 二分图匹配 (匈牙利算法)

    题目传送门 毒瘤出题人zzk出了个二分图匹配的题(18.10.04模拟赛T2),逼我来学二分图匹配. 网络流什么的llx讲完之后有点懵,还是匈牙利比较好理解(绿与被绿). 对于左边的点一个一个匹配,记 ...

  9. Zabbix-3.0.3实现微信(WeChat)报警

    转自:http://blog.sina.com.cn/s/blog_87113ac20102w7hp.html Zabbix可以通过多种方式把告警信息发送到指定人,常用的有邮件,短信报警方式,但是越来 ...

  10. 将js进行到底:node学习3

    node重要API之NET--TCP编程之旅 废话:最近去了一趟上海会了会一个程序员朋友,途径SNH48握手会,说好我就去看看,没想到握手了王诗蒙,掉入巨坑:塞纳河.回来后边听着<春夏秋冬> ...