从零开始学spring cloud(七) -------- Spring Cloud OpenFegin
一、OpenFegin 介绍

Feign是一个声明性的Web服务客户端。 它使编写Web服务客户端变得更容易。 要使用Feign,请创建一个界面并对其进行注释。 它具有可插入的注释支持,包括Feign注释和JAX-RS注释。 Feign还支持可插拔编码器和解码器。 Spring Cloud增加了对Spring MVC注释的支持,并使用了Spring Web中默认使用的相同HttpMessageConverters。 Spring Cloud集成了Ribbon和Eureka,在使用Feign时提供负载均衡的http客户端。
二、将OpenFegin应用到项目中
添加依赖到项目中:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
新建一个FeignClient类
package com.zwjk.cloud.fegin; import com.zwjk.cloud.entity.User;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody; /**
* @author : Jixiaohu
* @Date : 2019-04-12.
* @Time : 16:50.
* @Description :
*/
@FeignClient("microservice-provider-user")
public interface UserFeignClient {
//@PathVariable得设置value
@GetMapping("/simple/{id}")
User findById(@PathVariable("id") Long id); //@PathVariable得设置value @PostMapping("/user")
User postUser(@RequestBody User user); // 该请求不会成功,只要参数是复杂对象,即使指定了是GET方法,feign依然会以POST方法进行发送请求。
@GetMapping("/get-user")
User getUser(User user);
}
在movie的controller中。使用feign调用user微服务提供的接口
package com.zwjk.cloud.controller; import com.zwjk.cloud.entity.User;
import com.zwjk.cloud.fegin.UserFeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate; /**
* @author : Jixiaohu
* @Date : 2019-04-11.
* @Time : 9:38.
* @Description :
*/
@RestController
public class MovieController { @Autowired
private UserFeignClient userFeignClient; @GetMapping("/movie/{id}")
public User findById(@PathVariable Long id) {
return this.userFeignClient.findById(id);
} @GetMapping("/test")
public User testPost(User user) {
return this.userFeignClient.postUser(user);
} @GetMapping("/test-get")
public User testGet(User user) {
return this.userFeignClient.getUser(user);
} }
package com.zwjk.cloud.controller; import com.zwjk.cloud.entity.User;
import com.zwjk.cloud.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*; /**
* @author : Jixiaohu
* @Date : 2019-04-11.
* @Time : 9:08.
* @Description :
*/
@RestController
public class UserController { @Autowired
private UserRepository userRepository; @GetMapping("/simple/{id}")
public User findById(@PathVariable Long id) {
return this.userRepository.getUserById(id);
} @PostMapping("/user")
public User postUser(@RequestBody User user) {
return user;
} // 该请求不会成功
@GetMapping("/get-user")
public User getUser(User user) {
return user;
}
}
访问地址:http://localhost:8010/movie/1
查看结果返回:

这边需要注意的是,在feignClient中,注释表明的说明。
这里,就简单实现了使用feignClient调用服务接口。
下面看一下,如何复写默认的FeignClientsConfihuration
https://cloud.spring.io/spring-cloud-static/spring-cloud-openfeign/2.1.0.RELEASE/single/spring-cloud-openfeign.html#spring-cloud-feign-overriding-defaults
官方文档中,有这样的说明

Spring Cloud的Feign支持的核心概念是指定客户端的概念。 每个feign客户端都是一组组件的一部分,这些组件一起工作以按需联系远程服务器,并且该集合具有一个名称,您可以使用@FeignClient注释将其作为应用程序开发人员提供。 Spring Cloud使用FeignClientsConfiguration按需为每个命名客户端创建一个新的集合作为ApplicationContext。 这包含(除其他外)feign.Decoder,feign.Encoder和feign.Contract。 可以使用@FeignClient批注的contextId属性覆盖该集合的名称。
Spring Cloud允许您通过使用@FeignClient声明其他配置(在FeignClientsConfiguration之上)来完全控制假装客户端。 例:
@FeignClient(name = "stores", configuration = FooConfiguration.class)
public interface StoreClient {
//..
}

