SpringCloud聲明式Feign客戶端調(diào)用工具使用
前言
springcloud 支持兩種客戶端調(diào)用工具:
- RestTemplate,基本上不使用
- Feign,采用接口加注解方式,可讀性較強(qiáng)
注:本來打算繼續(xù)使用 consul 作為注冊中心來進(jìn)行 Feign 客戶端調(diào)用的,provide 配置如下,無奈一直調(diào)用不到 注冊上來的服務(wù)名,只好改用 Eureka 來使用,如有知道原因請指教!
##服務(wù)端口號
server:
port: 8501
spring:
application:
##服務(wù)別名--服務(wù)注冊到consul名稱
name: consul-member
##注冊中心consul地址
cloud:
consul:
host: localhost
port: 8500
discovery:
service-name: ${spring.application.name} # 服務(wù)提供名稱
# ## consul ip地址
# hostname: 192.168.3.91Eureka Feign客戶端調(diào)用
這里我們基于 SpringCloud整合之Eureka高可用集群 項目來展示。provide 我們不需要做任何修改,只需要在 consumer order模塊加入以下依賴:
<!--springcloud 整合openfeign--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency>
feign 調(diào)用接口
MemberApiFeign.java: @FeignClient指定provide服務(wù)別名,接口copy調(diào)用即可:
package com.baba.wlb.api.feign;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* @Author wulongbo
* @Date 2021/1/21 19:17
* @Version 1.0
*/
@FeignClient(name = "app-member")
public interface MemberApiFeign {
// Feign 書寫方式以springMVC接口形式書寫
// @FeignClient調(diào)用服務(wù)接口name就是服務(wù)名稱
@RequestMapping("/getMember")
String getMember();
}控制頁面
AppFeignController.java: 注入 Feign 接口
package com.baba.wlb.api.controller;
import com.baba.wlb.api.feign.MemberApiFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @Author wulongbo
* @Date 2021/1/21 19:21
* @Version 1.0
*/
@RestController
public class AppFeignController {
@Autowired
private MemberApiFeign memberApiFeign;
@RequestMapping("/getFeignOrder")
public String getFeignOrder() {
return memberApiFeign.getMember();
}
}啟動類
AppOrder啟動類:添加 @EnableFeignClients 開啟Feign客戶端權(quán)限
package com.baba.wlb;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
/**
* @Author wulongbo
* @Date 2021/1/9 15:39
* @Version 1.0
*/@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class AppOrder {
public static void main(String[] args) {
SpringApplication.run(AppOrder.class, args);
// 第二種方式 如果使用rest方式以別名方式進(jìn)行調(diào)用依賴ribbon負(fù)載均衡器
// 第二種方式 @LoadBalanced能讓restTemplate 模板在請求時擁有客戶端負(fù)載均衡的能力
}
// 解決RestTemplate 找不到原因, 把RestTemplate注冊到Springboot容器中 @Bean
// 第一種方式
// @Bean
// RestTemplate restTemplate(){
// return new RestTemplate();
// }
// 第二種方式 @LoadBalanced能讓restTemplate 模板在請求時擁有客戶端負(fù)載均衡的能力
// @Bean
// @LoadBalanced
// RestTemplate restTemplate() {
// return new RestTemplate();
// }
// @EnableFeignClients 開啟Feign客戶端權(quán)限
}注:啟動類位置要放正確

啟動 Eureka Server服務(wù),啟動 member provide 服務(wù),啟動 order consumer 服務(wù)后訪問:
http://localhost:8200/getFeignOrder

以上就是SpringCloud聲明式Feign客戶端調(diào)用工具使用的詳細(xì)內(nèi)容,更多關(guān)于SpringCloud聲明式Feign的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
關(guān)于Spring?Data?Jpa?自定義方法實現(xiàn)問題
這篇文章主要介紹了關(guān)于Spring?Data?Jpa?自定義方法實現(xiàn)問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-12-12
Spring的同一個服務(wù)會加載多次的問題分析及解決方法
這篇文章主要介紹了Spring的同一個服務(wù)為什么會加載多次,我們先來梳理一下?Web?容器中如何加載?Bean,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-10-10
SpringBoot訪問MongoDB數(shù)據(jù)庫的兩種方式
MongoDB是一種非關(guān)系型數(shù)據(jù)庫,通過文檔存儲數(shù)據(jù),適用于大規(guī)模數(shù)據(jù)存儲和高并發(fā)訪問,這篇文章主要介紹了SpringBoot訪問MongoDB數(shù)據(jù)庫的兩種方式,感興趣想要詳細(xì)了解可以參考下文2023-05-05
SpringBoot實現(xiàn)支付寶沙箱支付的完整步驟
沙箱支付是一種用于模擬真實支付環(huán)境的測試工具,它提供了一個安全的測試環(huán)境,供開發(fā)者在不影響真實交易的情況下進(jìn)行支付功能的開發(fā)和測試,這篇文章給大家介紹了SpringBoot實現(xiàn)支付寶沙箱支付的完整步驟,需要的朋友可以參考下2024-04-04
使用GSON庫轉(zhuǎn)換Java對象為JSON對象的進(jìn)階實例詳解
這篇文章主要介紹了使用GSON庫轉(zhuǎn)換Java對象為JSON對象的進(jìn)階實例詳解,包括注冊TypeAdapter及處理Enum類型等實際運用中可能遇到的一些復(fù)雜問題,需要的朋友可以參考下2016-06-06
Java中的BlockingQueue阻塞隊列原理以及實現(xiàn)詳解
這篇文章主要介紹了Java中的BlockingQueue阻塞隊列原理以及實現(xiàn)詳解,在最常見的使用到這個阻塞隊列的地方,就是我們耳熟能詳?shù)木€程池里面了,作為我們線程池的一大最大參與者,也是AQS的一個具體實現(xiàn),需要的朋友可以參考下2023-12-12

