侧边栏壁纸
博主头像
张种恩的技术小栈博主等级

行动起来,活在当下

  • 累计撰写 748 篇文章
  • 累计创建 65 个标签
  • 累计收到 39 条评论

目 录CONTENT

文章目录

SpringCloud(9)之Feign负载均衡

zze
zze
2018-10-19 / 0 评论 / 0 点赞 / 628 阅读 / 4128 字

不定期更新相关视频,抖音点击左上角加号后扫一扫右方侧边栏二维码关注我~正在更新《Shell其实很简单》系列

介绍

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

Feign 能干什么?

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

使用

1、复制 "microservicecloud-consumer-dept-80" 子工程改名为 "microservicecloud-consumer-dept-feign",新增依赖:

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

2、新建接口,使用 Feign 提供的注解标识该接口为 Feign 客户端:

// zze.springcloud.service.DeptClientService
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import zze.springcloud.entities.Dept;

import java.util.List;

@FeignClient(value = "microservicecloud-provider-dept") // 标识调用哪个微服务
@RequestMapping("/dept")
public interface DeptClientService {
    @GetMapping("/get/{id}")
    public Dept get(@PathVariable Long id);

    @GetMapping("/list")
    public List<Dept> list();

    @PostMapping("/add")
    public boolean add(Dept dept);
}

3、修改主启动类,使用注解启用 Feign:

// zze.springcloud.Application_80
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 = {"zze.springcloud.service"}) // 指定 Feign 客户端的扫描包
public class Application_80 {

    public static void main(String[] args) {
        SpringApplication.run(Application_80.class, args);
    }
}

4、修改 Controller,使用 Feign 客户端:

// zze.springcloud.controller.DeptController
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import zze.springcloud.entities.Dept;
import zze.springcloud.service.DeptClientService;

import java.util.List;

@RestController
@RequestMapping("/consumer/dept")
public class DeptController {

    @Autowired
    private DeptClientService deptService;

    @PostMapping("/add")
    public boolean add(@RequestBody Dept dept) {
        return deptService.add(dept);
    }

    @GetMapping("/get/{id}")
    public Dept get(@PathVariable Long id) {
        return deptService.get(id);
    }

    @GetMapping("/list")
    public List<Dept> list() {
        return deptService.list();
    }
}

5、测试:

1、启动 Eureka 集群 7001、7002、7003
2、启动 Provider 集群 8001、8002、8003
3、启动 microservicecloud-consumer-dept-feign,访问 http://localhost/consumer/dept/get/1,返回结果,成功。

image.png

Feign 集成了 Ribbon,利用 Ribbon 维护了 microservicecloud-provider-dept 服务的信息,并且通过轮询实现了客户端的负载均衡。与 Ribbon 不同的是,Feign 无需手动使用 RestTemplate 构建请求,只需要以声明式方法定义服务绑定接口,优雅而简单的实现了服务调用。

0

评论区