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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Spring Cloud【Finchley】-06服务消费者整合Feign

發布時間:2025/3/21 javascript 17 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring Cloud【Finchley】-06服务消费者整合Feign 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

文章目錄

  • 概述
  • 實例
    • 新建工程
    • 增加maven依賴
    • 創建一個Feign接口,并添加@FeignClient注解
    • 修改Controller層,將RestTemplate改為調用Feign接口
    • 啟動類增加@EnableFeiginClients注解
    • 測試
  • 源碼

概述

回想下我們在使用Eureka 和 Ribbon的時候是怎么調用注冊在Eureka Server上的微服務的地址呢?

可以看到其實是通過拼接的方式,當然了我們上面的這個例子只有一個參數 id,看起來沒有這麻煩。

設想下如果有多個參數呢?
假設URL如下
http://localhost:8080/search?name=小工匠&age=20&username=artisan

那我們用RestTemplate如何調用對方的微服務呢? 可以采用如下方式

@GetMapping("/searchUser")public User searchUser(String name ,String age ,String username) {Map<String, Object> paraMap = new HashMap<String ,Object>() {{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);}

是不是已經很麻煩了?

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


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

Feign是Netflix開發的聲明模板化的HTTP客戶端。 在Spring Cloud中使用Feign,只需要創建一個接口,并在接口上添加一些注解即可。 Spring Cloud對Feign進行了增強,使Feign支持了SpringMVC的總結,并整合了Ribbon和Eureka。


實例

新建工程

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


下面根據官方文檔操作即可

增加maven依賴

<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-openfeign</artifactId> </dependency>

創建一個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);}

FeignClient中的microservice-provider-user是要調用的微服務的名稱,用于創建Ribbon負載均衡器。

因為我們這里使用了Eureka,所以Ribbon會把microservice-provider-user解析成Eureka Server中注冊的服務。

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


修改Controller層,將RestTemplate改為調用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 {@Autowiredprivate UserFeignClient userClient;@GetMapping("/movie/{id}")public User findById(@PathVariable Long id) {return userClient.findById(id);} }

啟動類增加@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);} }

測試

  • 啟動eureka server微服務
  • 啟動2個 provider-user微服務
  • 啟動該微服務
  • 2次請求http://localhost:7901/movie/1 ,觀察 provider-user微服務的日志打印情況。

    8900端口

    8901端口

    通過日志可以看到不僅實現了聲明式的REST API調用,同時也實現了客戶端的負載均衡。


    源碼

    https://github.com/yangshangwei/SpringCloudMaster

    《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀

    總結

    以上是生活随笔為你收集整理的Spring Cloud【Finchley】-06服务消费者整合Feign的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。