java使用Feign實現(xiàn)聲明式Restful風格調(diào)用
一、Feign簡介
Feign是netflix開發(fā)的聲明式、模板化的http客戶端,在使用時就像調(diào)用本地(服務消費者自己)的方法一般,幫助我們更加優(yōu)雅的調(diào)用服務提供者的API。Feign自身支持springMVC,還整合了Eureka、Ribbon,極大的簡化了Feign的使用。就整合Euraka而言,只需和普通的服務配置Eureka server的信息即可。整合Ribbon,就意味著不再需要通過標注@LoadBalanced的實例化后的RestTemplate去調(diào)用服務提供者方法了。Feign只需通過簡單的定義一個接口即可實現(xiàn)負載均衡。
二、在服務消費者中使用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中的其中一個服務,利用它可以實現(xiàn)負載均衡。也可以結(jié)合value來指定@FeignClient(name="user-provider",value = "http://localhost:8000/")
3、修改Controller,不再調(diào)用@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服務實例
如上測試結(jié)果可以看到ticket-consumer-feign消費者順利調(diào)用user-provider-eureka服務提供者的方法,并且實現(xiàn)了負載均衡。
三、使用Feign構(gòu)造多參數(shù)請求
1、get請求:多個參數(shù)就用多個@RequestParam標注幾個
@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請求就相對簡單的多
// 服務消費者方
@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
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
spring cloud eureka微服務之間的調(diào)用詳解
這篇文章主要介紹了spring cloud eureka微服務之間的調(diào)用詳解,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-07-07
RestTemplate設(shè)置超時時間及返回狀態(tài)碼非200處理
這篇文章主要為大家介紹了RestTemplate設(shè)置超時時間及返回狀態(tài)碼非200處理,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-06-06
在SpringBoot中使用MongoDB完成數(shù)據(jù)存儲
本文主要介紹了在SpringBoot中如惡化使用MongoDB完成數(shù)據(jù)存儲,接下來這篇我們將圍繞MongoDB進行,MongoDB是一個開源的,面向文檔的NoSQL數(shù)據(jù)庫管理系統(tǒng),使用類似JSON的BSON(二進制JSON)格式來存儲數(shù)據(jù),具有靈活的數(shù)據(jù)模型和強大的查詢功能,需要的朋友可以參考下2023-11-11
Java如何獲取resources下的文件路徑和創(chuàng)建臨時文件
這篇文章主要介紹了Java如何獲取resources下的文件路徑和創(chuàng)建臨時文件,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-12-12
packages思維及使用Java添加Android平臺特定實現(xiàn)
這篇文章主要為大家介紹了packages思維及使用Java添加Android平臺特定實現(xiàn)在Flutter框架里的體現(xiàn)和運用詳解,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-12-12

