SpringCloud聲明式Feign客戶端調(diào)用工具使用
前言
springcloud 支持兩種客戶端調(diào)用工具:
- RestTemplate,基本上不使用
- Feign,采用接口加注解方式,可讀性較強(qiáng)
注:本來打算繼續(xù)使用 consul 作為注冊中心來進(jìn)行 Feign 客戶端調(diào)用的,provide 配置如下,無奈一直調(diào)用不到 注冊上來的服務(wù)名,只好改用 Eureka 來使用,如有知道原因請指教!
##服務(wù)端口號(hào)
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高可用集群 項(xiàng)目來展示。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();
}
}啟動(dòng)類
AppOrder啟動(dòng)類:添加 @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 模板在請求時(shí)擁有客戶端負(fù)載均衡的能力
}
// 解決RestTemplate 找不到原因, 把RestTemplate注冊到Springboot容器中 @Bean
// 第一種方式
// @Bean
// RestTemplate restTemplate(){
// return new RestTemplate();
// }
// 第二種方式 @LoadBalanced能讓restTemplate 模板在請求時(shí)擁有客戶端負(fù)載均衡的能力
// @Bean
// @LoadBalanced
// RestTemplate restTemplate() {
// return new RestTemplate();
// }
// @EnableFeignClients 開啟Feign客戶端權(quán)限
}注:啟動(dòng)類位置要放正確

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

以上就是SpringCloud聲明式Feign客戶端調(diào)用工具使用的詳細(xì)內(nèi)容,更多關(guān)于SpringCloud聲明式Feign的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
SpringBoot打包成Docker鏡像的幾種實(shí)現(xiàn)方式
Spring Boot是一個(gè)用于構(gòu)建獨(dú)立的、可執(zhí)行的Spring應(yīng)用程序的框架,結(jié)合使用Spring Boot和Docker,可以方便地將應(yīng)用程序部署到不同的環(huán)境中本文,主要介紹了SpringBoot打包成Docker鏡像的幾種實(shí)現(xiàn)方式,感興趣的可以了解一下2024-01-01
Hibernate處理多對(duì)多關(guān)系的實(shí)現(xiàn)示例
本文介紹了Hibernate中實(shí)現(xiàn)多對(duì)多關(guān)系的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2026-01-01
Spring Boot中使用LDAP來統(tǒng)一管理用戶信息的示例
本篇文章主要介紹了Spring Boot中使用LDAP來統(tǒng)一管理用戶信息的示例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-01-01
Admin - SpringBoot + Maven 多啟動(dòng)環(huán)境配置實(shí)例詳解
這篇文章主要介紹了Admin - SpringBoot + Maven 多啟動(dòng)環(huán)境配置,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-03-03
SpringBoot啟動(dòng)時(shí)如何修改上下文
本文介紹了如何在Spring Boot啟動(dòng)時(shí)修改上下文,以便加載封裝JAR中的國際化文件,通過在resources目錄下的META-INF文件夾中的spring.factories文件中配置指定類,可以實(shí)現(xiàn)這一功能2024-11-11
Java實(shí)現(xiàn)合并兩個(gè)word文檔內(nèi)容
這篇文章主要為大家詳細(xì)介紹了如何使用Java實(shí)現(xiàn)合并兩個(gè)word文檔內(nèi)容,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-11-11
Java實(shí)現(xiàn)遞歸刪除菜單和目錄及目錄下所有文件
這篇文章主要為大家詳細(xì)介紹了Java如何實(shí)現(xiàn)遞歸刪除菜單和刪除目錄及目錄下所有文件,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以參考一下2025-03-03

