Feign【入门】
feign简介:
feign是一种声明式,模板化的HTTP客户端,spring cloud对feign进行了增强,使其支持SpringMvc的相关注解,并整合了ribbon做负载均衡。在spring cloud中使用feign做HTTP远程服务请求,可以做到就像调用本地方法一样,完全感知不到是在调用远程方法,具体特性如下:
- 可插拔的注解支持,包括feign注解和Jax-rs注解、
- 支持可插拔的HTTP编码器和解码器、
- 支持hystrix和它的fallback、
- 支持ribbon负载均衡、
- 支持HTTP请求和响应的压缩、
1、创建feign-demo工程
1.1、工程依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
<relativePath/>
</parent> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
</properties> <dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Spring Cloud OpenFeign的Starter的依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
1.2、工程启动类:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients; @SpringBootApplication
@EnableFeignClients
public class SpringCloudFeignApplication { public static void main(String[] args) {
SpringApplication.run(SpringCloudFeignApplication.class, args);
}
}
可以看到启动类上加了 @EnableFeignClients 注解,意思是当该工程在启动的时候,会进行包扫描,扫描该启动类包以下,子包中所有带 @FeignClient 注解的类(包括启动类所在包),并进行处理。
1.3、编写相关代码
HelloFeignService接口:
import cn.springcloud.book.feign.config.HelloFeignServiceConfig;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod; //https://api.caiyunapp.com/v2/TAkhjf8d1nlSlspN/121.6544,25.1552/forecast.json 彩云天气API
@FeignClient(name = "caiyunapp", url = "https://api.caiyunapp.com/v2/TAkhjf8d1nlSlspN/121.6544,25.1552", configuration = HelloFeignServiceConfig.class)
public interface HelloFeignService { @RequestMapping(value = "/forecast.json", method = RequestMethod.GET)
String searchRepo(); }
如上所示,@FeignClient注解手动指定了URL,最终会根据指定的URL和@RequestMapping对应的方法转换成完整的请求地址。如下:https://api.caiyunapp.com/v2/TAkhjf8d1nlSlspN/121.6544,25.1552/forecast.json
HelloFeignServiceConfig配置类:
import feign.Logger;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; @Configuration
public class HelloFeignServiceConfig { /**
* Logger.Level 的具体级别如下:
* NONE:不记录任何信息
* BASIC:仅记录请求方法、URL以及响应状态码和执行时间
* HEADERS:除了记录 BASIC级别的信息外,还会记录请求和响应的头信息
* FULL:记录所有请求与响应的明细,包括头信息、请求体、元数据
*
* @return
*/
@Bean
Logger.Level feignLoggerLevel() {
return Logger.Level.FULL;
} }
controller类:
import cn.springcloud.book.feign.service.HelloFeignService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController; @RestController
public class HelloFeignController { @Autowired
private HelloFeignService helloFeignService; // 服务消费者对位提供的服务
@GetMapping(value = "/search")
public String searchGithubRepoByStr(@RequestParam("str") String queryStr) {
return helloFeignService.searchRepo(queryStr);
} }
如上所示,controller类中注入了上面编写的接口类,直接调用了相关方法。
2、启动工程
2.1、执行命令:
mvn spring-boot:run
2.2、访问:losthost:8080/search/

