当前位置:网站首页>【微服务|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);
}
边栏推荐
- 几百行代码实现一个脚本解释器
- 如何遍历collections.OrderedDict,服了又忘记items
- Audio and video technology development weekly
- On the necessity of building a video surveillance convergence platform and its scenario application
- Necessary skills for test and development: actual combat of security test vulnerability shooting range
- Standard particle swarm optimization C language program
- 玩转双指针
- Is it true that qiniu business school gives away securities accounts? Is it safe to open an account
- inherit
- CI & CD must be known!
猜你喜欢

Design a stack with getmin function

Pager when importing text files from MySQL
![[noip2002 popularization group] cross the river pawn](/img/6c/31fa210e08c7fd07691a1c5320154e.png)
[noip2002 popularization group] cross the river pawn

On the necessity of building a video surveillance convergence platform and its scenario application

Project practice! Teach you JMeter performance test hand in hand

Oracledata installation problems

浅析搭建视频监控汇聚平台的必要性及场景应用

How do I get the STW (pause) time of a GC (garbage collector)?

CI & CD must be known!

测试开发必备技能:安全测试漏洞靶场实战
随机推荐
Differences between pragma and ifndef
Severe tire damage: the first rock band in the world to broadcast live on the Internet
[matlab traffic light identification] traffic light identification [including GUI source code 1908]
JS reverse massive star map sign signature
Project practice! Teach you JMeter performance test hand in hand
[applet] solution document using font awesome Font Icon (picture and text)
Database garbled
Detailed reading of the thesis: implementing volume models for handowriting text recognition
Principle of event delegation
控制器的功能和工作原理
在线直播源码,JS动态效果之,侧边栏滚动固定效果
mysql修改密码报错需要怎么做
Distributed transaction - Final consistency scheme based on message compensation (local message table, message queue)
Lamaba expression learning and common functional interfaces
Is it true that qiniu business school gives away securities accounts? Is it safe to open an account
If mysqlcdc sets multiple parallelism, will the incremental data repeat?
[CSP-J2020] 优秀的拆分
On the necessity of building a video surveillance convergence platform and its scenario application
Code understanding: implementing volume models for hangwriten text recognition
Why is the frame rate calculated by opencv wrong?