SpringCloud-feign组件

方面微服务之间的调用、访问 因为restTemplate使用虽然简单,但是在访问方需要指定被调用方的微服务名、url,这样在开发过程中、后期要进行为期其实非常麻烦。

SpringCloud提供了feign组件解决

用法类似于mybatis里面的mapper

xml sqlsession.xxxList(“com.woniuxy.product.findById”,”1001”)
mapper interface public List findById(int id) mapper.findById()
使用feign的时候也是通过对应的接口API调用就完事,不需要调用方自己去知道对方微服务名、url是什么,只需要通过接口调就行了

①在父工程的pom里面引入feign的依赖 openfeign

<dependency>
		    <groupId>org.springframework.cloud</groupId>
		    <artifactId>spring-cloud-openfeign-core</artifactId>
		    <version>2.1.0.RELEASE</version>
		</dependency>

②在user模块中引入feign的依赖

<dependency>
		    <groupId>org.springframework.cloud</groupId>
		    <artifactId>spring-cloud-starter-openfeign</artifactId>
		    <version>2.1.0.RELEASE</version>
		</dependency>

③在commons(公共)模块引入feign的依赖

<dependency>
		    <groupId>org.springframework.cloud</groupId>
		    <artifactId>spring-cloud-starter-openfeign</artifactId>
		    <version>2.1.0.RELEASE</version>
</dependency>

④被调用方负责在commons模块中去编写接口,内容如下:

在编写接口的时候,可以直接去拷贝handler中的方法,粘贴到接口中,只需要将方法体删除,如果在类上@ReuestMapping指定的前缀url,那么需要给每个方法的url加上该前缀
在这里插入图片描述

⑤在需要使用该接口的地方(user模块的handler中)注入该接口,然后将之前restTemplate调用方式替换掉

在这里插入图片描述

⑥在user模块的启动类上添加注解扫描该接口所在的包(commons中的)

在这里插入图片描述
⑦分别启动eureka、8001、8002、user,浏览器发送url请求
http://localhost/user/all

获得数据,over!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值