Spring Cloud07: Feign 声明式接口调用
一、什么是Feign
Feign也是去实现负载均衡,但是它的使用要比Ribbon更加简化,它实际上是基于Ribbon进行了封装,让我们可以通过调用接口的方式实现负载均衡。Feign和Ribbon都是由Netflix提供的,Feign是一个声明式、模板化的Web Service客户端,它简化了开发者编写Web服务客户端的操作,开发者可以通过简单的接口和注解来调用HTTP API,使得开发变得更加简化、快捷。Spring Cloud Feign也是基于Netflix Feign的二次开发,它整合了Ribbon和Hystrix,具有可插拔、基于注解、负载均衡、服务熔断等一系列的便捷功能,也就是说我们在实际开发中可以用Feign来取代Ribbon。
相比较于Ribbon+RestTemplate的方式,Feign大大简化了代码的开发,Feign支持多种注解,包括Feign注解、JAX-RS注解、Spring MVC注解等,Spring Cloud对Feign进行了优化,整合了Ribbon和Eureka,从而让Feign使用更加方便。
二、Ribbon和Feign的区别
Ribbon是一个通用的HTTP客户端工具,Feign是基于Ribbon实现的。
三、Feign的优点
1.Feign是一个声明式的Web Service客户端。
2.支持Feign注解、Spring MVC注解、JAX-RS注解
3.Feign是基于Ribbon实现,使用起来更加方便
4.Feign集成了Hystrix,具备服务熔断的功能
四、实战!
1.创建Module,配置pom.xml如下:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<version>2.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
<version>2.0.2.RELEASE</version>
</dependency>
2.创建配置文件application.yml,配置如下:
server:
port: 8050
spring:
application:
name: feign
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka
instance:
prefer-ip-address: true
3.创建启动类,代码如下:
package com.zing;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;
@SpringBootApplication
@EnableFeignClients
public class FeignApplication {
public static void main(String[] args) throws Exception {
SpringApplication.run(FeignApplication.class, args);
}
}
注解说明:
* @EnableFeignClients:声明其为Feign客户端
4.创建声明式接口,代码如下:
package com.zing.feign;
import java.util.Collection;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import com.zing.entity.Student;
@FeignClient(value = "provider")
public interface IFeignService {
@GetMapping("/student/findAll")
public Collection<Student> findAll();
@GetMapping("/student/index")
public String index();
}
5.Handler代码如下:
package com.zing.controller;
import java.util.Collection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.zing.entity.Student;
import com.zing.feign.IFeignService;
@RestController
@RequestMapping("/feign")
public class FeignHandler {
@Autowired
private IFeignService feignservice;
@GetMapping("/findAll")
public Collection<Student> findAll(){
return feignservice.findAll();
}
@GetMapping("/index")
public String index() {
return feignservice.index();
}
}
6.测试feign的负载均衡功能
(1)分别启动注册中心,两个不同端口的服务提供者,feign,注册中心页面如下:

(2)多次访问 http://localhost:8050/feign/index ,我们可以看到两个端口地址交替出现,证明Feign实现了负载均衡。如下图:


7.测试Feign的熔断机制
(1)我们先停掉所有的服务提供者,只保留注册中心和Feign的服务,打开注册中心如下图:

(2)再次访问 http://localhost:8050/feign/index 可看到如下图的内容:

(3)为了不直接暴露错误信息,我们需要添加服务熔断机制,修改application.yml如下:
server:
port: 8050
spring:
application:
name: feign
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka
instance:
prefer-ip-address: true
feign:
hystrix:
enabled: true
配置说明:
* feign.hystrix.enable:是否开启熔断机制,默认false。
(4)创建IFeignService的实现类FeignServiceImpl,在里面定义容错处理机制,通过@Component注解将FeignServiceImpl实例注入到IOC容器中,代码如下:
package com.zing.feign.impl;
import java.util.Collection;
import org.springframework.stereotype.Component;
import com.zing.entity.Student;
import com.zing.feign.IFeignService;
@Component
public class FeignServiceImpl implements IFeignService{
@Override
public Collection<Student> findAll() {
return null;
}
@Override
public String index() {
return "服务器维护中。。。";
}
}
(5)在IFeignService接口定义处定义@FeignClient的fallback属性来做降级处理,设置映射,映射到FeignServiceImpl中去。修改IFeignService后代码如下:
package com.zing.feign;
import java.util.Collection;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import com.zing.entity.Student;
import com.zing.feign.impl.FeignServiceImpl;
@FeignClient(value = "provider",fallback = FeignServiceImpl.class)
public interface IFeignService {
@GetMapping("/student/findAll")
public Collection<Student> findAll();
@GetMapping("/student/index")
public String index();
}
(6)重复(1)、(2)步,出现以下界面,证明服务熔断机制起效。如图:

