Feign负载均衡

Feign是一个声明式WebService客户端,简化了WebService客户端的编写。通过定义接口并添加注解即可实现服务调用,支持JAX-RS标准注解及SpringMVC标准注解。Feign与Eureka和Ribbon结合使用时,能支持负载均衡。

Feign介绍

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

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

编写接口
加上@FeignClient(value = “MICROSERVICECLOUD-DEPT”)注解

@FeignClient(value = "MICROSERVICECLOUD-DEPT")//value=微服务的名字
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 = "/dept/add",method = RequestMethod.POST)
  public boolean add(Dept dept);
}

调用者主启动类加上扫描使用了@FeignClient注解的类
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients(basePackages= {“com.atguigu.springcloud”})
@ComponentScan(“com.atguigu.springcloud”)
直接调用

@RestController
	public class DeptController_Feign
{
  @Autowired
  private DeptClientService service = null;
 
  @RequestMapping(value = "/consumer/dept/get/{id}")
  public Dept get(@PathVariable("id") Long id)
  {
   return this.service.get(id);
  }
 
  @RequestMapping(value = "/consumer/dept/list")
  public List<Dept> list()
  {
   return this.service.list();
  }
 
  @RequestMapping(value = "/consumer/dept/add")
  public Object add(Dept dept)
  {
   return this.service.add(dept);
  }
}

总结

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

### 原理 Feign本身并不直接实现负载均衡,其负载均衡依赖于Ribbon(或Spring Cloud新版的LoadBalancer)。Feign负载均衡流程遵循“服务发现→策略选择→请求分发”的模式,通过与服务注册中心的联动,实现服务实例的动态感知和流量的智能分配。具体如下: - 服务实例列表:通过服务名从注册中心(如Eureka)获取所有可用实例的IP和端口。例如,当使用`@FeignClient("user-service")`时,Feign会根据服务名`user-service`从注册中心获取对应服务的所有可用实例信息[^1][^3]。 - 智能路由:根据负载均衡策略,从实例列表中选择一个发起请求。若没有网关和注册中心,手动写死服务实例地址时,Feign 默认不能自动完成负载均衡,需配合Ribbon或其他负载均衡组件来实现[^1][^2][^3]。 ### 使用方法 Feign是一个声明式WebService客户端,使用Feign能让编写Web Service客户端更加简单。使用方法是定义一个接口,然后在上面添加注解,同时也支持JAX - RS标准的注解。示例如下: ```java import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.GetMapping; // 定义Feign客户端,指定服务名 @FeignClient(name = "user-service") public interface UserFeignClient { // 定义接口方法,映射到目标服务的API @GetMapping("/users") String getUsers(); } ``` 在上述代码中,定义了一个名为`UserFeignClient`的Feign客户端接口,它对应服务名为`user-service`的服务,并定义了一个`getUsers`方法,用于调用目标服务的`/users`接口。 ### 配置 在Spring Cloud项目中使用Feign进行负载均衡,通常需要进行以下配置: 1. **添加依赖**:在`pom.xml`中添加Feign和Ribbon(或Spring Cloud新版的LoadBalancer)的依赖。 ```xml <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-ribbon</artifactId> </dependency> ``` 2. **启用Feign**:在主应用类上添加`@EnableFeignClients`注解。 ```java import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.openfeign.EnableFeignClients; @SpringBootApplication @EnableFeignClients public class MyApplication { public static void main(String[] args) { SpringApplication.run(MyApplication.class, args); } } ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值