官方的github有說明如何使用Spring cloud alibaba,首先要再Common的專案新增spring cloud alibaba的依賴管理(要放在dependencies標籤外面哦)

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-alibaba-dependencies</artifactId>
            <version>2.2.3.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

使用依賴管理,這樣以後spring cloud alibaba相關的依賴在dependencies加入就不用寫版本了。

接下來

要學習spring cloud alibaba的組件之一,nacos,我們使用它來做註冊中心和配置中心,以後只要有服務上線,各種API就會註冊到nacos,這一塊官方也有說明

依照官方說明加入依賴,因為加在common專案,而其他微服務也都依賴common,這樣每個微服務上線就會註冊到nacos了

<dependency>
     <groupId>com.alibaba.cloud</groupId>
     <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
 </dependency>

然後要下載nacos server,這裡我使用docker安裝,打開後輸入帳號密碼(nacos/nacos),就會進入nacos了

20201122-更新
後來我發現用1.3.0版本會造成使用public一直跳訊息,所以改成1.1.3版本的nacos,使用起來就比較正常

下載zip檔案並解縮到/bin目錄

將解壓縮的資料夾放到指定目錄,下指令執行nacos

sh /Users/cheng/Public/nacos/bin/startup.sh -m standalone

停止nacos

sh /Users/cheng/Public/nacos/bin/shutdown.sh

接下來

要在微服務設定nacos的資訊,先在coupon這個微服務上設定(application.yml)

spring.cloud.nacos.discovery.server-addr=localhost:8848

接下來

再新增應用名稱,這樣註冊在nacos才容易辨識

接下來

用Annotation方式寫在啟動類別(YumallCouponApplication)上

@EnableDiscoveryClient

再來就可以啟動去看看是否有註冊

再來

將member服務也加入,做法與上述相同,啟動後就會顯示