spring cloud Feign(声明式服务调用)
来源:互联网 发布:淘宝极限词 编辑:程序博客网 时间:2024/06/06 01:25
1、快速入门:
简介:Feign整合了Ribbon和Hystrix,除了提供这两者的强大功能之外,还提供了一种声明式的web服务客户端定义方式。
使用步骤:
1、引入依赖
2、在应用主类上添加@EnableFeignClients注解,如下:
package com.wuyonghu.feign;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.client.discovery.EnableDiscoveryClient;import org.springframework.cloud.netflix.feign.EnableFeignClients;@EnableFeignClients@EnableDiscoveryClient@SpringBootApplicationpublic class FeignApplication { public static void main(String[] args) { SpringApplication.run(FeignApplication.class, args); }}
3、定义一个接口,使用@FeignClient注解来绑定服务:
package com.wuyonghu.feign.service;import org.springframework.cloud.netflix.feign.FeignClient;import org.springframework.web.bind.annotation.RequestMapping;// 使用该注解,表示这里要使用哪个服务提供者@FeignClient("wuyonghu-test-one")public interface HelloService { // 要调用该服务提供者的哪个方法 @RequestMapping("/hello") String hello();}
4、在controller中调用service中的方法:
package com.wuyonghu.feign.controller;import com.wuyonghu.feign.service.HelloService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.RestController;@RestControllerpublic class ConsunerController { @Autowired HelloService helloService; @RequestMapping(value = "/feign-consumer", method = RequestMethod.GET) public String helloCustomer() { return helloService.hello(); }}
解释说明:1、在controller层中调用helloService的hello方法,这里的hellService因为是HelloServer的实现类,而HelloService使用了@FeignClient注解,所以会自动将该注解对应的名字的服务提供者注入到服务中,而因为该服务存在hello的方法,所以这里调用.hello()方法的时候,相当于就调用了名为 wuyonghu-test-one的服务提供者的hello()的方法。
阅读全文
0 0
- Spring Cloud 声明式服务调用 Feign
- 声明式服务调用:Spring Cloud Feign
- spring cloud Feign(声明式服务调用)
- Spring Cloud入门教程(三):声明式服务调用(Feign)
- Spring Cloud中声明式服务调用Feign
- 06.Spring Cloud学习笔记之声明式服务调用组件Feign
- Spring Cloud学习--声明式调用(Feign)
- spring cloud feign 调用服务注意问题
- Spring Cloud学习系列第二章:使用Feign调用服务
- 【Spring Cloud】Feign接口调用
- Spring Cloud(五):服务消费者Feign
- 使用Spring Cloud Feign远程调用
- Spring Cloud之Feign调用token丢失
- 使用Spring Cloud Feign作为HTTP客户端调用远程HTTP服务
- 使用Spring Cloud Feign作为HTTP客户端调用远程HTTP服务
- 使用Spring Cloud Feign作为HTTP客户端调用远程HTTP服务
- 使用Spring Cloud Feign作为HTTP客户端调用远程HTTP服务
- 使用Spring Cloud Feign作为HTTP客户端调用远程HTTP服务
- redis详解-(8)持久化
- iOS 11 安全区域适配总结
- 使用thymeleaf模板出现的问题
- 网址记录
- Mysql密码安全策略
- spring cloud Feign(声明式服务调用)
- Java虚拟机运行时内存配置
- BroadcastReceiver基本概念和使用
- 算法系列15天速成——第五天 五大经典查找【中】
- 微服务项目整合遇到的一些问题
- 二叉树三种遍历方法的递归与非递归实现
- 背景切换效果显示
- 文章标题
- 操作系统笔记之基础