Spring Cloud(七):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>
  1. 创建配置文件 application.yml,配置如下:
server:
  port: 8050
spring:
  application:
    name: feign
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka
  instance:
    prefer-ip-address: true
  1. 创建启动类,代码如下:
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 客户端
  1. 创建声明式接口,代码如下:
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();
	}
	
}
  1. 测试 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 实现了负载均衡和服务熔断。了解了 Feign 的配置以及使用的基本原理,那么我们再来仔细学习下 Hystix 容错机制。让我们期待下一篇 Spring Cloud(八):Hystix 容错机制

开发工程师一只,也在不断的学习阶段,平时的小经验不定期分享。 希望看我写的文字的人,可以少走弯路 祝工作学习顺利。
博主经验有限,若有不足,欢迎交流,共同改进~ 愿与同在CSDN的你共同进步。

作者 | 甜蜜的小红薯
出品 | 小红薯

猜你喜欢

转载自blog.csdn.net/qq_36836370/article/details/130901115