認識Fegin
Feign是簡化Java HTTP客戶端開發的工具(java-to-httpclient-binder),它的靈感
來自於Retrofit、JAXRS-2.0和WebSocket。Feign的初衷是降低統一綁定Denominator到
HTTP API的復雜度,不區分是否為restful。
1.添加依賴
<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>
2.需要Eureka
@SpringBootApplication @EnableEurekaClient //eureka @EnableDiscoveryClient // @EnableFeignClients //相互調用 public class QaApplication { public static void main(String[] args) { SpringApplication.run(QaApplication.class, args); } }
3.接口調用
//調用其他模塊 @RequestMapping(value = "/label/{labelid}",method = RequestMethod.GET) public Result findLableById(@PathVariable String labelid) { Result result = labelClient.findById(labelid); return result; }
@FeignClient注解用於指定從哪個服務中調用功能 ,注意 里面的名稱與被調用的服務
名保持一致,並且不能包含下划線。
@RequestMapping注解用於對被調用的微服務進行地址映射。注意 @PathVariable注
解一定要指定參數名稱,否則出錯
@FeignClient("tensquare-base")//服務名 public interface LabelClient { @RequestMapping(value = "/label/{id}", method = RequestMethod.GET) public Result findById(@PathVariable(value="id") String id) ; }