五、总结
我们在本次的代码中,采用Feign实现了负载均衡和服务熔断。
Spring Cloud07: Feign 声明式接口调用的更多相关文章
- Spring Cloud Feign 声明式服务调用
目录 一.Feign是什么? 二.Feign的快速搭建 三.Feign的几种姿态 参数绑定 继承特性 四.其他配置 Ribbon 配置 Hystrix 配置 一.Feign是什么? 通过对前面Sp ...
- Spring Cloud Feign声明式服务调用(转载)+遇到的问题
转载:原文 总结: 1.pom添加依赖 2.application中填写正确的eureka配置 3.启动项中增加注解 @EnableFeignClients 4.填写正确的调用接口 通过原文使用Fei ...
- 笔记:Spring Cloud Feign 声明式服务调用
在实际开发中,对于服务依赖的调用可能不止一处,往往一个接口会被多处调用,所以我们通常会针对各个微服务自行封装一些客户端类来包装这些依赖服务的调用,Spring Cloud Feign 在此基础上做了进 ...
- spring cloud 系列第4篇 —— feign 声明式服务调用 (F版本)
源码Gitub地址:https://github.com/heibaiying/spring-samples-for-all 一.feign 简介 在上一个用例中,我们使用ribbon+restTem ...
- Spring Cloud 2-Feign 声明式服务调用(三)
Spring Cloud Feign 1. pom.xml 2. application.yml 3. Application.java 4. Client.java 简化RestTemplate调 ...
- 聊一聊声明式接口调用与Nacos的结合使用
背景 对于公司内部的 API 接口,在引入注册中心之后,免不了会用上服务发现这个东西. 现在比较流行的接口调用方式应该是基于声明式接口的调用,它使得开发变得更加简化和快捷. .NET 在声明式接口调用 ...
- Feign声明式服务调用
Feign是一种声明式.模板化的HTTP客户端(仅在Application Client中使用).声明式调用是指,就像调用本地方法一样调用远程方法,无需感知操作远程http请求. Spring Clo ...
- SpringCloud微服务实战二:Spring Cloud Ribbon 负载均衡 + Spring Cloud Feign 声明式调用
1.Spring Cloud Ribbon的作用 Ribbon是Netflix开发的一个负载均衡组件,它在服务体系中起着重要作用,Pivotal将其整合成为Spring Cloud Ribbon,与其 ...
- SpringCloud实战-Feign声明式服务调用
在前面的文章中可以发现当我们通过RestTemplate调用其它服务的API时,所需要的参数须在请求的URL中进行拼接,如果参数少的话或许我们还可以忍受,一旦有多个参数的话,这时拼接请求字符串就会效率 ...
随机推荐
- HackingLab基础关
目录 1:Key在哪里? 2:再加密一次你就得到key啦~ 3:猜猜这是经过了多少次加密? 4:据说MD5加密很安全,真的是么? 5:种族歧视 6:HAHA浏览器 7:key究竟在哪里呢? 8:key ...
- 神经网络与机器学习 笔记—Rosenblatt感知器收敛算法C++实现
Rosenblatt感知器收敛算法C++实现 算法概述 自己用C++实现了下,测试的例子和模式用的都是双月分类模型,关于双月分类相关看之前的那个笔记: https://blog.csdn.net/u0 ...
- 深入浅出带你玩转sqlilabs(五)-布尔/延时盲注与二次注入
SQL测试-基于布尔,延时盲注 布尔,延时注入常用到的判断语句 regexp regexp '^xiaodi[a-z]' 匹配xiaodi及xiaodi...等 if if(条件,5,0) 条件成立 ...
- 【Git】3. Git重要特性-分支操作,合并冲突详解
一.分支介绍 在版本控制过程当中,有时候需要同时推进多个任务,这样的话,就可以给每个任务创建单独的分支. 有了分支之后,对应的开发人员就可以把自己的工作从主线上分离出来,在做自己分支开发的时候,不会影 ...
- (邹博ML)凸优化
目录 凸集的基本概念 凸函数的基本概念 凸优化的一般提法 凸集基本概念 思考两个不能式 两个正数的算术平均数大于等于几何平均数 给定可逆对称阵Q,对于任意向量x,y,有: 思考凸集和凸函数 在机器学习 ...
- FTP主动模式(PORT)与被动模式(PASV)
待完善 参考文章0 参考文章1
- SpringBoot 项目 部署 jar方式
SpringBoot部署-jar方式 步骤1部署方式 Springboot 和我们之前学习的web 应用程序不一样,其本质上是一个 Java 应用程序,那么又如何部署呢? 通常来说,Springbo ...
- vue-router的几种用法
1.全局路由守卫 router.beforeEach((to, from, next) => { // ... }) 当一个导航触发时,全局前置守卫按照创建顺序调用.守卫是异步解析执行,此时导航 ...
- 【BUAA 软工热身作业】继往开来,勇攀高峰
项目 内容 课程:2020春季软件工程课程博客作业(罗杰,任健) 博客园班级链接 作业:热身作业,阅读并撰写博客 作业要求 课程目标 学习大规模软件开发的技巧与方法,锻炼开发能力 作业目标 学习前辈经 ...
- 如何提高CRM系统使用率?
随着时代的发展和市场的变化,客户在企业的眼中开始变得越来越重要.谁拥有更多的客户,谁就能在激烈的市场竞争中占据一席之地.现在很多企业通过CRM系统转变为了"以客户为中心".但是,许 ...