通過配置Spring Cloud應用指向MSE Nacos的服務地址,應用啟動後能夠自動向MSE Nacos註冊中心註冊服務執行個體資訊,並實現服務發現、組態管理等功能。本文介紹如何在Spring Cloud應用中接入MSE Nacos作為服務註冊中心,實現服務調用。
前提條件
如果通過公網的方式訪問MSE Nacos,請確保服務提供者和服務消費者都已具備訪問公網的能力,並且需要您對應用訪問的MSE Nacos進行白名單配置。具體操作,請參見設定白名單。
建立服務提供者
在本地建立服務提供者應用工程,然後添加依賴,並開啟服務註冊與發現功能,將註冊中心指定為建立的MSE Nacos。
建立Maven工程,命名為nacos-service-provider。
在pom.xml中添加如下依賴。
以Spring Boot 2.1.4.RELEASE和Spring Cloud Greenwich.SR1為例,依賴如下:
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.4.RELEASE</version> <relativePath/> </parent> <dependencies> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> <version>2.1.1.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Greenwich.SR1</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>樣本中使用的版本為Spring Cloud Greenwich,對應Spring Cloud Alibaba版本為2.1.1.RELEASE。
如果使用Spring Cloud Finchley版本,對應Spring Cloud Alibaba版本為2.0.1.RELEASE。
如果使用Spring Cloud Edgware版本,對應Spring Cloud Alibaba版本為1.5.1.RELEASE。
重要Spring Cloud Edgware版本的生命週期已結束,不推薦使用這個版本開發應用。
在src\main\java工程路徑下建立名為com.aliware.edas的Package。
在com.aliware.edas中建立服務提供者的啟動類ProviderApplication,並添加如下代碼。
package com.aliware.edas; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; @SpringBootApplication @EnableDiscoveryClient public class ProviderApplication { public static void main(String[] args) { SpringApplication.run(ProviderApplication.class, args); } }說明其中
@EnableDiscoveryClient註解表明此應用需開啟服務註冊與發現功能。在com.aliware.edas中建立EchoController。指定URL mapping為/echo/{String},指定HTTP方法為GET,方法參數從URL路徑中獲得,回顯收到的參數。
package com.aliware.edas; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; @RestController public class EchoController { @RequestMapping(value = "/echo/{string}", method = RequestMethod.GET) public String echo(@PathVariable String string) { return string; } }在src\main\resources工程路徑下建立檔案application.properties,在application.properties中添加如下配置,並指定Nacos Server的訪問地址。
spring.application.name=service-provider server.port=18081 spring.cloud.nacos.discovery.server-addr=mse.XX.nacos.mse.aliyuncs.com:8848在執行個體列表頁面,可以查看MSE Nacos的外網訪問地址,格式為mse.XX.nacos.mse.aliyuncs.com。
重要如果您使用的服務註冊中心為MSE的Zookeeper或者Eureka,那麼您需要將本步驟的註冊中心代碼換成Zookeeper或者Eureka相應的代碼,具體詳情請參見MSE叢集託管使用說明。
查看應用是否註冊成功。
執行nacos-service-provider中ProviderApplication的main函數,啟動應用。
登入MSE註冊配置中心管理主控台,並在頂部功能表列選擇地區。
在左側導覽列,選擇注册配置中心 > 实例列表。單擊目標執行個體名稱。
在左側導覽列,選擇服务管理 > 服务列表。
在服務列表頁面,查看應用是否註冊成功。
建立服務消費者
本步驟除介紹服務註冊的功能,還將介紹Nacos服務發現與RestTemplate和FeignClient兩個用戶端如何配合使用。
建立Maven工程,命名為nacos-service-consumer。
在pom.xml中添加如下依賴。
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.4.RELEASE</version> <relativePath/> </parent> <dependencies> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> <version>2.1.1.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Greenwich.SR1</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>在src\main\java工程路徑下建立名為com.aliware.edas的Package。
在com.aliware.edas中配置RestTemplate和FeignClient。
在com.aliware.edas中建立一個介面類EchoService,添加
@FeignClient註解,並配置對應的HTTP URL地址及HTTP方法。package com.aliware.edas; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; @FeignClient(name = "service-provider") public interface EchoService { @RequestMapping(value = "/echo/{str}", method = RequestMethod.GET) String echo(@PathVariable("str") String str); }在com.aliware.edas中建立啟動類ConsumerApplication並添加相關配置。
使用
@EnableDiscoveryClient註解啟用服務註冊與發現。使用
@EnableFeignClients註解啟用FeignClient。使用
@LoadBalanced註解將RestTemplate與服務發現整合。
package com.aliware.edas; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.cloud.openfeign.EnableFeignClients; import org.springframework.context.annotation.Bean; import org.springframework.web.client.RestTemplate; @SpringBootApplication @EnableDiscoveryClient @EnableFeignClients public class ConsumerApplication { @LoadBalanced @Bean public RestTemplate restTemplate() { return new RestTemplate(); } public static void main(String[] args) { SpringApplication.run(ConsumerApplication.class, args); } }
在com.aliware.edas中建立類TestController,以示範和驗證服務發現功能。
package com.aliware.edas; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; @RestController public class TestController { @Autowired private RestTemplate restTemplate; @Autowired private EchoService echoService; @RequestMapping(value = "/echo-rest/{str}", method = RequestMethod.GET) public String rest(@PathVariable String str) { return restTemplate.getForObject("http://service-provider/echo/" + str, String.class); } @RequestMapping(value = "/echo-feign/{str}", method = RequestMethod.GET) public String feign(@PathVariable String str) { return echoService.echo(str); } }在src\main\resources工程路徑下建立檔案application.properties,在application.properties中添加如下配置,指定Nacos Server的地址。
spring.application.name=service-consumer server.port=18082 spring.cloud.nacos.discovery.server-addr=mse.XX.nacos.mse.aliyuncs.com:8848在執行個體列表頁面,可以查看MSE Nacos的外網訪問地址,格式為
mse.XX.nacos.mse.aliyuncs.com。重要如果您使用的服務註冊中心為MSE的Zookeeper或者Eureka,那麼您需要將本步驟的註冊中心代碼換成Zookeeper或者Eureka相應的代碼,具體詳情請參見MSE使用說明。
查看應用是否註冊成功。
執行nacos-service-consumer中ConsumerApplication的main函數,啟動應用。
登入MSE註冊配置中心管理主控台,並在頂部功能表列選擇地區。
在左側導覽列,選擇注册配置中心 > 实例列表。單擊目標執行個體名稱。
在左側導覽列,選擇服务管理 > 服务列表。
在服務列表頁面,查看應用是否註冊成功。
本地測試
在本地測試消費者對提供者的服務調用結果。
Linux/Unix/Mac系統:使用如下命令進行服務調用。
curl http://127.0.0.1:18082/echo-rest/rest-rest curl http://127.0.0.1:18082/echo-feign/feign-restWindows系統:在瀏覽器中輸入http://127.0.0.1:18082/echo-rest/rest-rest和http://127.0.0.1:18082/echo-feign/feign-rest。
如圖所示表明服務調用成功。

常見問題
本地開發的Spring Cloud微服務應用,其服務註冊中心為MSE上建立的Nacos,應用運行後,在MSE服務管理頁面看不到服務資訊,如何處理?
您需要對您的應用訪問進行白名單配置。具體操作,請參見設定白名單。
MSE預設設定為127.0.0.1/32,表示禁止所有地址的訪問。
MSE支援哪些Spring Cloud版本?
Spring Cloud Greenwich對應的Spring Cloud Alibaba版本為2.1.1.RELEASE。
Spring Cloud Finchley對應的Spring Cloud Alibaba版本為 2.0.1.RELEASE。
Spring Cloud Edgware對應的 Spring Cloud Alibaba版本為1.5.1.RELEASE。
Spring Cloud Edgware版本的生命週期已結束,不推薦使用此版本開發應用。