当前位置:网站首页>[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);
}
边栏推荐
- Feign implements path escape through custom annotations
- 109. 简易聊天室12:实现客户端一对一聊天
- 高通平台 Camera 之 MCLK 配置
- [Matlab bp regression prediction] GA Optimized BP regression prediction (including comparison before optimization) [including source code 1901]
- 知识点滴 - 关于汉语学习的网络资源
- Unity out ref params
- Function and working principle of controller
- Necessary skills for test and development: actual combat of security test vulnerability shooting range
- Feign通过自定义注解实现路径的转义
- MySQL gets the current date of the year
猜你喜欢

Feign implements path escape through custom annotations

Oracledata installation problems

Mask's miserable and inspirational childhood, who is introverted by campus violence

Meta universe standard forum established
![[csp-j2020] excellent splitting](/img/05/90f9cf71791b3cdc37073eaf5db989.png)
[csp-j2020] excellent splitting

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

2022高处安装、维护、拆除考试题及答案

开关电源电压型与电流型控制

电源插座是如何传输电的?困扰小伙伴这么多年的简单问题

IP数据报的发送和转发过程
随机推荐
Find an SQL that can judge the data in the table and only fill in the SQL that is not overwritten
Where does the storm go? Whose pot is the weather forecast wrong?
Unity out ref params
Feign通过自定义注解实现路径的转义
Is it better for a novice to open a securities account? Is it safe to open a stock account
学习太极创客 — MQTT 第二章(六)MQTT 遗嘱
RxSwift --(1)创建一个项目
论文详读:IMPROVING CONVOLUTIONAL MODELS FOR HANDWRITTEN TEXT RECOGNITION
Mise en place d'un cadre d'essai d'automatisation de l'interface utilisateur - - rédaction d'une application d'automatisation
109. 简易聊天室12:实现客户端一对一聊天
Huawei's 9-year experience as a software testing director
2022西式面点师(高级)考试试题模拟考试平台操作
native关键字的作用
基于微信小程序的婚纱影楼门户小程序
Flexible IP network test tool -- x-launch
并发之wait/notify说明
Severe tire damage: the first rock band in the world to broadcast live on the Internet
S32ds jump to defaultisr
What to do when MySQL changes the password and reports an error
2022年安全员-A证考试题库及模拟考试