使用了 https://nacos.io/zh-cn/docs/quick-start-docker.html
中的stondalon啟動nacos
docker-compose -f example/standalone-derby.yaml up
啟動完在瀏覽器上執行 http://127.0.0.1:8848/nacos/
輸入帳號nacos/密碼nacos,後
Configuration增加一個服務的設定
Data ID: test-provider.yaml
Group: DEFAULT_GROUP
Configuration Content: server:
port: 8888
在spring boot的pom.xml設定以下depenency, 版號不可乎略
<!--nacos-config-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
<version>0.9.0.RELEASE</version><!--必需設定版本號-->
</dependency>
<!--nacos-discovery-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>0.9.0.RELEASE</version><!--必需設定版本號-->
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-actuator</artifactId>
<version>2.1.6.RELEASE</version><!--必需設定版本號-->
</dependency>
在spring boot的resource增加bootstrap.yml,
spring:
application:
name: test-provider
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
config: # 以下只能增加在bootstrap.yml中
server-addr: 127.0.0.1:8848
file-extension: yaml
prefix: test-provider
在啟動入口增加@EnableDiscoveryClient
package com.leon456.testprovider;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient
public class TestProviderApplication {
public static void main(String[] args) {
SpringApplication.run(TestProviderApplication.class, args);
}
}
啟動後在console上看到port為nacos上設定的8888,非預設的8080