依赖
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.Driver
url: jdbc:mysql://127.0.0.1:3306/wddatabase?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf8&autoReconnect=true&allowMultiQueries=true
username: root
password: root
application:
name: wd-admin
profiles:
active: dev
cloud:
nacos:
config:
server-addr: 192.168.140.135:8848
file-extension: properties
# nacos注册中心地址,注意:结尾不能带斜杠/
discovery:
server-addr: 192.168.140.135:8848
namespace: 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.Driver
url: jdbc:mysql://127.0.0.1:3306/wddatabase?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf8&autoReconnect=true&allowMultiQueries=true
username: root
password: root
application:
name: wd-admin
profiles:
active: dev
cloud:
nacos:
config:
server-addr: 192.168.140.135:8848
file-extension: properties
# nacos注册中心地址,注意:结尾不能带斜杠/
group: admin
namespace: 7ca03771-8cda-43d1-8880-c4f66077ddf9
discovery:
server-addr: 192.168.140.135:8848
group: admin
namespace: 7ca03771-8cda-43d1-8880-c4f66077ddf9
命名空间新建
新建配置,保存
dataId是由application.name和profiles.active组成
再次进行测试,修改成功