依赖
nacos服务发现和注册的依赖
<dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
配置
spring:datasource:driver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://127.0.0.1:3306/wddatabase?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf8&autoReconnect=true&allowMultiQueries=trueusername: rootpassword: rootapplication:name: wd-adminprofiles:active: devcloud:nacos:config:server-addr: 192.168.140.135:8848file-extension: properties# nacos注册中心地址,注意:结尾不能带斜杠/discovery:server-addr: 192.168.140.135:8848namespace: public
访问nacos地址
http://192.168.140.135:8848/
dataId是由配置信息里的application.name和profiles.active和文件类型组成的
group信息修改
代码示例
@Value("${user.id}")
private String id;
@Value("${user.name}")
private String name;
@Value("${user.age}")
private String age;/*** 测试** @return CommonResult*/
@PostMapping("/")
public CommonResult<String> test() {System.out.println(id + name + age);return CommonResult.success();
}
修改配置信息
测试运行
自定义配置namespace和服务发现namespace
先把配置配置好
spring:datasource:driver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://127.0.0.1:3306/wddatabase?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf8&autoReconnect=true&allowMultiQueries=trueusername: rootpassword: rootapplication:name: wd-adminprofiles:active: devcloud:nacos:config:server-addr: 192.168.140.135:8848file-extension: properties# nacos注册中心地址,注意:结尾不能带斜杠/group: adminnamespace: 7ca03771-8cda-43d1-8880-c4f66077ddf9discovery:server-addr: 192.168.140.135:8848group: adminnamespace: 7ca03771-8cda-43d1-8880-c4f66077ddf9
命名空间新建
新建配置,保存
dataId是由application.name和profiles.active组成
再次进行测试,修改成功