文章目錄
- 1. 什麼是 Nacos?
-
- Nacos 的關鍵特性
- 2. 安裝 Nacos
- 3. 接入Nacos注冊中心
-
- 項目建構
- 服務提供者
- 服務消費者
文章所有參考代碼都已上傳GitHub:https://github.com/Djnsakn/spring-cloud-alibaba-learning
本系列環境:Intellij IDEA 2019.1.1、JDK 1.8、Spring Boot 2.1.8.RELEASE、Spring Cloud Greenwich.SR3
1. 什麼是 Nacos?
Nacos 緻力于幫助您發現、配置和管理微服務。Nacos 提供了一組簡單易用的特性集,幫助您快速實作動态服務發現、服務配置、服務中繼資料及流量管理。
Nacos 幫助您更靈活和容易地建構、傳遞和管理微服務平台。 Nacos 是建構以“服務”為中心的現代應用架構 (例如微服務範式、雲原生範式) 的服務基礎設施。
使用 Nacos 簡化服務發現、配置管理、服務治理及管理的解決方案,讓微服務的發現、管理、共享、組合更加容易。
Nacos 的關鍵特性
- 服務發現和服務健康監測
- 動态配置服務
- 動态 DNS 服務
- 服務及其中繼資料管理
2. 安裝 Nacos
使用之前請先檢視版本說明。
下載下傳位址:https://github.com/alibaba/nacos/releases
本文版本:1.2.1
下載下傳完成後,解壓,然後啟動伺服器,startup.sh腳本位于Nacos解壓後的bin目錄下:
Linux/Unix/Mac
啟動指令(standalone代表着單機模式運作,非叢集模式):
sh startup.sh -m standalone
如果您使用的是ubuntu系統,或者運作腳本報錯提示[[符号找不到,可嘗試如下運作:
bash startup.sh -m standalone
Windows
啟動指令:
cmd startup.cmd
或者輕按兩下startup.cmd運作檔案。
啟動完成之後,通路:http://127.0.0.1:8848/nacos/,可以進入Nacos的服務管理頁面,如下:
![](https://img.laitimes.com/img/9ZDMuAjOiMmIsIjOiQnIsICM38FdsYkRGZkRG9lcvx2bjxiNx8VZ6l2cs0TPB9keVRkT3NGROBDOsJGcohVYsR2MMBjVtJWd0ckW65UbM5WOHJWa5kHT20ESjBjUIF2X0hXZ0xCMx81dvRWYoNHLrdEZwZ1Rh5WNXp1bwNjW1ZUba9VZwlHdssmch1mclRXY39CXldWYtlWPzNXZj9mcw1ycz9WL49zZuBnL4YzNzUTM1YTM4ATNwAjMwIzLc52YucWbp5GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.png)
3. 接入Nacos注冊中心
項目建構
本文的項目結構如下:
依次建立maven父子項目,具體代碼可參考
https://github.com/Djnsakn/spring-cloud-alibaba-learning
服務提供者
第一步:建立module,命名為:nacos-spring-cloud-provider-example 。
第二步:添加依賴,本文的 pom.xml 如下:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>nacos-spring-cloud-discovery-example</artifactId>
<groupId>com.example</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>nacos-spring-cloud-provider-example</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-client</artifactId>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.1.0.Final</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
第三步:建立應用主類:
package com.alibaba.nacos.example.spring.cloud;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@EnableDiscoveryClient // 通過 Spring Cloud 原生注解 @EnableDiscoveryClient 開啟服務注冊發現功能
public class NacosProviderApplication {
public static void main(String[] args) {
SpringApplication.run(NacosProviderApplication.class, args);
}
@Value("${server.port}")
String port;
@RestController
class EchoController {
@RequestMapping("/echo")
public String echo(String name) {
return "Hello Nacos Discovery " + name + " port:" + port;
}
}
}
第四步:配置服務名稱和Nacos位址
#server.port=8070
spring.application.name=service-provider
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
第五步:啟動上面建立的應用。啟動 --server.port=8070和 --server.port=8071 兩個執行個體。
在啟動都ok之後,我們可以通路Nacos的管理頁面http://127.0.0.1:8848/nacos/來檢視服務清單,此時可以看到如下内容:
服務消費者
第一步:建立module,命名為:nacos-spring-cloud-consumer-example 。
第二步:添加依賴,本文的 pom.xml 如下:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>nacos-spring-cloud-discovery-example</artifactId>
<groupId>com.example</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>nacos-spring-cloud-consumer-example</artifactId>
<properties>
<spring-cloud-openfeign.version>2.0.0.RELEASE</spring-cloud-openfeign.version>
<spring-cloud-netflix.version>2.0.0.RELEASE</spring-cloud-netflix.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
<version>${spring-cloud-netflix.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
<version>${spring-cloud-openfeign.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<exclusions>
<exclusion>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-client</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-client</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
第三步:應用主類為:
package com.alibaba.nacos.example.spring.cloud;
import org.springframework.beans.factory.annotation.Autowired;
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.cloud.openfeign.FeignClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
@SpringBootApplication
@EnableFeignClients // 使用Feign
@EnableDiscoveryClient // 通過 Spring Cloud 原生注解 @EnableDiscoveryClient 開啟服務注冊發現功能
public class NacosConsumerApplication {
@LoadBalanced // 給 RestTemplate 執行個體添加 @LoadBalanced 注解,開啟 @LoadBalanced 與 Ribbon 的內建
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
public static void main(String[] args) {
SpringApplication.run(NacosConsumerApplication.class, args);
}
// 使用過RestTemplate來向服務的某個具體執行個體發起HTTP請求
@RestController
public class TestController {
private final RestTemplate restTemplate;
@Autowired
public TestController(RestTemplate restTemplate) {this.restTemplate = restTemplate;}
@RequestMapping(value = "/echo", method = RequestMethod.GET)
public String echo(String name) {
return restTemplate.getForObject("http://service-provider/echo?name=" + name, String.class);
}
}
// 使用Feign
@RestController
static class TestControllerFeign {
@Autowired
Client client;
@GetMapping("/test")
public String test(String name) {
return client.hello(name);
}
}
@FeignClient("service-provider")
interface Client {
@RequestMapping(value = "/echo", method = RequestMethod.GET)
String hello(@RequestParam(name = "name") String name);
}
}
第四步:配置服務名稱和Nacos位址,讓服務消費者可以發現上面已經注冊到Nacos的服務。
server.port=8080
spring.application.name=service-consumer
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
第五步:啟動服務消費者,然後通過curl或者postman等工具發起通路,或者直接在浏覽器輸入,下面以curl為例:
可以看到,兩次不同請求的時候,真正實際調用的服務提供者執行個體是不同的,也就是說,通過LoadBalancerClient接口在擷取服務執行個體的時候,已經實作了對服務提供方執行個體的負載均衡。
具體代碼可參考
https://github.com/Djnsakn/spring-cloud-alibaba-learning