当前位置:网站首页>【微服务|OpenFeign】OpenFeign快速入门|基于Feign的服务调用
【微服务|OpenFeign】OpenFeign快速入门|基于Feign的服务调用
2022-06-28 04:21:00 【步尔斯特】
OpenFeign
OpenFeign为微服务架构下服务之间的调用提供了解决方案,OpenFeign是一种声明式、模板化的HTTP客户端。
在Spring Cloud中使用OpenFeign,可以做到使用HTTP请求访问远程服务,就像调用本地方法一样的,开发者完全感知不到这是在调用远程方法,更感知不到在访问HTTP请求。
OpenFeign可以用来简化HTTP的调用,前面我们使用的RestTemplate实现REST API调用,代码大致如下:
@GetMapping("/buy/{id}")
public Product order() {
Product product = restTemplate.getForObject("http://shop-serviceproduct/product/1", Product.class);
return product; }
由代码可知,我们是使用拼接字符串的方式构造URL的,该URL只有一个参数。但是,在现实中,URL中往往含有多个参数。这时候我们如果还用这种方式构造URL,那么就会非常痛苦。
feign客户端调用的事项:
- 如果请求参数没有加上注解的话,默认采用post请求发送。
- 服务的名称命名不能够有下划线,只能使用中划线,否则会报下列的错。
feign和opoenfeign
- 他们底层都是内置了Ribbon,去调用注册中心的服务。
- Feign是Netflix公司写的,是SpringCloud组件中的一个轻量级RESTful的HTTP服务客户端,是SpringCloud中的第一代负载均衡客户端。
- OpenFeign是SpringCloud自己研发的,在Feign的基础上支持了Spring MVC的注解,如@RequesMapping等等。是SpringCloud中的第二代负载均衡客户端。
- Feign本身不支持Spring MVC的注解,使用Feign的注解定义接口,调用这个接口,就可以调用服务注册中心的服务。
- OpenFeign的@FeignClient可以解析SpringMVC的@RequestMapping注解下的接口,并通过动态代理的方式产生实现类,实现类中做负载均衡并调用其他服务。
Feign是Springcloud组件中的一个轻量级Restful的HTTP服务客户端,Feign内置了Ribbon,用来做客户端负载均衡,去调用服务注册中心的服务。Feign的使用方式是:使用Feign的注解定义接口,调用这个接口,就可以调用服务注册中心的服务。
<!--feign-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
OpenFeign是springcloud在Feign的基础上支持了SpringMVC的注解,如@RequestMapping等等。OpenFeign的@FeignClient可以解析SpringMVC的@RequestMapping注解下的接口,并通过动态代理的方式产生实现类,实现类中做负载均衡并调用其他服务。
<!--openfeign-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
基于Feign的服务调用
引入依赖
在服务消费者 shop_service_order 添加Fegin依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
启动类添加Feign的支持
/** * 消费者 * * @author issavior */
@EnableFeignClients("com.ossa.common.feignapi")
@EnableDiscoveryClient
@SpringBootApplication(scanBasePackages = "com.ossa")
public class ConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class, args);
}
}
通过@EnableFeignClients注解开启Spring Cloud Feign的支持功能
启动类激活FeignClient
创建一个Feign接口,此接口是在Feign中调用微服务的核心接口
在服务消费者 shop_service_order 添加一个 ProductFeginClient 接口
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
/** * @author issavior */
@FeignClient(value = "ossa-service-producer")
@RequestMapping(value = "/producer")
public interface ProducerFeign {
/** * 根据ID查询商品 * * @param id 商品的主键ID * @return 相关商品的信息 */
@GetMapping(value = "/{id}")
String producerById(@PathVariable(value = "id") String id);
}
- 定义各参数绑定时,@PathVariable、@RequestParam、@RequestHeader等可以指定参数属性,在Feign中绑定参数必须通过value属性来指明具体的参数名,不然会抛出异常
- @FeignClient:注解通过name指定需要调用的微服务的名称,用于创建Ribbon的负载均衡器。
所以Ribbon会把 ossa-service-producer 解析为注册中心的服务。
配置请求提供者的调用接口
修改 OrderController ,添加ProductFeginClient的自动注入,并在order方法中使用ProductFeginClient 完成微服务调用。
@Autowired
private ProducerFeign producerFeign;
@GetMapping("/feign/{id}")
public String feignConsumerById(@PathVariable(value = "id") String id) {
return producerFeign.producerById(id);
}
边栏推荐
- One article explains in detail | those things about growth
- How do I get the STW (pause) time of a GC (garbage collector)?
- Flexible IP network test tool -- x-launch
- C语言全局变量(c文件和h文件中的全局变量、静态全局变量)使用注意事项
- Web3来临时的风口浪尖
- With the transformation of automatic empowerment, Feihe dairy accelerates its move towards digitalization!
- Why is the frame rate calculated by opencv wrong?
- Mise en place d'un cadre d'essai d'automatisation de l'interface utilisateur - - rédaction d'une application d'automatisation
- Distributed transaction - Final consistency scheme based on message compensation (local message table, message queue)
- 100+数据科学面试问题和答案总结 - 机器学习和深度学习
猜你喜欢

CI & CD must be known!

Matlab exercises -- routine operation of matrix

Why are cloud vendors targeting this KPI?

With favorable policies, more than 20 provinces and cities have launched the yuanuniverse development plan

MySQL gets the current date of the year

Project practice! Teach you JMeter performance test hand in hand

Web3来临时的风口浪尖

【Matlab BP回归预测】GA优化BP回归预测(含优化前的对比)【含源码 1901期】

IP数据报的发送和转发过程

灵活的IP网络测试工具——— X-Launch
随机推荐
How do I get the STW (pause) time of a GC (garbage collector)?
The growth summer challenge is coming | learn and create two major tracks, and start the tutor registration!
To quickly download JDK, in addition to the official Oracle download, is there a download address for the latest version available in China
A queue of two stacks
leetcode:714. The best time to buy and sell stocks includes handling fee [DP dual status]
Congratulations to myself, official account has more than ten thousand fans
flinkcdc采集oracle,oracle数据库是CDB的
Severe tire damage: the first rock band in the world to broadcast live on the Internet
Sword finger offer 49 Ugly number (three finger needling technique)
Find an SQL that can judge the data in the table and only fill in the SQL that is not overwritten
Establishment of SSH Framework (Part 2)
CUPTI error: CUPTI could not be loaded or symbol could not be found.
Multithreading and high concurrency V: detailed explanation of wait queue, executor and thread pool (key)
Idle interrupt cannot be cleared
2022年中国音频市场年度综合分析
UI自動化測試框架搭建 —— 編寫一個APP自動化
On the necessity of building a video surveillance convergence platform and its scenario application
Notepad++ -- column editing mode -- Usage / instance
2022年中國音頻市場年度綜合分析
C语言全局变量(c文件和h文件中的全局变量、静态全局变量)使用注意事项