SpringCloud学习笔记(五)之Feign负载均衡

Easter79
• 阅读 550

是什么

是一个声明式WebService客户端。使用Feign能让编写Web Service客户端更加简单,使用方法是定义一个接口,然后在上面添加注解,同时也支持JAX-RS标准的注解。Feign也支持可拔插式的编码器和解码器。Spring Cloud对Feign进行了封装,使其支持了Spring MVC标准注解和HttpMessageConverters。Feign可以与Eureka和Ribbon组合使用以支持负载均衡。

只需要创建一个接口,然后在上面添加注解即可。

干什么

使编写Java Http客户端变得更容易。

前面使用Ribbon+RestTemplate时,利用RestTemplate对http请求的封装处理,形成了一套模板化的调用方法。但是在实际开发中,由于对服务依赖的调用可能不止一处,往往一个接口会被多次调用,所以通常都会针对每个微服务自行封装一些客户端类来包装这些依赖服务的调用。所以,Feign在此基础上做了进一步封装,由他来帮助我们定义和实现依赖服务接口的定义。在Feign的实现下,我们只需要创建一个接口并使用注解的方式来配置它(以前是Dao接口上面标注Mapper注解,现在是一个微服务接口上面标注一个Feign注解即可),即可完成对服务提供方的接口绑定,简化使用Spring Cloud Ribbon时,自动封装服务调用客户端的开发量。

Feign集成了Ribbon

利用Ribbon维护了MicroServiceCloud-Dept的服务列表信息,并且通过轮询实现了客户端的负载均衡。而与Ribbon不同的是,通过feign只需要定义服务绑定接口且以声明式的方式,优雅而简单的实现了服务调用。

案例

1. 创建micoservicecloud-consumer-dept-feign的maven工程

2. 在pom.xml中添加依赖

<dependencies>
    <dependency>
        <groupId>com.minn.springcloud</groupId>
        <artifactId>micoservicecloud-api</artifactId>
        <version>${project.version}</version>
    </dependency>
    <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>
    <!-- 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>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-feign</artifactId>
    </dependency>
</dependencies>

3.  在公共的maven工程micoservicecloud-api中添加公共接口类DeptClientService

package com.minn.springcloud;

import com.minn.springcloud.entities.Dept;
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 java.util.List;

@FeignClient(value = "MICOSERVICECLOUD-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 = "/detp/add",method = RequestMethod.POST)
    public boolean add(Dept dept);
}

4. 公共的maven工程micoservicecloud-api中pom.xml中添加feign的依赖:

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-feign</artifactId>
</dependency>

5.  micoservicecloud-consumer-dept-feign工程添加application.yml

server:
  port: 80
eureka:
  client:
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
    register-with-eureka: false

6.  micoservicecloud-consumer-dept-feign工程添加controller类调用公共工程中的接口类DeptClientService

package com.minn.springcloud.controller;

import com.minn.springcloud.DeptClientService;
import com.minn.springcloud.entities.Dept;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;

@RestController
public class DeptController_Consumer {
    @Autowired
    private DeptClientService deptClientService;

    @RequestMapping(value = "/consumer/dept/add")
    public boolean add(Dept dept){
        return deptClientService.add(dept);
    }

    @RequestMapping(value = "/consumer/dept/get/{id}")
    public Dept get(@PathVariable("id") Long id){
        return deptClientService.get(id);
    }

    @RequestMapping(value = "/consumer/dept/list")
    public List<Dept> list(){
        return deptClientService.list();
    }
}

7. micoservicecloud-consumer-dept-feign工程添加启动类

package com.minn.springcloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;

@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients(basePackages = {"com.minn.springcloud"})
public class DeptConsumer80_Feign_App {
    public static void main(String[] args) {
        SpringApplication.run(DeptConsumer80_Feign_App.class,args);
    }
}

小总结

Feign通过接口的方法调用Rest服务(之前是Ribbon + RestTemplate)

该请求发送给Eureka服务器(http://MICOSERVICECLOUD-DEPT/dept/list),通过Feign直接找到服务接口,由于在进行服务调用的时候融合了Ribbon技术,所以也支持负载均衡作用。

点赞
收藏
评论区
推荐文章
blmius blmius
2年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
Jacquelyn38 Jacquelyn38
2年前
2020年前端实用代码段,为你的工作保驾护航
有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )
Easter79 Easter79
2年前
springboot+springcloud微服务入门
MicroService实现技术:  用springBoot来创建单个服务,用SpringCloud来管理这些微服务。  SpringCloud的五大神兽  1.注册/服务发现——NetflixEureka    管理服务器地址和ip的  2.客服端负载均衡——NetflixRibbon\\Feign    服
Stella981 Stella981
2年前
Feign请求响应结果被截取com.fasterxml.jackson.core.io.JsonEOFException
在生产环境使用feign调用外部接口时,偶尔会出现下面错误2020101511:00:18,535ERRORcom.shein.abc.rmp.controller.RecExplainConfigControllerrec_explain_query.failffeign.codec.DecodeExc
Easter79 Easter79
2年前
SpringCloud Ribbon
客户端负载均衡器:RibbonRibbon是一个客户端负载平衡器,它可以很好地控制HTTP和TCP客户端的行为。Feign已经使用Ribbon,所以如果你使用@FeignClient,那么这一节也适用。Ribbon中的中心概念是命名客户端的概念。每个负载平衡器是组合的组合的一部分,它们一起工作以根据需要联系远程服务
Stella981 Stella981
2年前
Spring Boot + Spring Cloud 构建微服务系统(三):服务消费和负载(Feign)
SpringCloudFeignSpringCloudFeign是一套基于NetflixFeign实现的声明式服务调用客户端。它使得编写Web服务客户端变得更加简单。我们只需要通过创建接口并用注解来配置它既可完成对Web服务接口的绑定。它具备可插拔的注解支持,包括Feign注解、JAXRS注解。它也支持可插拔的编码器和解码器。Spri
Stella981 Stella981
2年前
Nepxion Discovery
NepxionDiscovery基于Nacos实现SpringCloud灰度发布和路由前言NepxionDiscovery是一款对SpringCloudDiscovery服务注册发现、Ribbon负载均衡、Feign和RestTe
Easter79 Easter79
2年前
SpringCloud(第 016 篇)电影微服务,定制Feign,一个Feign功能禁用Hystrix,另一个Feign功能启用Hystrix
SpringCloud(第016篇)电影微服务,定制Feign,一个Feign功能禁用Hystrix,另一个Feign功能启用Hystrix一、大致介绍1、在一些场景中,部分功能需
Stella981 Stella981
2年前
OpenJDK11与Spring Cloud Finchley的不兼容问题与解决
本文的环境:OpenJDK11.0.4,SpringCloudfinchleySR4,SpringBoot2.0.3最近遇到了一个问题,在feign调用的时候,时常会出现这样一个奇怪的错误:2019100708:00:00.620ERRORxxx,e1ba4c7540954aa3,871b99c4576d42e3
Easter79 Easter79
2年前
SpringCloud学习笔记(四)之Ribbon负载均衡
    在微服务架构中,业务都会被拆分成一个独立的服务,服务与服务的通讯是基于httprestful的。Springcloud有两种服务调用方式,一种是ribbonrestTemplate,另一种是feign。简介是基于NetflixRibbon实现的一套客户端负载均衡的工具。简单的说,Ribbon是
Easter79
Easter79
Lv1
今生可爱与温柔,每一样都不能少。
文章
2.8k
粉丝
5
获赞
1.2k