一、在不是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); } }
服务生产者:
运行项目,结果以下: