史上最簡單的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"?>
<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.forezp</groupId>
<artifactId>service-feign</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging >jar</packaging>
<name>service-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>
</properties>
<dependencies>
<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>
<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-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
</project>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
向服務註冊中心註冊它自己,這時service-ribbon既是服務提供者,也是服務消費者,配置檔案application.yml
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
server:
port: 8765
spring:
application:
name: service-feign
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
在程式的入口類,需要通過註解@EnableFeignClients來開啟feign:
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class ServiceFeignApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceFeignApplication.class, args);
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
定義一個feign介面類,通過@ FeignClient(“服務名”),來指定呼叫哪個服務:
/**
* Created by fangzhipeng on 2017/4/6.
*/
@FeignClient(value = "service-hi")
public interface SchedualServiceHi {
@RequestMapping(value = "/hi",method = RequestMethod.GET)
String sayHiFromClientOne(@RequestParam(value = "name") String name);
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
在web層的controllrt:
@RestController
public class HiController {
@Autowired
SchedualServiceHi schedualServiceHi;
@RequestMapping(value = "/hi",method = RequestMethod.GET)
public String sayHi(@RequestParam String name){
return schedualServiceHi.sayHiFromClientOne(name);
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
hi forezp,i am from port:8762
hi forezp,i am from port:8763
四、更改feign的配置
在宣告feignclient的時候,不僅要指定服務名,同時需要制定服務配置類:
@FeignClient(name = "stores", configuration = FooConfiguration.class)
public interface StoreClient {
//..
}
- 1
- 2
- 3
- 4
- 5
- 1
- 2
- 3
- 4
- 5
重寫配置,需要加@Configuration註解,並重寫下面的兩個bean,栗子:
@Configuration
public class FooConfiguration {
@Bean
public Contract feignContractg() {
return new feign.Contract.Default();
}
@Bean
public BasicAuthRequestInterceptor basicAuthRequestInterceptor() {
return new BasicAuthRequestInterceptor("user", "password");
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
五、參考資料
優秀文章推薦:
相關推薦
史上最簡單的SpringCloud教程 | 第三篇: 服務消費者(Feign)
上一篇文章,講述了通過restTemplate+ribbon去消費服務,這篇文章主要講述通過feign去消費服務。 一、Feign簡介 Feign是一個宣告式的web服務客戶端,它使得寫web服務變得更簡單。使用Feign,只需要建立一個介面並註解。它具有可插拔的註
史上最簡單的SpringCloud教程 | 第三篇: 服務消費者(Feign)(Finchley版本)
上一篇文章,講述瞭如何通過RestTemplate+Ribbon去消費服務,這篇文章主要講述如何通過Feign去消費服務。 一、Feign簡介 Feign是一個宣告式的偽Http客戶端,它使得寫Http客戶端變得更簡單。使用Feign,只需要建立一個介面並註解
SpringCloud教程 | 第三篇: 服務消費者(Feign)(Finchley版本)
lns efault ret java 服務註冊 star tco target hub 上一篇文章,講述了如何通過RestTemplate+Ribbon去消費服務,這篇文章主要講述如何通過Feign去消費服務。 一、Feign簡介 Feign是一個聲明式的偽Http客戶端
【轉載】SpringCloud教程 | 第三篇: 服務消費者(Feign)
上一篇文章,講述瞭如何通過RestTemplate+Ribbon去消費服務,這篇文章主要講述如何通過Feign去消費服務。 一、Feign簡介 Feign是一個宣告式的偽Http客戶端,它使得寫Http客戶端變得更簡單。使用Feign,只需要建立一個
SpringCloud教程 | 第三篇: 服務消費者(Feign)
上一篇文章,講述了通過restTemplate+ribbon去消費服務,這篇文章主要講述通過feign去消費服務。 一、Feign簡介 Feign是一個宣告式的web服務客戶端,它使得寫web服務變得更簡單。使用Feign,只需要建立一個介面並註解。它具有可插拔的註解特性,
[SpringCloud] 入門-第三篇: 服務消費者(feign)
1. 什麼是feign 上個文章,我們是使用restTemplate + Ribbon 來實現分散式服務之間的生產和消費的呼叫的, 這篇文章來講一講SpringCloud分散式服務之間呼叫的另一個方案, feign Feign是一個宣告式的偽Http客戶端,
SpringCloud | 第三篇: 服務消費者(Feign)
上一篇文章,講述瞭如何通過RestTemplate+Ribbon去消費服務,這篇文章主要講述如何通過Feign去消費服務。 一、Feign簡介 Feign是一個宣告式的偽Http客戶端,它使得寫Http客戶端變得更簡單。使用Feign,只需要建立一個介面並註解。它具有
SpringCloud教程 | 第二篇: 服務消費者(Feign)
一、Feign簡介Feign是一個宣告式的偽Http客戶端,它使得寫Http客戶端變得更簡單。使用Feign,只需要建立一個介面並註解。它具有可插拔的註解特性,可使用Feign 註解和JAX-RS註解。Feign支援可插拔的編碼器和解碼器。Feign預設集成了Ribbon,並
史上最簡單的SpringCloud教程 | 第三篇: 服務消費者(Feign)
最新Finchley版本請訪問: https://www.fangzhipeng.com/springcloud/2018/08/30/sc-f3-feign/ 或者 http://blog.csdn.net/forezp/article/details/810409
史上最簡單的SpringCloud教程 | 第九篇: 服務鏈路追蹤(Spring Cloud Sleuth)(Finchley版本)
這篇文章主要講述服務追蹤元件zipkin,Spring Cloud Sleuth集成了zipkin元件。 一、簡介 Add sleuth to the classpath of a Spring Boot application (see below for Maven
史上最簡單的SpringCloud教程 | 第九篇: 服務鏈路追蹤(Spring Cloud Sleuth)
這篇文章主要講述服務追蹤元件zipkin,Spring Cloud Sleuth集成了zipkin元件。 一、簡介 Add sleuth to the classpath of a Spring Boot application (see below fo
Intellij idea史上最簡單的教程之Linux下安裝與破解Intellij idea2017
成功 zxvf java 新建 pre form 旗艦版 lan intel 一、前言 這一節我們介紹在Linux下如何安裝與破解Intellij idea2017。現在有很多公司開發環境都是Linux,所以掌握在Linux環境下使用Idea辦公也是咱們必須得掌握的技能。
史上最簡單MySQL教程詳解(基礎篇)之多表聯合查詢
常用術語 內連線 外連線 左外連線 右外連線 注意事項: 自連線 子查詢 在上篇文章史上最簡單MySQL教程詳解(基礎篇)之資料庫設計正規化及應用舉例我們介紹過,在關係型資料庫中,我們通常為了減少資料的冗餘量將對資料表進行規範,將
史上最簡單MySQL教程詳解(進階篇)之儲存引擎介紹及預設引擎設定
什麼是儲存引擎? 與其他資料庫例如Oracle 和SQL Server等資料庫中只有一種儲存引擎不同的是,MySQL有一個被稱為“Pluggable Storage Engine Architecture”(可替換儲存引擎架構)的特性,也就意味著My
在eclipse上部署springcloud小例子--第三篇:斷路器(Hystrix)
在微服務架構中,根據業務來拆分成一個個的服務,服務與服務之間可以相互呼叫(RPC),在Spring Cloud可以用RestTemplate+Ribbon和Feign來呼叫。為了保證其高可用,
史上最簡單易懂的二叉樹遍歷(先序,中序,後序)
背景描述 二叉樹遍歷相信大家在學習資料結構的時候都學習過,有遞迴方法和非遞迴方法,遞迴方法簡單,容易理解,不在本次的討論範圍內。因此本篇文章主要是討論非遞迴的方法,也就是迭代法。這種方法網上有很多解題方法,先序,後序,中序還都不一樣,很難理解。即便當時理解了,
企業級 SpringCloud 教程 (三)服務消費者(Feign)
pom https www. ram cat -h 客戶端 tin script 上一篇文章,講述了如何通過RestTemplate+Ribbon去消費服務,這篇文章主要講述如何通過Feign去消費服務。一、Feign簡介 Feign是一個聲明式的偽Http客戶端,它使得寫
業余草 SpringCloud教程 | 第九篇: 服務鏈路追蹤(Spring Cloud Sleuth)(Finchley版本)
描述 -s util ont packaging tdd res [] 新建 這篇文章主要講述服務追蹤組件zipkin,Spring Cloud Sleuth集成了zipkin組件。 一、簡介 Add sleuth to the classpath of a Spr
【SpringCloud Greenwich版本】第三章:服務消費者(Feign)
一、SpringCloud版本 本文介紹的Springboot版本為2.1.1.RELEASE,SpringCloud版本為Greenwich.RC1,JDK版本為1.8,整合環境為IntelliJ IDEA 二、Feign介紹 Feign是一個宣告式的Web服務客戶端。這使得W
SpringCloud教程第4篇:Hystrix(F版本)
在微服務架構中,根據業務來拆分成一個個的服務,服務與服務之間可以相互呼叫(RPC),在Spring Cloud可以用RestTemplate+Ribbon和Feign來呼叫。為了保證其高可用,單個服務通常會叢集部署。由於網路原因或者自身的原因,服務並不能保證100%可用,如果單個服務出現問題,呼叫這個服務就會