SpringCloud - Feign与Ribbon请求负载均衡实践

本文涉及的产品
传统型负载均衡 CLB,每月750个小时 15LCU
应用型负载均衡 ALB,每月750个小时 15LCU
网络型负载均衡 NLB,每月750个小时 15LCU
简介: SpringCloud - Feign与Ribbon请求负载均衡实践

1】Feign是什么

Feign是一个声明式Web服务客户端。使用Feign能让编写Web服务客户端更加简单,它的使用方法是定义一个接口,然后在上面添加注解,同时也支持JAX-RS标准的注解。


Feign也支持可插拔式的编码器和解码器。SpringCloud对Feign进行了封装,使其支持了SpringMVC标准注解和HttpMessageConverters。Feign可以与Eureka和Ribbon组合使用以支持负载均衡。

官网文档地址:http://projects.spring.io/spring-cloud/spring-cloud.html#spring-cloud-feign

源码地址:https://github.com/OpenFeign/feign


【2】Feign能干什么

Feign旨在使编写Java Http客户端变得更加容易。

前面在使用Ribbon+RestTemplate时,利用了RestTemplate对http请求的封装处理,形成了一套模板化的调用方法。但是在实际开发中,由于对服务依赖的调用可能不止一处,往往一个接口会被多处调用,所以通常都会针对每个微服务自行封装一些客户端类来包装这些依赖服务的调用。

Feign在此基础上做了进一步封装,由他来帮助我们定义和实现依赖服务接口的定义。在Feign的实现下,我们只需创建一个接口并使用注解的方式来配置(比如以前是一个Dao接口上面标注@Mapper注解,现在是一个微服务接口上面标注一个Feign注解即可),即可完成对服务提供方的接口绑定,简化了使用Spring Cloud Ribbon时自动封装服务调用客户端的开发量。

Feign集成了Ribbon

前面我们利用Ribbon维护了MicroServiceCloud-Dept的服务列表信息,并且通过轮询实现了客户端的负载均衡。而与Ribbon不同的是,通过Feign只需要定义服务绑定接口且以声明式的方法,优雅而简单的实现了服务调用。

【3】Feign项目实战

① 创建feign工程

参考microservicecloud-consumer-dept-80项目创建microservicecloud-consumer-dept-feign


② pom中添加对Feign的支持

<dependencies>
  <dependency><!-- 自己定义的api -->
    <groupId>com.web.springcloud</groupId>
    <artifactId>microservicecloud-api</artifactId>
    <version>${project.version}</version>
  </dependency>
  <!-- Cloud-Feign相关 -->
  <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-feign</artifactId>
  </dependency>
  <!-- Cloud-Ribbon相关 -->
  <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-eureka</artifactId>
  </dependency>
  <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-ribbon</artifactId>
  </dependency>
  <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-config</artifactId>
  </dependency>
    <!-- Spring Boot相关 -->
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
  </dependency>
  <!-- 修改后立即生效,热部署相关 -->
  <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>springloaded</artifactId>
  </dependency>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-devtools</artifactId>
  </dependency>
</dependencies>

③ 修改microservicecloud-api Module

pom中添加feign依赖

<!-- Cloud-Feign相关 -->
<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-feign</artifactId>
</dependency>

com.web.springcloud.service包下添加DeptClientService

package com.web.springcloud.service;
import java.util.List;
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;
import com.web.springcloud.entities.Dept;
//绑定服务实例与接口
@FeignClient(value = "MICROSERVICECLOUD-DEPT")//注意这里
public interface DeptClientService{
  @RequestMapping(value = "/dept/get/{id}", method = RequestMethod.GET)
  public Dept get(@PathVariable("id") long id);
  @RequestMapping(value = "/dept/list", method = RequestMethod.GET)
  public List<Dept> list();
  @RequestMapping(value = "/dept/add", method = RequestMethod.POST)
  public boolean add(Dept dept);
}

④ 修改microservicecloud-consumer-dept-feign 的Controller

方法实现交给DeptClientService 远程调用服务提供方。

@RestController
public class DeptController_Consumer
{
  @Autowired
  private DeptClientService service;
  //引入公共Module(api)中的DeptClientService ,直接调用
  @RequestMapping(value = "/consumer/dept/get/{id}")
  public Dept get(@PathVariable("id") Long id)
  {
    return this.service.get(id);
  }
  @RequestMapping(value = "/consumer/dept/list")
  public List<Dept> list()
  {
    return this.service.list();
  }
  @RequestMapping(value = "/consumer/dept/add")
  public Object add(Dept dept)
  {
    return this.service.add(dept);
  }
}

由此可见Feign只是又进一步进行了封装,使得调用服务更加方便。

⑤ 修改microservicecloud-consumer-dept-feign 的主启动类

