Spring Cloud Alibaba-整合Feign调用接口

echo编辑整理,欢迎转载,转载请声明文章来源。欢迎添加echo微信(微信号:t2421499075) 交流学习。


该文章是系列文章,如果不熟悉西SpringCloud的,还是需要先看看前面的文章。

当我们通过前面的学习,完成了服务注册与发现之后,我们发现我们的通信方式稍微有点复杂,所以这里单独讲讲Feign的整合。这里基于《Spring Cloud Alibaba-接入Nacos注册中心了解服务注册与发现》文章中的项目

添加相关的依赖

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

<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-dependencies</artifactId>
	<version>Hoxton.SR9</version>
	<type>pom</type>
	<scope>import</scope>
</dependency>

调整consumer项目的接口调用方式

  • 第一步:在启动类上面添加如下注解
@EnableFeignClients

添加Feign接口类

该接口类对应的接口,就是我们服务提供者里面的方法

@FeignClient("spring-cloud-nacos-server")
public interface TestClient {

    @GetMapping("/test")
    public String test();

}

改写从consumer中test的调用方式

@RestController
public class Test {

    @Autowired
    private TestClient testClient;

    @GetMapping("/test")
    public String test() {
        String result = testClient.test();
        return "消费者端调用提供者端返回消息:" + result;
    }

}

启动服务,使用工具调用,结果如下

在这里插入图片描述

  • 上一篇:Spring Cloud Alibaba-接入Nacos注册中心了解服务注册与发现
  • 下一篇:Spring Cloud Alibaba-使用nacos做注册中心
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

xlecho

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值