Spring CloudFinchley】-06服務(wù)消費(fèi)者整合Feign

      網(wǎng)友投稿 664 2025-04-01

      文章目錄


      概述

      實(shí)例

      新建工程

      增加maven依賴

      創(chuàng)建一個(gè)Feign接口,并添加@FeignClient注解

      修改Controller層,將RestTemplate改為調(diào)用Feign接口

      啟動(dòng)類增加@EnableFeiginClients注解

      測(cè)試

      源碼

      概述

      回想下我們?cè)谑褂肊ureka 和 Ribbon的時(shí)候是怎么調(diào)用注冊(cè)在Eureka Server上的微服務(wù)的地址呢?

      可以看到其實(shí)是通過(guò)拼接的方式,當(dāng)然了我們上面的這個(gè)例子只有一個(gè)參數(shù) id,看起來(lái)沒(méi)有這麻煩。

      設(shè)想下如果有多個(gè)參數(shù)呢?

      假設(shè)URL如下

      http://localhost:8080/search?name=小工匠&age=20&username=artisan

      那我們用RestTemplate如何調(diào)用對(duì)方的微服務(wù)呢? 可以采用如下方式

      @GetMapping("/searchUser") public User searchUser(String name ,String age ,String username) { Map paraMap = new HashMap() { { put("name",name); put("age",age); put("username",username); } }; return this.restTemplate.getForObject("http://microservice-provider-user/search?name={name}&age={age}&username={username}", User.class, paraMap); }

      1

      2

      3

      4

      5

      6

      7

      8

      9

      10

      Spring Cloud【Finchley】-06服務(wù)消費(fèi)者整合Feign

      11

      12

      13

      是不是已經(jīng)很麻煩了?

      spring Cloud為我們整合了Fegin解決上述苦惱。

      Feign官方文檔: https://cloud.spring.io/spring-cloud-static/Finchley.SR2/single/spring-cloud.html#_spring_cloud_openfeign

      Feign是Netflix開發(fā)的聲明模板化的HTTP客戶端。 在Spring Cloud中使用Feign,只需要?jiǎng)?chuàng)建一個(gè)接口,并在接口上添加一些注解即可。 Spring Cloud對(duì)Feign進(jìn)行了增強(qiáng),使Feign支持了SpringMVC的總結(jié),并整合了Ribbon和Eureka。

      實(shí)例

      新建工程

      在父工程上右鍵,新建Maven Module ,如下

      下面根據(jù)官方文檔操作即可

      增加maven依賴

      org.springframework.cloud spring-cloud-starter-openfeign

      1

      2

      3

      4

      創(chuàng)建一個(gè)Feign接口,并添加@FeignClient注解

      package com.artisan.micorservice.feignclient; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import com.artisan.micorservice.model.User; @FeignClient("microservice-provider-user") public interface UserFeignClient { @RequestMapping(method = RequestMethod.GET, value = "/user/{id}") public User findById(@PathVariable Long id); }

      1

      2

      3

      4

      5

      6

      7

      8

      9

      10

      11

      12

      13

      14

      15

      16

      17

      FeignClient中的microservice-provider-user是要調(diào)用的微服務(wù)的名稱,用于創(chuàng)建Ribbon負(fù)載均衡器。

      因?yàn)槲覀冞@里使用了Eureka,所以Ribbon會(huì)把microservice-provider-user解析成Eureka Server中注冊(cè)的服務(wù)。

      另外,也可以通過(guò)url屬性指定請(qǐng)求的URL ,比如 @FeignClient("microservice-provider-user", url="http://localhost:8900/")

      修改Controller層,將RestTemplate改為調(diào)用Feign接口

      package com.artisan.micorservice.controller; 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.RestController; import com.artisan.micorservice.feignclient.UserFeignClient; import com.artisan.micorservice.model.User; @RestController public class MovieController { @Autowired private UserFeignClient userClient; @GetMapping("/movie/{id}") public User findById(@PathVariable Long id) { return userClient.findById(id); } }

      1

      2

      3

      4

      5

      6

      7

      8

      9

      10

      11

      12

      13

      14

      15

      16

      17

      18

      19

      20

      21

      22

      啟動(dòng)類增加@EnableFeiginClients注解

      package com.artisan.micorservice; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.openfeign.EnableFeignClients; @EnableDiscoveryClient @SpringBootApplication @EnableFeignClients public class MicorserviceConsumerFeginApplication { public static void main(String[] args) { SpringApplication.run(MicorserviceConsumerFeginApplication.class, args); } }

      1

      2

      3

      4

      5

      6

      7

      8

      9

      10

      11

      12

      13

      14

      15

      16

      測(cè)試

      啟動(dòng)eureka server微服務(wù)

      啟動(dòng)2個(gè) provider-user微服務(wù)

      啟動(dòng)該微服務(wù)

      2次請(qǐng)求http://localhost:7901/movie/1 ,觀察 provider-user微服務(wù)的日志打印情況。

      8900端口

      8901端口

      通過(guò)日志可以看到不僅實(shí)現(xiàn)了聲明式的REST API調(diào)用,同時(shí)也實(shí)現(xiàn)了客戶端的負(fù)載均衡。

      源碼

      https://github.com/yangshangwei/SpringCloudMaster

      Spring Spring Cloud

      版權(quán)聲明:本文內(nèi)容由網(wǎng)絡(luò)用戶投稿,版權(quán)歸原作者所有,本站不擁有其著作權(quán),亦不承擔(dān)相應(yīng)法律責(zé)任。如果您發(fā)現(xiàn)本站中有涉嫌抄襲或描述失實(shí)的內(nèi)容,請(qǐng)聯(lián)系我們jiasou666@gmail.com 處理,核實(shí)后本網(wǎng)站將在24小時(shí)內(nèi)刪除侵權(quán)內(nèi)容。

      版權(quán)聲明:本文內(nèi)容由網(wǎng)絡(luò)用戶投稿,版權(quán)歸原作者所有,本站不擁有其著作權(quán),亦不承擔(dān)相應(yīng)法律責(zé)任。如果您發(fā)現(xiàn)本站中有涉嫌抄襲或描述失實(shí)的內(nèi)容,請(qǐng)聯(lián)系我們jiasou666@gmail.com 處理,核實(shí)后本網(wǎng)站將在24小時(shí)內(nèi)刪除侵權(quán)內(nèi)容。

      上一篇:制造ERP系統(tǒng)的需求和產(chǎn)能計(jì)劃
      下一篇:如何批量修改wps表的行和列
      相關(guān)文章
      国产精品亚洲一区二区三区| 国产色在线|亚洲| 亚洲国产AV一区二区三区四区| 久久精品国产亚洲AV大全| 亚洲AV无码国产在丝袜线观看| 伊人久久亚洲综合| 区久久AAA片69亚洲| 亚洲人成亚洲人成在线观看| 国产成人精品日本亚洲专区61 | 亚洲精品无码国产| 亚洲伊人久久精品影院| 亚洲一区二区高清| 国产亚洲精品国看不卡| 亚洲线精品一区二区三区| 亚洲精品无码成人AAA片| 国产亚洲AV无码AV男人的天堂| 亚洲精品字幕在线观看| 亚洲av永久无码制服河南实里 | 亚洲午夜国产精品| 亚洲AV无码一区二区三区在线| 精品亚洲AV无码一区二区| 国产亚洲国产bv网站在线| 亚洲人成自拍网站在线观看| 亚洲国产精品成人午夜在线观看| 国产成人人综合亚洲欧美丁香花 | 亚洲夜夜欢A∨一区二区三区| 亚洲中文字幕无码一区| 亚洲av无码成人黄网站在线观看| 亚洲A∨无码无在线观看| 激情内射亚洲一区二区三区| 亚洲日韩国产精品无码av| 国产精品亚洲午夜一区二区三区| 自拍偷区亚洲国内自拍| jzzijzzij在线观看亚洲熟妇| 亚洲第一区精品日韩在线播放| 亚洲一级片免费看| 久久精品国产亚洲香蕉| 亚洲资源在线视频| 国产精品亚洲综合久久| 国产成人亚洲毛片| 亚洲中文字幕无码一久久区|