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

本文涉及的产品
应用型负载均衡 ALB,每月750个小时 15LCU
网络型负载均衡 NLB,每月750个小时 15LCU
传统型负载均衡 CLB,每月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 {};
}


相关实践学习
每个IT人都想学的“Web应用上云经典架构”实战
本实验从Web应用上云这个最基本的、最普遍的需求出发,帮助IT从业者们通过“阿里云Web应用上云解决方案”,了解一个企业级Web应用上云的常见架构,了解如何构建一个高可用、可扩展的企业级应用架构。
目录
相关文章
|
1月前
|
负载均衡 NoSQL Redis
不增加 GPU,首 Token 延迟下降50%|LLM 服务负载均衡的新实践
针对LLM服务的特点,Higress AI网关以插件形式提供了面向LLM服务的负载均衡算法,包括全局最小请求数负载均衡、前缀匹配负载均衡以及GPU感知负载均衡,能够在不增加硬件成本的前提下,提升系统的吞吐能力、降低响应延迟,并实现更公平、高效的任务调度。
|
5月前
|
弹性计算 负载均衡 网络协议
阿里云SLB深度解析:从流量分发到架构优化的技术实践
本文深入探讨了阿里云负载均衡服务(SLB)的核心技术与应用场景,从流量分配到架构创新全面解析其价值。SLB不仅是简单的流量分发工具,更是支撑高并发、保障系统稳定性的智能中枢。文章涵盖四层与七层负载均衡原理、弹性伸缩引擎、智能DNS解析等核心技术,并结合电商大促、微服务灰度发布等实战场景提供实施指南。同时,针对性能调优与安全防护,分享连接复用优化、DDoS防御及零信任架构集成的实践经验,助力企业构建面向未来的弹性架构。
485 76
|
5月前
|
负载均衡 前端开发 Java
SpringCloud调用组件Feign
本文深入探讨微服务Spring体系中的Feign组件。Feign是一个声明式Web服务客户端,支持注解、编码器/解码器,与Spring MVC注解兼容,并集成Eureka、负载均衡等功能。文章详细介绍了SpringCloud整合Feign的步骤,包括依赖引入、客户端启用、接口创建及调用示例。同时,还涵盖了Feign的核心配置,如超时设置、拦截器实现(Basic认证与自定义)和日志级别调整。最后,总结了`@FeignClient`常用属性,帮助开发者更好地理解和使用Feign进行微服务间通信。
483 1
|
10月前
|
负载均衡 算法 Java
除了 Ribbon,Spring Cloud 中还有哪些负载均衡组件?
这些负载均衡组件各有特点,在不同的场景和需求下,可以根据项目的具体情况选择合适的负载均衡组件来实现高效、稳定的服务调用。
881 61
|
8月前
|
存储 弹性计算 负载均衡
活动实践 | ALB 实现跨地域负载均衡
本方案通过阿里云的云企业网(CEN)、转发路由器(TR)、专有网络(VPC)、云服务器(ECS)和应用型负载均衡(ALB),实现跨地域的应用负载均衡。它扩展了系统的吞吐能力,提升了可用性和安全性。用户可通过资源编排服务(ROS)一键部署,并进行负载测试验证。清理资源也简便快捷。
|
10月前
|
负载均衡 监控 网络协议
SpringCloud之Ribbon使用
通过以上步骤,就可以在Spring Cloud项目中有效地使用Ribbon来实现服务调用的负载均衡,提高系统的可靠性和性能。在实际应用中,根据具体的业务场景和需求选择合适的负载均衡策略,并进行相应的配置和优化,以确保系统的稳定运行。
437 15
|
11月前
|
JSON Java 数据格式
【微服务】SpringCloud之Feign远程调用
本文介绍了使用Feign作为HTTP客户端替代RestTemplate进行远程调用的优势及具体使用方法。Feign通过声明式接口简化了HTTP请求的发送,提高了代码的可读性和维护性。文章详细描述了Feign的搭建步骤,包括引入依赖、添加注解、编写FeignClient接口和调用代码,并提供了自定义配置的示例,如修改日志级别等。
681 1
|
前端开发 API 微服务
SpringCloud微服务之间使用Feign调用不通情况举例
SpringCloud微服务之间使用Feign调用不通情况举例
1484 2
|
Java API 开发者
【已解决】Spring Cloud Feign 上传文件,提示:the request was rejected because no multipart boundary was found的问题
【已解决】Spring Cloud Feign 上传文件,提示:the request was rejected because no multipart boundary was found的问题
1657 0
|
6月前
|
负载均衡 Dubbo Java
Spring Cloud Alibaba与Spring Cloud区别和联系?
Spring Cloud Alibaba与Spring Cloud区别和联系?

热门文章

最新文章