SpringCloud Feign的使用代碼實(shí)例
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')@EnableFeignClientspublic 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: 8781eureka: client: serviceUrl: defaultZone: http://localhost:8761/eureka/spring: application: name: order-service#設(shè)置調(diào)用服務(wù)超時(shí)時(shí)間#product-service為服務(wù)名稱,也可以設(shè)置為默認(rèn)值defaultfeign: client: config: product-service: connectTimeout: 5000 readTimeout: 11000
7.訪問(wèn)地址
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. 前端從瀏覽器的渲染到性能優(yōu)化2. 無(wú)線標(biāo)記語(yǔ)言(WML)基礎(chǔ)之WMLScript 基礎(chǔ)第1/2頁(yè)3. ASP刪除img標(biāo)簽的style屬性只保留src的正則函數(shù)4. 讀大數(shù)據(jù)量的XML文件的讀取問(wèn)題5. 解析原生JS getComputedStyle6. PHP循環(huán)與分支知識(shí)點(diǎn)梳理7. css代碼優(yōu)化的12個(gè)技巧8. 利用CSS3新特性創(chuàng)建透明邊框三角9. ASP實(shí)現(xiàn)加法驗(yàn)證碼10. ASP基礎(chǔ)入門(mén)第三篇(ASP腳本基礎(chǔ))
