最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SpringCloud?Feign實(shí)現(xiàn)微服務(wù)之間相互請(qǐng)求問題

 更新時(shí)間:2022年06月26日 08:55:29   作者:鱷魚兒  
Feign是Netflix開發(fā)的聲明式、模板化的HTTP客戶端,?Feign可以幫助我們更快捷、優(yōu)雅地實(shí)現(xiàn)微服務(wù)之間的調(diào)用,這篇文章主要介紹了SpringCloud?Feign實(shí)現(xiàn)微服務(wù)之間相互請(qǐng)求,需要的朋友可以參考下

上篇文章說了通過RestTemplate實(shí)現(xiàn)微服務(wù)之間訪問:http://www.fzitv.net/article/252981.htm,這篇文章將通過Feign實(shí)現(xiàn)微服務(wù)之間訪問。
代碼基于RestTemplate實(shí)現(xiàn)微服務(wù)之間訪問基礎(chǔ)上進(jìn)行修改。

??Feign簡(jiǎn)介

Github:https://github.com/OpenFeign/feign

Feign是Netflix開發(fā)的聲明式、模板化的HTTP客戶端, Feign可以幫助我們更快捷、優(yōu)雅地實(shí)現(xiàn)微服務(wù)之間的調(diào)用。
1.Feign可幫助我們更加便捷,優(yōu)雅的調(diào)用HTTP API。
2.在SpringCloud中,使用Feign非常簡(jiǎn)單——創(chuàng)建一個(gè)接口,并在接口上添加一些注解,代碼就完成了。
3.Feign支持多種注解,例如Feign自帶的注解或者JAX-RS注解等。
4.SpringCloud對(duì)Feign進(jìn)行了增強(qiáng),使Feign支持了SpringMVC注解,并整合了Ribbon和Eureka,從而讓Feign的使用更加方便。

??Spring Cloud 組件依賴版本

官網(wǎng)文檔

本文參考使用組件依賴如下

??Feign實(shí)現(xiàn)服務(wù)之間訪問

創(chuàng)建微服務(wù)項(xiàng)目,結(jié)構(gòu)如下圖所示

