Feign声明式远程调用
1简介
Feign是一个声明式的http客户端,它的目的就是让远程调用更简洁。Feign提供了http请求的模板,通过编写简单的接口和插入注解,就可以定义好http请求参数、格式、地址等信息。
Feign整合了Ribbon(负载均衡)和Hystix(服务熔断),可以让我们不再需要显示地使用这两个组件。
SpringCloudFeign在netflixFeign的基础上扩展了对springMVC注解的支持,在其实现下,我们只需创建一个接口并用注解的方式来配置它,即可完成对服务提供方的接口绑定。简化了SpringCloudRibbon自行封装服务调用客户点的开发量。
2使用(远程调用其他服务)
2.1引入依赖
这里要注意spring boot和spring cloud版本兼容的问题。
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
2.2编写接口,告诉springcloud这个接口需要调用远程服务
这里以会员服务member想要调用优惠卷服务coupon的/coupon/coupon/member/list接口为例,需要在member服务中心编写一个接口。


2.3开启远程调用功能
在会员服务的程序入口处开启远程调用功能@EnableFeignClients

2.4测试远程调用功能
在会员服务member的controller中写一个测试方法,调用远程接口

重启mymail-member和mymail-coupon服务,一定要确保在nacos注册中心中这两个服务都正常,在浏览器中输入:http://localhost:8000/member/member/coupons
可以看到请求到优惠卷服务的接口了。

2.5启动过程可能遇到的错误
java: 找不到符号 符号: 方法 setCouponName(java.lang.String) 位置: 类型为com.lyh.mymail.coupon.entity.CouponE
解决方法如下:

网友评论