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());
}
Feign 第一个Feign程序 一的更多相关文章
- 撸了一个 Feign 增强包 V2.0 升级版
前言 大概在两年前我写过一篇 撸了一个 Feign 增强包,当时准备是利用 SpringBoot + K8s 构建应用,这个库可以类似于 SpringCloud 那样结合 SpringBoot 使用声 ...
- Exception:public class feign.codec.EncodeException feign.codec.EncodeException: 'Content-Type' cannot contain wildcard type '*'
一.异常出现的场景 Spring Cloud 服务A通过feign调用服务B;之前是好好的,但今天突然就不好了,抛以下异常===> 出现原因补充,Spring Boot默认的JSON方式 Ja ...
- 客户端负载均衡Feign之四:Feign配置
Ribbon配置 在Feign中配置Ribbon非常简单,直接在application.properties中配置即可,如: # 设置连接超时时间 ribbon.ConnectTimeout=500 ...
- SpringCloud全家桶学习之Feign负载均衡----Feign(四)
一.Feign概述 (1)Feign是什么? 官网地址:https://projects.spring.io/spring-cloud/spring-cloud.html#spring-cloud-f ...
- DirectX游戏编程(一):创建一个Direct3D程序
一.环境 Visual Studio 2012,DirectX SDK (June 2010) 二.准备 1.环境变量(如没有配置请添加) 变量名:DXSDK_DIR 变量值:D:\Software\ ...
- 第一个python程序
一个python程序的两种执行方式: 1.第一种方式是通过python解释器: cmd->python->进入python解释器->编写python代码->回车. 2.第二种方 ...
- 编写第一个MapReduce程序—— 统计气温
摘要:hadoop安装完成后,像学习其他语言一样,要开始写一个“hello world!” ,看了一些学习资料,模仿写了个程序.对于一个C#程序员来说,写个java程序,并调用hadoop的包,并跑在 ...
- 1.3 第一个C#程序
几乎没一门编程语言的第一个程序都叫“你好,世界”,所以先在visual studio 中创建一个Helloworld程序. 各部分的详细内容: Main方法是程序运行的起点,最重要的代码就写在Main ...
- 一个.net程序员的安卓之旅-Eclipse设置代码智能提示功能
一个.net程序员的安卓之旅-代码智能提示功能 过完年回来就决心开始学安卓开发,就网上买了个内存条加在笔记本上(因为笔记本原来2G内存太卡了,装了vs2010.SQL Server 2008.orac ...
随机推荐
- 让Spring不再难懂-aop篇
什么是aop AOP(Aspect-OrientedProgramming,面向方面编程),可以说是OOP(Object-Oriented Programing,面向对象编程)的补充和完善.OOP允许 ...
- 用go写爬虫服务并发请求,限制并发数
java写爬虫服务,思路是线程池,任务队列,限制并行线程数即可. go要用另一种设计思路,不能在线程层面限制,协程的异步请求,如果不作处理,并行发出所有网络请求,因网络请求数过多,会抛出异常 低版本的 ...
- z-index优先级小结
z-index是深度属性,设置元素在z轴上面的堆叠顺序. 强调:z-index必须和定位元素position:absollute|relative|fixed一起使用,否则无效 1.z-index属性 ...
- mysql简介/安装以及破解密码等
1.什么是数据库: 数据库即存放数据的仓库,只不过这个仓库是在计算机存储设备上,而且数据是按一定的格式存放的 过去人们将数据存放在文件柜里,现在数据量庞大,已经不再适用 数据库是长期存放在计算机内.有 ...
- 判断两个数组是否相似 (arraysSimilar)
题目 解答 思路 具体实现代码 总结 题目 题目来自 慕课网 JavaScript 深入浅出 1-6 编程练习 请在 index.html 文件中,编写 arraysSimilar 函数,实现判断传入 ...
- ionic2踩坑之兼容android4.3及以下版本
一个命令就行了 ionic plugin add cordova-plugin-crosswalk-webview --save 执行完之后重新打包. 但是如果要兼容4.0及以下的话.... 帮不了你 ...
- 史上最全Java面试题全集
2013年年底的时候,我看到了网上流传的一个叫做<Java面试题大全>的东西,认真的阅读了以后发现里面的很多题目是重复且没有价值的题目,还有不少的参考答案也是错误的,于是我花了半个月时间对 ...
- sql性能优化浅谈
sql性能优化总结: 最近随着数据越来越多,数据库性能问题暴露的越来越严重.几百万,上千万,甚至过亿的数据处理速度会非常的慢. 下面对工作中遇到的问题做下总结,希望以后能对日后的工作有所帮助. 不同的 ...
- MyBatis学习总结之一对多映射
1.首先创建2张表:students 和grades create table grades( gid ) primary key, gname varchar() ); create table s ...
- python ftp sftp
ftp 上传下载文件 12345678910111213141516171819202122232425262728293031323334 from ftplib import FTPimport ...