root 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">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.ber</groupId>
    <artifactId>SpringCloud-Feign</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>pom</packaging>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.12.RELEASE</version>
        <relativePath/>
    </parent>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
        <java.version>1.8</java.version>
        <spring-boot.version>2.3.12.RELEASE</spring-boot.version>
        <spring-cloud-alibaba.version>2.2.8.RELEASE</spring-cloud-alibaba.version>
        <spring-cloud.version>Hoxton.SR12</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bootstrap</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>${spring-cloud-alibaba.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <modules>
        <!-- 注冊(cè)中心 -->
        <module>ber-nacos</module>
    </modules>

</project>

ber-nacos 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>SpringCloud-Feign</artifactId>
        <groupId>com.ber</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>ber-nacos</artifactId>
    <packaging>pom</packaging>

    <modules>
        <module>nacos-consumer</module>
        <module>nacos-provider</module>
        <module>nacos-consumer-feign</module>
    </modules>
</project>

?創(chuàng)建nacos-consumer-feign微服務(wù)

nacos-consumer-feign 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>ber-nacos</artifactId>
        <groupId>com.ber</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>nacos-consumer-feign</artifactId>


    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
        <!-- 負(fù)載均衡 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-loadbalancer</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <!-- feign -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
    </dependencies>

</project>

application.properties

spring.application.name=nacos-consumer-feign

spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848

server.port=8895

feign.hystrix.enabled=true

啟動(dòng)類,注意添加注解@EnableFeignClients、@EnableDiscoveryClient

package com.ber.nacos.feign;

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

/**
 * @author: ber
 * @date: 2022/6/25 0025 22:43
 * -------------------------------
 * Github:https://github.com/berbai
 * Blog:https://blog.csdn.net/Ber_Bai
 */
@EnableFeignClients
@EnableDiscoveryClient
@SpringBootApplication
public class FeignApplication {
    public static void main(String[] args) {
        SpringApplication.run(FeignApplication.class, args);
    }
}

controller接口,通過/queryMsg/{msgStr}訪問provider微服務(wù)接口

package com.ber.nacos.feign.controller;

import com.ber.nacos.feign.service.MsgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author: ber
 * @date: 2022/6/25 0025 22:46
 * -------------------------------
 * Github:https://github.com/berbai
 * Blog:https://blog.csdn.net/Ber_Bai
 */
@RestController
public class MsgController {

    @Autowired
    MsgService msgService;

    /**
     * 獲取消息
     *
     * @param msgStr 消息
     * @return
     */
    @GetMapping("/queryMsg/{msgStr}")
    public String getMsg(@PathVariable(value = "msgStr") String msgStr) {
        return msgService.getMsg(msgStr);
    }
}

創(chuàng)建feign client

FeignClient 默認(rèn)集成了 Ribbon,使用@FeignClient注解將MsgService 接口作為 FeignClient ,其屬性名稱與服務(wù)名稱nacos-provider對(duì)應(yīng)

package com.ber.nacos.feign.service;

import com.ber.nacos.feign.service.fallback.MsgServiceFallback;
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;

/**
 * @author: ber
 * @date: 2022/6/25 0025 22:54
 * -------------------------------
 * Github:https://github.com/berbai
 * Blog:https://blog.csdn.net/Ber_Bai
 */
@Component
@FeignClient(name = "nacos-provider", fallback = MsgServiceFallback.class)
public interface MsgService {

    @GetMapping("/getMsg/{msgStr}")
    String getMsg(@PathVariable("msgStr") String msgStr);
}

Feign的Fallback機(jī)制,在網(wǎng)絡(luò)請(qǐng)求時(shí),可能會(huì)出現(xiàn)異常請(qǐng)求,如果還想再異常情況下使系統(tǒng)可用,那么就需要容錯(cuò)處理,執(zhí)行設(shè)置的容錯(cuò)業(yè)務(wù)代碼。
當(dāng)接口請(qǐng)求不成功時(shí),就會(huì)調(diào)用MsgServiceFallback類這里的實(shí)現(xiàn)

package com.ber.nacos.feign.service.fallback;

import com.ber.nacos.feign.service.MsgService;
import org.springframework.stereotype.Component;

/**
 * @author: ber
 * @date: 2022/6/25 0025 22:58
 * -------------------------------
 * Github:https://github.com/berbai
 * Blog:https://blog.csdn.net/Ber_Bai
 */
@Component
public class MsgServiceFallback implements MsgService {
    @Override
    public String getMsg(String msgStr) {
        return "請(qǐng)求失敗";
    }
}

?nacos-provider微服務(wù)

代碼參考上篇文章《RestTemplate實(shí)現(xiàn)微服務(wù)之間訪問

??Feign微服務(wù)之間訪問測(cè)試

啟動(dòng)nacos-consumer-feignnacos-provider,訪問http://localhost:8848/nacos,使用 nacos/nacos 登陸后,可以發(fā)現(xiàn)服務(wù)列表中,兩個(gè)微服務(wù)已經(jīng)注冊(cè),如下圖所示。

訪問http://localhost:8895/queryMsg/hello,即consumer-feign的接口,可以得到如下圖所示的界面

?Feign容錯(cuò)機(jī)制

修改feign client代碼如下所示
改為@GetMapping("/getMsg-error/{msgStr}"),即原本的queryMsg會(huì)被請(qǐng)求到getMsg-error,而provider并沒有提供getMsg-error,此時(shí)feign會(huì)進(jìn)入容錯(cuò)機(jī)制。

package com.ber.nacos.feign.service;

import com.ber.nacos.feign.service.fallback.MsgServiceFallback;
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;

/**
 * @author: ber
 * @date: 2022/6/25 0025 22:54
 * -------------------------------
 * Github:https://github.com/berbai
 * Blog:https://blog.csdn.net/Ber_Bai
 */
@Component
@FeignClient(name = "nacos-provider", fallback = MsgServiceFallback.class)
public interface MsgService {

    @GetMapping("/getMsg-error/{msgStr}")
    String getMsg(@PathVariable("msgStr") String msgStr);
}

再次訪問http://localhost:8895/queryMsg/hello,即consumer-feign的接口,可以得到如下圖所示的界面

到此這篇關(guān)于SpringCloud Feign實(shí)現(xiàn)微服務(wù)之間相互請(qǐng)求的文章就介紹到這了,更多相關(guān)SpringCloud Feign微服務(wù)請(qǐng)求內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • MybatisPlus查詢條件空字符串和NULL問題背景分析

    MybatisPlus查詢條件空字符串和NULL問題背景分析

    文章詳細(xì)分析了MybatisPlus在處理查詢條件時(shí),空字符串和NULL值的問題,MP 3.3.0及以上版本提供了多種解決方法,包括在Bean屬性上使用注解、全局配置等,推薦使用全局配置的方式來解決這個(gè)問題,以避免在SQL查詢中出現(xiàn)不必要的空字符串條件,感興趣的朋友跟隨小編一起看看吧
    2025-03-03
  • JavaWeb實(shí)現(xiàn)學(xué)生信息管理系統(tǒng)(3)

    JavaWeb實(shí)現(xiàn)學(xué)生信息管理系統(tǒng)(3)

    這篇文章主要為大家詳細(xì)介紹了JavaWeb實(shí)現(xiàn)學(xué)生信息管理系統(tǒng)第三篇,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-08-08
  • Java設(shè)計(jì)模式中的外觀模式詳解

    Java設(shè)計(jì)模式中的外觀模式詳解

    外觀模式為多個(gè)復(fù)雜的子系統(tǒng),提供了一個(gè)一致的界面,使得調(diào)用端只和這個(gè)接口發(fā)生調(diào)用,而無須關(guān)系這個(gè)子系統(tǒng)內(nèi)部的細(xì)節(jié)。本文將通過示例詳細(xì)為大家講解一下外觀模式,需要的可以參考一下
    2023-02-02
  • java 實(shí)現(xiàn)單鏈表逆轉(zhuǎn)詳解及實(shí)例代碼

    java 實(shí)現(xiàn)單鏈表逆轉(zhuǎn)詳解及實(shí)例代碼

    這篇文章主要介紹了java 實(shí)現(xiàn)單鏈表逆轉(zhuǎn)實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下
    2017-02-02
  • Spring Boot非Web項(xiàng)目運(yùn)行配置的方法教程

    Spring Boot非Web項(xiàng)目運(yùn)行配置的方法教程

    這篇文章主要介紹了Spring Boot非Web項(xiàng)目運(yùn)行配置的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Spring Boot具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-09-09
  • idea提交文件時(shí)如何忽略某些文件的提交

    idea提交文件時(shí)如何忽略某些文件的提交

    這篇文章主要介紹了idea提交文件時(shí)如何忽略某些文件的提交問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-02-02
  • java文件操作代碼片斷實(shí)例實(shí)現(xiàn)統(tǒng)計(jì)文件中字母出現(xiàn)的個(gè)數(shù)功能

    java文件操作代碼片斷實(shí)例實(shí)現(xiàn)統(tǒng)計(jì)文件中字母出現(xiàn)的個(gè)數(shù)功能

    本文介紹java讀文件實(shí)例,實(shí)現(xiàn)統(tǒng)計(jì)某一目錄下每個(gè)文件中出現(xiàn)的字母?jìng)€(gè)數(shù)、數(shù)字個(gè)數(shù)、空格個(gè)數(shù)及行數(shù),除此之外沒有其他字符,大家參考使用吧
    2014-01-01
  • Alibaba?Fastjson之超好用的JOSN解析庫(kù)

    Alibaba?Fastjson之超好用的JOSN解析庫(kù)

    這篇文章主要介紹了Alibaba?Fastjson之超好用的JOSN解析庫(kù),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-10-10
  • JVM代碼運(yùn)行邏輯解讀

    JVM代碼運(yùn)行邏輯解讀

    這篇文章主要介紹了JVM代碼運(yùn)行邏輯,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2025-04-04
  • 淺談JDK8中的Duration Period和ChronoUnit

    淺談JDK8中的Duration Period和ChronoUnit

    在JDK8中,引入了三個(gè)非常有用的時(shí)間相關(guān)的API:Duration,Period和ChronoUnit。他們都是用來對(duì)時(shí)間進(jìn)行統(tǒng)計(jì)的,本文將會(huì)詳細(xì)講解一下這三個(gè)API的使用
    2021-06-06

最新評(píng)論

湟源县| 乌苏市| 工布江达县| 油尖旺区| 无棣县| 图木舒克市| 马龙县| 西乡县| 蒙城县| 北海市| 宁明县| 忻城县| 乐山市| 江川县| 涡阳县| 蓬安县| 桦川县| 益阳市| 屯留县| 江陵县| 饶河县| 西乌| 望奎县| 廉江市| 旬邑县| 舞阳县| 区。| 长乐市| 南部县| 罗定市| 阳山县| 遂平县| 伊川县| 含山县| 湖州市| 合肥市| 突泉县| 容城县| 称多县| 综艺| 工布江达县|