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

SpringBoot使用Feign進行服務間通信的實現(xiàn)示例代碼

 更新時間:2024年01月19日 11:10:03   作者:wx59bcc77095d22  
Feign是一個開源的Java HTTP客戶端,可以幫助我們在SpringBoot應用中快速構建和使用HTTP客戶端,方便實現(xiàn)服務間的通信,本文就來介紹一下SpringBoot使用Feign進行服務間通信的實現(xiàn)示例代碼,感興趣的可以了解一下

一、前言

在分布式系統(tǒng)中,服務間通信是非常常見的情況。Feign是一個開源的Java HTTP客戶端,可以幫助我們在SpringBoot應用中快速構建和使用HTTP客戶端,方便實現(xiàn)服務間的通信。與其他HTTP客戶端相比,F(xiàn)eign具有簡化 HTTP API定義、支持多種HTTP請求方法、支持請求和響應的壓縮、支持請求和響應的日志記錄、支持多種負載均衡器、支持自定義攔截器和錯誤處理器等特點。

二、SpringBoot集成

1.添加依賴

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
    <version>3.1.5</version>
</dependency>

2.啟用Feign客戶端

我們需要在啟動類上添加@EnableFeignClients注解,啟用Feign客戶端。

package com.example.nettydemo;

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


@SpringBootApplication
@EnableFeignClients
public class NettyDemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(NettyDemoApplication.class, args);
    }
}

3.定義Feign客戶端接口

@FeignClient注解里面的url指定需要請求的URL地址,name指定客戶端的名稱。

package com.example.nettydemo.feign;

import com.example.nettydemo.entity.User;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

/**
 * @author qx
 * @date 2023/12/28
 * @des Feign客戶端
 */
@FeignClient(url = "http://127.0.0.1:8090/user", name = "user")
public interface UserFeignClient {


    @GetMapping("/{id}")
    User selectUserById(@PathVariable("id") Long id);

}

4.定義目標控制層接口

package com.example.nettydemo.controller;

import com.example.nettydemo.entity.User;
import com.example.nettydemo.service.UserService;
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.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author qx
 * @date 2023/12/28
 * @des
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;


    @GetMapping("/{id}")
    public User selectUserById(@PathVariable("id") Long id) {
        return userService.getUserById(id);
    }
}

5.創(chuàng)建Feign測試控制層

package com.example.nettydemo.controller;

import com.example.nettydemo.entity.User;
import com.example.nettydemo.feign.UserFeignClient;
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.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author qx
 * @date 2023/12/28
 * @des Feign測試
 */
@RestController
@RequestMapping("/userFeign")
public class UserFeignController {

    @Autowired
    private UserFeignClient userFeignClient;

    @GetMapping("/{id}")
    public User getUserInfo(@PathVariable("id") Long id) {
        return userFeignClient.selectUserById(id);
    }
}

6.測試

我們先測試目標請求接口是否正確。

SpringBoot使用Feign進行服務間通信_通信

然后我們再使用Feign的方式請求接口的方式進行測試。

SpringBoot使用Feign進行服務間通信_通信_02

這樣我們使用Feign方式請求,成功請求目的地址獲取到了一樣的數(shù)據(jù)。

