当前位置:网站首页>[microservices openfeign] openfeign quick start service invocation based on feign
[microservices openfeign] openfeign quick start service invocation based on feign
2022-06-28 04:57:00 【Bulst】
List of articles
OpenFeign
OpenFeign It provides a solution for the call between services under the microservice architecture ,OpenFeign It's declarative 、 templated HTTP client .
stay Spring Cloud Use in OpenFeign, It can be used HTTP Request access to remote services , It's like calling a local method , Developers are totally unaware that this is a call to a remote method , More imperceptible in the visit HTTP request .
OpenFeign Can be used to simplify HTTP Call to , We used RestTemplate Realization REST API call , The code looks like this :
@GetMapping("/buy/{id}")
public Product order() {
Product product = restTemplate.getForObject("http://shop-serviceproduct/product/1", Product.class);
return product; }
From the code , We use concatenated strings to construct URL Of , The URL There is only one parameter . however , In reality ,URL It often contains multiple parameters . At this time, if we still construct in this way URL, Then it will be very painful .
feign Matters to be called by the client :
- If the request parameter is not annotated , By default post Request to send .
- The service name cannot be underlined , Only middle dash can be used , Otherwise, the following errors will be reported .
feign and opoenfeign
- They are all built-in at the bottom Ribbon, To call the registry service .
- Feign yes Netflix Written by the company , yes SpringCloud One of the lightweight components RESTful Of HTTP Service client , yes SpringCloud The first generation load balancing client in .
- OpenFeign yes SpringCloud Self developed , stay Feign Based on the support of Spring MVC Annotations , Such as @RequesMapping wait . yes SpringCloud Second generation load balancing client in .
- Feign Itself does not support Spring MVC Annotations , Use Feign The annotation defines the interface , Call this interface , You can call the service in the service registry .
- OpenFeign Of @FeignClient Can be parsed SpringMVC Of @RequestMapping Interface under annotation , And through the way of dynamic proxy to produce the implementation class , Load balancing in the implementation class and calling other services .
Feign yes Springcloud One of the lightweight components Restful Of HTTP Service client ,Feign Built in Ribbon, It is used for client load balancing , To call the service in the service registry .Feign Is used in the following way : Use Feign The annotation defines the interface , Call this interface , You can call the service in the service registry .
<!--feign-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
OpenFeign yes springcloud stay Feign Based on the support of SpringMVC Annotations , Such as @RequestMapping wait .OpenFeign Of @FeignClient Can be parsed SpringMVC Of @RequestMapping Interface under annotation , And through the way of dynamic proxy to produce the implementation class , Load balancing in the implementation class and calling other services .
<!--openfeign-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
be based on Feign Service call for
Introduce dependencies
In serving consumers shop_service_order add to Fegin rely on
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
Startup class add Feign Support for
/** * consumer * * @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);
}
}
adopt @EnableFeignClients Annotations to open Spring Cloud Feign Support functions of
Start class activation FeignClient
Create a Feign Interface , This interface is in Feign The core interface for invoking microservices in
In serving consumers shop_service_order Add one ProductFeginClient Interface
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 {
/** * according to ID Query products * * @param id The primary key of the item ID * @return Information about related products */
@GetMapping(value = "/{id}")
String producerById(@PathVariable(value = "id") String id);
}
- When defining the binding of each parameter ,@PathVariable、@RequestParam、@RequestHeader Parameter properties can be specified , stay Feign The binding parameter in must pass value Property to specify the specific parameter name , Otherwise, an exception will be thrown
- @FeignClient: Comments by name Specify the name of the micro service to be called , Used to create Ribbon Load balancer for .
therefore Ribbon Will be able to ossa-service-producer Service resolved to registry .
Configure the calling interface of the request provider
modify OrderController , add to ProductFeginClient Automatic injection , And in order Method used in ProductFeginClient Complete the microservice call .
@Autowired
private ProducerFeign producerFeign;
@GetMapping("/feign/{id}")
public String feignConsumerById(@PathVariable(value = "id") String id) {
return producerFeign.producerById(id);
}
边栏推荐
- CI & CD must be known!
- C语言中函数是什么?编程中的函数与数学中的函数区别?理解编程语言中的函数
- 活性染料研究:Lumiprobe AF594 NHS 酯,5-异构体
- 摄像头基础知识
- 100+ data science interview questions and answers Summary - machine learning and deep learning
- C语言全局变量(c文件和h文件中的全局变量、静态全局变量)使用注意事项
- 并发之wait/notify说明
- 深度强化学习笔记
- 玩转双指针
- Severe tire damage: the first rock band in the world to broadcast live on the Internet
猜你喜欢

Severe tire damage: the first rock band in the world to broadcast live on the Internet

Pager when importing text files from MySQL

Binary sort tree: BST

wordpress zibll子比主题6.4.1开心版 免授权

MySQL gets the current date of the year

基于微信小程序的婚纱影楼门户小程序

测试开发必备技能:安全测试漏洞靶场实战

native关键字的作用

2022年安全员-A证考试题库及模拟考试

DH parameters of robotics and derivation using MATLAB symbolic operation
随机推荐
Audio and video technology development weekly
Unity delegate
Meta universe standard forum established
Huawei's 9-year experience as a software testing director
活性染料研究:Lumiprobe AF594 NHS 酯,5-异构体
Binary sort tree: BST
How do I get the STW (pause) time of a GC (garbage collector)?
Light collector, Yunnan Baiyao!
CUPTI error: CUPTI could not be loaded or symbol could not be found.
高通平台 Camera 之 MCLK 配置
Mask's miserable and inspirational childhood, who is introverted by campus violence
Pager when importing text files from MySQL
Cgo+gsoap+onvif learning summary: 8. Summary of arm platform cross compilation operation and common problems
Multi thread implementation rewrites run (), how to inject and use mapper file to operate database
2022电力电缆判断题模拟考试平台操作
穿越封锁的最新利器,速度最快梯没有之一。
CUPTI error: CUPTI could not be loaded or symbol could not be found.
羧酸研究:Lumiprobe 磺基花青7二羧酸
恭喜我自己,公众号粉丝破万
Detailed reading of the thesis: implementing volume models for handowriting text recognition