说明访问成功!
Feign【入门】的更多相关文章
- 客户端负载均衡Feign之一:申明式服务调用Feign入门示例
Spring Cloud提供了Ribbon和Feign作为客户端的负载均衡. 前面使用了Ribbon做客户端负载均衡,使用Hystrix做容错保护,这两者被作为基础工具类框架被广泛地应用在各个微服务的 ...
- spring cloud 学习(3) - feign入门
feign 是一个让rest服务调用更简洁的开源项目,很多介绍文章或书也称它为声明式REST调用.传统的web service中,通过引用wsdl来自动生成一些client的代理类(或stub代码), ...
- Feign快速入门
一.Feign简介1.Feign是一个声明式的web服务客户端,使用Feign编写web服务客户端更加容易2.具有可插拔注解支持,包括Feign注解和JAX-RS注解,还支持可插拔的编码器与解码器3. ...
- SpringCloud之Feign声明式调用原理及配置
1 什么是Feign Feign是一种声明式.模板化的HTTP客户端(仅在Application Client中使用).声明式调用是指,就像调用本地方法一样调用远程方法,无需感知操作远程http请求. ...
- Feign声明式服务调用
Feign是一种声明式.模板化的HTTP客户端(仅在Application Client中使用).声明式调用是指,就像调用本地方法一样调用远程方法,无需感知操作远程http请求. Spring Clo ...
- spring cloud 入门系列五:使用Feign 实现声明式服务调用
一.Spring Cloud Feign概念引入通过前面的随笔,我们了解如何通过Spring Cloud ribbon进行负责均衡,如何通过Spring Cloud Hystrix进行服务断路保护,两 ...
- Spring Cloud 入门教程(六): 用声明式REST客户端Feign调用远端HTTP服务
首先简单解释一下什么是声明式实现? 要做一件事, 需要知道三个要素,where, what, how.即在哪里( where)用什么办法(how)做什么(what).什么时候做(when)我们纳入ho ...
- springcloud 入门 5 (feign源码分析)
feign:(推荐使用) Feign是受到Retrofit,JAXRS-2.0和WebSocket的影响,它是一个jav的到http客户端绑定的开源项目. Feign的主要目标是将Java Http ...
- Spring Cloud 入门 之 Feign 篇(三)
原文地址:Spring Cloud 入门 之 Feign 篇(三) 博客地址:http://www.extlight.com 一.前言 在上一篇文章<Spring Cloud 入门 之 Ribb ...
- 从实例一步一步入门学习SpringCloud的Eureka、Ribbon、Feign、熔断器、Zuul的简单使用(附代码下载)
场景 SpringCloud -创建统一的依赖管理: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/102530574 Sprin ...
随机推荐
- codeforces 761 D. Dasha and Very Difficult Problem(二分+贪心)
题目链接:http://codeforces.com/contest/761/problem/D 题意:给出一个长度为n的a序列和p序列,求任意一个b序列使得c[i]=b[i]-a[i],使得c序列的 ...
- 在windows上,使用虚拟机安装苹果操作系统
以下是我这两天安装这个苹果操作系统时,所看的文档,集合.已经成功,再次做一个摘录. 分别看了一下几个链接: http://www.bubuko.com/infodetail-2257390.html ...
- 树莓派的Respbian或者ubuntu系统下安装opencv最有效的方法
第一种方法当然首选和其他安装包相同的方法pip install opencv-python安装失败后果断选择第二种方法,这第二种方法尝试过很多次了,屡试不爽 第二种方法:sudo apt-get in ...
- js 数组方法的作用,各方法是否改变原有的数组
不会改变原来数组的有: concat()---连接两个或更多的数组,并返回结果. every()---检测数组元素的每个元素是否都符合条件. some()---检测数组元素中是否有元素符合指定条件. ...
- 【LeetCode】DFS 总结
DFS(深度优先搜索) 常用来解决可达性的问题. 两个要点: 栈:用栈来保存当前节点信息,当遍历新节点返回时能够继续遍历当前节点.可以使用递归栈. 标记:和 BFS 一样同样需要对已经遍历过的节点进行 ...
- eclipse中注释模板使用
步骤: Window -->Preferences-->Java-->Code Style -->Code Templates-->Comments 1.Files可对整 ...
- FreeSql (十八)导航属性
导航属性是 FreeSql 的特色功能之一,可通过约定配置.或自定义配置对象间的关系. 导航属性有 OneToMany, ManyToOne, ManyToMany, OneToOne, Parent ...
- Winform中自定义xml配置文件,并配置获取文件路径
场景 在Winform程序中,需要将一些配置项存到配置文件中,这时就需要自定义xml的配置文件格式.并在一些工具类中去获取配置文件的路径并加载其内容. 关注公众号霸道的程序猿获取编程相关电子书.教程推 ...
- 手把手教你搭建Jenkins实现自动化部署Jar
centeros7 安装配置环境jdk1.8 1.先卸载centeros自带jdk rpm -qa | grep openjdk 查询出来的自带的openjdk 2.删除 rpm -e --nodep ...
- 阿里云CentOS7.3服务器通过Docker安装Nginx
前言 小编环境: 阿里云CentOS7.3服务器 docker 下面分享一次小编在自己的阿里云CentOS7.3服务器上使用Docker来安装Nginx的一次全过程 温馨小提示: 如果只是希望单纯使用 ...