SpringCloud之Feign


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

聲明式REST客戶端:Feign

  創建一個maven工程eureka_feign_client,pom文件如下:

<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
@EnableFeignClients
public class FeignApplication {
 
    public static void main(String[] args) {
        SpringApplication.run(FeignApplication.class, args);
    }
 
}

  定義服務接口類UserClient.java,使用@FeignClient("service01")注解來綁定該接口對應service01服務

@FeignClient("service01")
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層中調用上面定義的UserClient,具體如下

@RestController
public 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();
    }
 
 
}

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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM