SpringBoot使用Feign進(jìn)行服務(wù)間通信的實(shí)現(xiàn)示例代碼
一、前言
在分布式系統(tǒng)中,服務(wù)間通信是非常常見的情況。Feign是一個(gè)開源的Java HTTP客戶端,可以幫助我們?cè)赟pringBoot應(yīng)用中快速構(gòu)建和使用HTTP客戶端,方便實(shí)現(xiàn)服務(wù)間的通信。與其他HTTP客戶端相比,F(xiàn)eign具有簡化 HTTP API定義、支持多種HTTP請(qǐng)求方法、支持請(qǐng)求和響應(yīng)的壓縮、支持請(qǐng)求和響應(yīng)的日志記錄、支持多種負(fù)載均衡器、支持自定義攔截器和錯(cuò)誤處理器等特點(diǎn)。
二、SpringBoot集成
1.添加依賴
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> <version>3.1.5</version> </dependency>
2.啟用Feign客戶端
我們需要在啟動(dòng)類上添加@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指定需要請(qǐng)求的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.定義目標(biāo)控制層接口
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測(cè)試控制層
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測(cè)試 */ @RestController @RequestMapping("/userFeign") public class UserFeignController { @Autowired private UserFeignClient userFeignClient; @GetMapping("/{id}") public User getUserInfo(@PathVariable("id") Long id) { return userFeignClient.selectUserById(id); } }
6.測(cè)試
我們先測(cè)試目標(biāo)請(qǐng)求接口是否正確。
然后我們?cè)偈褂肍eign的方式請(qǐng)求接口的方式進(jìn)行測(cè)試。
這樣我們使用Feign方式請(qǐng)求,成功請(qǐng)求目的地址獲取到了一樣的數(shù)據(jù)。
到此這篇關(guān)于SpringBoot使用Feign進(jìn)行服務(wù)間通信的實(shí)現(xiàn)示例代碼的文章就介紹到這了,更多相關(guān)SpringBoot Feign服務(wù)間通信 內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- SpringBoot使用quartz,注入feignClient,client為null問題
- springboot+feign+Hystrix整合(親測(cè)有效)
- SpringBoot之使用Feign實(shí)現(xiàn)微服務(wù)間的交互
- SpringBoot動(dòng)態(tài)Feign服務(wù)調(diào)用詳解
- SpringBoot Feign使用教程超全面講解
- SpringBoot + openFeign實(shí)現(xiàn)遠(yuǎn)程接口調(diào)用的過程
- springboot單獨(dú)使用feign簡化接口調(diào)用方式
- springboot集成Feign的實(shí)現(xiàn)示例
相關(guān)文章
Spring Gateway處理微服務(wù)的路由轉(zhuǎn)發(fā)機(jī)制
我們?cè)敿?xì)地介紹了Spring Gateway,這個(gè)基于Spring 5、Spring Boot 2和Project Reactor的API網(wǎng)關(guān),通過這篇文章,我們可以清晰地看到Spring Gateway的工作原理,以及它的強(qiáng)大之處,感興趣的朋友一起看看吧2024-08-08SpringMVC Idea 搭建 部署war的詳細(xì)過程
本文介紹了如何在IntelliJ IDEA中使用Maven模板創(chuàng)建一個(gè)Web項(xiàng)目,并詳細(xì)說明了如何配置web.xml、創(chuàng)建springmvc-servlet.xml和application.properties文件,以及如何使用Maven打包生成WAR文件并部署到Tomcat服務(wù)器,感興趣的朋友跟隨小編一起看看吧2025-01-01Spring boot中使用Spring-data-jpa方便快捷的訪問數(shù)據(jù)庫(推薦)
Spring Data JPA 是 Spring 基于 ORM 框架、JPA 規(guī)范的基礎(chǔ)上封裝的一套JPA應(yīng)用框架,可使開發(fā)者用極簡的代碼即可實(shí)現(xiàn)對(duì)數(shù)據(jù)的訪問和操作。這篇文章主要介紹了Spring-boot中使用Spring-data-jpa方便快捷的訪問數(shù)據(jù)庫,需要的朋友可以參考下2018-05-05Java數(shù)據(jù)結(jié)構(gòu)之優(yōu)先級(jí)隊(duì)列(PriorityQueue)用法詳解
優(yōu)先級(jí)隊(duì)列是一種先進(jìn)先出的數(shù)據(jù)結(jié)構(gòu),操作的數(shù)據(jù)帶有優(yōu)先級(jí),這種數(shù)據(jù)結(jié)構(gòu)就是優(yōu)先級(jí)隊(duì)列(PriorityQueue)。本文將詳細(xì)講講Java優(yōu)先級(jí)隊(duì)列的用法,感興趣的可以了解一下2022-07-07