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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

Feign的使用

發布時間:2024/4/13 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Feign的使用 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
我們使用Feign來進行應用間的通信,我們在調用方增加一個依賴,第一步加依賴,第二步在啟動類上加注解,<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-feign</artifactId> </dependency>@EnableEurekaClient @EnableFeignClients @SpringBootApplication public class OrderApplication {public static void main(String[] args) {SpringApplication.run(OrderApplication.class, args);} }定義好你要調用哪些接口@FeignClient(name = "product") public interface ProductFeignClient {@RequestMapping(value = "/msg", method = RequestMethod.GET)public String productMsg(); }這個方法名字可以和product不一樣的,上面加一個注解@FeignClient,name是表示你要訪問哪個應用的接口,我們要訪問的是product,就這么來寫,product下面的msg接口,你千萬要注意,你沒有寫過的話可能會有點奇怪,這不是服務端的寫法嗎,怎么客戶端寫成這樣子,這里寫完之后怎么來用,這里是一個接口,只是定義,定義完了,通信完全是一個類,調用某個方法@RestController public class ClientController2 {@Autowiredprivate ProductFeignClient productFeignClient;@GetMapping("/getProductMsg")public String findById(@PathVariable Long id) {String productMsg = this.productFeignClient.productMsg();System.out.println(productMsg);return productMsg;} }你可以看到剛剛我們在服務端,是一點改變都沒有的,完全沒有動過,只是在客戶端修改了相關的代碼,localhost:7900/msglocalhost:7901/msglocalhost:8010/getProductMsgFeign作為客戶端要去訪問別人的地址,Feign是一個聲明式客戶端,注意他并不是RPC,剛才我們已經使用過了,他是接口加注解的方式,也就是定義一個接口,然后在接口上添加注解,回憶我們剛才在程序的入口類,通過注解,來開啟Feign,然后定義了一個Feign接口,來指定調用哪個服務,Feign讓我們不習慣的原因呢,他感覺是在用RPC,完全感覺不到是遠程方法,更感覺不到這是一個HTTP請求,但是Feign本質還是HTTP客戶端,通過Feign我們可以遠程調用,開發者完全透明,得到與本地方法一樣的體驗,Fegin內部也使用了Ribbon做負載均衡 <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.learn</groupId><artifactId>order</artifactId><version>0.0.1-SNAPSHOT</version><packaging>jar</packaging><parent><groupId>cn.learn</groupId><artifactId>microcloud02</artifactId><version>0.0.1</version></parent><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><java.version>1.8</java.version><thymeleaf.version>3.0.9.RELEASE</thymeleaf.version><thymeleaf-layout-dialect.version>2.2.2</thymeleaf-layout-dialect.version></properties><dependencies><dependency><!-- 引入web模塊 --><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!-- Spring Boot進行單元測試的模塊 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-eureka</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-feign</artifactId></dependency></dependencies><!-- 這個插件,可以將應用打包成一個可執行的jar包 --><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project> #debug=true server.port=8010eureka.client.serviceUrl.defaultZone=http://admin:1234@10.40.8.152:8761/eurekaspring.application.name=order eureka.instance.prefer-ip-address=true eureka.instance.instance-id=${spring.application.name}:${spring.cloud.client.ipAddress}:${spring.application.instance_id:${server.port}} eureka.client.healthcheck.enabled=trueproduct.ribbon.NFLoadBalancerRuleClassName=com.netflix.loadbalancer.RoundRobinRule package com.learn.cloud.client;import org.springframework.cloud.netflix.feign.FeignClient; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod;@FeignClient(name = "product") public interface ProductFeignClient {@RequestMapping(value = "/msg", method = RequestMethod.GET)public String productMsg(); } package com.learn.cloud.controller;import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController;import com.learn.cloud.client.ProductFeignClient;@RestController public class FeignClientController {@Autowiredprivate ProductFeignClient productFeignClient;@GetMapping("/getProductMsg")public String findById() {String productMsg = this.productFeignClient.productMsg();System.out.println(productMsg);return productMsg;} } package com.learn.cloud;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;@EnableEurekaClient @EnableFeignClients @SpringBootApplication public class OrderApplication {public static void main(String[] args) {SpringApplication.run(OrderApplication.class, args);} }

?

總結

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

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