史上最簡單的SpringCloud教程 | 第三篇: 服務消費者(Feign)
轉載請標明出處:
史上最簡單的SpringCloud教程 | 第三篇: 服務消費者(Feign) 本文出自方誌朋的博客
上一篇文章,講述了通過restTemplate+ribbon去消費服務,這篇文章主要講述通過feign去消費服務。
一、Feign簡介
Feign是一個聲明式的web服務客戶端,它使得寫web服務變得更簡單。使用Feign,只需要創建一個介面並註解。它具有可插拔的註解特性,包括Feign 註解和JAX-RS註解。Feign同時支持可插拔的編碼器和解碼器。spring cloud對Spring mvc添加了支持,同時在spring web中次用相同的HttpMessageConverter。當我們使用feign的時候,spring cloud 整和了Ribbon和eureka去提供負載均衡。
簡而言之:
- feign採用的是介面加註解
- feign 整合了ribbon
二、準備工作
繼續用上一節的工程: 啟動eureka-server,埠為8761; 啟動service-hi 兩次,埠分別為8762 、8773.
三、創建一個feign的服務
創建一個spring-boot工程,取名為:serice-feign,它的pom文件為:
<?xml version="1.0" encoding="UTF-8"?>n<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"n xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">n <modelVersion>4.0.0</modelVersion>nn <groupId>com.forezp</groupId>n <artifactId>service-feign</artifactId>n <version>0.0.1-SNAPSHOT</version>n <packaging>jar</packaging>nn <name>service-feign</name>n <description>Demo project for Spring Boot</description>nn <parent>n <groupId>org.springframework.boot</groupId>n <artifactId>spring-boot-starter-parent</artifactId>n <version>1.5.2.RELEASE</version>n <relativePath/> <!-- lookup parent from repository -->n </parent>nn <properties>n <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>n <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>n <java.version>1.8</java.version>n </properties>nn <dependencies>n <dependency>n <groupId>org.springframework.cloud</groupId>n <artifactId>spring-cloud-starter-eureka</artifactId>n </dependency>n <dependency>n <groupId>org.springframework.cloud</groupId>n <artifactId>spring-cloud-starter-feign</artifactId>n </dependency>n <dependency>n <groupId>org.springframework.boot</groupId>n <artifactId>spring-boot-starter-web</artifactId>n </dependency>nn <dependency>n <groupId>org.springframework.boot</groupId>n <artifactId>spring-boot-starter-test</artifactId>n <scope>test</scope>n </dependency>n </dependencies>nn <dependencyManagement>n <dependencies>n <dependency>n <groupId>org.springframework.cloud</groupId>n <artifactId>spring-cloud-dependencies</artifactId>n <version>Dalston.RC1</version>n <type>pom</type>n <scope>import</scope>n </dependency>n </dependencies>n </dependencyManagement>nn <build>n <plugins>n <plugin>n <groupId>org.springframework.boot</groupId>n <artifactId>spring-boot-maven-plugin</artifactId>n </plugin>n </plugins>n </build>nn <repositories>n <repository>n <id>spring-milestones</id>n <name>Spring Milestones</name>n <url>https://repo.spring.io/milestone</url>n <snapshots>n <enabled>false</enabled>n </snapshots>n </repository>n </repositories>n</project>n
向服務註冊中心註冊它自己,這時service-ribbon既是服務提供者,也是服務消費者,配置文件application.yml
eureka:n client:n serviceUrl:n defaultZone: http://localhost:8761/eureka/nserver:n port: 8765nspring:n application:n name: service-feignn
在程序的入口類,需要通過註解@EnableFeignClients來開啟feign:
@SpringBootApplicationn@EnableDiscoveryClientn@EnableFeignClientsnpublic class ServiceFeignApplication {nn public static void main(String[] args) {n SpringApplication.run(ServiceFeignApplication.class, args);n }n}n
定義一個feign介面類,通過@ FeignClient(「服務名」),來指定調用哪個服務:
/**n * Created by fangzhipeng on 2017/4/6.n */n@FeignClient(value = "service-hi")npublic interface SchedualServiceHi {n @RequestMapping(value = "/hi",method = RequestMethod.GET)n String sayHiFromClientOne(@RequestParam(value = "name") String name);n}n
在web層的controllrt:
@RestControllernpublic class HiController {nn @Autowiredn SchedualServiceHi schedualServiceHi;n @RequestMapping(value = "/hi",method = RequestMethod.GET)n public String sayHi(@RequestParam String name){n return schedualServiceHi.sayHiFromClientOne(name);n }n}n
訪問 http://localhost:8765/hi?name=forezp, 瀏覽器交替顯示:
hi forezp,i am from port:8762
hi forezp,i am from port:8763
四、更改feign的配置
在聲明feignclient的時候,不僅要指定服務名,同時需要制定服務配置類:
@FeignClient(name = "stores", configuration = FooConfiguration.class)npublic interface StoreClient {n //..n}n
重寫配置,需要加@Configuration註解,並重寫下面的兩個bean,栗子:
@Configurationnpublic class FooConfiguration {n @Beann public Contract feignContractg() {n return new feign.Contract.Default();n }nn @Beann public BasicAuthRequestInterceptor basicAuthRequestInterceptor() {n return new BasicAuthRequestInterceptor("user", "password");n }n}n
本文源碼下載:
forezp/SpringCloudLearning
五、參考資料
spring-cloud-feign
推薦閱讀:
※史上最簡單的SpringCloud教程: docker部署spring cloud項目
※【spring指南系列】使用Redis進行消息傳遞
※用小說的形式講解Spring(1) —— 為什麼需要依賴注入
※Netflix Zuul與Nginx的性能對比
※史上最簡單的 SpringCloud 教程(1) 服務的註冊與發現(Eureka)
TAG:Spring | SpringBoot |