Spring Cloud Feign是一套基於Netflix Feign實現的聲明式服務調用客戶端。它使得編寫Web服務客戶端變得更加簡單。咱們只須要經過建立接口並用註解來配置它既可完成對Web服務接口的綁定。它具有可插拔的註解支持,包括Feign註解、JAX-RS註解。它也支持可插拔的編碼器和解碼器。Spring Cloud Feign還擴展了對Spring MVC註解的支持,同時還整合了Ribbon和Eureka來提供均衡負載的HTTP客戶端實現。java
分佈式應用早在十幾年前就開始出現,各自的應用運行在各自的tomcat,jboss一類的容器中,他們之間的相互調用變成了一種遠程調用,而實現遠程調用的方式不少。按照協議劃分,能夠有RPC,Webservice,http。不一樣的框架也對他們有了各自的實現,如dubbo(x),motan就都是RPC框架,本文所要講解的Feign即可以理解爲一種http框架,用於分佈式服務之間經過Http進行接口交互。說他是框架,有點過了,能夠理解爲一個http工具,只不過在spring cloud全家桶的體系中,它比httpclient,okhttp,retrofit這些http工具都要強大的多。web
Feign 採用的是基於接口的註解spring
Feign 整合了ribbonapache
繼續用上一節的工程, 啓動eureka-server,端口爲8761; 啓動service-hi 兩次,端口分別爲8762 、8773瀏覽器
建立一個feign的服務tomcat
新建一個spring-boot工程,取名爲serice-feign,在它的pom文件引入Feign的起步依賴spring-cloud-starter-feign、Eureka的起步依賴spring-cloud-starter-eureka、Web的起步依賴spring-boot-starter-webapp
若是使用idea本身的導入的spring boot節點有能夠能引入不了spring-cloud-starter-feign maven,框架
個人解決方法是換一個版本的spring boot的父節點maven
具體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>cn.zhiwei</groupId> <artifactId>serice-feign</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>serice-feign</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.2.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.BUILD-SNAPSHOT</spring-cloud.version> </properties> <dependencies> <!--引入Feign的起步依賴spring-cloud-starter-feign、Eureka的起步依賴spring-cloud-starter-eureka、 Web的起步依賴spring-boot-starter-web--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-feign</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版本選擇Dalston.RC1負責下載不了feign包--> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Dalston.RC1</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> <repositories> <repository> <id>spring-snapshots</id> <name>Spring Snapshots</name> <url>https://repo.spring.io/snapshot</url> <snapshots> <enabled>true</enabled> </snapshots> </repository> <repository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/milestone</url> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> </project>
在工程的配置文件application.yml文件,指定程序名爲service-feign,端口號爲8765,服務註冊地址爲http://localhost:8761/eureka/ ,代碼以下:
eureka: client: serviceUrl: defaultZone: http://localhost:8761/eureka/ server: port: 8765 spring: application: name: service-feign
在程序的啓動類ServiceFeignApplication ,加上@EnableFeignClients註解開啓Feign的功能
package cn.zhiwei; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.netflix.feign.EnableFeignClients; @SpringBootApplication @EnableDiscoveryClient @EnableFeignClients//註解開啓Feign的功能: public class SericeFeignApplication { public static void main(String[] args) { SpringApplication.run(SericeFeignApplication.class, args); } }
定義一個feign接口,經過@ FeignClient(「服務名」),來指定調用哪一個服務。好比在代碼中調用了service-hi服務的「/hi」接口,代碼以下:
package cn.zhiwei.service; import org.springframework.cloud.netflix.feign.FeignClient; import org.springframework.stereotype.Component; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; /** * 定義一個feign接口,經過@ FeignClient(「服務名」),來指定調用哪一個服務。 * 好比在代碼中調用了service-hi服務的「/hi」接口 * Created by Administrator on 2018/4/6. */ @FeignClient(value = "service-hi") @Component public interface SchedualServiceHi { @RequestMapping(value = "/hi",method = RequestMethod.GET) String sayHiFromClientOne(@RequestParam(value = "name") String name); }
在Web層的controller層,對外暴露一個」/hi」的API接口,經過上面定義的Feign客戶端SchedualServiceHi 來消費服務。代碼以下:
package cn.zhiwei.controller; import cn.zhiwei.service.SchedualServiceHi; 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.RequestParam; import org.springframework.web.bind.annotation.RestController; /** * 在Web層的controller層,對外暴露一個」/hi」的API接口, * 經過上面定義的Feign客戶端SchedualServiceHi 來消費服務 * Created by Administrator on 2018/4/6. */ @RestController public class HiController { @Autowired SchedualServiceHi schedualServiceHi; @RequestMapping(value = "/hi",method = RequestMethod.GET) public String sayHi(@RequestParam String name){ return schedualServiceHi.sayHiFromClientOne(name); } }
啓動程序,屢次訪問http://localhost:8765/hi?name=威哥,瀏覽器交替顯示:
結果與上篇博客的效果同樣