Original address :Spring Cloud introduction And Feign piece ( 3、 ... and )

Blog address :http://www.extlight.com

One 、 Preface

In the last article 《Spring Cloud introduction And Ribbon piece ( Two )》 This paper introduces the in Ribbon Using load balancing to call microservices , But there is a problem : Each request method of the consumer needs to splice the URL Address , There are hard coding problems and it does not conform to the idea of object-oriented programming . If the service name changes , The consumer side also needs to be modified .

This article will introduce Feign To solve the problems above .

Two 、 Brief introduction

Feign It's a declarative one Web Service client . Use Feign Can make writing Web Service The client is simpler , Support and at the same time Eureka、Ribbon Combined to support load balancing .

Spring Cloud Yes Feign It was packaged , To support Spring MVC Standard notes and HttpMessageConverters.

Feign The way to use it is to define an interface , Then add... To it @FeignClient annotation .

3、 ... and 、 Practical drill

This test case is based on the case described in the previous published article , If you are not sure, please move to 《Spring Cloud introduction And Ribbon piece ( Two )》 Browse .

3.1 Add dependency

stay common-api and order-server Add dependency in project :

<!-- feign -->

3.2 Define a new interface

stay common-api Create a new interface in the project :

public interface GoodsServiceClient { @RequestMapping("/goods/goodsInfo/{goodsId}")
public Result goodsInfo(@PathVariable("goodsId") String goodsId);

Use @FeignClient The annotation specifies the name of the microservice to be called , Encapsulates the call USER-API The process of , Call template as consumer .

Be careful :Feign The definition of interface is best related to that of external development controller The method definitions in are consistent , The definition here is similar to goods-server In the project controller The method of class definition is consistent .

3.3 Modify call

stay order-server In the project , Use GoodsServiceClient Access to product information :

public class OrderServiceImpl implements OrderService{ // @Autowired
// private RestTemplate restTemplate; @Autowired
private GoodsServiceClient goodsServiceClient; @Override
public void placeOrder(Order order) throws Exception{ //Result result = this.restTemplate.getForObject("http://GOODS/goods/goodsInfo/" + order.getGoodsId(), Result.class); Result result = this.goodsServiceClient.goodsInfo(order.getGoodsId()); if (result != null && result.getCode() == 200) {
System.out.println("===== Place the order ====");
} }

Use it directly Feign Encapsulating the template calls the server , Free from trouble URL Splicing problem , So as to realize object-oriented programming .

3.4 start-up Feign function

Add on the startup class @EnableEeignClients annotation :

@EnableFeignClients(basePackages = {"com.extlight.springcloud"})
public class OrderServerApplication { public static void main(String[] args) {
SpringApplication.run(OrderServerApplication.class, args);

because order-server The project quoted common-api Medium GoodsServiceClient, It's different from a project , To instantiate objects , So you need to @EnableFeignClients Add the package path to be scanned in the annotation .

Use Postman Request order system , The result of the request is as follows :

The request is successful , because Feign Encapsulates the Ribbon, It also realizes the function of load balancing .

Four 、 Source code of case

Feign demo Source code

Spring Cloud introduction And Feign piece ( 3、 ... and ) More articles about

  1. Spring Cloud introduction And Hystrix piece ( Four )

    Original address :Spring Cloud introduction And Hystrix piece ( Four ) Blog address :http://www.extlight.com One . Preface In microservice applications , There is a certain dependency relationship between services , If a target service calls ...

  2. Spring Cloud introduction And Config piece ( 6、 ... and )

    Original address :Spring Cloud introduction And Config piece ( 6、 ... and ) Blog address :http://www.extlight.com One . Preface As the business expands , To facilitate the development and maintenance of the project , We usually split big projects ...

  3. Spring Cloud introduction And Zuul piece ( 5、 ... and )

    Original address :Spring Cloud introduction And Zuul piece ( 5、 ... and ) Blog address :http://www.extlight.com One . Preface As the business expands , Micro services will not increase , Correspondingly, it's open to the outside world API Interface ...

