java使用Feign實現(xiàn)聲明式Restful風(fēng)格調(diào)用
一、Feign簡介
Feign是netflix開發(fā)的聲明式、模板化的http客戶端,在使用時就像調(diào)用本地(服務(wù)消費者自己)的方法一般,幫助我們更加優(yōu)雅的調(diào)用服務(wù)提供者的API。Feign自身支持springMVC,還整合了Eureka、Ribbon,極大的簡化了Feign的使用。就整合Euraka而言,只需和普通的服務(wù)配置Eureka server的信息即可。整合Ribbon,就意味著不再需要通過標(biāo)注@LoadBalanced的實例化后的RestTemplate去調(diào)用服務(wù)提供者方法了。Feign只需通過簡單的定義一個接口即可實現(xiàn)負(fù)載均衡。
二、在服務(wù)消費者中使用Feign
1、添加Feign依賴
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-feign</artifactId> </dependency>
2、創(chuàng)建一個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中的其中一個服務(wù),利用它可以實現(xiàn)負(fù)載均衡。也可以結(jié)合value來指定@FeignClient(name="user-provider",value = "http://localhost:8000/")
3、修改Controller,不再調(diào)用@LoadBalanced標(biāo)注的RestTemplate,而是通過標(biāo)注@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服務(wù)實例,其配置文件中的application.name=user-provider;
啟動discovery-eureka服務(wù)實例;
啟動ticket-consumer-feign服務(wù)實例
如上測試結(jié)果可以看到ticket-consumer-feign消費者順利調(diào)用user-provider-eureka服務(wù)提供者的方法,并且實現(xiàn)了負(fù)載均衡。
三、使用Feign構(gòu)造多參數(shù)請求
1、get請求:多個參數(shù)就用多個@RequestParam標(biāo)注幾個
@FeignClient(name = "user-provider")
public interface UserFeignService {
@RequestMapping(value = "/getuserinfo",method = RequestMethod.GET)
CommonResult getUserByName(@RequestParam(required = false,value = "name") String name);
}
或者用Map來封裝參數(shù)
@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請求就相對簡單的多
// 服務(wù)消費者方
@FeignClient(name="user-provider")
public interface UserServiceFeign {
@RequestMapping(value="/getuserbyname",method = RequestMethod.POST)
public COmmonResult getUserByName(@RequestBody User user);
}
//服務(wù)提供者
@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
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
SpringCloud輪詢拉取注冊表與服務(wù)發(fā)現(xiàn)流程詳解
這篇文章主要介紹了SpringCloud輪詢拉取注冊表與服務(wù)發(fā)現(xiàn),現(xiàn)在很多創(chuàng)業(yè)公司都開始往springcloud靠了,可能是由于文檔和組件比較豐富的原因吧,畢竟是一款目前來說比較完善的微服務(wù)架構(gòu)2022-11-11
大數(shù)據(jù)Kafka:消息隊列和Kafka基本介紹
本文對消息隊列的應(yīng)用場景,優(yōu)缺點,消息隊列的兩種方式,常見的消息隊列產(chǎn)品以及Kafka的特點和應(yīng)用場景做了詳細(xì)的講解,需要的朋友可以參考下,希望可以對大家有所幫助2021-08-08
SpringBoot自定義定時任務(wù)的實現(xiàn)示例
本文主要介紹了SpringBoot自定義定時任務(wù),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-05-05
java復(fù)制文件的4種方式及拷貝文件到另一個目錄下的實例代碼
這篇文章主要介紹了java復(fù)制文件的4種方式,通過實例帶給大家介紹了java 拷貝文件到另一個目錄下的方法,需要的朋友可以參考下2018-06-06

