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

简介: 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 {};
}


相关实践学习
部署高可用架构
本场景主要介绍如何使用云服务器ECS、负载均衡SLB、云数据库RDS和数据传输服务产品来部署多可用区高可用架构。
负载均衡入门与产品使用指南
负载均衡(Server Load Balancer)是对多台云服务器进行流量分发的负载均衡服务,可以通过流量分发扩展应用系统对外的服务能力,通过消除单点故障提升应用系统的可用性。 本课程主要介绍负载均衡的相关技术以及阿里云负载均衡产品的使用方法。
目录
相关文章
|
1月前
|
负载均衡 算法
ribbon的7种负载均衡算法和替换方法
ribbon的7种负载均衡算法和替换方法
34 0
ribbon的7种负载均衡算法和替换方法
|
1月前
|
缓存 Java API
【云原生】Spring Cloud Gateway的底层原理与实践方法探究
【云原生】Spring Cloud Gateway的底层原理与实践方法探究
|
2天前
|
负载均衡 算法
SpringCloud&Ribbon负载均衡原理与实践
SpringCloud&Ribbon负载均衡原理与实践
10 3
|
18天前
|
Nacos
SpringCloud Feign使用
SpringCloud Feign使用
23 1
|
25天前
|
负载均衡 网络协议 Java
构建高效可扩展的微服务架构:利用Spring Cloud实现服务发现与负载均衡
本文将探讨如何利用Spring Cloud技术实现微服务架构中的服务发现与负载均衡,通过注册中心来管理服务的注册与发现,并通过负载均衡策略实现请求的分发,从而构建高效可扩展的微服务系统。
|
1月前
|
负载均衡 算法 Java
【Springcloud Alibaba微服务分布式架构 | Spring Cloud】之学习笔记(四)Ribbon的使用
【Springcloud Alibaba微服务分布式架构 | Spring Cloud】之学习笔记(四)Ribbon的使用
25 0
|
1月前
|
SpringCloudAlibaba 负载均衡 Java
【二】SpringCloud Alibaba之Nacos整合篇(配置负载均衡)
【二】SpringCloud Alibaba之Nacos整合篇(配置负载均衡)
271 0
|
1月前
|
负载均衡
【二十】搭建SpringCloud项目四(Ribbon)
【二十】搭建SpringCloud项目四(Ribbon)
20 0
|
1月前
【十九】搭建SpringCloud项目三(Feign)
【十九】搭建SpringCloud项目三(Feign)
22 0
|
1月前
|
存储 负载均衡 Java
【Spring底层原理高级进阶】微服务 Spring Cloud 的注册发现机制:Eureka 的架构设计、服务注册与发现的实现原理,深入掌握 Ribbon 和 Feign 的用法 ️
【Spring底层原理高级进阶】微服务 Spring Cloud 的注册发现机制:Eureka 的架构设计、服务注册与发现的实现原理,深入掌握 Ribbon 和 Feign 的用法 ️