Offer to come, dig friends take it! I am participating in the 2022 Spring Recruitment series -Debug recording task, click to view the details of the activity.

I. Problem Description:

Spring Cloud has been updated to 2021.0.1. Today we are going to learn about OpenFeign.

Fegin definition: Feign is a declarative Web service client. It makes writing Web service clients much easier. To use Feign, create an interface and annotate it. It has pluggable annotation support, including Feign annotations and JAX-RS annotations. Feign also supports pluggable encoders and decoders. Spring Cloud adds support for Spring MVC annotations and the use of HttpMessageConverters annotations used by default in Spring Web. Spring Cloud integrates Ribbon with Eureka and Spring Cloud LoadBalancer to provide a load-balanced HTTP client when using Feign.

Ii. Solutions:

Dependencies and Configurations

Build. gradle files and YML configuration files are mainly defined since

The build.gradle file is configured as follows. I used Spring-boot 2.6.4 and Spring-Cloud 2021.0.1.

Plugins {id 'Java' id 'org.springframework.boot' version '2.6.4' id 'io.spring. Dependency -management' version '1.0.11.RELEASE'} group 'com.ssm' version = '0.0.1-SNAPSHOT' sourceCompatibility = 1.8 repositories {mavenLocal() maven { url 'https://maven.aliyun.com/nexus/content/groups/public/' } } ext { set('springCloudVersion', Dependencies "2021.0.1")} {implementation 'org. Springframework. The boot: spring - the boot - starter - web' implementation 'org.springframework.boot:spring-boot-starter-actuator' implementation 'org.springframework.cloud:spring-cloud-starter-openfeign' testImplementation 'org.springframework.boot:spring-boot-starter-test' } dependencyManagement { imports { mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}" } }Copy the code

Yml file I mainly modified the port and define the service name, need to be added synchronously.

server:
  port: 8081
spring:
  application:
    name: order-service
Copy the code

Start files and feign calls

The startup class adds @enableFeignClients to ensure that the @FeignClient interface can also be scanned.

@EnableFeignClients
@SpringBootApplication
public class OrderApp {

    public static void main(String[] args) { SpringApplication.run(OrderApp.class); }}Copy the code

Microservice invocation interface

  1. The service logical interface is added@FeignClientConfiguring to invoke the Provider service.
@feignClient (name = "payments-service ", url = "127.0.0.1:8091", path = "/payment")
public interface PaymentFegin {

    @PostMapping("/create")
    PaymentVo create(@RequestBody @Validated PaymentDto paymentDto);
}
Copy the code
  1. PaymentFeginInject it into the calling class
@Slf4j
@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private PaymentFegin paymentFegin;

    @GetMapping("/create")
    public OrderVo create(@Validated OrderDto orderDto) {
        log.info("uri:/order/create lang:{}", LocaleContextHolder.getLocale().getLanguage());
        OrderVo orderVo = new OrderVo();
        orderVo.setId(1L);
        orderDto.setCode("ORT0000001");

        PaymentDto paymentDto = new PaymentDto();
        paymentDto.setId(orderDto.getId());
        paymentDto.setCode(orderDto.getCode());
        paymentFegin.create(paymentDto);
        returnorderVo; }}Copy the code

Log print

  1. Set the log LEVEL

Feign provides log printing, which can be configured to adjust the log level to understand the details of HTTP requests in Fegin (in other words, to monitor and output calls to the Feign interface).

  1. The log levels are as follows
  • NONE: By default, no logs are displayed
  • BASIC: Records only the request method, URL, response status code, and execution time
  • HEADERS: In addition to the custom information in BASIC, there are request and response HEADERS
  • In addition to the information defined in HEADERS, there is also the body of the request and response and metadata.
  1. The statement code
@Configuration
public class FeignConfig {

    @Bean
    Logger.Level feignLoggerLevel(a) {
        returnLogger.Level.FULL; }}Copy the code

Iii. Summary:

Feign is a declarative Web services client that makes writing Web services clients very easy by creating an interface and adding annotations to the interface.

Reference address: Spring-cloud-OpenFeign