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

SpringCloud OpenFeign基本介紹與實(shí)現(xiàn)示例

 更新時(shí)間:2023年02月21日 16:44:53   作者:尚少  
OpenFeign源于Netflix的Feign,是http通信的客戶端。屏蔽了網(wǎng)絡(luò)通信的細(xì)節(jié),直接面向接口的方式開發(fā),讓開發(fā)者感知不到網(wǎng)絡(luò)通信細(xì)節(jié)。所有遠(yuǎn)程調(diào)用,都像調(diào)用本地方法一樣完成

介紹

  在上面一篇介紹Nacos的文章最后,兩個(gè)服務(wù)的相互調(diào)用是用的RestTemplate類完成的。但這種方式不是很推薦,更佳的方式是用OpenFeign組件去調(diào)用。OpenFeign是官方推出的服務(wù)調(diào)用和負(fù)載均衡組件,基于Ribbon和Hystrix,前身是第一代Spring Cloud的Feign,對(duì)Feign進(jìn)行了擴(kuò)展,支持了SpringMvc的相關(guān)注解。

常用注解

  OpenFeign是使用接口+注解實(shí)現(xiàn)的,因此了解它的常用注解是必要的,有以下幾個(gè):

@EnableFeignClients:在啟動(dòng)類上添加,用于開啟OpenFeign功能。當(dāng)項(xiàng)目啟動(dòng)時(shí),會(huì)掃描帶有@FeignClient的接口,生成代理類并注冊(cè)到Spring容器中

@FeignClient:通知OpenFeign組件對(duì)該注解下的接口進(jìn)行解析,通過動(dòng)態(tài)代理的方式產(chǎn)生實(shí)現(xiàn)類,完成服務(wù)調(diào)用

@RequestMapping:SpringMvc中的注解,不過此時(shí)該注解表示發(fā)起Request請(qǐng)求(默認(rèn)Get方式)

@GetMapping:SpringMvc中的注解,不過此時(shí)該注解表示發(fā)起Get請(qǐng)求

@PostMapping:SpringMvc中的注解,不過此時(shí)該注解表示發(fā)起Post請(qǐng)求

代碼實(shí)現(xiàn)

  首先得把Nacos啟動(dòng)

  服務(wù)提供方,

  bootstrap.yml:

server:
  port: 8083
  servlet:
    context-path: /nacosProvider

spring:
  application:
    name: nacos-provider
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848

  引入依賴:

<dependencies>
    <dependency>
        <groupId>com.alibaba.cloud</groupId>
        <artifactId>spring-cloud-starter-alibaba-nacos-discovery
        </artifactId>
        <version>2.2.0.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
    </dependency>
</dependencies>

  服務(wù)方法:

@Controller
@RequestMapping("/provide")
public class ProviderController {
    @RequestMapping("/distribute")
    @ResponseBody
    public String distribute() {
        return "吃雞胸肉";
    }
    @RequestMapping("/distribute1")
    @ResponseBody
    public String distribute1(String name, Integer age) {
        return "姓名:" + name + ",年齡:" + age;
    }
    @PostMapping("/distribute2")
    @ResponseBody
    public String distribute2(@RequestBody Person p) {
        return "身高:" + p.getHeight() + ",膚色:" + p.getSkin();
    }
}
import lombok.Data;
@Data
public class Person {
    private Integer height;
    private String skin;
}

  服務(wù)調(diào)用方,

  bootstrap.yml

server:
  port: 8082
  servlet:
    context-path: /nacosInvoke

spring:
  application:
    name: nacos-invoke
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848

  引入依賴:

<dependencies>
    <dependency>
        <groupId>com.alibaba.cloud</groupId>
        <artifactId>spring-cloud-starter-alibaba-nacos-discovery
        </artifactId>
        <version>2.2.0.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-openfeign</artifactId>
        <version>2.2.0.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
    </dependency>
</dependencies>

  啟動(dòng)類上添加注解:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;
//開啟OpenFeign
@EnableFeignClients
@SpringBootApplication
public class NacosInvokeApplication {
    public static void main(String[] args) {
        SpringApplication.run(NacosConfigApplication.class, args);
    }
}

  創(chuàng)建@FeignClient修飾的接口:

