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>自定义接口:
importfeign.Param; importfeign.RequestLine; publicinterfaceremoteService{ @RequestLine("GET/producerHello?name={name}") StringsayHello(@Param(value="name")Stringname); }测试类:
importfeign.Feign; importfeign.Request; importfeign.Retryer; publicclasstest{ publicstaticvoidmain(String[]args){ remoteServiceservice=Feign.builder() .options(newRequest.Options(1000,3500)) .retryer(newRetryer.Default(5000,5000,3)) .target(remoteService.class,"http://localhost:8402"); Stringresult=service.sayHello("scott"); System.out.println(result); } }服务生产端:
自定义接口:
@RestController publicclassHelloController{ @RequestMapping("/producerHello") publicStringHello(@RequestParam("name")Stringname){ return"hello"+name+",thisisdemo-client1messge"; }2、在不是SpringCloud项目运用feign的实例(交换数据是对象)。
需要导入的包和1的包一样。
服务消费者:
自定义接口:
importfeign.Headers; importfeign.RequestLine; publicinterfaceremoteService2{ @Headers({"Content-Type:application/json","Accept:application/json"}) @RequestLine("POST/users/list") UsergetOwner(Useruser); }测试类
importfeign.Feign; importfeign.Request; importfeign.Retryer; importfeign.jackson.JacksonDecoder; importfeign.jackson.JacksonEncoder; publicclasstest{ publicstaticvoidmain(String[]args){ remoteService2service=Feign.builder() .encoder(newJacksonEncoder()) .decoder(newJacksonDecoder()) .options(newRequest.Options(1000,3500)) .retryer(newRetryer.Default(5000,5000,3)) .target(remoteService2.class,"http://localhost:8402"); Useru=newUser(); u.setId("1233"); u.setName("yaohuiqin"); Userresult=service.getOwner(u); System.out.println(result); } }实体类:
publicclassUser{ Stringname; Stringid; ................ }服务生产者:
@RestController publicclassHelloController{ @RequestMapping(value="/users/list",method={RequestMethod.GET,RequestMethod.POST,RequestMethod.PUT}) @ResponseBody publicUserlist(@RequestBodyUseruser)throwsInterruptedException{ System.out.println(user.getName()); user.setId("3333"); user.setName(user.getName().toUpperCase()); returnuser; } } 在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的值。
importorg.springframework.cloud.netflix.feign.FeignClient; importorg.springframework.web.bind.annotation.RequestMapping; importorg.springframework.web.bind.annotation.RequestParam; @FeignClient(name="YAOHUIQIN8401") publicinterfaceHelloRemote{ //这个接口要和远程调用的接口一只,参数,接口名,返回类型 @RequestMapping(value="/hello/provider") publicStringhelloProvider(); @RequestMapping(value="/producerHello") publicStringsayHello(@RequestParam(value="name")Stringname); }添加controller类:
importorg.springframework.beans.factory.annotation.Autowired; importorg.springframework.web.bind.annotation.PathVariable; importorg.springframework.web.bind.annotation.RequestMapping; importorg.springframework.web.bind.annotation.ResponseBody; importorg.springframework.web.bind.annotation.RestController; @RestController publicclassRemoteController{ @Autowired HelloRemotehelloRemote; @RequestMapping(value="/hello") publicStringhello(){ returnhelloRemote.helloProvider(); } @RequestMapping("/consumerHello/{name}") publicStringindex(@PathVariable("name")Stringname){ returnhelloRemote.sayHello(name); } }服务生产者:
importorg.springframework.web.bind.annotation.*; @RestController publicclassHelloController{ @RequestMapping(value="/hello/provider") publicStringhelloProvider(){ return"Iamisprovider,helloworld"; } @RequestMapping("/producerHello") publicStringHello(@RequestParam("name")Stringname){ return"hello"+name+",thisisdemo-client1messge"; } }运行项目,结果如下:
本文内容总结:
原文链接:https://www.cnblogs.com/yaohuiqin/p/8989645.html