文章目錄
- 環境準備
- 一、URL中接收參數
- 二、接收一個參數
- 三、接收多個參數
- 四、傳遞對象
- 五、傳遞JSON格式數據
環境準備
下面的配置,服務調用方加入即可。
- 依賴導入:
<!-- openfeign依賴--><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-openfeign</artifactId></dependency>
- 開啟Feign功能:
@EnableFeignClients //開啟feign功能
@SpringBootApplication
public class OrderServiceApplication {public static void main(String[] args) {SpringApplication.run(OrderServiceApplication.class, args);}
}
- 創建Openfeign接口
//假定:被調用服務的名稱為product-service,服務的接口路徑統一為`product`開頭
//path是可加 可不加的,value必須有,用于指定被調用服務
@FeignClient(value="product-service",path="/product")//FeignClient用于綁定需要調用的服務
public interface ProductApi {//path可以有可無,使用path,遠程調用時,默認會加上path前綴路徑
}
做足準備后,接下來我們就可以使用Openfeign來進行遠程調用了。
一、URL中接收參數
- 假定這是服務提供方的接口:
@RestController
@RequestMapping("/product")
public class ProductController {@Autowiredprivate ProductService productService;@RequestMapping("/{productId}")public ProductInfo getProductInfoById(@PathVariable("productId") Integer productId){return productService.selectProductById(productId);}
}
- 定義Openfeign接口
@FeignClient(value="product-service",path="/product")//FeignClient用于綁定需要調用的服務
public interface ProductApi {//@PathVariable不可省略!把變量申明為url的一部分@RequestMapping("/{productId}")ProductInfo getProductInfo(@PathVariable("productId ") Integer productId);
}
我們發現實際上Openfeign接口的定義和服務提供方Controller接口的定義是非常相似的,這有助于我們去書寫Openfeign接口。
- 通過Openfeign進行遠程調用
order是服務調用方的一個接口,意思是查詢orderId為1的訂單信息。
二、接收一個參數
假定這是服務提供方接口(回顯服務):
@RestController
@RequestMapping("/product")
public class ProductController {@RequestMapping("/p1")public String p1(Integer id){return "product-service 接收到參數, id:"+id;}
}
定義Openfeign接口:
//同樣地,和服務提供方接口寫法類似
@FeignClient(value="product-service",path="/product")//FeignClient用于綁定需要調用的服務
public interface ProductApi {@RequestMapping("/p1")String p1(@RequestParam("id")Integer id);//@RequestParam不可省略
}
然后我們寫一個測試接口,用Openfeign去調用ProductController的p1方法:
@RestController
@RequestMapping("/feign")
public class FeignController {@Autowiredprivate ProductApi productApi;@RequestMapping("/o1")public String o1(Integer id){return productApi.p1(id);}
}
運行結果:
三、接收多個參數
假定這是服務提供方接口(回顯服務):
@RestController
@RequestMapping("/product")
public class ProductController {@Autowiredprivate ProductService productService;@RequestMapping("/p2")public String p2(Integer id, String name){return "product-service 接收到參數, id:"+id+",name:"+name;}
}
定義Openfeign接口:
@FeignClient(value="product-service",path="/product")//FeignClient用于綁定需要調用的服務
public interface ProductApi {//同樣地,@RequestParam都不能夠省略@RequestMapping("/p2")String p2(@RequestParam("id")Integer id,@RequestParam("name") String name);
}
然后通過下面的測試接口調用Openfeign接口進行遠程調用:
@RestController
@RequestMapping("/feign")
public class FeignController {@Autowiredprivate ProductApi productApi;@RequestMapping("/o2")public String o2(Integer id,String name){return productApi.p2(id,name);}
}
運行結果:
四、傳遞對象
假定這是服務提供方接口(回顯服務):
@RestController
@RequestMapping("/product")
public class ProductController {@Autowiredprivate ProductService productService;@RequestMapping("/p3")public String p3(ProductInfo productInfo){return "product-service 接收到參數: productInfo"+productInfo.toString();}}
遠程調用接口:
@FeignClient(value="product-service",path="/product")//FeignClient用于綁定需要調用的服務
public interface ProductApi {@RequestMapping("/p3")public String p3(@SpringQueryMap ProductInfo productInfo);}
@SpringQueryMap
會把Java對象的字段映射成HTTP 請求的查詢參數,同樣地,是不可省略的注解
通過Openfeign進行遠程調用:
@RestController
@RequestMapping("/feign")
public class FeignController {@Autowiredprivate ProductApi productApi;@RequestMapping("/o3")public String o3(){ProductInfo productInfo=new ProductInfo();productInfo.setId(45);productInfo.setProductName("T恤");return productApi.p3(productInfo);}
}
運行結果:
五、傳遞JSON格式數據
假定這是我們的服務提供方(回顯服務):
@RestController
@RequestMapping("/product")
public class ProductController {@RequestMapping("/p4")public String p4(@RequestBody ProductInfo productInfo){return "product-service 接收到參數: productInfo"+productInfo.toString();}}
定義Openfeign接口:
@FeignClient(value="product-service",path="/product")//FeignClient用于綁定需要調用的服務
public interface ProductApi {@RequestMapping("/p4")public String p4(@RequestBody ProductInfo productInfo);
}
@RequestBody
不可省略,表示接收json格式數據,并且轉換成Java對象
通過下面的測試接口,實現遠程調用:
@RestController
@RequestMapping("/feign")
public class FeignController {@Autowiredprivate ProductApi productApi;@RequestMapping("/o4")public String o4(){ProductInfo productInfo=new ProductInfo();productInfo.setId(45);productInfo.setProductName("T恤");return productApi.p4(productInfo);}
}
運行結果: