微服务学习笔记七 Spring Cloud Feign负载均衡及服务熔断

本文涉及的产品
云原生网关 MSE Higress,422元/月
服务治理 MSE Sentinel/OpenSergo,Agent数量 不受限
任务调度 XXL-JOB 版免费试用,400 元额度,开发版规格
简介: 微服务学习笔记七 Spring Cloud Feign负载均衡及服务熔断

**Feign:**

与Ribbon一样,Feign也是由Netflix提供的,Feign是一个声明式、

模块化的Web Service客户端,它简化了开发者编写Web客户端的操作,

开发者可以通过简单的接口和注解来调用HTTP API ,Spring Cloud Feign,

它整合了Ribbon和Hystrix,具有可插拔、基于注解、负载均衡、服务熔断

等一系列便捷功能。

相比较于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注解、SpringMVC注解、JAX-RS注解。

3)Feign基于Ribbon实现,使用起来更加简单。

4)Feign集成了Hystrix,具备服务熔断的功能。

![在这里插入图片描述](https://ucc.alicdn.com/images/user-upload-01/20200713174109858.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ0OTY5NjQz,size_16,color_FFFFFF,t_70)

创建Module,pom.xml添加依赖


```yaml

<dependencies>

   <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>

</dependencies>

```

创建配置文件,application.yml


```yaml

server:

 port: 8050

spring:

 application:

   name: feign

eureka:

 client:

   service-url:

     defaultZone: http://localhost:8761/eureka/

 instance:

   prefer-ip-address: true

```

创建启动类


```java

package com.shuang;


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) {

       SpringApplication.run(FeignApplication.class,args);

   }

}

```

创建声明式接口


```java

package com.shuang.feign;


import com.shuang.entity.Student;

import org.springframework.cloud.openfeign.FeignClient;

import org.springframework.web.bind.annotation.GetMapping;


import java.util.Collection;


@FeignClient(value = "provider")

public interface FeignProviderClient {

   @GetMapping("/student/findAll")

   public Collection<Student> findAll();


   @GetMapping("/student/index")

   public String index();

}

```

Handler


```java

package com.shuang.controller;


import com.shuang.entity.Student;

import com.shuang.feign.FeignProviderClient;

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 java.util.Collection;


@RestController

@RequestMapping("/feign")

public class FeignHandler {

   @Autowired

   private FeignProviderClient feignProviderClient;


   @GetMapping("/findAll")

   public Collection<Student> findAll(){

       return feignProviderClient.findAll();

   }


   @GetMapping("/index")

   public String index(){

       return feignProviderClient.index();

   }

}

```

服务熔断,application.yml中添加熔断机制


```java

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:是否开启熔断器。

创建 FeignProviderClient 接口的实现类FeignError,定义容错处理逻辑,通过@Component

注解将FeignError实例注入IOC容器中。




```java

package com.shuang.feign.impl;


import com.shuang.entity.Student;

import com.shuang.feign.FeignProviderClient;

import org.springframework.stereotype.Component;


import java.util.Collection;


@Component

public class FeignError implements FeignProviderClient {

   @Override

   public Collection<Student> findAll() {

       return null;

   }


   @Override

   public String index() {

       return "服务器维护中。。。。";

   }

}

```

在FeignProviderClient定义处通过@FeignClient的fallback属性设置映射。


```java

package com.shuang.feign;


import com.shuang.entity.Student;

import com.shuang.feign.impl.FeignError;

import org.springframework.cloud.openfeign.FeignClient;

import org.springframework.web.bind.annotation.GetMapping;


import java.util.Collection;


@FeignClient(value = "provider", fallback = FeignError.class)

public interface FeignProviderClient {

   @GetMapping("/student/findAll")

   public Collection<Student> findAll();


   @GetMapping("/student/index")

   public String index();

}


```


**依次启动:注册中心、Feign**



![在这里插入图片描述](https://ucc.alicdn.com/images/user-upload-01/20200713174030134.png)

相关实践学习
每个IT人都想学的“Web应用上云经典架构”实战
本实验从Web应用上云这个最基本的、最普遍的需求出发,帮助IT从业者们通过“阿里云Web应用上云解决方案”,了解一个企业级Web应用上云的常见架构,了解如何构建一个高可用、可扩展的企业级应用架构。
目录
相关文章
|
1月前
|
负载均衡 算法 Java
【SpringCloud(4)】OpenFeign客户端:OpenFeign服务绑定;调用服务接口;Feign和OpenFeign
Feign是一个WebService客户端。使用Feign能让编写WebService客户端更加简单。 它的使用方法是定义一个服务接口然后再上面添加注解。Feign也支持可拔插式的编码器和解码器。SpringCloud对Feign进行了封装,十七支持了SpringMVC标准注解和HttpMessageConverters。 Feign可用于Eureka和Ribbon组合使用以支持负载均衡
630 138
|
1月前
|
监控 算法 NoSQL
Go 微服务限流与熔断最佳实践:滑动窗口、令牌桶与自适应阈值
🌟蒋星熠Jaxonic:Go微服务限流熔断实践者。分享基于滑动窗口、令牌桶与自适应阈值的智能防护体系,助力高并发系统稳定运行。
Go 微服务限流与熔断最佳实践:滑动窗口、令牌桶与自适应阈值
|
2月前
|
数据可视化 Java BI
将 Spring 微服务与 BI 工具集成:最佳实践
本文探讨了 Spring 微服务与商业智能(BI)工具集成的潜力与实践。随着微服务架构和数据分析需求的增长,Spring Boot 和 Spring Cloud 提供了构建可扩展、弹性服务的框架,而 BI 工具则增强了数据可视化与实时分析能力。文章介绍了 Spring 微服务的核心概念、BI 工具在企业中的作用,并深入分析了两者集成带来的优势,如实时数据处理、个性化报告、数据聚合与安全保障。同时,文中还总结了集成过程中的最佳实践,包括事件驱动架构、集中配置管理、数据安全控制、模块化设计与持续优化策略,旨在帮助企业构建高效、智能的数据驱动系统。
186 1
将 Spring 微服务与 BI 工具集成:最佳实践
|
2月前
|
存储 安全 Java
管理 Spring 微服务中的分布式会话
在微服务架构中,管理分布式会话是确保用户体验一致性和系统可扩展性的关键挑战。本文探讨了在 Spring 框架下实现分布式会话管理的多种方法,包括集中式会话存储和客户端会话存储(如 Cookie),并分析了它们的优缺点。同时,文章还涵盖了与分布式会话相关的安全考虑,如数据加密、令牌验证、安全 Cookie 政策以及服务间身份验证。此外,文中强调了分布式会话在提升系统可扩展性、增强可用性、实现数据一致性及优化资源利用方面的显著优势。通过合理选择会话管理策略,结合 Spring 提供的强大工具,开发人员可以在保证系统鲁棒性的同时,提供无缝的用户体验。
|
1月前
|
监控 Cloud Native Java
Spring Boot 3.x 微服务架构实战指南
🌟蒋星熠Jaxonic,技术宇宙中的星际旅人。深耕Spring Boot 3.x与微服务架构,探索云原生、性能优化与高可用系统设计。以代码为笔,在二进制星河中谱写极客诗篇。关注我,共赴技术星辰大海!(238字)
Spring Boot 3.x 微服务架构实战指南
|
1月前
|
负载均衡 Java API
《深入理解Spring》Spring Cloud 构建分布式系统的微服务全家桶
Spring Cloud为微服务架构提供一站式解决方案,涵盖服务注册、配置管理、负载均衡、熔断限流等核心功能,助力开发者构建高可用、易扩展的分布式系统,并持续向云原生演进。
|
2月前
|
监控 安全 Java
Spring Cloud 微服务治理技术详解与实践指南
本文档全面介绍 Spring Cloud 微服务治理框架的核心组件、架构设计和实践应用。作为 Spring 生态系统中构建分布式系统的标准工具箱,Spring Cloud 提供了一套完整的微服务解决方案,涵盖服务发现、配置管理、负载均衡、熔断器等关键功能。本文将深入探讨其核心组件的工作原理、集成方式以及在实际项目中的最佳实践,帮助开发者构建高可用、可扩展的分布式系统。
202 1
|
6月前
|
负载均衡 前端开发 应用服务中间件
Tomcat的负载均衡和动静分离(与nginx联动)
总的来说,负载均衡和动静分离是提高Web应用性能的两个重要手段。通过合理的配置和使用,我们可以让Web应用更好地服务于用户。
207 21
|
缓存 负载均衡 算法
解读 Nginx:构建高效反向代理和负载均衡的秘密
解读 Nginx:构建高效反向代理和负载均衡的秘密
283 2
|
11月前
|
负载均衡 前端开发 应用服务中间件
负载均衡指南:Nginx与HAProxy的配置与优化
负载均衡指南:Nginx与HAProxy的配置与优化
698 3

热门文章

最新文章