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.測試
我們先測試目標請求接口是否正確。
然后我們再使用Feign的方式請求接口的方式進行測試。
這樣我們使用Feign方式請求,成功請求目的地址獲取到了一樣的數(shù)據(jù)。
到此這篇關于SpringBoot使用Feign進行服務間通信的實現(xiàn)示例代碼的文章就介紹到這了,更多相關SpringBoot Feign服務間通信 內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Spring Gateway處理微服務的路由轉發(fā)機制
我們詳細地介紹了Spring Gateway,這個基于Spring 5、Spring Boot 2和Project Reactor的API網(wǎng)關,通過這篇文章,我們可以清晰地看到Spring Gateway的工作原理,以及它的強大之處,感興趣的朋友一起看看吧2024-08-08Spring 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-05Java數(shù)據(jù)結構之優(yōu)先級隊列(PriorityQueue)用法詳解
優(yōu)先級隊列是一種先進先出的數(shù)據(jù)結構,操作的數(shù)據(jù)帶有優(yōu)先級,這種數(shù)據(jù)結構就是優(yōu)先級隊列(PriorityQueue)。本文將詳細講講Java優(yōu)先級隊列的用法,感興趣的可以了解一下2022-07-07