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

本文涉及的产品
注册配置 MSE Nacos/ZooKeeper,118元/月
服务治理 MSE Sentinel/OpenSergo,Agent数量 不受限
云原生网关 MSE Higress,422元/月
简介: 微服务学习笔记七 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)

相关实践学习
SLB负载均衡实践
本场景通过使用阿里云负载均衡 SLB 以及对负载均衡 SLB 后端服务器 ECS 的权重进行修改,快速解决服务器响应速度慢的问题
负载均衡入门与产品使用指南
负载均衡(Server Load Balancer)是对多台云服务器进行流量分发的负载均衡服务,可以通过流量分发扩展应用系统对外的服务能力,通过消除单点故障提升应用系统的可用性。 本课程主要介绍负载均衡的相关技术以及阿里云负载均衡产品的使用方法。
目录
相关文章
|
8天前
|
网络协议 Java Shell
java spring 项目若依框架启动失败,启动不了服务提示端口8080占用escription: Web server failed to start. Port 8080 was already in use. Action: Identify and stop the process that’s listening on port 8080 or configure this application to listen on another port-优雅草卓伊凡解决方案
java spring 项目若依框架启动失败,启动不了服务提示端口8080占用escription: Web server failed to start. Port 8080 was already in use. Action: Identify and stop the process that’s listening on port 8080 or configure this application to listen on another port-优雅草卓伊凡解决方案
40 7
|
3月前
|
前端开发 Java 开发者
Spring生态学习路径与源码深度探讨
【11月更文挑战第13天】Spring框架作为Java企业级开发中的核心框架,其丰富的生态系统和强大的功能吸引了无数开发者的关注。学习Spring生态不仅仅是掌握Spring Framework本身,更需要深入理解其周边组件和工具,以及源码的底层实现逻辑。本文将从Spring生态的学习路径入手,详细探讨如何系统地学习Spring,并深入解析各个重点的底层实现逻辑。
97 9
|
3月前
|
负载均衡 算法 Java
除了 Ribbon,Spring Cloud 中还有哪些负载均衡组件?
这些负载均衡组件各有特点,在不同的场景和需求下,可以根据项目的具体情况选择合适的负载均衡组件来实现高效、稳定的服务调用。
227 5
|
2月前
|
负载均衡 Java Nacos
常见的Ribbon/Spring LoadBalancer的负载均衡策略
自SpringCloud 2020版起,Ribbon被弃用,转而使用Spring Cloud LoadBalancer。Ribbon支持轮询、随机、加权响应时间和重试等负载均衡策略;而Spring Cloud LoadBalancer则提供轮询、随机及Nacos负载均衡策略,基于Reactor实现,更高效灵活。
177 0
|
4月前
|
前端开发 Java 数据库
SpringBoot学习
【10月更文挑战第7天】Spring学习
51 9
|
3月前
|
Java Kotlin 索引
学习Spring框架特性及jiar包下载
Spring 5作为最新版本,更新了JDK基线至8,修订了核心框架,增强了反射和接口功能,支持响应式编程及Kotlin语言,引入了函数式Web框架,并提升了测试功能。Spring框架可在其官网下载,包括文档、jar包和XML Schema文档,适用于Java SE和Java EE项目。
48 0
|
4月前
|
XML Java 数据格式
Spring学习
【10月更文挑战第6天】Spring学习
36 1
|
4月前
|
Java 测试技术 开发者
springboot学习四:Spring Boot profile多环境配置、devtools热部署
这篇文章主要介绍了如何在Spring Boot中进行多环境配置以及如何整合DevTools实现热部署,以提高开发效率。
157 2
|
4月前
|
前端开发 Java 程序员
springboot 学习十五:Spring Boot 优雅的集成Swagger2、Knife4j
这篇文章是关于如何在Spring Boot项目中集成Swagger2和Knife4j来生成和美化API接口文档的详细教程。
496 1
|
4月前
|
Java API Spring
springboot学习七:Spring Boot2.x 拦截器基础入门&实战项目场景实现
这篇文章是关于Spring Boot 2.x中拦截器的入门教程和实战项目场景实现的详细指南。
56 0
springboot学习七:Spring Boot2.x 拦截器基础入门&实战项目场景实现