SpringCloud day(7) Feign负载均衡

一、Feign介绍

1.1 概述

Feign是一个声明式的Web服务客户端,使得编写Web服务客户端变得非常容易。它的使用方法是定义一个接口,然后在上面添加注解,同时也支持JAX-RS标准的注解,Feign也支持可拔插式的编码器和解码器,Spring cloud对Feign进行了封装,使其支持Spring MVC标准注解和HttpMessageConvents。Feign可以与Eureka和Ribbon组合使用以支持负载均衡。

1.2 微服务地址获取方法

  • 微服务名称获得微服务的地址
  • 通过接口+注解,获取我们的微服务。(Feign)

1.3 由来

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

二、配置Feign

2.1 pom

<dependency>
            <groupId>com.Rubbish</groupId>
            <artifactId>rubbish-service</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>

2.2 yml配置文件

spring:
  datasource:
    username: ybx
    password: 510363
    url: jdbc:mysql://localhost:3306/Bishe
    driver-class-name: com.mysql.cj.jdbc.Driver
  jpa:
    show-sql: true
    hibernate:
      ddl-auto: update
eureka:
  client:
    register-with-eureka: false
    service-url:
      defaultZone: http://eureka7003.com:7003/eureka,http://eureka7004.com:7004/eureka,http://eureka7005.com:7005/eureka

server:
  port: 9002


2.3 FeignService编写

  • value的意思是与哪个名称的服务绑定
@FeignClient(value = "Rubbish-Server")
public interface FeignService {
    @RequestMapping("/custom/get/all")
    public List<Rubbish> getAll();
    @RequestMapping("/custom/get/{id}")
    public Rubbish getById(@PathVariable("id")int id);
    @RequestMapping("/custom/delete/{id}")
    public void deleteById(@PathVariable("id")int id);
    @RequestMapping("/custom/post/add")
    public Rubbish save(Rubbish rubbish);
    @RequestMapping("/custom/put")
    public Rubbish updateById(Rubbish rubbish);
    @RequestMapping("/custom/get/discovery")
    public Object getDiscovery();
}


2.4 controller层

@RestController
public class FeignCntroller {
    @Autowired
    FeignService service;
    @RequestMapping("/custom/get/all")
    public List<Rubbish> getAll(){
        return service.getAll();
    }
    @RequestMapping("/custom/get/{id}")
    public Rubbish getById(@PathVariable("id")int id){
        return service.getById(id);
    }
    @RequestMapping("/custom/delete/{id}")
    public void deleteById(@PathVariable("id")int id){
        service.deleteById(id);
    }
    @RequestMapping("/custom/post/add")
    public Rubbish save(Rubbish rubbish){
        return service.save(rubbish);
    }
    @RequestMapping("/custom/put")
    public Rubbish updateById(Rubbish rubbish){
        return service.updateById(rubbish);
    }
    @RequestMapping("/custom/get/discovery")
    public Object getDiscovery() {
        return service.getDiscovery();
    }
}

2.5 主启动类的修改

@SpringBootApplication
@EnableFeignClients(basePackages = {"com.rubbish.rubbishcustomfeign"})
public class RubbishCustomFeignApplication {
    public static void main(String[] args) {
        SpringApplication.run(RubbishCustomFeignApplication.class, args);
    }
}

2.6 feign模块目录结构

在这里插入图片描述

2.7 测试

  • 默认采用轮询的负载均衡算法。
    在这里插入图片描述

三、总结

3.1 Feign与Ribbon关系

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

四、feign原理

4.1 原理图

在这里插入图片描述

4.2 原理

  • 动态代理+反射
  • @EnableFeignClients会扫描得到@FeignClient标记的接口Interfice,为其创建代理对象Proxy。
  • 通过反射获取到Interfice接口上服务名的信息,方法的注解信息,以及参数的信息,将信息拼接成http://服务名/地址/xx
  • 通过feign集成的ribbon拿到本地的缓存服务列表,将服务名替换成对应的ip。
  • 然后通过Proxy通过RedisTemplete调用远程接口。将返回的结果解析成对应的java对象即可。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值