[菜鳥SpringCloud入門]第四章:遠程調用服務實戰

在這裏插入圖片描述

前言

歡迎來到菜鳥SpringCloud入門實戰系列(SpringCloudForNoob),該系列經過層層遞進的實戰視角,來一步步學習和理解SpringCloud。html

本系列適合有必定Java以及SpringBoot基礎的同窗閱讀。java

每篇文章末尾都附有本文對應的Github源代碼,方便同窗調試。git

Github倉庫地址:程序員

https://github.com/qqxx6661/springcloud_for_noobgithub

菜鳥SpringCloud入門實戰系列

你能夠經過如下兩種途徑查看菜鳥SpringCloud入門實戰系列web

前文回顧:面試

實戰版本

  • SpringBoot:2.0.3.RELEASE
  • SpringCloud:Finchley.RELEASE

-----正文開始-----

遠程調用服務實戰

本章節中須要有三個角色:服務註冊中心(對應前文中咱們的eureka子模塊)、服務提供者(對應前文中咱們的eureka-hi子模塊)、服務消費者,其中服務註冊中心就是咱們上一篇的eureka單機版啓動既可,流程是首先啓動註冊中心,服務提供者生產服務並註冊到服務中心中,消費者從服務中心中獲取服務並執行。算法

服務提供者:使用原來的eureka-hi子模塊

子模塊不須要作更改。spring

值得注意的是,你能夠使用@EnableDiscoveryClient代替@EnableEurekaClient後端

二者的區別:

https://www.jianshu.com/p/f6db3117864f

註解@EnableEurekaClient上有@EnableDiscoveryClient註解,能夠說基本就是EnableEurekaClient有@EnableDiscoveryClient的功能,另外上面的註釋中提到,其實@EnableEurekaClientz註解就是一種方便使用eureka的註解而已,能夠說使用其餘的註冊中心後,均可以使用@EnableDiscoveryClient註解,可是使用@EnableEurekaClient的情景,就是在服務採用eureka做爲註冊中心的時候,使用場景較爲單一。

服務消費者:新建service-feign子模塊

建立子模塊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;
}
複製代碼
  • 行爲:GET
  • 資源:/hi/{name}

編寫調用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);
}

複製代碼

說明:

  • 經過@FeignClient標識當前接口是一個Feign客戶端,value = "service-hi"表示其針對的是名爲service-hi的服務。
  • service-hi則是咱們eureka-hi子模塊的spring.application.name,這個name已經在eureka註冊過
  • sayHiFromServiceHi方法爲假裝成HTTP客戶端方法,與eureka-hi的[GET] /hi/{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);
    }
}
複製代碼

重點:

  • serviceHi.sayHiFromServiceHi(name)即經過Feign調用僞HTTP客戶端的服務接口。

至此咱們已經配置完畢,依次啓動服務eureka、eureka-hi和service-feign。能夠看到兩個服務都已經在eureka註冊:

在這裏插入圖片描述

訪問 http://localhost:8765/hello/rude3knife ,即service-feign提供的服務接口。 這個服務接口會經過Feign去調用服務eureka-hi提供的服務接口,結果顯示服務間調用成功。

在這裏插入圖片描述

在該調用中,咱們feign並不須要指定端口號,它並不知道這個方法所在的服務提供者如今在哪一個端口運行,咱們只須要向eureka尋求服務。

三個模塊的拓撲圖以下:

在這裏插入圖片描述

測試Feign負載均衡

Feign會對服務調用進行負載平衡,咱們須要同時打開兩個eureka-hi服務,因爲在同一臺電腦上,就得把端口號從8763改成8764,而後同時開啓8763和8764兩個服務。

要同時運行兩個端口不一樣的相同服務,須要在run configuration裏面把allow parallle打開:

在這裏插入圖片描述

運行結構是這樣的:

在這裏插入圖片描述
能夠看到有兩個Service-Hi進行了註冊:

在這裏插入圖片描述

訪問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我的主頁:

github.com/qqxx6661

原創博客主要內容

  • Java知識點複習全手冊
  • Leetcode算法題解析
  • 劍指offer算法題解析
  • Python爬蟲相關技術實戰
  • 後端開發相關技術實戰
  • SpringCloud入門實戰

同步更新公衆號及如下博客

1. Csdn

blog.csdn.net/qqxx6661

擁有專欄:

  • Leetcode題解(Java/Python)
  • Python爬蟲實戰
  • Java程序員知識點複習手冊
  • SpringCloud入門實戰

2. 知乎

www.zhihu.com/people/yang…

擁有專欄:

  • Java程序員面試複習手冊
  • LeetCode算法題詳解與代碼實現
  • 後臺開發實戰

3. 掘金

juejin.im/user/5b4801…

4. 簡書

www.jianshu.com/u/b5f225ca2…

我的公衆號:Rude3Knife

我的公衆號:Rude3Knife

若是文章對你有幫助,不妨收藏起來並轉發給您的朋友們~

相關文章
相關標籤/搜索