  4. Spring Cloud introduction And Ribbon piece ( Two )

    Original address :Spring Cloud introduction And Ribbon piece ( Two ) Blog address :http://www.extlight.com One . Preface Last one <Spring Cloud introduction And Eureka ...

  5. Spring Cloud introduction And Eureka piece ( One )

    Original address :Spring Cloud introduction And Eureka piece ( One ) Blog address :http://www.extlight.com One . Preface Spring Cloud It's an ordered set of frames . It USES Sp ...

  6. &lt;Spring Cloud&gt; Introduction 4 Feign

    1.Feign It used to be Ribbon+RestTemplate call , It's called by the name of the microservice , Load balancing But for interface programming , Provides Feign 2. Realization 2.1 Introduce coordinates stay ms-comm ...

  7. Spring Cloud Introductory tutorial ( 6、 ... and ): In declarative form REST client Feign Call remote HTTP service

    Let's start with a brief explanation of what a declarative implementation is ? One thing to do , There are three things you need to know ,where, what, how. That is, where ( where) In what way (how) What do you do (what). When to do (when) We include ho ...

  8. Spring Cloud Introductory tutorial ( 3、 ... and ): Configure auto refresh

    Configuration management , Only when the application starts will it read GIT The content of , After that, as long as the application does not restart ,GIT Modification of the document in , The app can't perceive , Even if rebooted Config Server Not good either. . Like the last unit (Spring Cloud ...

  9. Spring Cloud Introductory tutorial ( 8、 ... and ): Circuit breaker index data monitoring Hystrix Dashboard and Turbine

    1. Hystrix Dashboard ( Circuit breaker :hystrix The dashboard )  Hystrix A very important function is , Can pass HystrixCommand Collect relevant data and indicators . Hystrix Dashboa ...

Random recommendation

  1. yuv rgb Pixel format 1

    =========== size ============= commonly , The video data collected directly is RGB24 The format of RGB24 The size of a frame size=width×heigth×3 Byte, RGB32 Of size=wid ...

  2. POJ 1781 In Danger Joseph Ring Bit operation solution

    Joseph Ring , This time the mold is fixed 2. Suppose it's not a fixed mode 2, So the general time efficiency is O(n). But this time, because of the fixed die 2, So be able to take advantage of 2 The particularity of , Improve time efficiency to O(1). The law can be seen in the figure below : watermark/2/tex ...

  3. MUI Development record —— My attendance

    It's been a long time since we updated our technology blog , Because I've been working on cross platform recently web app Application development , Because I just did this , I don't have much experience to share with you , But I'm a busy person , I decided to take time out of my busy schedule to sort out an article , Record the development history ......— ...

  4. Java Interview treasure -2017

    Java Interview treasure 2017 edition One . Java Base part ........................................................................... ...

  5. 【NLP】BLEU The full mark is 100 Points ?

    To solve this problem , The first thing you need to know is BLEU How the value is calculated . BLEU The full name is Bilingual Evaulation Understudy. It means bilingual assessment . So-called Understudy( As a substitute ), intend ...

  6. Python Foundation and advancement

    1 Python Foundation and advancement Welcome to Python The world Build the programming environment Variable | character string | notes | Error elimination He only uses one picture , Just put Python I got the list in ! Use If Statement for conditional testing It's more accurate to use a dictionary ...

  7. Impression notes can't connect to the server (internet explore The problem of )


  8. Reading notes (chapter7)

    Chapter vii. link Linking is the process of collecting various parts of code and data and combining them into a single file .1. This file can be loaded into memory and executed :2. It can also be executed at load time , That is, when the program is loaded into the memory by the loader and executed :3. It can even be carried out in transportation ...

  9. bootstrap Add... To the design site � Code highlighting plug-in

    The name of this plug-in is google-code-prettify The effect before using the plug-in : The effect of using plug-ins : Next step : (1) Download two files http://codecloud.sinaapp.com/ ...

  10. 【markdown】 markdown grammar

    Introduce several markdown Grammar learning address and related tools Reference link coding gitlab markdown offical markdown editor markdown editor2