当前位置:网站首页>Analysis of fegin

Analysis of fegin

2022-06-22 21:46:00 InfoQ


One 、Feign

Basic concepts

  • Feign yes Netflix Developed declarative 、 templated HTTP client , Feign Can help us more quickly 、 Call... Gracefully HTTP API.\
  • Feign Support multiple annotations , for example Feign Own notes or JAX-RS Annotations etc. .\
  • Spring Cloud Yes Feign Enhanced , send Feign Support Spring MVC annotation , And integrated Ribbon and Eureka, So that Feign It is more convenient to use .\
  • Spring Cloud Feign Is based on Netflix feign Realization , Integrated Spring Cloud Ribbon and Spring Cloud Hystrix, In addition to providing the powerful functions of both , It also provides a declarative Web How the service client is defined .\
  • Spring Cloud Feign Help us define and implement the definition of dependent service interfaces . stay Spring Cloud feign Under the realization of , Just create an interface and configure it with annotations , The interface binding of the service provider can be completed , Simplified in use Spring Cloud Ribbon Encapsulate the development amount of the service invocation client .

Two 、 Implement service calls

1、 Add on the caller side pom rely on

stay statistics Add in microservice

You can delete the previously added ribbon rely on , because openfeign Encapsulated ribbon Load balancing

<!-- The service call -->

<dependency>

 <groupId>org.springframework.cloud</groupId>

 <artifactId>spring-cloud-starter-openfeign</artifactId>

</dependency>

2、 Start the class to add comments

@EnableFeignClients

3、 Create remote call packages and interfaces

establish client Bao He UcenterClient Interface

@FeignClient The annotation is used to specify which service to invoke function.  , The name is consistent with the name of the called service .

@GetMapping Annotation is used to map the address of the invoked microservice .

@PathVariable The annotation must specify the parameter name , Otherwise mistakes

@Component Annotations prevent , Inject... At other locations UcenterClient when idea Report errors

package com.guli.statistics.client;

@Component

@FeignClient(&quot;guli-ucenter&quot;)

public interface UcenterClient {



 /**

 *  Be careful : It must be written as  @PathVariable(&quot;day&quot;), In parentheses &quot;day&quot; It can't be less

 * @param day

 * @return

 */

 @GetMapping(value = &quot;/admin/ucenter/member/count-register/{day}&quot;)

 public R registerCount(@PathVariable(&quot;day&quot;) String day);

}

4、 Call microservices

modify DailyService Medium createStatisticsByDay Method , call client The interface

Realization

@Autowired

private UcenterClient ucenterClient;



@Transactional(rollbackFor = Exception.class)

@Override

public void createStatisticsByDay(String day) {



 // Delete the existing statistics object

 ...



 // Get statistics

 Integer registerNum = (Integer) ucenterClient.registerCount(day).getData().get(&quot;countRegister&quot;);

 ...



 // Create statistics object

 ...

}

5、Swagger test

start-up eureka、ucenter、statistics Three microservices

stay swagger Medium test

\

3、 ... and 、 Test load balancing

原网站

版权声明
本文为[InfoQ]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/173/202206222003467240.html