Spring Cloud Feign是一套基於Netflix Feign實現的聲明式服務調用客戶端。它使得編寫Web服務客戶端變得更加簡單。咱們只須要經過建立接口並用註解來配置它既可完成對Web服務接口的綁定。它具有可插拔的註解支持,包括Feign註解、JAX-RS註解。它也支持可插拔的編碼器和解碼器。Spring Cloud Feign還擴展了對Spring MVC註解的支持,同時還整合了Ribbon和Eureka來提供均衡負載的HTTP客戶端實現。web
實戰:spring
利用以前博客中構建的eureka-server
做爲服務註冊中心、eureka-client
做爲服務提供者做爲基礎apache
構建項目,命名爲:eureka-consumer-ribbon
。在pom.xml
中增長下面的依賴:app
<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>springCloud</groupId>
<artifactId>eureka-consumer-feign</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>負載均衡
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.4.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>maven
<dependencies>
<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-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
</dependencies>函數
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<profiles>
<profile>
<id>jdk-1.8</id>
<activation>
<activeByDefault>true</activeByDefault>
<jdk>1.8</jdk>
</activation>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
</properties>
</profile>
</profiles>
</project>spring-boot
資源文件application.properties微服務
spring.application.name=eureka-feign
server.port=2102測試
eureka.client.serviceUrl.defaultZone=http://localhost:1001/eureka/
修改應用主類。經過@EnableFeignClients
註解開啓掃描Spring Cloud Feign客戶端的功
package com.cloud;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class ApplicationFeign {
public static void main(String[] args) {
new SpringApplicationBuilder(ApplicationFeign.class).web(true).run(args);
}
}
建立一個Feign的客戶端接口定義。使用@FeignClient
註解來指定這個接口所要調用的服務名稱,接口中定義的各個函數使用Spring MVC的註解就能夠來綁定服務提供方的REST接口,好比下面就是綁定eureka-client
服務的/dc
接口的例
package com.cloud.service;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
@FeignClient("eureka-client")
public interface DcClient {
@GetMapping("/dc")
String consumer();
}
修改Controller。經過定義的feign客戶端來調用服務提供方的接口
package com.cloud.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import com.cloud.service.DcClient;
@RestController
public class DcController {
@Autowired
DcClient dcClient;
@GetMapping("/consumer")
public String dc() {
return dcClient.consumer();
}
}
經過Spring Cloud Feign來實現服務調用的方式更加簡單了,經過@FeignClient
定義的接口來統一的生命咱們須要依賴的微服務接口。而在具體使用的時候就跟調用本地方法一點的進行調用便可。因爲Feign是基於Ribbon實現的,因此它自帶了客戶端負載均衡功能,也能夠經過Ribbon的IRule進行策略擴展。另外,Feign還整合的Hystrix來實現服務的容錯保護
能夠在建立一個客戶端其名稱eureka-client,修改端口號,資源文件入下
spring.application.name=eureka-client
server.port=2002
eureka.client.serviceUrl.defaultZone=http://localhost:1001/eureka/,
啓動2個客戶端,能夠測試負載均衡。