SpringCloud Feign的使用代碼實(shí)例
更新時(shí)間:2020年03月13日 11:30:01 作者:玉天恒
這篇文章主要介紹了SpringCloud Feign的使用代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
1.官方文檔
https://cloud.spring.io/spring-cloud-static/spring-cloud-openfeign/2.2.2.RELEASE/reference/html/
2.添加依賴
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency>
3.添加啟動(dòng)類注解
import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.openfeign.EnableFeignClients; @SpringBootApplication //@MapperScan("cn.ytheng.order_service") @EnableFeignClients public class OrderServiceApplication { public static void main(String[] args) { SpringApplication.run(OrderServiceApplication.class, args); } }
4.添加Feign接口
import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestParam; /** * * 商品服務(wù)客戶端 * product-service: 調(diào)用服務(wù)名稱,即spring.application.name * */ @FeignClient(name = "product-service") public interface ProductClient { @GetMapping("/api/v1/product/find") String getById(@RequestParam("id") int id); }
5.添加Controller
import cn.theng.order_service.service.ProductClient; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/api/v1/order") public class ProductOrderController { @Autowired private ProductClient productClient; @PostMapping("/test2") public Object test2(@RequestParam("product_id") int productId) { String product = productClient.getById(productId); return "success"; } }
6.添加application.yml配置
server: port: 8781 eureka: client: serviceUrl: defaultZone: http://localhost:8761/eureka/ spring: application: name: order-service #設(shè)置調(diào)用服務(wù)超時(shí)時(shí)間 #product-service為服務(wù)名稱,也可以設(shè)置為默認(rèn)值default feign: client: config: product-service: connectTimeout: 5000 readTimeout: 11000
7.訪問地址
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- SpringCloud Feign的使用簡(jiǎn)介
- 完美解決SpringCloud-OpenFeign使用okhttp替換不生效問題
- 基于springcloud異步線程池、高并發(fā)請(qǐng)求feign的解決方案
- 淺談SpringCloud feign的http請(qǐng)求組件優(yōu)化方案
- SpringCloud Open feign 使用okhttp 優(yōu)化詳解
- SpringCloud Feign轉(zhuǎn)發(fā)請(qǐng)求頭(防止session失效)的解決方案
- SpringCloud OpenFeign Post請(qǐng)求400錯(cuò)誤解決方案
- SpringCloud Feign如何在遠(yuǎn)程調(diào)用中傳輸文件
- SpringCloud之Feign遠(yuǎn)程接口映射的實(shí)現(xiàn)
- SpringCloud 服務(wù)負(fù)載均衡和調(diào)用 Ribbon、OpenFeign的方法
- Springcloud基于OpenFeign實(shí)現(xiàn)服務(wù)調(diào)用代碼實(shí)例
- SpringCloud Feign服務(wù)調(diào)用請(qǐng)求方式總結(jié)
- SpringCloud項(xiàng)目集成Feign、Hystrix過程解析
- 詳解springcloud 基于feign的服務(wù)接口的統(tǒng)一hystrix降級(jí)處理
- 基于Feign實(shí)現(xiàn)異步調(diào)用
相關(guān)文章
一篇文章帶你深入理解JVM虛擬機(jī)讀書筆記--鎖優(yōu)化
這篇文章深入介紹了JVM虛擬機(jī)的鎖優(yōu)化,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2021-09-09Java Web實(shí)現(xiàn)自動(dòng)登陸功能
這篇文章主要為大家詳細(xì)介紹了Java Web實(shí)現(xiàn)自動(dòng)登陸功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08