添加依赖
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
<version>${nacos.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>${nacos.version}</version>
</dependency>
我这里使用的版本号是2.2.8
<nacos.version>2.2.8.RELEASE</nacos.version>
在application.yml或application.properties中添加nacos配置信息
#Nacos注册中心配置
spring.cloud.nacos.config.server-addr=127.0.0.1:8848
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
spring.application.name=provide
server.port=8080
在启动类上添加 @EnableDiscoveryClient 注解以启动服务注册和发现功能
@SpringBootApplication
@EnableDiscoveryClient
public class NacosSpringcloudDemoApplication {
public static void main(String[] args) {
SpringApplication.run(NacosSpringcloudDemoApplication.class, args);
}
}
如果想要nacos服务端的配置文件实现动态刷新功能,可以在controller层加上@RefreshScope注解
@RestController
@RefreshScope
public class UserController {
}
我这里使用的springboot版本号是2.3.12.RELEASE,springboot和springcloud的版本号需要对应
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.12.RELEASE</version>
<relativePath/>
</parent>
Spring Cloud Alibaba Version | Spring Cloud Version | Spring Boot Version |
---|---|---|
2021.0.5.0* | Spring Cloud 2021.0.5 | 2.6.13 |
2021.0.4.0 | Spring Cloud 2021.0.4 | 2.6.11 |
2021.0.1.0 | Spring Cloud 2021.0.1 | 2.6.3 |
2021.1 | Spring Cloud 2020.0.1 | 2.4.2 |