SpringCloud之聲明式服務調用 Feign(三)


一 Feign簡介

Feign是一種聲明式、模板化的HTTP客戶端,也是netflix公司組件。使用feign可以在遠程調用另外服務的API,如果調用本地API一樣。
我們知道,阿里巴巴的doubbo采用二進制的RPC協議進行底層通訊,客戶端可以使用類似本地方法一樣調用。那么,雖然Feign同樣可以有這種效果,但是底層還是通過HTTP協議調取restful的API的方式。
通過Feign, 我們能把HTTP遠程調用對開發者完全透明,得到與調用本地方法一致的編碼體驗。

在實際開發中,對於服務依賴的調用可能不止一處,往往一個接口會被多處調用,所以我們通常會針對各個微服務自行封裝一些客戶端類來包裝這些依賴服務的調用,Spring Cloud Feign 在此基礎上做了進一步的封裝,由他來幫助我們定義和實現依賴服務接口的定義,我們只需要創建一個接口並用注解的方式來配置他,即可完成對服務提供方的接口綁定,簡化了在使用 Spring Cloud Ribbon 時自行封裝服務調用客戶端的開發量。

搭建聲明式服務Feign(feign-client)

接到上篇“SpringCloud之實現客戶端的負載均衡Ribbon(二)

繼續在springcloud工程中添加模塊feign-client,也是通過start.spring.io提供的模板創建

新的目錄

生成的pom.xml文件為

<?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.xuan</groupId>
    <artifactId>feign-client</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>feign-client</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</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>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

修改啟動文件FeignClientApplication.java,增加相關注解。

package com.xuan.feignclient;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class FeignClientApplication {

    public static void main(String[] args) {
        SpringApplication.run(FeignClientApplication.class, args);
    }
}

增加 @FeignClient 注解的接口來綁定具體的服務,增加服務HelloService.java

package com.xuan.feign;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;

@FeignClient(value = "eureka-client")
public interface HelloService {
    @RequestMapping(value = "/hello")
    String hello();
}

@FeignClient可以使用name和url來綁定。 以前使用@FeignClient注解的時候使用url參數的使用就不需要使用name屬性了,現在不然,需要在url屬性的基礎上也要使用name屬性,此時的name屬性只是一個標識。value和name互為別名,只需要設置一個就可以了。

比較有用的四個注解 name , url, fallback , path

  • name 指定微服務的實例名稱,唯一,必填,通過實例名稱可以得到實例對應的訪問地址
  • fallback 配置熔斷
  • url 配置一個絕對的地址訪問,默認為空字符串,當其不空時,則使用該地址訪問
  • path 配置一個所有方法級別的mappings 相當於在類上加 requestMapping, 例如上面的 UserServiceAPI 所有訪問地址為 /user/xxx

增加測試的消費接口ConsumerController.java

 

package com.xuan.feign;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ConsumerController {
    @Autowired
    HelloService helloService;

    @RequestMapping(value = "feign-consumer", method = RequestMethod.GET)
    public String helloConsumer(){
        return helloService.hello();
    }
}

修改配置文件”application.properties“,找到注冊中心和定義自身的服務名和端口,

spring.application.name=feign-consumer
server.port=9991
eureka.client.service-url.defaultZone=http://localhost:8080/eureka/

添加完成后工程的目錄結構為

分別啟動模塊了:

1.EurekaServerApplication

2.EurekaClientApplication,EurekaClientApplication1,EurekaClientApplication2

3.FeignClientApplication

 啟動后打開http://localhost:8080/顯示如圖:

訪問Feign模塊提供的接口http://localhost:9991/feign-consumer,刷新一次也會訪問到不同的提供者上面去,原因是feign內部也使用了ribbon做負載均衡。

源碼地址:https://gitee.com/xuantest/SpringCloud-Feign

 


免責聲明!

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



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