亲宝软件园·资讯

展开

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

人气:1

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

您可能感兴趣的文章:

加载全部内容

相关教程
猜你喜欢
用户评论