springcloud整合feign

在springcloud框架中可以使用Feign客户端调用微服务,那么就要对Feign框架进行整合,首先在项目pom.xml中加入openfeign的依赖。

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

Feign是面向接口的编程,只要对接口使用注解就能完成微服务的调用,底层是通过动态代理实现的。@FeignClient可以声明要调用的微服务名,对接口中声明的方法使用@RequestMapping来映射要调用的微服务接口路径。

@FeignClient("service-provider")
public interface NacosFeignClient {
    @RequestMapping(value = "/echo/{str}",method = RequestMethod.GET)
    String echo(@PathVariable("str") String str);
}

调用时只要注入Feign到属性中,直接调用方法即可。

        @Autowired
        NacosFeignClient nacosFeignClient;

        @RequestMapping(value = "/feign/{str}", method = RequestMethod.GET)
        public String feign(@PathVariable String str) {
            return nacosFeignClient.echo(str);
        }

这样就完成了Feign的整合,接下来就是Feign的底层实现了。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值