import com.gs.nacos_invoke.dto.Person;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
/**
 * value值是服務(wù)提供方的服務(wù)名稱
 */
@FeignClient(value = "nacos-provider")
public interface InvokeClient {
    @GetMapping("/nacosProvider/provide/distribute")
    String distribute();
    @GetMapping("/nacosProvider/provide/distribute1")
    String distribute1(@RequestParam("name") String name, 
    @RequestParam("age") Integer age);
    @PostMapping("/nacosProvider/provide/distribute2")
    String distribute2(@RequestBody Person p);
}

  Person類(服務(wù)調(diào)用方再創(chuàng)建一個(gè),不是同一個(gè)):

import lombok.Data;
@Data
public class Person {
    private Integer height;
    private String skin;
}

  編寫控制器,使用接口請(qǐng)求提供方的服務(wù):

import com.gs.nacos_invoke.dto.Person;
import com.gs.nacos_config.feign.InvokeClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private InvokeClient invokeClient;
    @GetMapping("/invoke")
    public void invoke(String name, Integer age) {
        String str = invokeClient.distribute();
        System.out.println(str);
    }
    @GetMapping("/invoke1")
    public void invoke1() {
        String str = invokeClient.distribute1("coder", 20);
        System.out.println(str);
    }
    @GetMapping("/invoke2")
    public void invoke2() {
        Person p = new Person();
        p.setHeight(183);
        p.setSkin("黃皮膚");
        String s = invokeClient.distribute2(p);
        System.out.println(s);
    }
}

注意事項(xiàng)

  OpenFeign是基于Ribbon的,所以它默認(rèn)是負(fù)載均衡的。其次,它也是基于Hystrix的,有超時(shí)降級(jí)的處理:默認(rèn)服務(wù)提供方的接口超時(shí)時(shí)間是1s,超過1s服務(wù)調(diào)用方會(huì)報(bào)錯(cuò)。1s是可以配置的,按照業(yè)務(wù)需要調(diào)整。@FeignClient注解有個(gè)fallback屬性,當(dāng)該屬性有值時(shí),服務(wù)提供方超時(shí),會(huì)返回程序所指定的降級(jí)值。

  服務(wù)調(diào)用方,bootstrap.yml添加(更規(guī)范的做法是引入spring-cloud-starter-alibaba-nacos-config依賴,nacos中新建配置,然后在這個(gè)配置中添加):

feign:
  hystrix:
    enabled: true
ribbon:
    # 請(qǐng)求連接的超時(shí)時(shí)間
    ConnectionTimeout: 3000
    # 請(qǐng)求處理的超時(shí)時(shí)間
    ReadTimeout: 3000

hystrix:
    command:
        default:
            execution:
                isolation:
                    thread:
                        timeoutInMilliseconds: 3000

  接口修改為:

import com.gs.nacos_invoke.dto.Person;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
@FeignClient(value = "nacos-provider",
        fallback = InvokeClientFallback.class)
public interface InvokeClient {
    @GetMapping("/nacosProvider/provide/distribute")
    String distribute();
    @GetMapping("/nacosProvider/provide/distribute1")
    String distribute1(@RequestParam("name") String name, 
    @RequestParam("age") Integer age);
    @PostMapping("/nacosProvider/provide/distribute2")
    String distribute2(@RequestBody Person p);
}

  fallback指定的類:

import com.gs.nacos_invoke.dto.Person;
import org.springframework.stereotype.Component;
@Component
public class InvokeClientFallback implements InvokeClient {
    @Override
    public String distribute() {
        return "超時(shí)3s";
    }
    @Override
    public String distribute1(String name, Integer age) {
        return "超時(shí)3s";
    }
    @Override
    public String distribute2(Person p) {
        return "超時(shí)3s";
    }
}

  服務(wù)提供方ProviderController類的方法中,加入Thread.sleep(4000);或者throw new RuntimeException("拋異常");來觸發(fā)降級(jí)(拋出未捕獲的異常也能觸發(fā))。

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

