您好,登錄后才能下訂單哦!
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.添加啟動類注解
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ù)超時時間 #product-service為服務(wù)名稱,也可以設(shè)置為默認(rèn)值default feign: client: config: product-service: connectTimeout: 5000 readTimeout: 11000
7.訪問地址
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。