欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

OpenFeign服務(wù)接口調(diào)用的過程詳解

 更新時間:2022年10月06日 10:29:18   作者:小鐘要學(xué)習(xí)?。?!  
Feign是一個聲明式WebService客戶端。使用Feign能讓編寫Web?Service客戶端更加簡單。它的使用方法是定義一個服務(wù)接口然后在上面添加注解,這篇文章主要介紹了OpenFeign服務(wù)接口調(diào)用,需要的朋友可以參考下

OpenFeign服務(wù)接口調(diào)用

1、概述

Feign是一個聲明式WebService客戶端。使用Feign能讓編寫Web Service客戶端更加簡單。它的使用方法是定義一個服務(wù)接口然后在上面添加注解。

Feign也支持可拔插式的編碼器和解碼器。Spring Cloud對Feign進(jìn)行了封裝,使其支持了Spring MVC標(biāo)準(zhǔn)注解和HttpMessageConverters。

Feign可以與Eureka和Ribbon組合使用以支持負(fù)載均衡

源碼地址:https://github.com/spring-cloud/spring-cloud-openfeign

Feign能干什么

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

Feign集成了Ribbon

利用Ribbon維護(hù)了Payment的服務(wù)列表信息,并且通過輪詢實現(xiàn)了客戶端的負(fù)載均衡。而與Ribbon不同的是,通過feign只需要定義服務(wù)綁定接口且以聲明式的方法,優(yōu)雅而簡單的實現(xiàn)了服務(wù)調(diào)用

Feign和OpenFeign兩者的區(qū)別

FeignOpenFeign
Feign是Spring Cloud組件中的一個輕量級RESTful的HTTP服務(wù)客戶端
Feign內(nèi)置了Ribbon,用來做客戶端負(fù)載均衡,去調(diào)用服務(wù)注冊中心的服務(wù)。Feign的使用方式是:使用Feign的注解定義接口,調(diào)用這個接口,就可以調(diào)用服務(wù)注冊中心的服務(wù)
OpenFeign是Spring Cloud 在Feign的基礎(chǔ)上支持了SpringMVC的注解,如@RequesMapping等等。OpenFeign的@FeignClient可以解析SpringMVC的@RequestMapping注解下的接口,并通過動態(tài)代理的方式產(chǎn)生實現(xiàn)類,實現(xiàn)類中做負(fù)載均衡并調(diào)用其他服務(wù)。
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>

2、OpenFeign使用步驟

1、創(chuàng)建接口和使用注解@FeignClient

2、新建模塊cloud-consumer-feign-order80

3、修改pom.xml依賴文件

<?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>springcloud2022</artifactId>
        <groupId>com.zcl.springcloud</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

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

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

    <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.zcl.springcloud</groupId>
            <artifactId>cloud-api-commons</artifactId>
            <version>1.0-SNAPSHOT</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>
        <!--一般基礎(chǔ)通用配置-->
        <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>
    </dependencies>

</project>

[外鏈圖片轉(zhuǎn)存失敗,源站可能有防盜鏈機(jī)制,建議將圖片保存下來直接上傳(img-pA7pLsSP-1664844187557)(image/42、feign整合Ribbon.png)]

4、添加YAML配置文件

不將模塊注冊到Eureka服務(wù)中心

server:
  port: 80

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

5、創(chuàng)建啟動類

既然使用的是OpenFeign那必須使用@EnableFeignClients注解進(jìn)行開啟

package com.zcl.springcloud;

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

/**
 * 描述:Feign項目啟動類
 *
 * @author zhong
 * @date 2022-09-19 13:32
 */
@SpringBootApplication
@EnableFeignClients
public class OrderFeignMain80 {
    public static void main(String[] args) {
        SpringApplication.run(OrderFeignMain80.class, args);
    }
}

6、業(yè)務(wù)類

業(yè)務(wù)邏輯接口+@FeignClient注解完成服務(wù)調(diào)用,找指定微服務(wù)的實例和調(diào)用地址,訪問的就是8001對外暴露的地址

主啟動類上必須使用@EnableFeignClients注解進(jìn)行激活開啟

package com.zcl.springcloud.service;

import com.zcl.springcloud.entities.CommonResult;
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;

/**
 * 描述:Feign業(yè)務(wù)測試接口
 *
 * @author zhong
 * @date 2022-09-19 13:35
 */
@Component
@FeignClient(value = "CLOUD-PAYMENT-SERVICE")
public interface PaymentFeignService {
    /**
     * 根據(jù)id去調(diào)用
     * @param id
     * @return
     */
    @GetMapping("/payment/get/{id}")
    CommonResult getPaymentById(@PathVariable("id") Long id);
}

上面定義的接口其實就是服務(wù)提供者對外暴露的接口

7、創(chuàng)建控制器

package com.zcl.springcloud.controller;

import com.zcl.springcloud.entities.CommonResult;
import com.zcl.springcloud.entities.Payment;
import com.zcl.springcloud.service.PaymentFeignService;
import lombok.extern.slf4j.Slf4j;
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;

/**
 * 描述:測試控制器
 *
 * @author zhong
 * @date 2022-09-19 14:11
 */
@Slf4j
@RestController
public class OrderFeignController {

    /**
     * 注入Feign業(yè)務(wù)接口
     */
    @Resource
    private PaymentFeignService paymentFeignService;

