1、在不是SpringCloud項目運用feign的實例(交換數據為不是對象)。
服務消費端:
需要導入的maven包(其中feign-core表示運用feign時需要導入的包,而feign-jackson的作用是,服務消費端與生產端之間交換的數據往往是一或多個對象,feign同樣提供基於json的對象轉換工具,方便我們直接以對象形式交互)
<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>
自定義接口:
import feign.Param;
import feign.RequestLine;
public interface remoteService {
@RequestLine("GET /producerHello?name={name}")
String sayHello(@Param(value = "name") String name);
}
測試類:
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);
}
}
服務生產端:
自定義接口:
@RestController
public class HelloController {
@RequestMapping("/producerHello")
public String Hello(@RequestParam("name") String name){
return "hello " + name + ",this is demo-client1 messge";
}
2、在不是SpringCloud項目運用feign的實例(交換數據是對象)。
需要導入的包和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);
}
}
服務生產者:
運行項目,結果如下:

