概述
用一個簡單的服務提供者和消費者案例,看懂Nacos.(對于使用過Eureka 的朋友,這個案例秒懂)
服務提供者
POM 依賴
建立一個工程名為
hello-spring-cloud-alibaba-provider
的服務提供者項目,
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">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.hello.sc.alibaba</groupId>
<artifactId>hello-spring-cloud-alibaba-parent</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<artifactId>hello-spring-cloud-alibaba-provider</artifactId>
<packaging>jar</packaging>
<name>hello-spring-cloud-alibaba-provider</name>
<dependencies>
<!-- Spring Boot Begin -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- Spring Boot End -->
<!-- Spring Cloud Begin -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!-- Spring Cloud End -->
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>com.hello.sc.alibaba.nacos.provider.NacosProviderApplication</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</project>
建立啟動類
@SpringBootApplication
@EnableDiscoveryClient
public class NacosProviderApplication {
public static void main(String[] args) {
SpringApplication.run(NacosProviderApplication.class,args);
}
}
建立資源映射類
通過
@EnableDiscoveryClient
注解表明是一個 Nacos 用戶端,該注解是 Spring Cloud 提供的原生注解
@RestController
public class ProviderResource {
@GetMapping(value = "/echo/{message}")
public String echo(@PathVariable String message) {
return "Hello Nacos Discovery " + message;
}
}
application.yml
spring:
application:
name: hello-spring-cloud-alibaba-provider
cloud:
nacos:
discovery:
server-addr: localhost:8848 #Nacos 所在的機器ip:port
server:
port: 8081
management:
endpoints:
web:
exposure:
include: "*"
啟動工程
通過浏覽器通路
http://localhost:8848/nacos
,即 Nacos Server 網址
Nacos Starter 更多配置項資訊
配置項 | Key | 預設值 | 說明 |
---|---|---|---|
服務端位址 | spring.cloud.nacos.discovery.server-addr | 無 | Nacos Server 啟動監聽的ip位址和端口 |
服務名 | spring.cloud.nacos.discovery.service | ${spring.application.name} | 給目前的服務命名 |
權重 | spring.cloud.nacos.discovery.weight | 1 | 取值範圍 1 到 100,數值越大,權重越大 |
網卡名 | spring.cloud.nacos.discovery.network-interface | 無 | 當IP未配置時,注冊的IP為此網卡所對應的IP位址,如果此項也未配置,則預設取第一塊網卡的位址 |
注冊的IP位址 | spring.cloud.nacos.discovery.ip | 無 | 優先級最高 |
注冊的端口 | spring.cloud.nacos.discovery.port | -1 | 預設情況下不用配置,會自動探測 |
命名空間 | spring.cloud.nacos.discovery.namespace | 無 | 常用場景之一是不同環境的注冊的區分隔離,例如開發測試環境和生産環境的資源(如配置、服務)隔離等。 |
AccessKey | spring.cloud.nacos.discovery.access-key | 無 | 當要上阿裡雲時,阿裡雲上面的一個雲賬号名 |
SecretKey | spring.cloud.nacos.discovery.secret-key | 無 | 當要上阿裡雲時,阿裡雲上面的一個雲賬号密碼 |
Metadata | spring.cloud.nacos.discovery.metadata | 無 | 使用 Map 格式配置,使用者可以根據自己的需要自定義一些和服務相關的中繼資料資訊 |
日志檔案名 | spring.cloud.nacos.discovery.log-name | 無 | |
接入點 | spring.cloud.nacos.discovery.enpoint | UTF-8 | 地域的某個服務的入口域名,通過此域名可以動态地拿到服務端位址 |
是否內建 Ribbon | ribbon.nacos.enabled | true | 一般都設定成 true 即可 |
服務消費者
概述
服務消費者的建立與服務提供者大同小異,這裡采用最原始的一種方式,即顯示的使用 LoadBalanceClient 和 RestTemplate 結合的方式來通路。
POM 依賴
<?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">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.hello.sc.alibaba</groupId>
<artifactId>hello-spring-cloud-alibaba-parent</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<artifactId>hello-spring-cloud-alibaba-consumer</artifactId>
<packaging>jar</packaging>
<name>hello-spring-cloud-alibaba-consumer</name>
<dependencies>
<!-- Spring Boot Begin -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- Spring Boot End -->
<!-- Spring Cloud Begin -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!-- Spring Cloud End -->
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>com.hello.sc.alibaba.nacos.consumer.NacosConsumerApplication</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</project>
編寫啟動類
package com.hello.sc.alibaba.nacos.consumer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@SpringBootApplication
@EnableDiscoveryClient
public class NacosConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(NacosConsumerApplication.class,args);
}
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
application.yml
spring:
application:
name: hello-spring-cloud-alibaba-consumer
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848 # Nacos 所在機器的IP:port
server:
port: 9091
management:
endpoints:
web:
exposure:
include: "*"
編寫資源映射類
package com.hello.sc.alibaba.nacos.consumer.rest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
@RestController
public class ConsumerResource {
@Autowired
private LoadBalancerClient loadBalancerClient;
@Autowired
private RestTemplate restTemplate;
@Value("${spring.application.name}")
private String appName;
@GetMapping(value = "/echo/app/name")
public String echo() {
//使用 LoadBalanceClient 和 RestTemplate 結合的方式來通路
ServiceInstance serviceInstance = loadBalancerClient.choose("hello-spring-cloud-alibaba-provider");
String url = String.format("http://%s:%s/echo/%s", serviceInstance.getHost(), serviceInstance.getPort(), appName);
return restTemplate.getForObject(url, String.class);
}
}
啟動工程
通過浏覽器通路
http://localhost:8848/nacos
,即 Nacos Server 網址
消費服務提供者提供的消息http://localhost:9091/echo/app/name
到此,使用RestTemplate消費提供者提供的資訊搭建完畢,下一章節我們将使用Feign 來實作服務之間的通信問題