java使用Feign实现声明式Restful风格调用

所属分类: 软件编程 / java 阅读数: 48
收藏 0 赞 0 分享

一、Feign简介

Feign是netflix开发的声明式、模板化的http客户端,在使用时就像调用本地(服务消费者自己)的方法一般,帮助我们更加优雅的调用服务提供者的API。Feign自身支持springMVC,还整合了Eureka、Ribbon,极大的简化了Feign的使用。就整合Euraka而言,只需和普通的服务配置Eureka server的信息即可。整合Ribbon,就意味着不再需要通过标注@LoadBalanced的实例化后的RestTemplate去调用服务提供者方法了。Feign只需通过简单的定义一个接口即可实现负载均衡。

二、在服务消费者中使用Feign

1、添加Feign依赖

<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-feign</artifactId>
</dependency>

2、创建一个feign接口,并在头部加上@FeignClient注解

import com.simons.cn.util.CommonResult;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
 
@FeignClient(name = "user-provider")
public interface UserFeignService {
 
  @RequestMapping(value = "/getuserinfo",method = RequestMethod.GET)
  CommonResult getUserByName(@RequestParam(required = false,value = "name") String name);
 
}

这里的name="user-provider" 会被解析为注册到Eureka server上的其中一个客户端,换句话说就是注册到Eureka中的其中一个服务,利用它可以实现负载均衡。也可以结合value来指定@FeignClient(name="user-provider",value = "http://localhost:8000/")

3、修改Controller,不再调用@LoadBalanced标注的RestTemplate,而是通过标注@FeignClient的自定义接口

import com.simons.cn.UserFeignService;
import com.simons.cn.util.CommonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
 
@Slf4j
@RestController
public class TicketFeignController {
 
  @Autowired
  private UserFeignService userFeignService;
 
  @GetMapping("/ticketpurchase")
  public CommonResult purchaseTicket(@RequestParam(required = false,value = "name") String name){
    CommonResult result = userFeignService.getUserByName(name);
    return result;
  }
 
}

4、修改启动类,头部添加@EnableFeignClients注解

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
 
@EnableFeignClients
@EnableDiscoveryClient
@SpringBootApplication
public class TicketConsumerFeignApplication {
 
  public static void main(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")
public interface UserFeignService {
 
  @RequestMapping(value = "/getuserinfo",method = RequestMethod.GET)
  CommonResult getUserByName(@RequestParam(required = false,value = "name") String name);
 
}

或者用Map来封装参数

@FeignClient(name="user-provider")
public interface UserServiceFeign {
  @RequestMapping(value = "/getuserinfo",method = RequestMethod.GET)
  public CommonResult getUserByName(@RequestParam Map<String,Object> map);
}
@RestController
public class TicketController {
  @Autowired
  private UserServiceFeign userServiceFeign;
 
  @GetMapping("ticketpurchase")
  public CommonResult (Long id, String actId) {
    Map map = new HashMap<String, Object>();
    map.put("id", id);
    map.put("actId", actId);
    return this.userServiceFeign.getUserByName(map);
  }
}

2、post请求就相对简单的多

// 服务消费者方
@FeignClient(name="user-provider")
public interface UserServiceFeign {
 
  @RequestMapping(value="/getuserbyname",method = RequestMethod.POST)
  public COmmonResult getUserByName(@RequestBody User user);
 
}
//服务提供者
@Slf4j
@RestController
public class UserController {
 
  @Autowired
  private UserServiceImpl userService;
 
  @GetMapping(value = "/getuserinfo")
  public CommonResult getUserInfo(@RuquestBody User user){
    List<User> userList = userService.getUserByName(user.getName());
    return CommonResult.success(CommonEnum.SUCESS.getCode(), CommonEnum.SUCESS.getMessage(),userList);
  }
}

项目的github

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

spring boot 静态资源处理方法

本篇文章主要介绍了spring boot 静态资源处理方法。小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

详解spring boot 使用application.properties 进行外部配置

这篇文章主要介绍了详解spring boot 使用application.properties 进行外部配置,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Java实现分页的前台页面和后台代码

这篇文章主要为大家详细介绍了Java实现分页的前台页面和后台代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

详解spring面向切面aop拦截器

spring中有很多概念和名词,比如过滤器、拦截器、aop等。这篇文章主要介绍了详解spring面向切面aop拦截器,有兴趣的可以了解一下。
收藏 0 赞 0 分享

Java easyui树形表格TreeGrid的实现代码

这篇文章主要为大家详细介绍了Java easyui树形表格TreeGrid的实现代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Java的NIO与IO的详解及对比

这篇文章主要介绍了Java的NIO与IO的详解及对比的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

Java中网络IO的实现方式(BIO、NIO、AIO)介绍

这篇文章主要介绍了Java中网络IO的实现方式(BIO、NIO、AIO)介绍的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

Java 敏感信息加密处理

本文主要介绍了Java 敏感信息加密处理的相关知识:1)敏感信息加密处理我们要实现什么;2)敏感信息加密处理我做了些什么;3)敏感信息加密实现方法。具有很好的参考价值,下面跟着小编一起来看下吧
收藏 0 赞 0 分享

详解使用Spring3 实现用户登录以及权限认证

这篇文章主要介绍了详解使用Spring3 实现用户登录以及权限认证,这里整理了详细的代码,有需要的小伙伴可以参考下。
收藏 0 赞 0 分享

Java的内存机制详解

本文主要介绍了Java的内存机制的相关知识,具有很好的参考价值,下面跟着小编一起来看下吧
收藏 0 赞 0 分享
查看更多