当前位置:网站首页>[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);
}
边栏推荐
猜你喜欢

短视频本地生活版块成为热门,如何把握新的风口机遇?

Multi thread implementation rewrites run (), how to inject and use mapper file to operate database

Excel将一行的内容进行复制时,列与列之间是用制表符“\t”进行分隔的

Sword finger offer 53 - I. find the number I in the sorted array (improved bisection)

学习太极创客 — MQTT 第二章(六)MQTT 遗嘱
![[NOIP2002 普及组] 过河卒](/img/6c/31fa210e08c7fd07691a1c5320154e.png)
[NOIP2002 普及组] 过河卒

Necessary skills for test and development: actual combat of security test vulnerability shooting range

mysql----where 1=1是什么意思

Sword finger offer 49 Ugly number (three finger needling technique)

Huawei's 9-year experience as a software testing director
随机推荐
判断对象中是否存在某一个属性
基于微信小程序的婚纱影楼门户小程序
Congratulations to myself, official account has more than ten thousand fans
控制器的功能和工作原理
lotus v1.16.0 calibnet
PMP考试成绩多久出来?这些你务必知道!
求一个能判断表中数据,只填充不覆盖的sql
Code understanding: implementing volume models for hangwriten text recognition
Google Earth Engine(GEE)——全球洪水数据库 v1 (2000-2018年)
Differences between pragma and ifndef
高通平台 Camera 之 MCLK 配置
Analysis of distributed transaction solution Seata golang
Feign remote call fallback callback failed, no effect
项目经理考完PMP就够了?不是的!
[early knowledge of activities] list of recent activities of livevideostack
并发之wait/notify说明
【微服务|OpenFeign】OpenFeign快速入门|基于Feign的服务调用
PCR/qPCR研究:Lumiprobe丨dsGreen 用于实时 PCR
[noip2002 popularization group] cross the river pawn
Matlab exercises -- routine operation of matrix