首页 > 其他分享 >Spring Cloud Feign

Spring Cloud Feign

时间:2023-02-07 09:22:11浏览次数:43  
标签:Feign 调用 Spring Eureka Cloud 使用 Ribbon 客户端

Feign的作用和定位,与Ribbon的关系

Feign 是一个声明式的 HTTP 客户端,提供了简化 HTTP 客户端调用的功能。它使用注解和接口来定义和发送请求,可以简化很多客户端代码。Feign可以帮助我们更快捷、优雅地调用HTTP API。

   

Ribbon 是一个客户端负载均衡器,可以根据配置的策略,将请求分配到不同的服务实例上。Feign 和 Ribbon 都是 Netflix 的开源项目,Feign 可以在它的客户端代码中集成 Ribbon 进行负载均衡。

因此,Feign 和 Ribbon 可以说是配套使用的,Feign 可以简化客户端代码,Ribbon 可以实现客户端的负载均衡,两者一起可以提供简单高效的客户端调用方案。

fegin的使用

不使用fegin

public class ExampleWithoutFeign {

public static void main(String[] args) {

// 创建 REST 客户端

RestTemplate restTemplate = new RestTemplate();

   

// 发送 GET 请求,获取结果

String result = restTemplate.getForObject("http://user-service/users", String.class);

System.out.println(result);

}

}

使用feign

@FeignClient(value = "user-service")

public interface UserServiceClient {

@GetMapping("/users")

String getUsers();

}

   

public class ExampleWithFeign {

public static void main(String[] args) {

// 创建应用上下文

ApplicationContext context = new SpringApplicationBuilder(ExampleWithFeign.class)

.web(WebApplicationType.NONE)

.run();

   

// 从上下文中获取 UserServiceClient 实例

UserServiceClient client = context.getBean(UserServiceClient.class);

   

// 通过 UserServiceClient 实例发送请求,获取结果

String result = client.getUsers();

System.out.println(result);

}

}

Ribbon的缺点:

1.在实际生产中,Ribbon调用服务提供者,通过restTemplate调用,缺点是,多个地方调用,同一个请求要写多次,不方便统一维护;

2.调用方式,与传统的MVC方式不一样,原来是直接注入Service,所有在实际使用我们跟希望保持与原来的一致;

   

Tips:

  • 为什么加上@EnableFeignClients 注解需要注释掉@EnableEurekaClient

@EnableFeignClients注解是用来开启Feign的功能,而@EnableEurekaClient注解是用来开启Eureka客户端的功能。当一个应用需要使用Feign进行服务调用时,需要加上@EnableFeignClients注解。如果该应用既要使用Eureka客户端,又要使用Feign,那么两个注解都需要加上。但是,如果加上了@EnableEurekaClient注解,而又不需要使用Eureka客户端,那么可以注释掉该注解。

这样做的原因是因为Feign自带了负载均衡功能,所以如果要使用Feign进行服务调用,则不需要使用Eureka的负载均衡功能,因此需要注释掉@EnableEurekaClient注解。

  • 既然fegin使用了负载均衡,为什么还需要用eureka

Feign是通过Ribbon来实现负载均衡的,但是需要注册到服务注册中心(例如Eureka)来管理服务发现和消费。注册到Eureka中后,Eureka可以维护一份服务的实例列表,Feign通过Eureka来发现和选择可用的服务实例进行调用,这样可以保证服务的高可用性和容错性。

总而言之,Eureka主要用于服务发现和管理,而Feign则主要用于服务调用,两者结合可以实现一个完整的服务消费者架构。

  • 使用fegin还需要使用ribbon吗

Feign 库内部已经集成了 Ribbon,因此在使用 Feign 时一般不需要再显式地使用 Ribbon。但是在某些特殊情况下,可能会希望直接使用 Ribbon,此时可以选择不使用 Feign。

   

  • 是否可以理解为,feign使服务的调用看起来像本地调用

是的,Feign 可以使服务间调用看起来像本地调用,它帮助隐藏了通信的细节,如 Http 请求和序列化,使得调用服务的代码更简洁易读。通过 Feign,我们可以使用声明式接口定义客户端,以便与远程服务交互。

  • 什么是声明式接口

"Declarative interface" refers to an interface that specifies the desired outcome of a piece of code, rather than how to achieve it. In the context of software development, this term is often used to describe APIs or libraries that allow developers to express their requirements in a high-level, abstract way, leaving the details of implementation to the underlying framework. This approach can help simplify the development process, increase the readability and maintainability of code, and provide a more abstract and flexible programming model.

   

   

   

   

   

 

标签:Feign,调用,Spring,Eureka,Cloud,使用,Ribbon,客户端
From: https://www.cnblogs.com/tulucc/p/17097277.html

相关文章