簡(jiǎn)介
前四個(gè)字母為Nameing和Configuration的前兩個(gè)字母揣云,最后的s為Service柴灯。Nacos是一個(gè)更易于構(gòu)建原生應(yīng)用的動(dòng)態(tài)服務(wù)發(fā)現(xiàn)、配置管理和服務(wù)管理平臺(tái)瞧壮。
能夠替代Eureka做服務(wù)注冊(cè)中心、替代Config做服務(wù)配置中心
Nacos支持AP直砂、CP的切換
Docker安裝單機(jī)版Nacos
- 進(jìn)入一個(gè)文件夾幌陕,然后Clone項(xiàng)目
git clone https://github.com/nacos-group/nacos-docker.git
cd nacos-docker
- 使用MySQL5.7
docker-compose -f example/standalone-mysql-5.7.yaml up
- 使用MySQL8
docker-compose -f example/standalone-mysql-8.yaml up
- 瀏覽器訪問(wèn):http://ip:8848/nacos,如:http://192.168.138.135:8848/nacos 賬號(hào)密碼都默認(rèn)為:nacos
Nacos 作為服務(wù)注冊(cè)中心
服務(wù)提供者
新建module
pom.xml的依賴
<dependencies>
<!--SpringCloud ailibaba nacos -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!-- SpringBoot整合Web組件 -->
<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.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
application.yml
server:
port: 9001
spring:
application:
name: nacos-payment-provider
cloud:
nacos:
discovery:
server-addr: 192.168.138.135:8848 # 配置Nacos地址
management:
endpoints:
web:
exposure:
include: '*'
啟動(dòng)類
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient
public class PaymentApplication9001 {
public static void main(String[] args) {
SpringApplication.run(PaymentApplication9001.class,args);
}
}
一個(gè)簡(jiǎn)單的測(cè)試controller
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TestController {
@Value("${server.port}")
private String serverPort;
@GetMapping(value = "/payment/nacos")
public String getPayment() {
String result = "nacos registry, serverPort: " + serverPort;
return result;
}
}
參考上面步驟存崖,再新建一個(gè)提供者實(shí)例冻记,端口9002,或者使用idea啟動(dòng)多實(shí)例来惧,用于下面演示負(fù)載均衡
服務(wù)消費(fèi)者
新建module
pom.xml的依賴
<dependencies>
<!--SpringCloud ailibaba nacos -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!-- SpringBoot整合Web組件 -->
<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.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
application.yml
server:
port: 8400
spring:
application:
name: nacos-order-consumer
cloud:
nacos:
discovery:
server-addr: 192.168.138.135:8848 # 配置Nacos地址
# 消費(fèi)者將要去訪問(wèn)的微服務(wù)名稱(注冊(cè)成功進(jìn)nacos的微服務(wù)提供者)
service-url:
nacos-user-service: http://nacos-payment-provider
啟動(dòng)類
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient
public class OrderApplication8400 {
public static void main(String[] args) {
SpringApplication.run(OrderApplication8400.class, args);
}
}
RestTemplate配置類
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
@Configuration
public class ApplicationContextConfig {
@Bean
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
一個(gè)簡(jiǎn)單的測(cè)試controller
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
@RestController
public class TestController {
@Autowired
private RestTemplate restTemplate;
@Value("${service-url.nacos-user-service}")
private String serverUrl;
@GetMapping("/consumer/payment/nacos")
public String getPayment() {
String url = serverUrl + "/payment/nacos";
return restTemplate.getForObject(url, String.class);
}
}
測(cè)試
啟動(dòng)2個(gè)提供者和1個(gè)消費(fèi)者服務(wù)
瀏覽器反復(fù)訪問(wèn) http://localhost:8400/consumer/payment/nacos 可以看到會(huì)輪詢請(qǐng)求提供者服務(wù)的不同實(shí)例
之所以可以實(shí)現(xiàn)負(fù)載均衡冗栗,是因?yàn)閚acos的依賴默認(rèn)引入了ribbon
Nacos作為配置中心
新建module
pom.xml
<dependencies>
<!--nacos-config-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
<!--nacos-discovery-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!--web + actuator-->
<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.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
bootstrap.yml
server:
port: 3377
spring:
application:
name: nacos-config-client
cloud:
nacos:
discovery:
server-addr: 192.168.138.135:8848 # Nacos服務(wù)注冊(cè)中心地址
config:
server-addr: 192.168.138.135:8848 #Nacos作為配置中心地址
file-extension: yaml #指定yaml格式的配置
# group: DEFAULT_GROUP
# namespace: eeba7661-6b5e-46c6-aa45-5df57f48091f
# ${spring.application.name}-${spring.profile.active}.${spring.cloud.nacos.config.file-extension}
# nacos-config-client-dev.yaml
# nacos-config-client-test.yaml ----> config.info
application.yml
spring:
profiles:
active: dev # 表示開(kāi)發(fā)環(huán)境
#active: test # 表示測(cè)試環(huán)境
啟動(dòng)類
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient
public class ConfigApplication3377 {
public static void main(String[] args) {
SpringApplication.run(ConfigApplication3377.class, args);
}
}
測(cè)試controller
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RefreshScope // 支持Nacos的動(dòng)態(tài)刷新功能
public class TestController {
@Value("${config.info}")
private String configInfo;
@GetMapping("/config/info")
public String getConfigInfo() {
return configInfo;
}
}
Naocs的配置文件的dataId命名規(guī)則:
${prefix}-${spring.profile.active}.${file-extension}
如:nacos-config-client-dev.yaml
這里的后綴應(yīng)該是yaml而不是yml
prefix
默認(rèn)為spring.application.name
的值,也可以通過(guò)配置項(xiàng)spring.cloud.nacos.config.prefix
來(lái)配置供搀。
spring.profiles.active
即為當(dāng)前環(huán)境對(duì)應(yīng)的 profile隅居,詳情可以參考 Spring Boot文檔。 注意:當(dāng)spring.profiles.active
為空時(shí)趁曼,對(duì)應(yīng)的連接符-
也將不存在军浆,dataId 的拼接格式變成${prefix}.${file-extension}
file-exetension
為配置內(nèi)容的數(shù)據(jù)格式,可以通過(guò)配置項(xiàng)spring.cloud.nacos.config.file-extension
來(lái)配置挡闰。目前只支持properties
和yaml
類型
在控制面板中配置一個(gè)yaml文件乒融,用于測(cè)試
配置內(nèi)容如下:
然后啟動(dòng)該服務(wù),就會(huì)自動(dòng)去讀取 nacos-config-client-dev.yaml的配置內(nèi)容
訪問(wèn):http://localhost:3377/config/info 可以看到配置信息
接著摄悯,在Nacos控制面板編輯配置內(nèi)容赞季,將version=1
改為version=2
再訪問(wèn)http://localhost:3377/config/info 可以看到信息發(fā)生了改變,實(shí)現(xiàn)了自動(dòng)刷新功能