SpringCloud Feign配置應(yīng)用詳細(xì)介紹
前言
服務(wù)消費(fèi)者調(diào)用服務(wù)提供者的時(shí)候使用RestTemplate技術(shù)

存在不便之處:
- 拼接url
- restTmplate.getForObJect
這兩處代碼都比較模板化,能不能不讓我我們來(lái)寫(xiě)這種模板化的東西,另外來(lái)說(shuō),拼接url非常的low,拼接字符串,拼接參數(shù),很low還容易出錯(cuò)
1、Feign簡(jiǎn)介
Feign是Netflix開(kāi)發(fā)的一個(gè)輕量級(jí)RESTful的HTTP服務(wù)客戶(hù)端(用它來(lái)發(fā)起請(qǐng)求,遠(yuǎn)程調(diào)用的),是以Java接口注解的方式調(diào)用Http請(qǐng)求,而不用像Java中通過(guò)封裝HTTP請(qǐng)求報(bào)文的方式直接調(diào)用,F(xiàn)eign被?泛應(yīng)用在Spring Cloud 的解決方案中。
類(lèi)似于Dubbo,服務(wù)消費(fèi)者拿到服務(wù)提供者的接口,然后像調(diào)用本地接口方法一樣去調(diào)用,實(shí)際發(fā)出的是遠(yuǎn)程的請(qǐng)求。
- Feign可幫助我們更加便捷,優(yōu)雅的調(diào)用HTTP API:不需要我們?nèi)テ唇觰rl然后呢調(diào)用restTemplate的api,在SpringCloud中,使用Feign?常簡(jiǎn)單,創(chuàng)建一個(gè)接口(在消費(fèi)者--服務(wù)調(diào)用方這一端),并在接口上添加一些注解,代碼就完成了
- SpringCloud對(duì)Feign進(jìn)行了增強(qiáng),使Feign支持了SpringMVC注解(OpenFeign)
本質(zhì):封裝了Http調(diào)用流程,更符合面向接口化的編程習(xí)慣,類(lèi)似于Dubbo的服務(wù)調(diào)用
2、Feign配置應(yīng)用
在服務(wù)調(diào)用者工程 (消費(fèi)) 創(chuàng)建接口(添加注解)(效果)Feign = RestTemplate+Ribbon+Hystrix
服務(wù)消費(fèi)者工程(自動(dòng)投遞微服務(wù))中引入Feign依賴(lài)(或者父類(lèi)工程)
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
</dependencies>服務(wù)消費(fèi)者工程(自動(dòng)投遞微服務(wù))啟動(dòng)類(lèi)使用注解@EnableFeignClients添加Feign支持
package com.lagou.edu;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients // 開(kāi)啟Feign客戶(hù)端功能
public class AutoDeliverApplication8091 {
public static void main(String[] args) {
SpringApplication.run(AutoDeliverApplication8091.class, args);
}
}注意:此時(shí)去掉Hystrix熔斷的支持注解@EnableCircuitBreaker即可包括引入的依賴(lài),因?yàn)镕eign會(huì)自動(dòng)引入。
配置文件
server:
port: 8091
# 注冊(cè)到Eureka服務(wù)中心
eureka:
client:
service-url:
# 注冊(cè)到集群,就把多個(gè)EurekaServer地址使用逗號(hào)連接起來(lái)即可;注冊(cè)到單實(shí)例(非集群模式),那就寫(xiě)一個(gè)
defaultZone: http://LagouCloudEurekaServerB:8762/eureka,http://LagouCloudEurekaServerA:8761/eureka
instance:
prefer-ip-address: true # 服務(wù)實(shí)例中顯示ip,而不是顯示主機(jī)名(兼容老的Eureka版本)
# 自定義實(shí)例顯示格式,加上版本號(hào),便于多版本管理,注意是ip-address,早期版本是ipAddress
instance-id: ${spring.cloud.client.ipaddress}:${spring.application.name}:${server.port}:@project.version@
spring:
application:
name: lagou-service-autodeliver
#針對(duì)的被調(diào)用方微服務(wù)名稱(chēng),不加就是全局生效
lagou-service-resume:
ribbon:
NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RoundRobinRule #負(fù)載策略調(diào)整
# springboot中暴露健康檢查等斷點(diǎn)接?
management:
endpoints:
web:
exposure:
include: "*"
# 暴露健康接?的細(xì)節(jié)
endpoint:
health:
show-details: always
創(chuàng)建Feign接口
package com.lagou.edu.controller.service;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
// @FeignClient標(biāo)明當(dāng)前類(lèi)是一個(gè)Feign客戶(hù)端,value指定該客戶(hù)端要請(qǐng)求的服務(wù)器名稱(chēng)(登記到注冊(cè)中心上服務(wù)提供者的服務(wù)名稱(chēng))
// name或value:調(diào)?的服務(wù)名稱(chēng),和服務(wù)提供者yml?件中spring.application.name保持?致
@FeignClient(value = "lagou-service-resume")
@RequestMapping("/resume")
public interface ResumeServiceFeignClient {
// feign要做的事情,拼裝url發(fā)起請(qǐng)求
// 我們調(diào)用該方法就是調(diào)用本地接口方法,那么實(shí)際上做的事遠(yuǎn)程請(qǐng)求
@GetMapping("/openstate/{userId}")
public Integer findDefaultResumeState(@PathVariable Long userId);
}注意:
- @FeignClient注解的name屬性用于指定要調(diào)用的服務(wù)提供者名稱(chēng),和服務(wù)提供者yml?件中spring.application.name保持一致
- 接口中的接口方法,就好比是遠(yuǎn)程服務(wù)提供者Controller中的Hander方法(只不過(guò)如同本地調(diào)用了),那么在進(jìn)行參數(shù)綁定的時(shí),可以使用@PathVariable、@RequestParam、RequestHeader等,這也是OpenFeign對(duì)SpringMVC注解的支持,但是需要注意value必須設(shè)置,否則會(huì)拋出異常
使用接口中方法完成遠(yuǎn)程調(diào)用(注入接口即可,實(shí)際注入的是接口的實(shí)現(xiàn))
package com.lagou.edu.controller;
import com.lagou.edu.controller.service.ResumeServiceFeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/autodeliver")
public class AutoDeliverController {
@Autowired
private ResumeServiceFeignClient resumeServiceFeignClient;
@GetMapping("/checkState/{userId}")
public Integer findResumeOpenState(@PathVariable Long userId) {
Integer defaultResumeState = resumeServiceFeignClient.findDefaultResumeState(userId);
return defaultResumeState;
}
}到此這篇關(guān)于SpringCloud Feign配置應(yīng)用詳細(xì)介紹的文章就介紹到這了,更多相關(guān)SpringCloud Feign內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot數(shù)據(jù)校驗(yàn)功能的實(shí)現(xiàn)
這篇文章主要介紹了SpringBoot數(shù)據(jù)校驗(yàn)功能的實(shí)現(xiàn),本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-02-02
Spring主配置文件(applicationContext.xml) 導(dǎo)入約束詳解
在本篇文章里我們給各位整理的是關(guān)于Spring主配置文件(applicationContext.xml) 導(dǎo)入約束的相關(guān)知識(shí)點(diǎn)內(nèi)容,需要參考下。2019-08-08
java實(shí)現(xiàn)隨機(jī)輸出300題四則運(yùn)算
本文主要介紹了java實(shí)現(xiàn)隨機(jī)輸出300題四則運(yùn)算實(shí)例,具有很好的參考價(jià)值。下面跟著小編一起來(lái)看下吧2017-03-03
關(guān)于Java鎖性能提高(鎖升級(jí))機(jī)制的總結(jié)
這篇文章主要介紹了關(guān)于Java鎖性能提高(鎖升級(jí))機(jī)制的總結(jié),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-05-05

