第二篇 Spring Cloud Alibaba實戰(一)Nacos服務註冊與發現

一。 為什麼使用spring cloud alibaba

很多人可能會問,有了spring cloud這個微服務的框架,為什麼又要使用spring cloud alibaba這個框架了?最重要的原因在於spring cloud中的幾乎所有的元件都使用Netflix公司的產品,然後在其基礎上做了一層封裝。然而Netflix的服務發現元件Eureka已經停止更新,在使用的時候發現過其一個細小的Bug;而其他的眾多元件也會陸續停止維護。所以急需其他的一些替代產品,也就是spring cloud alibaba,目前正處於蓬勃發展的態式。

二。 安裝註冊中心Nacos

nacos是阿里巴巴研發的一個集註冊中心與配置中心於一體的管理平臺,使用其非常的簡單。

下載nacos

https://github。com/alibaba/nacos/releases

我下載的是1。3。2版本,如下圖所示:

第二篇 Spring Cloud Alibaba實戰(一)Nacos服務註冊與發現

1、Windows系統直接解壓,找到bin目錄,雙擊startup。cmd啟動。如果出現一閃而退,請參照以下方法解決:

(1)、編輯startup。cmd,把裡面的MODE修改一下,儲存後重新啟動(我就是使用該方法解決了問題),參考下圖:

第二篇 Spring Cloud Alibaba實戰(一)Nacos服務註冊與發現

(2)、檢查jdk是否為jdk8 64bit,並配置環境變數,且環境變數必須為JAVA_HOME

(3)、檢查maven,並配置環境變數

2、Linux系統也要安裝好jdk環境和maven環境,執行mvn -version出現下面內容則成功:

第二篇 Spring Cloud Alibaba實戰(一)Nacos服務註冊與發現

然後到nacos/bin目錄下執行該命令啟動

sh startup。sh -m standalone

nacos預設埠是8848,http://ip:8848/nacos訪問,輸入賬號密碼:nacos nacos即可。

第二篇 Spring Cloud Alibaba實戰(一)Nacos服務註冊與發現

三、將服務部署到nacos

整合springcloud和springcloud alibaba,注意版本號要對應。

第一步,加依賴

<!——整合springcloud——> org。springframework。cloud spring-cloud-dependencies Greenwich。SR1 pom import <!——整合springcloud - alibaba ——> org。springframework。cloud spring-cloud-alibaba-dependencies 0。9。0。RELEASE pom import <!——整合springcloud alibaba nacos——> org。springframework。cloud spring-cloud-starter-alibaba-nacos-discovery

第二步,加註解(這個新版省略了註解)

第三步,寫配置,在yml檔案中加入nacos配置

spring: cloud: nacos: discovery: #指定nacos server的地址 server-addr: localhost:8848 #指定服務名稱 application: name: user-center

此時重新整理nacos訪問頁面,將會看到服務已經註冊上了。

第二篇 Spring Cloud Alibaba實戰(一)Nacos服務註冊與發現

四、測試

需求:為內容服務引入使用者服務中的使用者資訊。

方案:採用RestTemplate方式來調取使用者中心介面,透過DiscoveryClient獲取nacos上的使用者中心例項。

1、使用者服務提供一個查詢使用者資訊介面

@RestController@RequestMapping(“/users”)public class UserController { @Resource private UserService userService; @GetMapping(“/{id}”) public User findById(@PathVariable Integer id){ return this。userService。findById(id); }}

2、內容中心介面

@RestController@RequestMapping(“/shares”)public class ShareController { @Resource private ShareService shareService; @GetMapping(“/{id}”) public ShareDTO findById(@PathVariable Integer id) { return this。shareService。findById(id); }}@Servicepublic class ShareService { @Resource private ShareMapper shareMapper; @Resource private RestTemplate restTemplate; @Resource private DiscoveryClient discoveryClient; public ShareDTO findById(Integer id) { Share share = this。shareMapper。selectByPrimaryKey(id); Integer userId = share。getUserId(); //使用者中心的所有例項資訊 List instances = discoveryClient。getInstances(“user-center”); String targetUrl = instances 。stream() 。map(instance -> instance。getUri()。toString() + “/users/{id}”) 。findFirst() 。orElseThrow(() -> new IllegalArgumentException(“當前沒有例項物件”)); //根據userId查詢使用者資訊 UserDTO userDTO = this。restTemplate。getForObject(targetUrl, UserDTO。class, userId); ShareDTO shareDTO = new ShareDTO(); BeanUtils。copyProperties(share, shareDTO); shareDTO。setWxNickname(userDTO。getWxNickname()); return shareDTO; }}

請求結果如下圖展示:author欄位是來自使用者服務的

第二篇 Spring Cloud Alibaba實戰(一)Nacos服務註冊與發現

OK,nacos入門就到這裡,下一篇我們來學習ribbon負載均衡哦。

有問題歡迎小夥伴留言。