八、声明式 REST 客户端-Feign 的使用
来源:互联网 发布:英雄联盟怎么玩 知乎 编辑:程序博客网 时间:2024/06/15 12:57
一、 什么是 Feign?
Feign 是一种声明式、模板化的 HTTP 客户端,在 Spring Cloud 中使用 Feign,可以做到使用 HTTP请求远程服务时能与调用本地方法一样的编码体验,开发者完全感知不到这是远程方法,更感知不到这是个 HTTP 请求。
Feign 的灵感来源于 Retrofit、JAXRS-2.0 和 WebSocket,它使得 Java HTTP 客户端编写更方便,旨在通过最少的资源和代码来实现和 HTTP API 的连接。
二、 引入 jar
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-feign</artifactId> </dependency>
说明:里面的 jar 包含有 spring-cloud-starter-Ribbon,是基于 Ribbon 来实现的
三、 使用
1、 加入注解
@EnableFeignClients
说明:这个是开启 Feign 注解
2、 添加 IuserBiz 接口类
package com.comsys.biz;import org.springframework.cloud.netflix.feign.FeignClient;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;@FeignClient(value = "spring-cloud-provider")public interface IUserBiz { @RequestMapping(value = "/api/user/{id}", method = RequestMethod.GET) String view(@PathVariable(value = "id") int id); }
3、方法请求
package com.comsys.controller;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.RestController;import com.comsys.biz.IUserBiz;@RestController @RequestMapping(value = "/feign/user", method = RequestMethod.POST) public class FeignUserController { @Autowired private IUserBiz userBiz; @RequestMapping(value = "/{id}", method = RequestMethod.GET) public String get(@PathVariable(value = "id") int id) { return userBiz.view(id); } }
阅读全文
0 0
- 八、声明式 REST 客户端-Feign 的使用
- 九、声明式 REST 客户端-Feign 的各种调用
- 十、声明式 REST 客户端-Feign 的高级特性
- SpringCloud 声明式REST客户端Feign
- SpringCloud(十):声明式RESt客户端:Feign
- SpringCloud系列(4)---Feign 声明式REST调用
- 使用Eureka, Ribbon, Feign实现REST服务客户端
- 微服务框架Spring Cloud介绍 Part4: 使用Eureka, Ribbon, Feign实现REST服务客户端
- 使用Feign实现声明式Restful风格调用
- SpringCloud-----2、Eureka实现服务治理(Feign客户端声明式负载均衡调用服务)
- Spring Cloud探路(三)REST 客户端Feign
- 疯狂Spring Cloud连载(10)REST客户端Feign介绍
- 【SpringCloud】(八):认识Feign及使用
- Feign的使用
- SpringBoot -- Feign 声明式web service
- Spring Cloud 声明式服务调用 Feign
- 声明式服务调用:Spring Cloud Feign
- spring cloud Feign(声明式服务调用)
- ajax参数介绍
- 常见HTTP状态码
- linux驱动的入口函数module_init的加载和释放
- 重定向&请求转发
- JavaScript开发面试题
- 八、声明式 REST 客户端-Feign 的使用
- eclipse mybatis 自动生成mapper命令
- Idea的sliksvn导入项目时重复提示输入用户名密码的问题
- 带你玩转Netty(一)
- 石子合并 nyoj737 四边形优化
- HTTP协议:socket处理ajax请求?
- javascript运行机制之执行顺序详解
- 1026. 程序运行时间
- 查找算法学习(3)