到此這篇關于SpringBoot使用Feign進行服務間通信的實現(xiàn)示例代碼的文章就介紹到這了,更多相關SpringBoot Feign服務間通信 內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • java的三種IO模型詳解(BIO、NIO、AIO)

    java的三種IO模型詳解(BIO、NIO、AIO)

    本文介紹了BIO、NIO和AIO三種不同的IO模型,分別分析了它們的工作機制、實現(xiàn)方式以及與BIO的對比,BIO是阻塞的,每個連接需要一個線程;NIO是同步非阻塞的,通過緩沖區(qū)和選擇器實現(xiàn)I/O多路復用;AIO是異步的,操作系統(tǒng)處理IO操作,完成后通知應用程序
    2024-11-11
  • Spring Gateway處理微服務的路由轉發(fā)機制

    Spring Gateway處理微服務的路由轉發(fā)機制

    我們詳細地介紹了Spring Gateway,這個基于Spring 5、Spring Boot 2和Project Reactor的API網(wǎng)關,通過這篇文章,我們可以清晰地看到Spring Gateway的工作原理,以及它的強大之處,感興趣的朋友一起看看吧
    2024-08-08
  • C++排序算法之桶排序原理及實現(xiàn)詳解

    C++排序算法之桶排序原理及實現(xiàn)詳解

    這篇文章主要介紹了C++排序算法之桶排序原理及實現(xiàn)詳解, C++ 桶排序是一種線性時間復雜度的排序算法,它通過將待排序元素分配到不同的桶中,然后對每個桶中的元素進行排序,最后將所有桶中的元素按順序合并得到有序序列,需要的朋友可以參考下
    2023-10-10
  • SpringMVC Idea 搭建 部署war的詳細過程

    SpringMVC Idea 搭建 部署war的詳細過程

    本文介紹了如何在IntelliJ IDEA中使用Maven模板創(chuàng)建一個Web項目,并詳細說明了如何配置web.xml、創(chuàng)建springmvc-servlet.xml和application.properties文件,以及如何使用Maven打包生成WAR文件并部署到Tomcat服務器,感興趣的朋友跟隨小編一起看看吧
    2025-01-01
  • Java線程池ForkJoinPool實例解析

    Java線程池ForkJoinPool實例解析

    這篇文章主要介紹了Java線程池ForkJoinPool實例解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-02-02
  • Java代碼中4種字符串拼接方式分析

    Java代碼中4種字符串拼接方式分析

    本文主要介紹了Java代碼中4種字符串拼接方式分析,主要介紹了“+”號、StringBuilder、StringJoiner、String#join,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-02-02
  • Spring boot中使用Spring-data-jpa方便快捷的訪問數(shù)據(jù)庫(推薦)

    Spring boot中使用Spring-data-jpa方便快捷的訪問數(shù)據(jù)庫(推薦)

    Spring Data JPA 是 Spring 基于 ORM 框架、JPA 規(guī)范的基礎上封裝的一套JPA應用框架,可使開發(fā)者用極簡的代碼即可實現(xiàn)對數(shù)據(jù)的訪問和操作。這篇文章主要介紹了Spring-boot中使用Spring-data-jpa方便快捷的訪問數(shù)據(jù)庫,需要的朋友可以參考下
    2018-05-05
  • Spring的組合注解和元注解原理與用法詳解

    Spring的組合注解和元注解原理與用法詳解

    這篇文章主要介紹了Spring的組合注解和元注解原理與用法,結合實例形式詳細分析了spring組合注解和元注解相關功能、原理、配置及使用方法,需要的朋友可以參考下
    2019-11-11
  • 使用Java獲取文件樹的代碼實現(xiàn)

    使用Java獲取文件樹的代碼實現(xiàn)

    Java語言提供了豐富的庫和工具,使得我們可以方便地獲取和操作Java文件的語法樹(AST, Abstract Syntax Tree),在這篇博客中,我們將探討如何使用Java來獲取一個Java文件的語法樹,并展示詳細的代碼示例和運行結果,需要的朋友可以參考下
    2024-08-08
  • Java數(shù)據(jù)結構之優(yōu)先級隊列(PriorityQueue)用法詳解

    Java數(shù)據(jù)結構之優(yōu)先級隊列(PriorityQueue)用法詳解

    優(yōu)先級隊列是一種先進先出的數(shù)據(jù)結構,操作的數(shù)據(jù)帶有優(yōu)先級,這種數(shù)據(jù)結構就是優(yōu)先級隊列(PriorityQueue)。本文將詳細講講Java優(yōu)先級隊列的用法,感興趣的可以了解一下
    2022-07-07

最新評論