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

简介: 微服务学习笔记七 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应用上云的常见架构,了解如何构建一个高可用、可扩展的企业级应用架构。
目录
相关文章
|
2月前
|
负载均衡 算法 Java
【SpringCloud(4)】OpenFeign客户端:OpenFeign服务绑定;调用服务接口;Feign和OpenFeign
Feign是一个WebService客户端。使用Feign能让编写WebService客户端更加简单。 它的使用方法是定义一个服务接口然后再上面添加注解。Feign也支持可拔插式的编码器和解码器。SpringCloud对Feign进行了封装,十七支持了SpringMVC标准注解和HttpMessageConverters。 Feign可用于Eureka和Ribbon组合使用以支持负载均衡
696 138
|
2月前
|
算法 Java 微服务
【SpringCloud(1)】初识微服务架构:创建一个简单的微服务;java与Spring与微服务;初入RestTemplate
微服务架构是What?? 微服务架构是一种架构模式,它提出将单一应用程序划分为一组小的服务,服务之间互相协调、互相配合,为用户提供最终价值。 每个服务允许在其独立的进程中,服务于服务间采用轻量级的通信机制互相协作(通常是Http协议的RESTful API或RPC协议)。 每个服务都围绕着具体业务进行构建,并且能够被独立的部署到生产环境、类生产环境等。另外应当尽量避免统一的、集中式的服务管理机制,对具体的一个服务而言,应根据上下文,选择合适的语言、工具对其进行构建
500 126
|
2月前
|
负载均衡 算法 Java
【SpringCloud(2)】微服务注册中心:Eureka、Zookeeper;CAP分析;服务注册与服务发现;单机/集群部署Eureka;连接注册中心
1. 什么是服务治理? SpringCloud封装了Netfix开发的Eureka模块来实现服务治理 在传统pc的远程调用框架中,管理每个服务与服务之间依赖关系比较复杂,管理比较复杂,所以需要使用服务治理,管理服务于服务之间依赖关系,可以实现服务调用、负载均衡、容错等,实现服务发现与注册
299 0
|
2月前
|
负载均衡 Java API
《深入理解Spring》Spring Cloud 构建分布式系统的微服务全家桶
Spring Cloud为微服务架构提供一站式解决方案,涵盖服务注册、配置管理、负载均衡、熔断限流等核心功能,助力开发者构建高可用、易扩展的分布式系统,并持续向云原生演进。
|
7月前
|
负载均衡 前端开发 应用服务中间件
Tomcat的负载均衡和动静分离(与nginx联动)
总的来说,负载均衡和动静分离是提高Web应用性能的两个重要手段。通过合理的配置和使用,我们可以让Web应用更好地服务于用户。
242 21
|
缓存 负载均衡 算法
解读 Nginx:构建高效反向代理和负载均衡的秘密
解读 Nginx:构建高效反向代理和负载均衡的秘密
298 2
|
负载均衡 前端开发 应用服务中间件
负载均衡指南:Nginx与HAProxy的配置与优化
负载均衡指南:Nginx与HAProxy的配置与优化
738 3
|
负载均衡 算法 应用服务中间件
nginx自定义负载均衡及根据cpu运行自定义负载均衡
nginx自定义负载均衡及根据cpu运行自定义负载均衡
372 1
|
运维 负载均衡 算法
SLB与NGINX的异同是什么
SLB与NGINX的异同是什么
1879 2
|
负载均衡 应用服务中间件 nginx
解决nginx配置负载均衡时invalid host in upstream报错
在Windows环境下,配置Nginx 1.11.5进行负载均衡时遇到问题,服务无法启动。错误日志显示“invalid host in upstream”。检查发现上游服务器列表中,192.168.29.128的主机地址无效。负载均衡配置中,两个服务器地址前误加了&quot;http://&quot;。修正方法是删除上游服务器列表和proxy_pass中的&quot;http://&quot;。问题解决后,Nginx服务应能正常启动。
1030 4
解决nginx配置负载均衡时invalid host in upstream报错

热门文章

最新文章