一、在不是SpringCloud項目運用feign的實例(交換數據爲不是對象)。html
服務消費端: java
須要導入的maven包(其中feign-core表示運用feign時須要導入的包,而feign-jackson的做用是,服務消費端與生產端之間交換的數據每每是一或多個對象,feign一樣提供基於json的對象轉換工具,方便咱們直接以對象形式交互)web
<dependency> <groupId>com.netflix.feign</groupId> <artifactId>feign-core</artifactId> <version>8.18.0</version> </dependency> <dependency> <groupId>com.netflix.feign</groupId> <artifactId>feign-jackson</artifactId> <version>8.18.0</version> </dependency>
自定義接口: spring
import feign.Param; import feign.RequestLine; public interface remoteService { @RequestLine("GET /producerHello?name={name}") String sayHello(@Param(value = "name") String name); }
測試類:json
import feign.Feign; import feign.Request; import feign.Retryer; public class test { public static void main(String[] args) { remoteService service = Feign.builder() .options(new Request.Options(1000, 3500)) .retryer(new Retryer.Default(5000, 5000, 3)) .target(remoteService.class, "http://localhost:8402"); String result = service.sayHello("scott"); System.out.println(result); } }
服務生產端:app
自定義接口:maven
@RestController public class HelloController { @RequestMapping("/producerHello") public String Hello(@RequestParam("name") String name){ return "hello " + name + ",this is demo-client1 messge"; }
二、在不是SpringCloud項目運用feign的實例(交換數據是對象)。ide
須要導入的包和1的包同樣。工具
服務消費者:測試
自定義接口:
import feign.Headers;
import feign.RequestLine;
public interface remoteService2 {
@Headers({"Content-Type: application/json","Accept: application/json"})
@RequestLine("POST /users/list")
User getOwner(User user);
}
測試類
實體類:
public class User { String name; String id; ................ }
服務生產者:
3. 在SpringCloud中運用feign實例
搭建好SpringCloud項目,包括服務註冊中心、服務消費者、服務生產者。
服務消費者:
添加maven:
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-feign</artifactId> <version>1.3.1.RELEASE</version> </dependency>
添加自定義接口:其中代碼中註解feignclient的屬性name表示服務消費者的項目名。註解RequestMapping的屬性value表示被調用的服務消費者對應的方法上@RequestMapping的value的值。
import org.springframework.cloud.netflix.feign.FeignClient; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; @FeignClient(name= "YAOHUIQIN8401") public interface HelloRemote { //這個接口要和遠程調用的接口一隻,參數,接口名,返回類型 @RequestMapping(value = "/hello/provider") public String helloProvider(); @RequestMapping(value = "/producerHello") public String sayHello(@RequestParam(value="name") String name); }
添加controller類:
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController; @RestController public class RemoteController { @Autowired HelloRemote helloRemote; @RequestMapping(value = "/hello") public String hello(){ return helloRemote.helloProvider(); } @RequestMapping("/consumerHello/{name}") public String index(@PathVariable("name") String name){ return helloRemote.sayHello(name); } }
服務生產者:
運行項目,結果以下: