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

一 Feign簡介html

Feign是一種聲明式、模板化的HTTP客戶端,也是netflix公司組件。使用feign能夠在遠程調用另外服務的API,若是調用本地API同樣。
咱們知道,阿里巴巴的doubbo採用二進制的RPC協議進行底層通信,客戶端可使用相似本地方法同樣調用。那麼,雖然Feign一樣能夠有這種效果,可是底層仍是經過HTTP協議調取restful的API的方式。
經過Feign, 咱們能把HTTP遠程調用對開發者徹底透明,獲得與調用本地方法一致的編碼體驗。java

在實際開發中,對於服務依賴的調用可能不止一處,每每一個接口會被多處調用,因此咱們一般會針對各個微服務自行封裝一些客戶端類來包裝這些依賴服務的調用,Spring Cloud Feign 在此基礎上作了進一步的封裝,由他來幫助咱們定義和實現依賴服務接口的定義,咱們只須要建立一個接口並用註解的方式來配置他,便可完成對服務提供方的接口綁定,簡化了在使用 Spring Cloud Ribbon 時自行封裝服務調用客戶端的開發量。git

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

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

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

新的目錄apache

生成的pom.xml文件爲restful

<?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,增長相關注解。app

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互爲別名,只須要設置一個就能夠了。maven

比較有用的四個註解 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

相關文章
相關標籤/搜索