欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

java使用Feign實現(xiàn)聲明式Restful風(fēng)格調(diào)用

 更新時間:2019年04月24日 10:14:06   作者:simonsfan  
這篇文章主要為大家詳細介紹了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)負載均衡。

二、在服務(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)負載均衡。也可以結(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)了負載均衡。

三、使用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)流程詳解

    這篇文章主要介紹了SpringCloud輪詢拉取注冊表與服務(wù)發(fā)現(xiàn),現(xiàn)在很多創(chuàng)業(yè)公司都開始往springcloud靠了,可能是由于文檔和組件比較豐富的原因吧,畢竟是一款目前來說比較完善的微服務(wù)架構(gòu)
    2022-11-11
  • Java Calendar類的詳解及使用實例

    Java Calendar類的詳解及使用實例

    這篇文章主要介紹了Java Calendar類的詳解及使用實例的相關(guān)資料,需要的朋友可以參考下
    2017-04-04
  • 大數(shù)據(jù)Kafka:消息隊列和Kafka基本介紹

    大數(shù)據(jù)Kafka:消息隊列和Kafka基本介紹

    本文對消息隊列的應(yīng)用場景,優(yōu)缺點,消息隊列的兩種方式,常見的消息隊列產(chǎn)品以及Kafka的特點和應(yīng)用場景做了詳細的講解,需要的朋友可以參考下,希望可以對大家有所幫助
    2021-08-08
  • Graphics2D 寫圖片中文亂碼問題及解決

    Graphics2D 寫圖片中文亂碼問題及解決

    這篇文章主要介紹了Graphics2D 寫圖片中文亂碼問題及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-11-11
  • SpringBoot自定義定時任務(wù)的實現(xiàn)示例

    SpringBoot自定義定時任務(wù)的實現(xiàn)示例

    本文主要介紹了SpringBoot自定義定時任務(wù),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2024-05-05
  • Java面試題沖刺第一天--基礎(chǔ)篇1

    Java面試題沖刺第一天--基礎(chǔ)篇1

    這篇文章主要為大家分享了最有價值的三道java面試題,涵蓋內(nèi)容全面,包括數(shù)據(jù)結(jié)構(gòu)和算法相關(guān)的題目、經(jīng)典面試編程題等,感興趣的小伙伴們可以參考一下
    2021-07-07
  • Java注釋和關(guān)鍵字實例詳解

    Java注釋和關(guān)鍵字實例詳解

    注釋是對程序語言的說明,有助于開發(fā)者和用戶之間的交流,方便理解程序,注釋不是編程語句,因此被編譯器忽略,下面這篇文章主要給大家介紹了關(guān)于Java注釋和關(guān)鍵字的相關(guān)資料,需要的朋友可以參考下
    2023-01-01
  • java復(fù)制文件的4種方式及拷貝文件到另一個目錄下的實例代碼

    java復(fù)制文件的4種方式及拷貝文件到另一個目錄下的實例代碼

    這篇文章主要介紹了java復(fù)制文件的4種方式,通過實例帶給大家介紹了java 拷貝文件到另一個目錄下的方法,需要的朋友可以參考下
    2018-06-06
  • Spring?Security登錄表單配置示例詳解

    Spring?Security登錄表單配置示例詳解

    這篇文章主要介紹了Spring?Security登錄表單配置,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-06-06
  • 淺談FileItem類的常用方法

    淺談FileItem類的常用方法

    下面小編就為大家?guī)硪黄獪\談FileItem類的常用方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-08-08

最新評論