Springcloud-Alibaba〗 〖eight articles OpenFeign

A. What is Feign?

Feign is a declarative WebService client. Feign make use of written Web Service clients easier.

Feign designed to make writing Java Http client easier.
When using the foregoing Ribbon + RestTemplate, using RestTemplate http request of the encapsulation process, forming a set of templates of the calling method. But in the actual development, due to the dependence of the call to service may be more than one, the interface is often a multiple call, so usually each micro-service package for some clients like to wrap themselves depend on these services call. So, Feign On this basis, made a further package by him to help us define and implement the definition of dependent services interface. In the realization of Feign, we need to create an interface and use annotations way to configure it ( previously marked Dao Interface Mapper above notes, now is a microblogging service interface on top of clothing marked a comment to Feign ), to complete the service provider interface binding side, simplifying the amount of development time using Spring Cloud Ribbon, automatic packaging service calls the client.

Feign integrated Ribbon
using the Ribbon maintains a list information Payment Services, and load balancing achieved by polling the client. The difference is that with the Ribbon, by feign only need to define the binding interface services and to methods declarative, elegant and simple implementation of a service call.

Two. OpenFeign and Feign difference

OpenFeign Feign
OpenFeign is springcloud support SpringMVC comment on the basis of Feign, such as @RequestMapping and so on. OpenFeign of @FeignClient interfaces can be resolved under @RequestMapping notes SpringMVC and generate dynamic proxy implementation class by the way, do load balancing implementation class and calls to other services. Feign is a lightweight HTTP service client Restful the Springcloud components, Feign built Ribbon, used to make the client load balancing, to call the service registration service center. Feign use is: Feign using annotations to define interfaces, call this interface, you can call the service registry service

OpenFeign

<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency> 

Feign

 <dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>

III. Service calls cloud-consumer-feign-order80 module

3.1 Directory Structure

Here Insert Picture Description

3.2 change pom

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>cloud2020</artifactId>
        <groupId>com.aiguigu.springcloud</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>cloud-consumer-feign-order80</artifactId>


    <dependencies>
        <!--openfeign-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
        <!--eureka client-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <!--引入自定义的api通用包,可以使用Payment支付Entity-->
        <dependency>
            <groupId>com.atguigu.springcloud</groupId>
            <artifactId>cloud-api-commons</artifactId>
            <version>${project.version}</version>
        </dependency>
        <!--web-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <!--一般基础通用配置-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.aiguigu.springcloud</groupId>
            <artifactId>cloud-api-commons</artifactId>
            <version>1.0-SNAPSHOT</version>
            <scope>compile</scope>
        </dependency>
    </dependencies>
</project>

3.3 build application.yml profile

server:
  port: 80

eureka:
  client:
    register-with-eureka: false
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka

3.4 Master Boot class

To turn Feign necessary to add @EnableFeignClients on the main startup class

package com.atguigu.springcloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;

@SpringBootApplication
@EnableFeignClients
public class OrderOpenFeignMain80 {
    public static void main(String[] args) {
        SpringApplication.run(OrderOpenFeignMain80.class,args);
    }
}

3.5 Business Class

service interfaces

Here is the corresponding service of our 8001/8002 service
Here Insert Picture Description

package com.atguigu.springcloud.service;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import com.atguigu.springcloud.entities.CommonResult;

@FeignClient(value = "cloud-payment-service")
@Component
public interface PaymentFeignService {

    @GetMapping(value = "/payment/get/{id}")
    public CommonResult getPaymentById(@PathVariable("id") Long id);
}

Here only implements the interface method does not achieve, because we are going to call the 8001/8002 cluster service

Controller layer

package com.atguigu.springcloud.controller;

import com.atguigu.springcloud.entities.CommonResult;
import com.atguigu.springcloud.service.PaymentFeignService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
public class OrderFeignController {

    @Resource
    private PaymentFeignService paymentFeignService;

    @GetMapping(value = "/consumer/payment/get/{id}")
    public CommonResult getPaymentById(@PathVariable("id") Long id){
        CommonResult paymentById = paymentFeignService.getPaymentById(id);
        return paymentById ;
    }
}

3.6 Test

Here Insert Picture Description

IV. Timeout control

Since Feign so naturally support Ribbon control this timeout is controlled by the Ribbon
Here Insert Picture Description
Add Profile (default 1s)

#设置feign 客户端超时时间(openFeign默认支持ribbon)
ribbon:
  #指的是建立连接后从服务器读取到可用资源所用的时间
  ReadTimeout: 5000
  #指的是建立连接所用的时间,适用于网络状况正常的情况下,两端连接所用的时间
  ConnectTimeout: 5000

V. Log Print

5.1 logging

Feign printing function provides a log, the log level can be adjusted by arranging, in order to understand the details Feign Http request.
Bluntly the situation is to call the interface to monitor and output

5.2 log level

  • NONE: default, does not show any log
  • BASIC: Only requests recording method, URL, and the response status code execution time
  • HEADERS: BASIC addition to the information defined in the header information as well as request and response
  • FULL: HEADERS defined in addition to the information, as well as text and metadata request and response

5.3 Adding configuration class

Adding a configuration classHere Insert Picture Description

package com.atguigu.springcloud.config;

import feign.Logger;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class FeignConfig {
    @Bean
    Logger.Level feignloggerLevel(){
        return Logger.Level.FULL;
    }
}

Increase the profile application.yml

logging:
  level:
    #feign日志以什么级别监控哪个接口
    com.atguigu.springcloud.service.PaymentFeignService: debug

Exposure to which service, the fully qualified name of the service level profile is added to the following

OpenFeign finished article, thumbs + attention!

White whore or something that does not exist -

Published 86 original articles · won praise 77 · views 10000 +

Guess you like

Origin blog.csdn.net/kingtok/article/details/105090682
Recommended