SpringCloud之Feign樣本詳解,springcloudfeign

來源:互聯網
上載者:User

SpringCloud之Feign樣本詳解,springcloudfeign

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用戶端

以上就是本文的全部內容,希望對大家的學習有所協助,也希望大家多多支援幫客之家。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.