日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

SpringCloud:Feign接口转换调用服务(Feign 基本使用、Feign 相关配置)

發(fā)布時間:2024/4/13 javascript 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 SpringCloud:Feign接口转换调用服务(Feign 基本使用、Feign 相关配置) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
現(xiàn)在為止所進(jìn)行的所有的 Rest 服務(wù)調(diào)用實(shí)際上都會出現(xiàn)一個非常尷尬的局面,例如:以如下代碼為例:Dept dept = this.restTemplate.exchange(DEPT_GET_URL + id, HttpMethod.GET,new HttpEntity<Object>(this.headers), Dept.class).getBody();所有的數(shù)據(jù)的調(diào)用和轉(zhuǎn)換都必須由用戶自己來完成,而我們本身不擅長這些,我們習(xí)慣的編程模式是:通過接口來實(shí)現(xiàn)業(yè)務(wù)的操作,而不是通過具體的 Rest 數(shù)據(jù)。 2.1、Feign 基本使用為了方便起見現(xiàn)在將“microcloud-consumer-80”模塊復(fù)制為了“microcloud-consumer-feign”模塊。1、 【microcloud-consumer-feign】為了可以使用到 feign 支持,需要修改 pom.xml 配置文件,引入相關(guān)依賴包:<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-feign</artifactId></dependency>feign 包含了 Ribbon 支持,所以導(dǎo)入了以上的依賴包之后就表示項目之中已經(jīng)存在有了 ribbon 相關(guān)支持庫。 2、 【microcloud-service】建立一個新的模塊,這個模塊專門負(fù)責(zé)客戶端接口的定義; 3、 【microcloud-service】修改 pom.xml 配置文件,引用“microcloud-api”模塊,這樣就可以使用到 VO 類了; 4、 【microcloud-service】此時如果要通過 Feign 進(jìn)行遠(yuǎn)程 Rest 調(diào)用,那么必須要考慮服務(wù)的認(rèn)證問題?!?此時可以刪除原始的 RestConfig 進(jìn)行的配置處理,然后添加feign的認(rèn)證配置類package cn.study.commons.config;import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration;import feign.auth.BasicAuthRequestInterceptor;@Configuration public class FeignClientConfig {@Beanpublic BasicAuthRequestInterceptor getBasicAuthRequestInterceptor() {return new BasicAuthRequestInterceptor("studyjava", "hello");} } 5、 【microcloud-service】建立一個 IDeptClientService 接口;package cn.study.service;import java.util.List;import org.springframework.cloud.netflix.feign.FeignClient; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod;import cn.study.commons.config.FeignClientConfig; import cn.study.vo.Dept;@FeignClient(value="MICROCLOUD-PROVIDER-DEPT",configuration=FeignClientConfig.class) public interface IDeptClientService {@RequestMapping(method=RequestMethod.GET,value="/dept/get/{id}")public Dept get(@PathVariable("id") long id) ;@RequestMapping(method=RequestMethod.GET,value="/dept/list")public List<Dept> list() ;@RequestMapping(method=RequestMethod.POST,value="/dept/add")public boolean add(Dept dept) ; } 6、 【microcloud-consumer-feign】修改 pom.xml 配置文件,引入 microcloud-service 開發(fā)包:<dependency><groupId>microcloud-service</groupId><artifactId>microcloud-service</artifactId><version>0.0.1-SNAPSHOT</version></dependency> 7、 【microcloud-consumer-feign】修改 ConsumerDeptController 控制器程序類;package cn.study.microcloud.controller;import javax.annotation.Resource;import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;import cn.study.service.IDeptClientService; import cn.study.vo.Dept;@RestController public class ConsumerDeptController {@Resourceprivate IDeptClientService deptService ;@RequestMapping(value = "/consumer/dept/get")public Object getDept(long id) {return this.deptService.get(id);}@RequestMapping(value = "/consumer/dept/list")public Object listDept() {return this.deptService.list();}@RequestMapping(value = "/consumer/dept/add")public Object addDept(Dept dept) throws Exception {return this.deptService.add(dept);} } 8、 【microcloud-consumer-feign】修改程序啟動主類,追加操作處理。package cn.study.microcloud;import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.cloud.netflix.feign.EnableFeignClients;@SpringBootApplication @EnableEurekaClient @EnableFeignClients(basePackages={"cn.study.service"}) //進(jìn)行接口IDeptClientService的掃描生成使得可以注入到ConsumerDeptController里面 public class Feign_Consumer_80_StartSpringCloudApplication {public static void main(String[] args) {SpringApplication.run(Feign_Consumer_80_StartSpringCloudApplication.class,args);} } 2.2、Feign 相關(guān)配置1、 【microcloud-consumer-feign】Feign 之中最為核心的作用就是將 Rest 服務(wù)的信息轉(zhuǎn)換為接口, 但是在實(shí)際的使用之中也需要考慮到一些配置情況,例如:數(shù)據(jù)壓縮,Rest 的核心本質(zhì)在于:JSON 數(shù)據(jù) 傳輸(XML、文本),于是就必須思考一種情況,如果用戶發(fā)送的數(shù)據(jù)很大,這個時候可以考慮 修改 application.properties 配置文件對傳輸數(shù)據(jù)進(jìn)行壓縮;feign.compression.request.mime-types=text/xml,application/xml,application/json feign.compression.request.min-request-size=2048 2、 如果有需要則可以在項目之中開啟 feign 的相關(guān)日志信息(默認(rèn)不開啟):· 【microcloud-consumer-feign】修改 application.properties 配置文件,追加日志追蹤:logging.level.root=DEBUG · 【microcloud-service】修改 FeignClientConfig,開啟日志的輸出:package cn.study.commons.config;import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration;import feign.Logger; import feign.auth.BasicAuthRequestInterceptor;@Configuration public class FeignClientConfig {@Beanpublic Logger.Level getFeignLoggerLevel() {return feign.Logger.Level.FULL ;}@Beanpublic BasicAuthRequestInterceptor getBasicAuthRequestInterceptor() {return new BasicAuthRequestInterceptor("studyjava", "hello");} } 再次運(yùn)行程序現(xiàn)在可以觀察到如下的流程:· 當(dāng)使用 Feign 要通過接口的方法訪問 Rest 服務(wù)的時候會根據(jù)設(shè)置的服務(wù)類型發(fā)出請求,這個請求是發(fā)送給 Eureka(地址: “http://MICROCLOUD-PROVIDER-DEPT/dept/list”);· 隨后由于配置了授權(quán)處理,所以繼續(xù)發(fā)送授權(quán)信息(“Authorization”);· 在進(jìn)行服務(wù)調(diào)用的時候 Feign 融合了 Ribbon 技術(shù),所以也支持有負(fù)載均衡的處理;總結(jié):Feign = RestTempate + HttpHeader + Ribbon + Eureka 綜合體 = 業(yè)務(wù)接口的自動實(shí)例化

?

總結(jié)

以上是生活随笔為你收集整理的SpringCloud:Feign接口转换调用服务(Feign 基本使用、Feign 相关配置)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。