java使用Feign實(shí)現(xiàn)聲明式Restful風(fēng)格調(diào)用
一、Feign簡(jiǎn)介
Feign是netflix開(kāi)發(fā)的聲明式、模板化的http客戶端,在使用時(shí)就像調(diào)用本地(服務(wù)消費(fèi)者自己)的方法一般,幫助我們更加優(yōu)雅的調(diào)用服務(wù)提供者的API。Feign自身支持springMVC,還整合了Eureka、Ribbon,極大的簡(jiǎn)化了Feign的使用。就整合Euraka而言,只需和普通的服務(wù)配置Eureka server的信息即可。整合Ribbon,就意味著不再需要通過(guò)標(biāo)注@LoadBalanced的實(shí)例化后的RestTemplate去調(diào)用服務(wù)提供者方法了。Feign只需通過(guò)簡(jiǎn)單的定義一個(gè)接口即可實(shí)現(xiàn)負(fù)載均衡。
二、在服務(wù)消費(fèi)者中使用Feign
1、添加Feign依賴
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-feign</artifactId> </dependency>
2、創(chuàng)建一個(gè)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" 會(huì)被解析為注冊(cè)到Eureka server上的其中一個(gè)客戶端,換句話說(shuō)就是注冊(cè)到Eureka中的其中一個(gè)服務(wù),利用它可以實(shí)現(xiàn)負(fù)載均衡。也可以結(jié)合value來(lái)指定@FeignClient(name="user-provider",value = "http://localhost:8000/")
3、修改Controller,不再調(diào)用@LoadBalanced標(biāo)注的RestTemplate,而是通過(guò)標(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、修改啟動(dòng)類(lèi),頭部添加@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);
}
}
測(cè)試:
啟動(dòng)多個(gè)user-provider-eureka服務(wù)實(shí)例,其配置文件中的application.name=user-provider;
啟動(dòng)discovery-eureka服務(wù)實(shí)例;
啟動(dòng)ticket-consumer-feign服務(wù)實(shí)例
如上測(cè)試結(jié)果可以看到ticket-consumer-feign消費(fèi)者順利調(diào)用user-provider-eureka服務(wù)提供者的方法,并且實(shí)現(xiàn)了負(fù)載均衡。
三、使用Feign構(gòu)造多參數(shù)請(qǐng)求
1、get請(qǐng)求:多個(gè)參數(shù)就用多個(gè)@RequestParam標(biāo)注幾個(gè)
@FeignClient(name = "user-provider")
public interface UserFeignService {
@RequestMapping(value = "/getuserinfo",method = RequestMethod.GET)
CommonResult getUserByName(@RequestParam(required = false,value = "name") String name);
}
或者用Map來(lái)封裝參數(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請(qǐng)求就相對(duì)簡(jiǎn)單的多
// 服務(wù)消費(fèi)者方
@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);
}
}
項(xiàng)目的github
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
spring cloud eureka微服務(wù)之間的調(diào)用詳解
這篇文章主要介紹了spring cloud eureka微服務(wù)之間的調(diào)用詳解,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-07-07
Java空格替換逗號(hào)的實(shí)現(xiàn)示例
在編程中,我們經(jīng)常需要對(duì)字符串進(jìn)行各種處理,其中一個(gè)常見(jiàn)的需求是將字符串中的逗號(hào)替換為空格,本文主要介紹了Java空格替換逗號(hào)的實(shí)現(xiàn)示例,具有一定的參考價(jià)值,感興趣的可以了解一下2024-01-01
RestTemplate設(shè)置超時(shí)時(shí)間及返回狀態(tài)碼非200處理
這篇文章主要為大家介紹了RestTemplate設(shè)置超時(shí)時(shí)間及返回狀態(tài)碼非200處理,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06
在SpringBoot中使用MongoDB完成數(shù)據(jù)存儲(chǔ)
本文主要介紹了在SpringBoot中如惡化使用MongoDB完成數(shù)據(jù)存儲(chǔ),接下來(lái)這篇我們將圍繞MongoDB進(jìn)行,MongoDB是一個(gè)開(kāi)源的,面向文檔的NoSQL數(shù)據(jù)庫(kù)管理系統(tǒng),使用類(lèi)似JSON的BSON(二進(jìn)制JSON)格式來(lái)存儲(chǔ)數(shù)據(jù),具有靈活的數(shù)據(jù)模型和強(qiáng)大的查詢功能,需要的朋友可以參考下2023-11-11
Java如何獲取resources下的文件路徑和創(chuàng)建臨時(shí)文件
這篇文章主要介紹了Java如何獲取resources下的文件路徑和創(chuàng)建臨時(shí)文件,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-12-12
第三方網(wǎng)站微信登錄java代碼實(shí)現(xiàn)
這篇文章主要為大家詳細(xì)介紹了第三方網(wǎng)站微信登錄的java代碼實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-04-04
Java實(shí)現(xiàn)動(dòng)態(tài)代理
本文給大家介紹的是java使用動(dòng)態(tài)代理類(lèi)實(shí)現(xiàn)動(dòng)態(tài)代理的方法和示例,這里推薦給大家,有需要的小伙伴參考下吧2015-02-02
packages思維及使用Java添加Android平臺(tái)特定實(shí)現(xiàn)
這篇文章主要為大家介紹了packages思維及使用Java添加Android平臺(tái)特定實(shí)現(xiàn)在Flutter框架里的體現(xiàn)和運(yùn)用詳解,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12

