Springcloud-Alibaba 〖八〗OpenFeign篇

PS: github倉庫倉庫地址項目都放到裏面了

一. 什麼是Feign?

Feign 是一個聲明式 WebService 客戶端。使用 Feign 能讓編寫Web Service 客戶端更加簡單。

Feign 旨在使編寫Java Http 客戶端變得更容易。
前面在使用 Ribbon+RestTemplate時,利用RestTemplate 對http請求的封裝處理,形成了一套模板化的調用方法。但是在實際開發中,由於對服務依賴的調用可能不止一處,往往一個接口會被多處調用,所以通常都會針對每個微服務自行封裝一些客戶端類來包裝這些依賴服務的調用。所以,Feign 在此基礎上做了進一步封裝,由他來幫助我們定義和實現依賴服務接口的定義。在Feign 的實現下,我們只需創建一個接口並使用註解的方式來配置它(以前是Dao接口上面標註Mapper註解,現在是一個微服務接口上面標註衣一個Feign註解即可),即可完成對服務提供方的接口綁定,簡化了使用Spring Cloud Ribbon時,自動封裝服務調用客戶端的開發量。

Feign集成了Ribbon
利用Ribbon維護了Payment 的服務列表信息,並且通過輪詢實現了客戶端的負載均衡。而與Ribbon不同的是,通過feign 只需要定義服務綁定接口且以聲明式的方法,優雅而簡單的實現了服務調用。

二. OpenFeign與Feign區別

OpenFeign Feign
OpenFeign是springcloud在Feign的基礎上支持了SpringMVC的註解,如@RequestMapping等等。OpenFeign的@FeignClient可以解析SpringMVC的@RequestMapping註解下的接口,並通過動態代理的方式產生實現類,實現類中做負載均衡並調用其他服務。 Feign是Springcloud組件中的一個輕量級Restful的HTTP服務客戶端,Feign內置了Ribbon,用來做客戶端負載均衡,去調用服務註冊中心的服務。Feign的使用方式是:使用Feign的註解定義接口,調用這個接口,就可以調用服務註冊中心的服務

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>

三. 服務調用cloud-consumer-feign-order80模塊

3.1 目錄結構

在這裏插入圖片描述

3.2 改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建 application.yml 配置文件

server:
  port: 80

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

3.4 主啓動類

要開啓Feign就要在主啓動類上加@EnableFeignClients

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 業務類

service接口

這裏對應的是我們8001/8002 服務的service
在這裏插入圖片描述

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);
}

這裏只實現接口,並不實現方法,因爲我們是去8001/8002集羣去調用服務

Controller層

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 測試

在這裏插入圖片描述

四. 超時控制

由於Feign天生支持Ribbon所以在超時控制這塊由Ribbon來控制
在這裏插入圖片描述
添加配置文件(默認1s)

#設置feign 客戶端超時時間(openFeign默認支持ribbon)
ribbon:
  #指的是建立連接後從服務器讀取到可用資源所用的時間
  ReadTimeout: 5000
  #指的是建立連接所用的時間,適用於網絡狀況正常的情況下,兩端連接所用的時間
  ConnectTimeout: 5000

五. 日誌打印

5.1 日誌功能

Feign 提供了日誌打印功能,可以通過配置來調整日誌級別,從而瞭解 Feign 中 Http 請求的細節。
說白了就是對接口的調用情況進行監控和輸出

5.2 日誌級別

  • NONE:默認的,不顯示任何日誌
  • BASIC:僅記錄請求方法、URL、響應狀態碼及執行時間
  • HEADERS:除了 BASIC 中定義的信息之外,還有請求和響應的頭信息
  • FULL:除了 HEADERS 中定義的信息之外,還有請求和響應的正文及元數據

5.3 添加配置類

添加一個配置類在這裏插入圖片描述

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;
    }
}

增加配置文件 application.yml

logging:
  level:
    #feign日誌以什麼級別監控哪個接口
    com.atguigu.springcloud.service.PaymentFeignService: debug

要暴露哪個服務,把服務的全限定名加到level配置文件下面

OpenFeign篇完,點贊+關注!

白嫖什麼的不存在的~

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章