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

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

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

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

1、概述

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

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

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

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

Feign能干什么

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

Feign集成了Ribbon

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

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

FeignOpenFeign
Feign是Spring Cloud組件中的一個(gè)輕量級(jí)RESTful的HTTP服務(wù)客戶端
Feign內(nèi)置了Ribbon,用來(lái)做客戶端負(fù)載均衡,去調(diào)用服務(wù)注冊(cè)中心的服務(wù)。Feign的使用方式是:使用Feign的注解定義接口,調(diào)用這個(gè)接口,就可以調(diào)用服務(wù)注冊(cè)中心的服務(wù)
OpenFeign是Spring Cloud 在Feign的基礎(chǔ)上支持了SpringMVC的注解,如@RequesMapping等等。OpenFeign的@FeignClient可以解析SpringMVC的@RequestMapping注解下的接口,并通過(guò)動(dòng)態(tài)代理的方式產(chǎn)生實(shí)現(xiàn)類,實(shí)現(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ī)制,建議將圖片保存下來(lái)直接上傳(img-pA7pLsSP-1664844187557)(image/42、feign整合Ribbon.png)]

4、添加YAML配置文件

不將模塊注冊(cè)到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)建啟動(dòng)類

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

package com.zcl.springcloud;

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

/**
 * 描述:Feign項(xiàng)目啟動(dòng)類
 *
 * @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ù)的實(shí)例和調(diào)用地址,訪問(wèn)的就是8001對(duì)外暴露的地址

主啟動(dòng)類上必須使用@EnableFeignClients注解進(jìn)行激活開(kāi)啟

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ù)測(cè)試接口
 *
 * @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);
}

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

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;

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

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

    /**
     * 測(cè)試調(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、啟動(dòng)項(xiàng)目測(cè)試

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

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

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

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

3、OpenFeign超時(shí)控制

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

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

2、在消費(fèi)者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ù)測(cè)試接口
 *
 * @author zhong
 * @date 2022-09-19 13:35
 */
@Component
@FeignClient(value = "CLOUD-PAYMENT-SERVICE")
public interface PaymentFeignService {

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

3、在FeignOrder80消費(fèi)者控制器上添加接口訪問(wèn)

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

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

4、啟動(dòng)項(xiàng)目測(cè)試

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

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

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

后端超時(shí)報(bào)錯(cuò)

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

5、在YAML配置文件中開(kāi)啟超時(shí)連接

設(shè)置配置文件下面的ribbon連接時(shí)間后等待三秒訪問(wèn)正常不會(huì)再報(bào)錯(cuò)

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

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

4、OpenFeign日志打印功能

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

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

1、日記級(jí)別

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

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

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

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

2、配置日記級(jí)別配置類

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

返回的日記級(jí)別對(duì)應(yīng)上面的4個(gè)

package com.zcl.springcloud.config;

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

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

3、YAML配置文件開(kāi)啟日記信息打印

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

啟動(dòng)羨慕測(cè)試輸出內(nèi)容

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

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

相關(guān)文章

最新評(píng)論