快速入門
安裝 Nacos
下載地址:https://github.com/alibaba/nacos/releases
本文版本:1.2.1
下載完成之後,解壓,在 bin 目錄下有啓動程序。
- window:startup.cmd
- linux:startup.sh
啓動之後,訪問 http://localhost:8848/nacos/
,進入 Nacos 的登錄界面,默認用戶名密碼爲:nacos,登錄之後進入管理界面
構建應用接入 Nacos
構建一個服務提供者以及一個服務消費者來驗證服務的註冊與發現
構建服務提供者
需要有 SpringBoot 基礎
第一步:創建一個新的 SpringBoot 應用,命名爲 nacos-provider
第二步:添加依賴
添加父依賴
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.5.RELEASE</version>
<relativePath/>
</parent>
添加依賴管理器
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>0.2.2.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
添加依賴
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Nacos服務註冊與發現模塊 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
第三步:創建啓動類並提供一個應用接口
@EnableDiscoveryClient
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Slf4j
@RestController
static class TestController {
@GetMapping("/hello")
public String hello(@RequestParam String name) {
log.info("execute hello method with name : " + name);
return "hello " + name;
}
}
}
@SpringBootApplication
用於定義 SpringBoot 應用的啓動類@EnableDiscoveryClient
Spring Cloud 提供的註解,用於開啓 Spring Cloud 的服務註冊與發現,
由於這裏引入了spring-cloud-starter-alibaba-nacos-discovery
,所以服務註冊與發現會使用 Nacos 的實現。
這與Eureka
或者Consul
的操作是一樣的。
第四步:配置服務名稱及 Nacos 地址
server:
port: 8080
spring:
application:
name: nacos-provider
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
第五步:啓動應用
如果打印如下日誌,表示註冊成功
nacos registry, nacos-provider 10.32.52.236:8080 register finished
修改端口號,同一個應用啓動多個實例,實現集羣
訪問 Nacos 管理界面,通過 服務管理 \> 服務列表
界面查看已註冊的服務
點擊詳情,可以查看集羣中每個服務實例的具體信息
構建服務消費者
第一步:創建一個新的 SpringBoot 應用,命名爲 nacos-consumer
第二步:添加依賴,與上面的構建服務提供者的內容一樣
第三步:創建啓動類並提供一個應用接口,在該接口實現對服務提供者提供的接口進行調用
@EnableDiscoveryClient
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Slf4j
@RestController
static class TestController {
@Autowired
LoadBalancerClient loadBalancerClient;
@GetMapping("/test")
public String test() {
ServiceInstance serviceInstance = loadBalancerClient.choose("nacos-provider");
String url = serviceInstance.getUri() + "/hello?name=" + "nacos";
RestTemplate restTemplate = new RestTemplate();
String result = restTemplate.getForObject(url, String.class);
return "Invoke : " + url + ", return : " + result;
}
}
}
這裏使用了 Spring Cloud Common
中的 LoadBalancerClient
接口來挑選服務實例信息,使用負載均衡的算法,
然後獲取被選中實例的URI,拼接上服務提供者接口的請求地址及參數,使用 RestTemplate
進行調用。
第四步:配置服務名稱及 Nacos 地址
跟上面的配置服務提供者基本一致,只是服務名及端口根據需要進行修改
server:
port: 8082
spring:
application:
name: nacos-consumer
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
第五步:啓動服務
訪問地址 http://localhost:8082/test
,查看日誌輸出
Invoke : http://10.32.52.236:8080/hello?name=nacos, return : hello nacos
Invoke : http://10.32.52.236:8081/hello?name=nacos, return : hello nacos
Invoke : http://10.32.52.236:8080/hello?name=nacos, return : hello nacos
Invoke : http://10.32.52.236:8081/hello?name=nacos, return : hello nacos
可以看到,多次請求會被依次分配給 nacos-provider
服務集羣中的兩個實例,可以分別查看兩個實例的日誌輸出進行驗證
其它服務間通信方式
使用RestTemplate
第一步:註冊
@Bean
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
}
@LoadBalanced 註解標記負載均衡
第二步:使用
@RestController
static class TestController {
@Autowired
RestTemplate restTemplate;
@GetMapping("/test")
public String test() {
String result = restTemplate.getForObject("http://nacos-provider/hello?name=nacos", String.class);
return "Return : " + result;
}
}
請求地址中,直接使用服務名代替 Host 部分,在真正調用時,SpringCloud 會將請求攔截,然後通過負載均衡器選出節點,並替換爲具體節點的IP與端口執行調用。
使用WebClient
第一步:註冊
@Bean
@LoadBalanced
public WebClient.Builder loadBalancedWebClientBuilder() {
return WebClient.builder();
}
@LoadBalanced 註解標記負載均衡,其原理與之前的RestTemplate時一樣的。
第二步:使用
@Slf4j
@RestController
static class TestController {
@Autowired
private WebClient.Builder webClientBuilder;
@GetMapping("/test")
public Mono<String> test() {
Mono<String> result = webClientBuilder.build()
.get()
.uri("http://nacos-provider/hello?name=nacos")
.retrieve()
.bodyToMono(String.class);
return result;
}
}
使用Feign
第一步:消費者服務添加對 feign
的依賴
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
第二步:修改服務啓動類,開啓 Feign 客戶端功能,並定義客戶端
@EnableDiscoveryClient
@SpringBootApplication
@EnableFeignClients
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@FeignClient("nacos-provider")
public static interface Client {
@GetMapping("/hello")
String hello(@RequestParam String name);
}
}
@EnableFeignClients
註解用於開啓掃描 Feign 客戶端的功能@FeignClient
註解用於定義 Feign 客戶端,需要指定這個接口所要調用的服務名稱,
接口中定義的方法使用 Spring MVC 的註解就可以來綁定服務提供方的 REST 接口。
注意:如果是 get 請求方式,@RequestParam 必須要帶,否則 get 請求會被轉換成 post 請求導致請求失效
第三步:通過 Spring 容器注入接口對象,直接調用
@RestController
public class FeignConsumerController {
@Autowired
private Application.Client client;
@GetMapping("/testWithFeign")
public String test() {
String result = client.hello("haha");
return "result : " + result;
}
}
優點:強類型檢查