这边有个警告,跟ribbon的自定义配置一样,自定义的配置,需要放在扫描包的外部,查看一下怎么写,https://github.com/OpenFeign/feign
下面看一下代码:
package com.zwjk.cloud.fegin; import com.zwjk.cloud.entity.User;
import com.zwjk.config.UserConfiguration;
import feign.Param;
import feign.RequestLine;
import org.springframework.cloud.openfeign.FeignClient; /**
* @author : Jixiaohu
* @Date : 2019-04-12.
* @Time : 16:50.
* @Description :
*/
@FeignClient(name = "microservice-provider-user", configuration = UserConfiguration.class)
public interface UserFeignClient {
//@PathVariable得设置value
@RequestLine("GET /simple/{id}")
User findById(@Param("id") Long id); //@PathVariable得设置value }
package com.zwjk.config; import feign.Contract;
import feign.Logger;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; /**
* @author : Jixiaohu
* @Date : 2019-04-13.
* @Time : 10:20.
* @Description :
*/
@Configuration
public class UserConfiguration { @Bean
public Contract feignContract() {
return new feign.Contract.Default();
} @Bean
Logger.Level feignLoggerLevel() {
return Logger.Level.FULL;
}
}
其他的不需要修改,下面看一下目录结构:

UserConfiguretion需要放在外面。
从零开始学spring cloud(七) -------- Spring Cloud OpenFegin的更多相关文章
- Spring系列(七) Spring MVC 异常处理
Servlet传统异常处理 Servlet规范规定了当web应用发生异常时必须能够指明, 并确定了该如何处理, 规定了错误信息应该包含的内容和展示页面的方式.(详细可以参考servlet规范文档) 处 ...
- spring学习七 spring和dynamic project进行整合
spring和web项目进行整合,其实就是在项目启动时,就创建spring容器,然后在servlet中使用spring容器进行开. 注意:为了页面可以访问到servlet,因此servlet必须放进t ...
- Spring学习(七)-----Spring Bean的5种作用域
在Spring中,bean作用域用于确定哪种类型的 bean 实例应该从Spring容器中返回给调用者.bean支持的5种范围域: 单例(singleton) - 每个Spring IoC 容器返回一 ...
- Spring学习(七)--Spring MVC的高级技术
一.Spring MVC配置的替代方案 我们已经了解如何通过AbstractAnnotationConfigDispatcherServlet- Initializer快速搭建了Spring MVC环 ...
- 从零开始学Python第七周:面向对象进阶(需修改)
一,类的属性 (1)示例 通过属性获取已经创建对象的个数 class Plane: pCount = 0 #类属性 def __init__(self,name,category): self.nam ...
- spring入门(七) spring mvc+mybatis+generator
1.Mybatis-Generator下载 地址:https://github.com/mybatis/generator/releases 我使用的是 mybatis-generator-core- ...
- 从零开始学Kotlin第七课
1.强制类型转换需要在后面加两个感叹号 2.如果需要在java代码调用kotlin的方法时候使用文件名+kt.方法 3.object 类名 是创建匿名内部类的写法 调用 传入class对象 4.在to ...
- 跟我学SpringCloud | 第七篇:Spring Cloud Config 配置中心高可用和refresh
SpringCloud系列教程 | 第七篇:Spring Cloud Config 配置中心高可用和refresh Springboot: 2.1.6.RELEASE SpringCloud: Gre ...
- 从零开始学spring cloud(十一) -------- hystrix监控
一.官方文档阅读 服务启动后,可以通过/health和hystrix.stream查看效果,实际上,访问上述两个地址,会出现404,这是因为spring boot版本的问题, 我在这里使用的sprin ...
随机推荐
- 无外接键盘安装 raspberry pi 3B+ 安装系统
从官网介绍看,当前raspbian和以前大家的记录略有不同,老的博客资料基本都是介绍下载raspbian,但现在raspbian已经不再维护镜像, raspbian系统开始由官方 pi foundat ...
- SOCKET选项
1. IP_TRANSPARENT [1]socket设置该选项后,可以处理发往非本机的数据包. [2]使用流程: 配置防火墙和路由: iptables -t mangle -A PREROUTING ...
- Emacs下scheme编程环境的设置
Scheme编程环境搭建 1.1 安装Chez Scheme git clone https://github.com/cisco/ChezScheme.git cd ChezScheme ./con ...
- TCP流量控制
TCP的流量控制,是为了更好的传输数据,控制流量不要发送太快而至于接收端没有足够的缓存的接收. 利用滑动窗口,可以很方便的控制传输 rwnd:可以控制接收窗口大小.ACK代表确认位,ack代表确认字段 ...
- Java学习笔记——鸵鸟学习记(三)
8,对象的创建与销毁 a, 构造方法——在构造对象的时候同时传入相关的属性 用于构造对象的方法(当创建对象时调用的方法) 规则:1)方法名与类名相同 2)无返回值 package my; public ...
- uni-app 调用支付宝支付
本文讲解 uni-app如何调用支付宝进行支付,服务端为 .net编写. 客户端:uni-app 编写 1.根据服务端生成的订单信息发起支付. 服务端:.net 编写 1.生成订单信息.2.接收支 ...
- js Base64 转化成图片格式
function dataURLtoFile(dataurl, filename = 'file') { let arr = dataurl.split(',') let mime = arr[0]. ...
- 实战ELK(8) 安装ElasticSearch中文分词器
安装 方法1 - download pre-build package from here: https://github.com/medcl/elasticsearch-analysis-ik/re ...
- iOS解决cell重用问题
在写sina 微博界面的过程中使用到了cell,那么就是在cell上添加一些控件,但是由于每条微博的内容都是不同的,所以在显示的过程中,出现了内容重叠的问题,其实就是UITableViewCell重用 ...
- 文件系统扫描工具-fsck
文件系统扫描工具-fsck 注意的是fsck扫描文件系统时一定要在单用户模式.修复模式或把设备umount后进行.建议在单用户模式下运行.如果扫描正常运行中的系统,会造成系统文件损坏. fsck不仅可 ...