歡迎來到菜鳥SpringCloud入門實戰系列(SpringCloudForNoob),該系列經過層層遞進的實戰視角,來一步步學習和理解SpringCloud。html
本系列適合有必定Java以及SpringBoot基礎的同窗閱讀。java
每篇文章末尾都附有本文對應的Github源代碼,方便同窗調試。git
Github倉庫地址:程序員
https://github.com/qqxx6661/springcloud_for_noobgithub
你能夠經過如下兩種途徑查看菜鳥SpringCloud入門實戰系列:web
前文回顧:面試
本章節中須要有三個角色:服務註冊中心(對應前文中咱們的eureka子模塊)、服務提供者(對應前文中咱們的eureka-hi子模塊)、服務消費者,其中服務註冊中心就是咱們上一篇的eureka單機版啓動既可,流程是首先啓動註冊中心,服務提供者生產服務並註冊到服務中心中,消費者從服務中心中獲取服務並執行。算法
子模塊不須要作更改。spring
值得注意的是,你能夠使用@EnableDiscoveryClient代替@EnableEurekaClient後端
二者的區別:
https://www.jianshu.com/p/f6db3117864f
註解@EnableEurekaClient上有@EnableDiscoveryClient註解,能夠說基本就是EnableEurekaClient有@EnableDiscoveryClient的功能,另外上面的註釋中提到,其實@EnableEurekaClientz註解就是一種方便使用eureka的註解而已,能夠說使用其餘的註冊中心後,均可以使用@EnableDiscoveryClient註解,可是使用@EnableEurekaClient的情景,就是在服務採用eureka做爲註冊中心的時候,使用場景較爲單一。
建立子模塊service-feign,步驟和以前相似,請參考教程第一章。
修改pom.xml,引入:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
複製代碼
而後在主程序引入:
@EnableFeignClients
@EnableEurekaClient
複製代碼
這時候,個人springboot2.0.3又出事了,@EnableFeignClients沒法引入,須要將pom.xml的引入修改成:
<dependency>
<groupId>org.springframework.cloud</groupId>
<!--spring boot 2.0.3版本解決方案:spring-cloud-starter-feign-->
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
複製代碼
補充:Spring Boot 2下使用Feign找不到@EnableFeignClients的解決辦法
https://blog.csdn.net/alinyua/article/details/80070890
該做者給了一個完整的解決沒法引入包問題的思路,文章篇幅很長,思路值得學習!
以後,修改application.yml:
server:
# 服務端口號
port: 8765
spring:
application:
# 服務名,即serviceId
name: service-feign
eureka:
client:
serviceUrl:
# 安全認證的服務註冊中心地址
defaultZone: http://localhost:8761/eureka
複製代碼
首先回顧一下eureka-hi的方法,它提供了一個上述eureka-hi服務提供了一個RESTful風格的接口:
/** 獲取端口號 */
@Value("${server.port}")
String port;
/**
* 定義一個簡單接口
* @param name
* @return
*/
@GetMapping("/hi/{name}")
public String home(@PathVariable String name){
return "hi " + name + ",I am from service-hi, port :" + port;
}
複製代碼
編寫調用eureka-hi提供的接口的本地接口ServiceHi.java,以下:
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
/**
* <p>一個Feign服務消費者接口</p>
**/
@FeignClient(value = "service-hi")
public interface ServiceHi {
/**
* <p>經過Feign僞Http客戶端調用service-hi提供的服務</p>
* @author hanchao 2018/5/19 17:59
**/
@GetMapping("/hi/{name}")
String sayHiFromServiceHi(@PathVariable(value = "name") String name);
}
複製代碼
說明:
以後,新建HelloController.java,以下:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
/**
* <p>服務消費控制層</p>
**/
@RestController
public class HelloController {
/** 注入服務"service-hi"的Feign客戶端ServiceHi */
@Autowired
private ServiceHi serviceHi;
/**
* 調用Feign客戶端提供的服務,自帶負載均衡
* @param name
* @return
*/
@GetMapping("/hello/{name}")
public String sayHi(@PathVariable String name){
//調用Feign客戶端ScheduleServiceHi的接口
return serviceHi.sayHiFromServiceHi(name);
}
}
複製代碼
重點:
至此咱們已經配置完畢,依次啓動服務eureka、eureka-hi和service-feign。能夠看到兩個服務都已經在eureka註冊:
訪問 http://localhost:8765/hello/rude3knife ,即service-feign提供的服務接口。 這個服務接口會經過Feign去調用服務eureka-hi提供的服務接口,結果顯示服務間調用成功。
在該調用中,咱們feign並不須要指定端口號,它並不知道這個方法所在的服務提供者如今在哪一個端口運行,咱們只須要向eureka尋求服務。
三個模塊的拓撲圖以下:
Feign會對服務調用進行負載平衡,咱們須要同時打開兩個eureka-hi服務,因爲在同一臺電腦上,就得把端口號從8763改成8764,而後同時開啓8763和8764兩個服務。
要同時運行兩個端口不一樣的相同服務,須要在run configuration裏面把allow parallle打開:
運行結構是這樣的:
訪問http://localhost:8765/hello/rude3knife
連續訪問兩次,發現兩次會分別取調用eureka-hi的兩個服務節點:
最後是負載平衡的拓撲圖:
https://github.com/qqxx6661/springcloud_for_noob/tree/master/04-servier-feign
Spring-Cloud筆記04:服務消費者Feign
https://blog.csdn.net/hanchao5272/article/details/80574441
springcloud(三):服務提供與調用
http://www.ityouknow.com/springcloud/2017/05/12/eureka-provider-constomer.html
菜鳥SpringCloud實戰專欄全導航:經過如下兩種途徑查看
我是蠻三刀把刀,後端開發。
主要關注後端開發,數據安全,爬蟲等方向。
來微信和我聊聊:yangzd1102
Github我的主頁:
同步更新公衆號及如下博客
1. Csdn
擁有專欄:
2. 知乎
擁有專欄:
3. 掘金
4. 簡書
若是文章對你有幫助,不妨收藏起來並轉發給您的朋友們~