当前位置:网站首页>[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);
}
边栏推荐
- On the necessity of building a video surveillance convergence platform and its scenario application
- 测试开发必备技能:安全测试漏洞靶场实战
- MySQL gets the current date of the year
- Google Earth engine (GEE) - global flood database V1 (2000-2018)
- 2022新版nft源码中国元宇宙数字藏品艺术品交易平台源码
- 2022年材料员-通用基础(材料员)操作证考试题库及答案
- The latest examination questions and answers for the eight members (standard members) of Liaoning architecture in 2022
- Why is the frame rate calculated by opencv wrong?
- PMP考试成绩多久出来?这些你务必知道!
- How do I get the STW (pause) time of a GC (garbage collector)?
猜你喜欢

Distributed transaction - Final consistency scheme based on message compensation (local message table, message queue)

Learning Tai Chi Maker - mqtt Chapter 2 (V) heartbeat mechanism

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

Meta universe standard forum established
![[NOIP2002 普及组] 过河卒](/img/6c/31fa210e08c7fd07691a1c5320154e.png)
[NOIP2002 普及组] 过河卒

Audio and video technology development weekly

并发之wait/notify说明

如何学习可编程逻辑控制器(PLC)?

Code understanding: implementing volume models for hangwriten text recognition

CUPTI error: CUPTI could not be loaded or symbol could not be found.
随机推荐
2022年安全员-A证考试题库及模拟考试
求一个能判断表中数据,只填充不覆盖的sql
Google Earth engine (GEE) - global flood database V1 (2000-2018)
通过例子学习Rust
改性三磷酸盐研究:Lumiprobe氨基-11-ddUTP
Learning Tai Chi Maker - mqtt Chapter 2 (V) heartbeat mechanism
电源插座是如何传输电的?困扰小伙伴这么多年的简单问题
PMP考试成绩多久出来?这些你务必知道!
Principle and implementation of SSD for target detection
!‘cat‘ 不是内部或外部命令,也不是可运行的程序 或批处理文件。
OracleData安装问题
Detailed reading of the thesis: implementing volume models for handowriting text recognition
One article explains in detail | those things about growth
店铺进销存管理系统源码
基于微信小程序的婚纱影楼门户小程序
代码理解:IMPROVING CONVOLUTIONAL MODELS FOR HANDWRITTEN TEXT RECOGNITION
Necessary skills for test and development: actual combat of security test vulnerability shooting range
大促场景下,如何做好网关高可用防护
[Matlab bp regression prediction] GA Optimized BP regression prediction (including comparison before optimization) [including source code 1901]
Standard particle swarm optimization C language program