    /**
     * 測試調(diào)用服務(wù)接口
     * @param id
     * @return
     */
    @GetMapping("/consumer/payment/get/{id}")
    public CommonResult<Payment> getPayment(@PathVariable("id") Long id) {
        // 調(diào)用服務(wù)接口
        return paymentFeignService.getPaymentById(id);
    }
}

8、啟動項目測試

  • 先啟動7001、7002Eureka集群
  • 再啟動8001、8002兩個微服務(wù)提供者
  • 啟動OpenFeign項目80
  • 調(diào)用接口訪問http://localhost/consumer/payment/get/1

多請求兩回會發(fā)現(xiàn)一樣的有輪詢負(fù)載均衡的效果

Feign自帶負(fù)載均衡配置項

{
  "code": 200,
  "message": "查詢成功,當(dāng)前提供者端口號為:8002",
  "data": {
    "id": 1,
    "serial": "1515154151515"
  }
}

3、OpenFeign超時控制

1、在8001提供者上添加一個新的控制器

/**
 * 測試Feign程序訪問停止三秒
 * @return
 */
@GetMapping("/feign/timeout")
public String paymentFeignTimeOut(){
    try {
        // 程序停止三秒
        TimeUnit.SECONDS.sleep(3);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    return serverPort;
}

2、在消費者Feign80上添加接口

package com.zcl.springcloud.service;

import com.zcl.springcloud.entities.CommonResult;
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;

/**
 * 描述:Feign業(yè)務(wù)測試接口
 *
 * @author zhong
 * @date 2022-09-19 13:35
 */
@Component
@FeignClient(value = "CLOUD-PAYMENT-SERVICE")
public interface PaymentFeignService {

    /**
     * 測試請求三秒不響應(yīng)接口
     * @return
     */
    @GetMapping("/payment/feign/timeout")
    String paymentFeignTimeOut();
}

3、在FeignOrder80消費者控制器上添加接口訪問

/**
 * 注入Feign業(yè)務(wù)接口
 */
@Resource
private PaymentFeignService paymentFeignService;

/**
 * 測試超時
 * @return
 */
@GetMapping("/payment/feign/timeout")
public String paymentFeignTimeOut()
{
    return paymentFeignService.paymentFeignTimeOut();
}

4、啟動項目測試

  • 先自測訪問提供者的業(yè)務(wù)是否正常:http://localhost:8001/payment/feign/timeout(等待三秒再響應(yīng)結(jié)果)
  • 通過Feign-Order80消費者的接口測試調(diào)用提供者的訪問是否正常:http://localhost/consumer/payment/feign/timeout

默認(rèn)連接是1秒鐘,程序設(shè)置了三秒導(dǎo)致連接超時

[外鏈圖片轉(zhuǎn)存失敗,源站可能有防盜鏈機(jī)制,建議將圖片保存下來直接上傳(img-MhfmrywP-1664844187558)(image/43、Feign連接超時.png)]

后端超時報錯

[外鏈圖片轉(zhuǎn)存失敗,源站可能有防盜鏈機(jī)制,建議將圖片保存下來直接上傳(img-a4usrSfL-1664844187558)(image/44、后端超時.png)]

5、在YAML配置文件中開啟超時連接

設(shè)置配置文件下面的ribbon連接時間后等待三秒訪問正常不會再報錯

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

#設(shè)置feign客戶端超時時間(OpenFeign默認(rèn)支持ribbon)
ribbon:
  #指的是建立連接所用的時間,適用于網(wǎng)絡(luò)狀況正常的情況下,兩端連接所用的時間
  ReadTimeout: 5000
  #指的是建立連接后從服務(wù)器讀取到可用資源所用的時間
  ConnectTimeout: 5000

4、OpenFeign日志打印功能

Feign 提供了日志打印功能,我們可以通過配置來調(diào)整日志級別,從而了解 Feign 中 Http 請求的細(xì)節(jié)。

就是對Feign接口的調(diào)用情況進(jìn)行監(jiān)控和輸出

1、日記級別

NONE:默認(rèn)的,不顯示任何日志;

BASIC:僅記錄請求方法、URL、響應(yīng)狀態(tài)碼及執(zhí)行時間;

HEADERS:除了 BASIC 中定義的信息之外,還有請求和響應(yīng)的頭信息;

FULL:除了 HEADERS 中定義的信息之外,還有請求和響應(yīng)的正文及元數(shù)據(jù)。

2、配置日記級別配置類

注意不要導(dǎo)錯包

返回的日記級別對應(yīng)上面的4個

package com.zcl.springcloud.config;

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

/**
 * 描述:Feign日記級別配置
 *
 * @author zhong
 * @date 2022-09-19 15:27
 */
@Configuration
public class FeignConfig {
    @Bean
    Logger.Level feignLoggerLevel()
    {
        return Logger.Level.FULL;
    }
}

3、YAML配置文件開啟日記信息打印

logging:
  level:
    # feign日志以什么級別監(jiān)控哪個接口
    com.zcl.springcloud.service.PaymentFeignService: debug

啟動羨慕測試輸出內(nèi)容

[外鏈圖片轉(zhuǎn)存失敗,源站可能有防盜鏈機(jī)制,建議將圖片保存下來直接上傳(img-z2Fg4dyi-1664844187559)(image/45、Feign日記輸出內(nèi)容.png)]

到此這篇關(guān)于OpenFeign服務(wù)接口調(diào)用的文章就介紹到這了,更多相關(guān)OpenFeign接口調(diào)用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論