国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > Java > 正文

SpringCloud之Feign示例詳解

2024-07-13 10:16:38
字體:
來源:轉載
供稿:網友

Feign簡介

Feign 是一個聲明web服務客戶端,這便得編寫web服務客戶端更容易,使用Feign 創建一個接口并對它進行注解,它具有可插拔的注解支持包括Feign注解與JAX-RS注解,Feign還支持可插拔的編碼器與解碼器,Spring Cloud 增加了對 Spring MVC的注解,Spring Web 默認使用了HttpMessageConverters, Spring Cloud 集成 Ribbon 和 Eureka 提供的負載均衡的HTTP客戶端 Feign.

聲明式REST客戶端:Feign

先要啟動eureka_register_service工程(注冊中心)和biz-service-0工程(服務生產者)

創建一個maven工程eureka_feign_client

pom.xml

<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.4.3.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><java.version>1.8</java.version></properties><dependencies>  <dependency>    <groupId>org.springframework.cloud</groupId>    <artifactId>spring-cloud-starter-feign</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-web</artifactId>  </dependency>  <dependency>    <groupId>org.springframework.boot</groupId>    <artifactId>spring-boot-starter-test</artifactId>    <scope>test</scope>  </dependency></dependencies><dependencyManagement>  <dependencies>    <dependency>  <groupId>org.springframework.cloud</groupId>  <artifactId>spring-cloud-dependencies</artifactId>  <version>Brixton.SR5</version>  <type>pom</type>  <scope>import</scope></dependency>  </dependencies></dependencyManagement> 

在應用主類中通過@EnableFeignClients注解開啟Feign功能

啟動文件FeignApplication.java

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

定義服務接口類UserClient.java

使用@FeignClient("biz-service-0")注解來綁定該接口對應biz-service-0服務

@FeignClient("biz-service-0")public interface UserClient {   @RequestMapping(method = RequestMethod.GET, value = "/getuser")  public User getuserinfo();     @RequestMapping(method = RequestMethod.GET, value = "/getuser")  public String getuserinfostr();     @RequestMapping(method = RequestMethod.GET, value = "/info")  public String info();} 

在web層中調用上面定義的UserController,具體如下

@RestControllerpublic class UserController {  @Autowired  UserClient userClient;  @RequestMapping(value = "/getuserinfo", method = RequestMethod.GET)  public User getuserinfo() {    return userClient.getuserinfo();  }  @RequestMapping(value = "/getuserinfostr", method = RequestMethod.GET)  public String getuserinfostr() {    return userClient.getuserinfostr();  }  @RequestMapping(value = "/info", method = RequestMethod.GET)  public String info() {    return userClient.info();  }} 

application.properties配置變量

spring.application.name=feign-consumerserver.port=8004eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/ 

訪問 http://127.0.0.1:8004/getuserinfo

總結:

其實通過Feign封裝了HTTP調用服務方法,使得客戶端像調用本地方法那樣直接調用方法,類似Dubbo中暴露遠程服務的方式,區別在于Dubbo是基于私有二進制協議,而Feign本質上還是個HTTP客戶端

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VeVb武林網。


注:相關教程知識閱讀請移步到JAVA教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 新田县| 西宁市| 呼图壁县| 六枝特区| 墨玉县| 蒙城县| 涿州市| 米林县| 靖宇县| 义马市| 普宁市| 永春县| 长宁县| 双流县| 万盛区| 博白县| 邹城市| 苍梧县| 栖霞市| 平潭县| 寿光市| 鄂温| 莲花县| 西盟| 广水市| 额济纳旗| 泸定县| 黔东| 洪江市| 兰考县| 榆林市| 永康市| 洛隆县| 商南县| 夏津县| 淮安市| 乐山市| 万荣县| 焦作市| 六安市| 蕲春县|