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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

FeignClient使用

發布時間:2025/3/20 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 FeignClient使用 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

? ? 在使用Spring Cloud開發微服務應用時中,各個微服務服務提供者都是以HTTP接口的形式對外提供服務,因此服務消費者在調用服務提供者時,通過HTTP Client的方式訪問。當然我們可以使用JDK原生的`URLConnection`、`Apache的Http Client`、`Netty的異步HTTP Client`, Spring的`RestTemplate`去實現服務間的調用。Spring Cloud對Fegin進行了增強,使Fegin支持了Spring MVC的注解,并整合了Ribbon和Eureka,從而讓Fegin的使用更加方便(在Spring Cloud中使用Feign, 我們可以做到使用HTTP請求遠程服務時能與調用本地方法一樣的編碼體驗)。

一、FeignClient工作原理

? ?總結來說,Feign的源碼實現的過程如下:

  • 首先通過@EnableFeignCleints注解開啟FeignCleint
  • 根據Feign的規則實現接口,并加@FeignCleint注解
  • 程序啟動后,會進行包掃描,掃描所有的@ FeignCleint的注解的類,并將這些信息注入到ioc容器中
  • 當接口的方法被調用,通過jdk的代理,來生成具體的RequesTemplate
  • RequesTemplate在生成Request
  • Request交給Client去處理,其中Client可以是HttpUrlConnection、HttpClient也可以是Okhttp
  • 最后Client被封裝到LoadBalanceClient類,這個類結合類Ribbon做到了負載均衡

? 工作原理參見:https://zhuanlan.zhihu.com/p/28593019

二、示例

? ? FeignClient相當于Spring Cloud中的RPC,使用示例如下:

? ?(1)Eureka-Server注冊中心

? ? ? ?application.yml配置如下:

#application.yml server: port: 1111 spring:application:name:eureka-server eureka:client:register-with-eureka: falsefetch-registry: falseserver-url:defaultZone: http://localhost:${server.port}/eureka/

? ? EurekaServerApplication配置如下:

@EnableEurekaServer @SpringBootApplication public class EurekaServerApplication {public static void main(String[] args) {SpringApplication.run(EurekaServerApplication.class,args)} }

(2)Eureka-Producer配置

? ? 定義遠程服務HelloController

@RestController public class HelloController {@GetMapping("/hello")public String xxx(@RequstParam String name) {return "hello" + name + ", I'm eureka producer service!";} }

? Eureka-Client中application.yml配置

server:port: 1112 spring:application:name: eureka-producer eureka:client:server-url:defaultZone: http://localhost:1111/eureka/

?EurekaProducerApplication

@EnableDiscoveryClient @SpringBootApplication public class EurekaProducerApplication {public static void main(String[] args) {SpringApplication.run(EurekaProducerApplication.class,args)} }

(3)Eureka-Consumer配置

? ?Controller層服務配置如下:

@RestController public class ConsumerController {@AutowiredHelloRemote helloRemote;@RequestMapping("/hello/{name}")public String hello(@PathVariable("name") String name) {return helloRemote.hello(name);} }

 HelloRemote配置

@FeignClient(name="eureka-producer") public interface HelloRemote {@RequstMapping("/hello")String hello(@RequstParam(value="name") String name); }

? application.yml文件配置

server:port: 1113 spring:application:name: eureka-consumer eureka:client:server-url:defaultZone: http://localhost:1111/eureka

?EurekaConsumerApplication配置

@EnableFeignClients @EnableDiscoveryClient @SpringBootApplication public class EurekaConsumerApplication {public static void main(String[] args) {SpringApplication.run(EurekaConsumerApplication.class,args)} }

參見:http://www.voidcn.com/article/p-kodllxvn-hr.html

? ? ? ? ? ?http://xujin.org/sc/sc-fegin01/

? ? ? ? ? http://www.cnblogs.com/jalja/p/7011439.html

? ? ? ? ?http://www.jianshu.com/p/f908171b5025

? ? ? ? ?http://spring-cloud.io/reference/feign/

總結

以上是生活随笔為你收集整理的FeignClient使用的全部內容,希望文章能夠幫你解決所遇到的問題。

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