java使用Feign实现声明式Restful风格调用
一、Feign简介
Feign是netflix开发的声明式、模板化的http客户端,在使用时就像调用本地(服务消费者自己)的方法一般,帮助我们更加优雅的调用服务提供者的API。Feign自身支持springMVC,还整合了Eureka、Ribbon,极大的简化了Feign的使用。就整合Euraka而言,只需和普通的服务配置Eurekaserver的信息即可。整合Ribbon,就意味着不再需要通过标注@LoadBalanced的实例化后的RestTemplate去调用服务提供者方法了。Feign只需通过简单的定义一个接口即可实现负载均衡。
二、在服务消费者中使用Feign
1、添加Feign依赖
org.springframework.cloud spring-cloud-starter-feign
2、创建一个feign接口,并在头部加上@FeignClient注解
importcom.simons.cn.util.CommonResult;
importorg.springframework.cloud.netflix.feign.FeignClient;
importorg.springframework.web.bind.annotation.RequestMapping;
importorg.springframework.web.bind.annotation.RequestMethod;
importorg.springframework.web.bind.annotation.RequestParam;
@FeignClient(name="user-provider")
publicinterfaceUserFeignService{
@RequestMapping(value="/getuserinfo",method=RequestMethod.GET)
CommonResultgetUserByName(@RequestParam(required=false,value="name")Stringname);
}
这里的name="user-provider" 会被解析为注册到Eurekaserver上的其中一个客户端,换句话说就是注册到Eureka中的其中一个服务,利用它可以实现负载均衡。也可以结合value来指定@FeignClient(name="user-provider",value="http://localhost:8000/")
3、修改Controller,不再调用@LoadBalanced标注的RestTemplate,而是通过标注@FeignClient的自定义接口
importcom.simons.cn.UserFeignService;
importcom.simons.cn.util.CommonResult;
importlombok.extern.slf4j.Slf4j;
importorg.springframework.beans.factory.annotation.Autowired;
importorg.springframework.web.bind.annotation.GetMapping;
importorg.springframework.web.bind.annotation.RequestParam;
importorg.springframework.web.bind.annotation.RestController;
@Slf4j
@RestController
publicclassTicketFeignController{
@Autowired
privateUserFeignServiceuserFeignService;
@GetMapping("/ticketpurchase")
publicCommonResultpurchaseTicket(@RequestParam(required=false,value="name")Stringname){
CommonResultresult=userFeignService.getUserByName(name);
returnresult;
}
}
4、修改启动类,头部添加@EnableFeignClients注解
importorg.springframework.boot.SpringApplication;
importorg.springframework.boot.autoconfigure.SpringBootApplication;
importorg.springframework.cloud.client.discovery.EnableDiscoveryClient;
importorg.springframework.cloud.netflix.feign.EnableFeignClients;
@EnableFeignClients
@EnableDiscoveryClient
@SpringBootApplication
publicclassTicketConsumerFeignApplication{
publicstaticvoidmain(String[]args){
SpringApplication.run(TicketConsumerFeignApplication.class,args);
}
}
测试:
启动多个user-provider-eureka服务实例,其配置文件中的application.name=user-provider;
启动discovery-eureka服务实例;
启动ticket-consumer-feign服务实例
如上测试结果可以看到ticket-consumer-feign消费者顺利调用user-provider-eureka服务提供者的方法,并且实现了负载均衡。
三、使用Feign构造多参数请求
1、get请求:多个参数就用多个@RequestParam标注几个
@FeignClient(name="user-provider")
publicinterfaceUserFeignService{
@RequestMapping(value="/getuserinfo",method=RequestMethod.GET)
CommonResultgetUserByName(@RequestParam(required=false,value="name")Stringname);
}
或者用Map来封装参数
@FeignClient(name="user-provider")
publicinterfaceUserServiceFeign{
@RequestMapping(value="/getuserinfo",method=RequestMethod.GET)
publicCommonResultgetUserByName(@RequestParamMapmap);
}
@RestController
publicclassTicketController{
@Autowired
privateUserServiceFeignuserServiceFeign;
@GetMapping("ticketpurchase")
publicCommonResult(Longid,StringactId){
Mapmap=newHashMap();
map.put("id",id);
map.put("actId",actId);
returnthis.userServiceFeign.getUserByName(map);
}
}
2、post请求就相对简单的多
//服务消费者方
@FeignClient(name="user-provider")
publicinterfaceUserServiceFeign{
@RequestMapping(value="/getuserbyname",method=RequestMethod.POST)
publicCOmmonResultgetUserByName(@RequestBodyUseruser);
}
//服务提供者
@Slf4j
@RestController
publicclassUserController{
@Autowired
privateUserServiceImpluserService;
@GetMapping(value="/getuserinfo")
publicCommonResultgetUserInfo(@RuquestBodyUseruser){
ListuserList=userService.getUserByName(user.getName());
returnCommonResult.success(CommonEnum.SUCESS.getCode(),CommonEnum.SUCESS.getMessage(),userList);
}
}
项目的github
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。