1、前言
Spring Cloud Alibaba 是阿裡巴巴提供的微服務開發一站式解決方案,目前已經加入Spring Cloud項目,跟随Spring Cloud一起維護。內建Nacos需要使用Spring Cloud Alibaba Nacos子產品。
拓展閱讀:Spring Cloud 加盟重量級成員Spring Cloud Alibaba,打造更符合中國國情的微服務體系
2、啟動nacos服務
在上文《Nacos快速入門(二):Nacos叢集安裝部署》談的了nacos的叢集部署,如果你沒有那麼多linux機器來搭建叢集,可以本地啟動非叢集模式nacos服務:
- Linux/Unix/Mac:sh startup.sh -m standalone
- Windows:cmd startup.cmd -m standalone
Windows下可以直接輕按兩下nacos/bin目錄下的
startup.cmd
3、搭建服務接入nacos注冊中心
搭建spring boot項目nacos-spring-cloud-discovery-example,編輯
pom.xml
<properties>
<spring-boot.version>2.0.4.RELEASE</spring-boot.version>
<spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
<spring-cloud-alibaba.version>0.2.2.RELEASE</spring-cloud-alibaba.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>${spring-cloud-alibaba.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
附錄:Spring Cloud Alibaba & Spring Cloud & Spring Boot相容版本
Spring Cloud Alibaba依賴坐标的
groupId
有兩個,對應的版本是不一樣的
- 項目孵化期間,使用的
為groupId
,相容版本:org.springframework.cloud
Spring Boot | Spring Cloud | Spring Cloud Alibaba |
---|---|---|
2.1.x | Greenwich | 0.9.x |
2.0.x | Finchley | 0.2.x |
1.5.x | Edgware | 0.1.x |
1.5.x | Dalston | 0.1.x |
- 項目孵化之後,使用的
為groupId
,相容版本:com.alibaba.cloud
Spring Cloud Version | Spring Cloud Alibaba Version | Spring Boot Version |
---|---|---|
-------- | -------- | -------- |
Spring Cloud Greenwich | 2.1.x.RELEASE | 2.1.x.RELEASE |
Spring Cloud Finchley | 2.0.x.RELEASE | 2.0.x.RELEASE |
Spring Cloud Edgware | 1.5.x.RELEASE | 1.5.x.RELEASE |
拓展閱讀:Spring Cloud Alibaba 釋出第一個正式版本,順利完成孵化!
3.1、服務建立者
第一步: 在nacos-spring-cloud-discovery-example下建立mavem子產品nacos-spring-cloud-provider-example,編輯pom.xml,加入nacos服務注冊發現子產品依賴
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
第二步: 建立提供者應用主類
@SpringBootApplication
@EnableDiscoveryClient
public class NacosProviderApplication {
public static void main(String[] args) {
SpringApplication.run(NacosProviderApplication.class, args);
}
@Value("${server.port}")
private String port ;
@RestController
class EchoController {
@RequestMapping(value = "/hello", method = RequestMethod.GET)
public String hello() {
// 傳回服務端口
return "service provider,port:" + port;
}
}
}
第三步: 在
application.properties
中配置應用名稱和nacos注冊中心位址
server.port=8080
spring.application.name=service-provider
# nacos注冊中心位址
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
第四步: 我們啟動兩個服務執行個體,啟動第一個執行個體後,我們修改server.port=8081,然後再啟動一個執行個體
如果不想修改配置,可以在啟動參數中添加 -Dserver.port=8081
參數來啟動
登入nacos控制台,檢視服務清單:
進入服務詳情:
3.2、服務消費者
第一步: 在nacos-spring-cloud-discovery-example下建立mavem子產品nacos-spring-cloud-consumer-example,編輯pom.xml,加入nacos服務注冊發現子產品依賴
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
第二步: 建立消費者應用主類
@SpringBootApplication
@EnableDiscoveryClient
public class NacosConsumerApplication {
@LoadBalanced
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
public static void main(String[] args) {
SpringApplication.run(NacosConsumerApplication.class, args);
}
@RestController
public class TestController {
private final RestTemplate restTemplate;
@Autowired
public TestController(RestTemplate restTemplate) {this.restTemplate = restTemplate;}
@RequestMapping(value = "/hello", method = RequestMethod.GET)
public String hello() {
// 調用服務提供者接口
return restTemplate.getForObject("http://service-provider/hello", String.class);
}
}
第三步: 在
application.properties
中配置應用名稱和nacos注冊中心位址
server.port=8090
spring.application.name=service-consumer
# nacos注冊中心位址
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
第四步: 啟動服務,檢視注冊中心,消費者也注冊上來了
到此,服務已經全部搭建好了,完整項目結構:
4、測試
消費者調用提供者接口測試
通路消費者接口:http://127.0.0.1:8090/hello,多次通路,調用的服務提供者執行個體是不同的
服務下線測試
把8081端口的服務提供者下線,再次請求接口,隻會調用8080端口服務提供者
nacos這裡做的是不很友好,服務下線成功與否控制台界面看不出來,相比之下eureka會友好些。