相關(guān)文章

  • spring打包到j(luò)ar包的問題解決

    spring打包到j(luò)ar包的問題解決

    這篇文章主要給大家介紹了關(guān)于spring打包到j(luò)ar包遇到的問題的解決方法,文中通過實(shí)例代碼結(jié)束的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者使用spring打包具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-08-08
  • mybatis-plus 自定義 Service Vo接口實(shí)現(xiàn)數(shù)據(jù)庫實(shí)體與 vo 對(duì)象轉(zhuǎn)換返回功能

    mybatis-plus 自定義 Service Vo接口實(shí)現(xiàn)數(shù)據(jù)庫實(shí)體與 vo

    這篇文章主要介紹了mybatis-plus 自定義 Service Vo接口實(shí)現(xiàn)數(shù)據(jù)庫實(shí)體與 vo 對(duì)象轉(zhuǎn)換返回功能,本文通過實(shí)例圖文相結(jié)合給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧
    2024-08-08
  • java中JVM中如何存取數(shù)據(jù)和相關(guān)信息詳解

    java中JVM中如何存取數(shù)據(jù)和相關(guān)信息詳解

    這篇文章主要介紹了JVM中如何存取數(shù)據(jù)和相關(guān)信息詳解,Java源代碼文件(.java后綴)會(huì)被Java編譯器編譯為字節(jié)碼文件,然后由JVM中的類加載器加載各個(gè)類的字節(jié)碼文件,加載完畢之后,交由JVM執(zhí)行引擎執(zhí)行。JVM中怎么存取數(shù)據(jù)和相關(guān)信息呢?,需要的朋友可以參考下
    2019-06-06
  • Mybatis之如何攔截慢SQL日志記錄

    Mybatis之如何攔截慢SQL日志記錄

    這篇文章主要介紹了Mybatis之如何攔截慢SQL日志記錄問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-05-05
  • SpringBoot響應(yīng)處理實(shí)現(xiàn)流程詳解

    SpringBoot響應(yīng)處理實(shí)現(xiàn)流程詳解

    這篇文章主要介紹了SpringBoot響應(yīng)處理實(shí)現(xiàn)流程,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧
    2022-10-10
  • Java語法基礎(chǔ)之循環(huán)結(jié)構(gòu)語句詳解

    Java語法基礎(chǔ)之循環(huán)結(jié)構(gòu)語句詳解

    這篇文章主要為大家詳細(xì)介紹了Java語法基礎(chǔ)之循環(huán)結(jié)構(gòu)語句,感興趣的小伙伴們可以參考一下
    2016-09-09
  • SpringCloud中的Seata基本介紹與安裝教程

    SpringCloud中的Seata基本介紹與安裝教程

    Seata 是一款開源的分布式事務(wù)解決方案,致力于提供高性能和簡單易用的分布式事務(wù)服務(wù),這篇文章主要介紹了SpringCloud之Seata基本介紹與安裝,需要的朋友可以參考下
    2024-01-01
  • Python安裝Jupyter Notebook配置使用教程詳解

    Python安裝Jupyter Notebook配置使用教程詳解

    這篇文章主要介紹了Python安裝Jupyter Notebook配置使用教程詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-09-09
  • EasyCode整合mybatis-plus的配置詳解

    EasyCode整合mybatis-plus的配置詳解

    本文主要介紹了EasyCode整合mybatis-plus的配置詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-09-09
  • Java簡單實(shí)現(xiàn)UDP和TCP的示例

    Java簡單實(shí)現(xiàn)UDP和TCP的示例

    下面小編就為大家?guī)硪黄狫ava簡單實(shí)現(xiàn)UDP和TCP的示例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-11-11

最新評(píng)論

巴彦淖尔市| 大埔区| 武陟县| 云浮市| 舞钢市| 乐至县| 昭觉县| 青河县| 临城县| 蚌埠市| 东莞市| 高要市| 河池市| 偃师市| 金溪县| 宝应县| 额尔古纳市| 石门县| 光泽县| 治县。| 东阳市| 扶余县| 嘉义市| 鲁甸县| 五指山市| 呼玛县| 洮南市| 那坡县| 通州区| 南投市| 万山特区| 德州市| 陕西省| 信阳市| 温宿县| 章丘市| 刚察县| 平和县| 门源| 电白县| 丹棱县|