Technology Sharing

Grain Mall Learning Notes-22-Distributed Components-SpringCloud-OpenFeign Test Remote Call

2024-07-11

한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina


In the previous section, we learned about the registration center. We know that before server A calls server B, it will obtain the IP address of service B from the registration center, and then server A will send a request to service B.

So how does server A make a request to service B?

This is the main issue to be addressed in this section.

In this project, the OpenFeign component is used to complete the calls between services. OpenFeign is an encapsulation of Http requests.

1. Introduction to OpenFeign

OpenFeign is a declarative HTTP client that aims to make remote calls easier.

Feign provides a template for HTTP requests. By writing a simple interface and inserting annotations, you can define the parameters, format, address and other information of the HTTP request.

Feign integrates Ribbon (load balancing) and Hystrix (service fuse), so we no longer need to use these two components explicitly.

SpringCloudFeign extends the support for SpringMVC annotations based on NetflixFeign. Under its implementation, we only need to create an interface and configure it with annotations to complete the interface binding of the service provider. This simplifies the development of SpringCloudRibbon's self-encapsulated service call client.

2. Steps to use OpenFeign

1. Scenario description

We will learn how to use OpenFeign with a scenario-based problem.会员服务To call优惠券服务An interface that returns the coupon information that the member can use.

The interface of this coupon is as follows. This is an interface used for testing and has no real business logic.
insert image description here

	@RequestMapping("member/list")
    public R list(){
        CouponEntity couponEntity = new CouponEntity();
        couponEntity.setCouponName("慢100减80");

        return R.ok().put("page", Arrays.asList(couponEntity));
    }