nacos可以用作注册中心,也可以用作配置中心
nacos简单入门
pom文件
<parent>
<artifactId>spring-boot-starter-parent</artifactId>
<groupId>org.springframework.boot</groupId>
<version>2.6.3</version>
</parent>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>2021.0.1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>2021.0.1.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>
<!--
nacos配置文件
-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 引入 Spring Cloud Alibaba Nacos Discovery 相关依赖,将 Nacos 作为注册中心,并实现对其的自动配置
没有它注册不上去
-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
</dependencies>
配置文件:bootstrap.yml
问题点:吧bootstrap.yml名字改成application.yml或者application.yaml,都不能获取到配置中的变量,有人说nacos必须要用bootstrap.yml
server:
port: 9003
spring:
application:
name: nacos-demo
cloud:
nacos:
discovery:
server-addr: **.***.***.***:8848
config:
server-addr: **.***.***.***:8848
namespace: public
group: DEFAULT_GROUP
file-extension: yaml
profiles:
active: dev
操作页面,地址:http://IP地址:8848/nacos
文件:nacos-demo-dev.yaml
组成:{spring.application.name}+{-}+{profiles.active}+{spring.cloud.nacos.config.file-extension}
四部分,必须一致
- {spring.application.name}:nacos-demo
- {-}:-
- {profiles.active}:dev(会根据配置信息,可为空,如果为空前面-符号不要)
- {spring.cloud.nacos.config.file-extension}:yaml
启动类加注解
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) {
SpringApplication.run(NacosApplication.class,args);
}
}
测试controller,@RefreshScope // 刷新配置,@Value(“${common}”)获取值
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.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/provider")
@RefreshScope // 刷新配置
public class NacosProviderController {
@Value("${info}")
private String info;
@Value("${common}")
private String common;
@GetMapping("/info")
public String configInfo() {
return info+"="+common;
}
}
如果需要共享文件,需要在bootstrap.yml中加共享配置;shared-configs:
注:共享文件配置和对应文件配置冲突,取对应文件
server:
port: 9003
spring:
application:
name: nacos-demo
cloud:
nacos:
discovery:
server-addr: ip:8848
config:
server-addr: ip:8848
namespace: public
group: DEFAULT_GROUP
file-extension: yaml
#新的共享配置+动态刷新
shared-configs:
- data-id: application.yaml
group: DEFAULT_GROUP
refresh: true #动态刷新配置开启
profiles:
active: dev