欢迎来到代码驿站!

JAVA代码

当前位置:首页 > 软件编程 > JAVA代码

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

时间:2021-08-08 08:26:11|栏目:JAVA代码|点击:

一、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

上一篇:IntelliJ IDEA中出现"PSI and index do not match"错误的解决办法

栏    目:JAVA代码

下一篇:JAVA代码开发规范

本文标题:java使用Feign实现声明式Restful风格调用

本文地址:http://www.codeinn.net/misctech/165232.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有