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

网友投稿 345 2023-01-08


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

一、Feign简介

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

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

1、添加Feign依赖

org.springframework.cloud

spring-cloud-starter-feign

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 = "ushHytgKkKCCer-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.sprhHytgKkKCCingframework.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 map);

}

@RestController

public class TicketController {

@Autowired

private UserServiceFeign userServiceFeign;

@GetMapping("ticketpurchase")

public CommonResult (Long id, String actId) {

Map map = new HashMap();

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 userList = userService.getUserByName(user.getName());

return CommonResult.success(CommonEnum.SUCESS.getCode(), CommonEnum.SUCESS.getMessage(),userLhHytgKkKCCist);

}

}

项目的github


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:微服务网关用限流算法(微服务限流框架)
下一篇:接口自动化框架环境切换(接口自动化框架环境切换方法)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~