spring cloud学习(四) Fegin 的使用
来源:互联网 发布:java大数据量并发处理 编辑:程序博客网 时间:2024/05/16 10:40
Feign 的使用
什么是Feign?
Feign : Declarative REST clients。
Feign 是一个声明web服务客户端,这便得编写web服务客户端更容易,使用Feign 创建一个接口并对它进行注解,它具有可插拔的注解支持包括Feign注解与JAX-RS注解,Feign还支持可插拔的编码器与解码器,Spring Cloud 增加了对 Spring MVC的注解,Spring Web 默认使用了HttpMessageConverters
, Spring Cloud 集成 Ribbon 和 Eureka 提供的负载均衡的HTTP客户端 Feign。(来源于Spring Cloud Netflix 官网文档)
- Feign 在 github 上的开源文档 : https://github.com/OpenFeign/feign/wiki
怎么使用Feign
首先继续使用上次的”服务注册中心”与”服务提供者”。接下来,就是将 Fegin 整合到”服务消费者”中。
引入依赖
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-feign</artifactId> </dependency>
注入注解
在 Spring Boot 主类加入注解@EnableFeignClients
开启 Fegin 功能。
注入接口
创建接口,具体如下:
@Component@FeignClient(value = "my-service")public interface ServiceClient { @RequestMapping("/service") String printf();}
- 使用
@Component
注解向 Spring Boot 中注入该组件。 - 使用
@FeignClient("")
注解来绑定该接口对应的服务 - 通过 Spring MVC 的注解来配置服务下的具体实现。
在Controller中调用接口
@RestControllerpublic class ConsumerController { @Autowired private ServiceClient serviceClient; @RequestMapping("/test") public String test(){ return serviceClient.printf(); }}
最后,启动”注册中心”、”服务提供者”、”服务消费者”等工程。访问 http://localhost:2222/test 就可以在”服务提供者”控制台查看到负载均衡的实现。
注意事项
注意注意注意
当Spring Cloud版本为 Brixton.RELEASE
,会抛出异常:
Attribute ‘value’ in annotation [org.springframework.cloud.netflix.feign.FeignClient] must be declared as an @AliasFor [serviceId], not [name]
解决办法:
将Spring Cloud版本改为 Brixton.SR5
或 Camden.RELEASE
即可。
参考资料
- (大神DD的博客)http://blog.didispace.com/springcloud2/
- (官方文档中文译本)https://springcloud.cc/spring-cloud-netflix-zhcn.html#spring-cloud-feign
个人参考项目
个人参考项目 : https://github.com/FunriLy/springcloud-study/tree/master/%E6%A1%88%E4%BE%8B3
- spring cloud学习(四) Fegin 的使用
- Spring cloud实战-Fegin简易使用
- spring cloud放弃系列之--4-fegin
- spring cloud 学习(四) Hystrix
- Spring cloud 微服务架构之Ribbon/Fegin连接超时ReadTimeout问题
- [spring cloud学习5]使用docker发布spring cloud应用
- 学习Spring Cloud第七课(Ribbon的基本使用)
- 学习Spring Cloud第七课(Ribbon的基本使用)
- 四、Spring Cloud 的 Feign 组件
- 四、Spring Cloud
- 基于Spring Boot和Spring Cloud实现微服务架构学习(四)-Spring Cloud总结
- 基于Spring Boot和Spring Cloud实现微服务架构学习(四)-Spring Cloud总结
- 基于Spring Boot和Spring Cloud实现微服务架构学习(四)-Spring Cloud总结
- 基于Spring Boot和Spring Cloud实现微服务架构学习(四)-Spring Cloud总结
- 基于Spring Boot和Spring Cloud实现微服务架构学习(四)-Spring Cloud总结
- 基于Spring Boot和Spring Cloud实现微服务架构学习(四)-Spring Cloud总结
- Spring Boot和Spring Cloud微服务架构学习(四)-Spring Cloud总结
- spring cloud bus 和 spring cloud stream 的使用场景
- Cannot create Parameters: [] 异常原因和解决方法
- 行人检测论文笔记:Ten Years of Pedestrian Detection, What Have We Learned?
- Mybatis Generator最详细的配置文件
- HDU 5929 Coconuts(离散化+dfs)
- unity通过改变相机视野实现UI元素放大缩小的效果
- spring cloud学习(四) Fegin 的使用
- oj1946: C语言实验——单个字符输入和输出(顺序结构)
- 基于stm32f407的无线视屏传输项目
- Promise概念和常用api
- 【MyBatis】延迟加载
- 行人检测论文笔记:How Far are We from Solving Pedestrian Detection?
- 知识点:XSS
- 简单计算器,四则运算,小括号,实数,剔除空格,封装calculator类
- build 中的配置说明