快速搭建feign遠程調用客戶端

1、快速建立springboot項目集成cloud包

2、pom中加入feign相關jar包

<!--feign 包--><dependency>    <groupId>org.springframework.cloud</groupId>    <artifactId>spring-cloud-starter-openfeign</artifactId></dependency>複製代碼

3、項目啓動類配置添加註解 @EnableFeignClients 啓用feignclient

4、編寫接口controller

package com.ang.feign.controller;import org.springframework.web.bind.annotation.PostMapping;import org.springframework.web.bind.annotation.RequestBody;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;/** * @author: xiuxian.wang * @description: * @date: 2019/10/30 **/@RestController@RequestMapping("/ang")public class AngController {    @PostMapping("/feign")    public String angFeign(@RequestBody String ang){        ang += "ang de fan ying";        return ang;    }}複製代碼

5、編寫client

package com.ang.feign.client;import org.springframework.cloud.openfeign.FeignClient;import org.springframework.web.bind.annotation.PostMapping;import org.springframework.web.bind.annotation.RequestBody;/** * @author: xiuxian.wang * @description: * @date: 2019/10/30 **/@FeignClient(value = "AngClient",url = "http://localhost:5214/ang/feign")public interface AngClient {    @PostMapping    String angReq(@RequestBody String ang);}複製代碼

6、編寫測試類

@Testvoid contextLoads() {    String req = client.angReq("hello ang and ");    log.info("ang client response info {}",req);}複製代碼

7、源碼地址:https://gitee.com/leisure-w/ang-feign.git

相關文章
相關標籤/搜索