一,我们需要引用的包(nacos需对应springboot的2.3.6.RELEASE这个版本)
<dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId> <version>2.2.3.RELEASE</version> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> <version>2.2.3.RELEASE</version> </dependency>
二,启动类的配置
package com.nl; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; @SpringBootApplication @EnableDiscoveryClient public class NacosApplication { public static void main(String[] args) { System.out.println("test"); SpringApplication.run(NacosApplication.class, args); } }
三,配置文件(需注意的点,配置必须在bootstrap.yml这里添加)
spring: profiles: active: dev application: name: test cloud: nacos: config: namespace: dev file-extension: yaml server-addr: https://nacos.test.com username: nacos_devtest password: 123456 discovery: namespace: dev server-addr: https://nacos.test.com username: nacos_devtest password: 123456
四,测试
import com.nl.NacosApplication; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; @RunWith(SpringRunner.class) @SpringBootTest(classes = NacosApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) public class NaocsTest { @Value("${test.name}") private String Name; @Test public void NacosTest() { System.out.println("Test" + Name); } }