@SpringBootApplication
@EnableEurekaClient 
@EnableFeignClients(basePackages={"com.web.springcloud"})//启用Feign
@ComponentScan("com.web.springcloud")
public class DeptConsumer80_Feign_App
{
  public static void main(String[] args)
  {
    SpringApplication.run(DeptConsumer80_Feign_App.class, args);
  }
}

⑥ 测试

依次启动服务中心 7001 7002 7003和服务提供者 8001 8002 8003,最后启动microservicecloud-consumer-dept-feign。

浏览器访问http://localhost/consumer/dept/list如下:

默认使用轮询算法,当然此时使用RestTemplate+Ribbon也是可以正常访问。

如下图所示,注册自定义IRule,则可以更改默认轮询算法:


【4】@FeignClient注解

/**
 * Annotation for interfaces declaring that a REST client with that interface should be
 * created (e.g. for autowiring into another component). If ribbon is available it will be
 * used to load balance the backend requests, and the load balancer can be configured
 * using a <code>@RibbonClient</code> with the same name (i.e. value) as the feign client.
 *
 * @author Spencer Gibb
 * @author Venil Noronha
 */
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface FeignClient {
  /**
   * The name of the service with optional protocol prefix. Synonym for {@link #name()
   * name}. A name must be specified for all clients, whether or not a url is provided.
   * Can be specified as property key, eg: ${propertyKey}.
   */
  @AliasFor("name")
  String value() default "";
  /**
   * The service id with optional protocol prefix. Synonym for {@link #value() value}.
   *// 已经过时,使用name替代
   * @deprecated use {@link #name() name} instead
   */
  @Deprecated
  String serviceId() default "";
  /**
   * The service id with optional protocol prefix. Synonym for {@link #value() value}.
   */
  @AliasFor("value")
  String name() default "";
  // 即,ServiceId===name===value
  /**
   * Sets the <code>@Qualifier</code> value for the feign client.
   */
  String qualifier() default "";
  /**
   * An absolute URL or resolvable hostname (the protocol is optional).
   */
  String url() default "";
  /**
   * Whether 404s should be decoded instead of throwing FeignExceptions
   */
  boolean decode404() default false;
  /**
   * A custom <code>@Configuration</code> for the feign client. Can contain override
   * <code>@Bean</code> definition for the pieces that make up the client, for instance
   * {@link feign.codec.Decoder}, {@link feign.codec.Encoder}, {@link feign.Contract}.
   *
   * @see FeignClientsConfiguration for the defaults
   */
  Class<?>[] configuration() default {};
  /**
   * Fallback class for the specified Feign client interface. The fallback class must
   * implement the interface annotated by this annotation and be a valid spring bean.
   */
  Class<?> fallback() default void.class;
  /**
   * Define a fallback factory for the specified Feign client interface. The fallback
   * factory must produce instances of fallback classes that implement the interface
   * annotated by {@link FeignClient}. The fallback factory must be a valid spring
   * bean.
   *
   * @see feign.hystrix.FallbackFactory for details.
   */
  Class<?> fallbackFactory() default void.class;
  /**
   * Path prefix to be used by all method-level mappings. Can be used with or without
   * <code>@RibbonClient</code>.
   */
  String path() default "";
  /**
   * Whether to mark the feign proxy as a primary bean. Defaults to true.
   */
  boolean primary() default true;
}

【5】FeignClientsConfiguration

Feign Client的配置类:

@Configuration
public class FeignClientsConfiguration {
  // 请求/响应信息转换器
  @Autowired
  private ObjectFactory<HttpMessageConverters> messageConverters;
  @Autowired(required = false)
  private List<AnnotatedParameterProcessor> parameterProcessors = new ArrayList<>();
  @Autowired(required = false)
  private List<FeignFormatterRegistrar> feignFormatterRegistrars = new ArrayList<>();
  @Autowired(required = false)
  private Logger logger;
  @Bean
  @ConditionalOnMissingBean
  public Decoder feignDecoder() {
    return new ResponseEntityDecoder(new SpringDecoder(this.messageConverters));
  }
  @Bean
  @ConditionalOnMissingBean
  public Encoder feignEncoder() {
    return new SpringEncoder(this.messageConverters);
  }
  @Bean
  @ConditionalOnMissingBean
  public Contract feignContract(ConversionService feignConversionService) {
    return new SpringMvcContract(this.parameterProcessors, feignConversionService);
  }
  // 注册FormattingConversionService--数据类型转换
  @Bean
  public FormattingConversionService feignConversionService() {
    FormattingConversionService conversionService = new DefaultFormattingConversionService();
    for (FeignFormatterRegistrar feignFormatterRegistrar : feignFormatterRegistrars) {
      feignFormatterRegistrar.registerFormatters(conversionService);
    }
    return conversionService;
  }
  // 与Hystrix整合
  @Configuration
  @ConditionalOnClass({ HystrixCommand.class, HystrixFeign.class })
  protected static class HystrixFeignConfiguration {
    @Bean
    @Scope("prototype")
    @ConditionalOnMissingBean
    @ConditionalOnProperty(name = "feign.hystrix.enabled", matchIfMissing = false)
    public Feign.Builder feignHystrixBuilder() {
      return HystrixFeign.builder();
    }
  }
  @Bean
  @ConditionalOnMissingBean
  public Retryer feignRetryer() {
    return Retryer.NEVER_RETRY;
  }
  @Bean
  @Scope("prototype")
  @ConditionalOnMissingBean
  public Feign.Builder feignBuilder(Retryer retryer) {
    return Feign.builder().retryer(retryer);
  }
  //如果缺少FeignLoggerFactory就注册FeignLoggerFactory
  @Bean
  @ConditionalOnMissingBean(FeignLoggerFactory.class)
  public FeignLoggerFactory feignLoggerFactory() {
    return new DefaultFeignLoggerFactory(logger);
  }
}

【6】@EnableFeignClients 注解

源码如下:

/**
 * Scans for interfaces that declare they are feign clients (via {@link FeignClient
 * <code>@FeignClient</code>}). 
 * // 扫描那些声明为feign client 的接口
 * Configures component scanning directives for use with
 * {@link org.springframework.context.annotation.Configuration
 * <code>@Configuration</code>} classes.
 *
 * @author Spencer Gibb
 * @author Dave Syer
 * @since 1.0
 */
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Documented
@Import(FeignClientsRegistrar.class)
public @interface EnableFeignClients {
  /**
   * Alias for the {@link #basePackages()} attribute. Allows for more concise annotation
   * declarations e.g.: {@code @ComponentScan("org.my.pkg")} instead of
   * {@code @ComponentScan(basePackages="org.my.pkg")}.
   * @return the array of 'basePackages'.
   */
  String[] value() default {};
  /**
   * Base packages to scan for annotated components.
   * <p>
   * {@link #value()} is an alias for (and mutually exclusive with) this attribute.
   * <p>//  value与该属性是互斥,不能共同使用。
   * Use {@link #basePackageClasses()} for a type-safe alternative to String-based
   * package names.
   *
   * @return the array of 'basePackages'.
   */
  String[] basePackages() default {};
  /**
   * Type-safe alternative to {@link #basePackages()} for specifying the packages to
   * scan for annotated components. The package of each class specified will be scanned.
   * <p>// 指定扫描的基础类型,可以与basePackages共同使用
   * Consider creating a special no-op marker class or interface in each package that
   * serves no purpose other than being referenced by this attribute.
   *
   * @return the array of 'basePackageClasses'.
   */
  Class<?>[] basePackageClasses() default {};
  /**
   * A custom <code>@Configuration</code> for all feign clients. Can contain override
   * <code>@Bean</code> definition for the pieces that make up the client, for instance
   * {@link feign.codec.Decoder}, {@link feign.codec.Encoder}, {@link feign.Contract}.
   *
   * @see FeignClientsConfiguration for the defaults
   */
  Class<?>[] defaultConfiguration() default {};
  /**
   * List of classes annotated with @FeignClient. If not empty, disables classpath scanning.
   * @return
   */
  Class<?>[] clients() default {};
}


相关实践学习
SLB负载均衡实践
本场景通过使用阿里云负载均衡 SLB 以及对负载均衡 SLB 后端服务器 ECS 的权重进行修改,快速解决服务器响应速度慢的问题
负载均衡入门与产品使用指南
负载均衡(Server Load Balancer)是对多台云服务器进行流量分发的负载均衡服务,可以通过流量分发扩展应用系统对外的服务能力,通过消除单点故障提升应用系统的可用性。 本课程主要介绍负载均衡的相关技术以及阿里云负载均衡产品的使用方法。
目录
相关文章
|
2月前
|
负载均衡 算法 架构师
Ribbon负载均衡
上一节就已经实现的负载均衡笔者并未深入探讨,本节通过分析负载均衡算法、Ribbon实现负载均衡的底层原理和实现过程,让大家对负载均衡有了一个大体认识,同时针对Ribbon自定义负载均衡策略,饥饿加载让大家对于Ribbon的了解又多一些。Ribbon实现的负载均衡只是方案之一,我们可以尽量多了解但不要局限于此。
|
22天前
|
负载均衡 网络协议 Unix
Nginx负载均衡与故障转移实践
Nginx通过ngx_http_upstream_module模块实现负载均衡与故障转移,适用于多服务器环境。利用`upstream`与`server`指令定义后端服务器组,通过`proxy_pass`将请求代理至这些服务器,实现请求分发。Nginx还提供了多种负载均衡策略,如轮询、权重分配、IP哈希等,并支持自定义故障转移逻辑,确保系统稳定性和高可用性。示例配置展示了如何定义负载均衡设备及状态,并应用到具体server配置中。
|
20天前
|
负载均衡 Java Nacos
SpringCloud基础1——远程调用、Eureka,Nacos注册中心、Ribbon负载均衡
微服务介绍、SpringCloud、服务拆分和远程调用、Eureka注册中心、Ribbon负载均衡、Nacos注册中心
SpringCloud基础1——远程调用、Eureka,Nacos注册中心、Ribbon负载均衡
|
5天前
|
负载均衡 Java 开发者
Ribbon框架实现客户端负载均衡的方法与技巧
Ribbon框架为微服务架构中的客户端负载均衡提供了强大的支持。通过简单的配置和集成,开发者可以轻松地在应用中实现服务的发现、选择和负载均衡。适当地使用Ribbon,配合其他Spring Cloud组件,可以有效提升微服务架构的可用性和性能。
7 0
|
2月前
|
存储 设计模式 缓存
OpenFeign集成Ribbon负载均衡-过滤和选择服务核心实现
该文章主要介绍了如何在OpenFeign中集成Ribbon以实现负载均衡,并详细分析了Ribbon中服务选择和服务过滤的核心实现过程。文章还涉及了Ribbon中负载均衡器(ILoadBalancer)和负载均衡策略(IRule)的初始化方式。
OpenFeign集成Ribbon负载均衡-过滤和选择服务核心实现
|
2月前
|
缓存 负载均衡 Java
OpenFeign最核心组件LoadBalancerFeignClient详解(集成Ribbon负载均衡能力)
文章标题为“OpenFeign的Ribbon负载均衡详解”,是继OpenFeign十大可扩展组件讨论之后,深入探讨了Ribbon如何为OpenFeign提供负载均衡能力的详解。
OpenFeign最核心组件LoadBalancerFeignClient详解(集成Ribbon负载均衡能力)
|
2月前
|
消息中间件 负载均衡 Kafka
Kafka 实现负载均衡与故障转移:深入分析 Kafka 的架构特点与实践
【8月更文挑战第24天】Apache Kafka是一款专为实时数据处理和流传输设计的高性能消息系统。其核心设计注重高吞吐量、低延迟与可扩展性,并具备出色的容错能力。Kafka采用分布式日志概念,通过数据分区及副本机制确保数据可靠性和持久性。系统包含Producer(消息生产者)、Consumer(消息消费者)和Broker(消息服务器)三大组件。Kafka利用独特的分区机制实现负载均衡,每个Topic可以被划分为多个分区,每个分区可以被复制到多个Broker上,确保数据的高可用性和可靠性。
43 2
|
2月前
|
负载均衡 Java API
深度解析SpringCloud微服务跨域联动:RestTemplate如何驾驭HTTP请求,打造无缝远程通信桥梁
【8月更文挑战第3天】踏入Spring Cloud的微服务世界,服务间的通信至关重要。RestTemplate作为Spring框架的同步客户端工具,以其简便性成为HTTP通信的首选。本文将介绍如何在Spring Cloud环境中运用RestTemplate实现跨服务调用,从配置到实战代码,再到注意事项如错误处理、服务发现与负载均衡策略,帮助你构建高效稳定的微服务系统。
57 2
|
3月前
|
缓存 负载均衡 算法
(四)网络编程之请求分发篇:负载均衡静态调度算法、平滑轮询加权、一致性哈希、最小活跃数算法实践!
先如今所有的技术栈中,只要一谈关于高可用、高并发处理相关的实现,必然会牵扯到集群这个话题,也就是部署多台服务器共同对外提供服务,从而做到提升系统吞吐量,优化系统的整体性能以及稳定性等目的。
|
2月前
|
负载均衡 前端开发 数据处理
"揭秘!ALB负载均衡器如何赋能Airflow,让数据处理命令请求在云端翩翩起舞,挑战性能极限,你不可不知的秘密!"
【8月更文挑战第20天】在现代云计算环境中,负载均衡ALB作为七层HTTP/HTTPS流量分发器,能显著提升系统的可用性和扩展性。结合Airflow这一开源工作流管理平台,ALB可以有效分发其REST API命令请求。通过配置ALB实例监听HTTP/S请求,并将多个Airflow实例加入目标组,再配合健康检查确保实例稳定,即可实现对Airflow命令的高效负载均衡,进而增强数据处理任务的可靠性和性能。
24 0
下一篇
无影云桌面