pom文件
<!-- nacos 服务发现 -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>2.1.1.RELEASE</version>
</dependency>
<!-- nacos 配置中心 -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
<version>2.1.1.RELEASE</version>
</dependency>
配置文件
–优先读取
bootstrap.yml
读取什么结尾的文件
唯一id
server:
port: 端口号
spring:
profiles:
active: 后缀(例:test)
application:
name: 服务名字
启动类上需要注解
@EnableDiscoveryClient
注意调整对应版本
版本不对应会出现冲突注册不上的情况–可以参照官网推荐匹配版本
文件热部署
配置文件
bootstrap.yml
spring:
cloud:
nacos:
server-addr: #nacos地址端口号
config:
file-extension: yml
namespace: #配置管理分组uuid
#添加配置时 Group 的值一定要和 spring.cloud.nacos.config.group 的配置值一致。我这里没有添加group分组
## 可以配置多个 Data Id 同时配置时,他的优先级关系是 [n]其中 n 的值越大,优先级越高
extension-configs[0]:
data-id: config-student.yaml
refresh: true
extension-configs[1]:
data-id: config-teacher.yaml
refresh: true
discovery:
namespace: 配置管理分组uuid
@RefreshScope 热部署关键注解
实体类Student
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
@Data
//@Configuration或者@Component
@Configuration
@RefreshScope
@ConfigurationProperties(prefix = "student")
public class Student {
private String name;
private Integer age;
}
实体类Teacher
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Component;
@Data
//@Configuration或者@Component
@Component
@RefreshScope
@ConfigurationProperties(prefix = "teacher")
public class Teacher {
private String name;
private Integer age;
}
实体类Teacher 第二种写法
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Component;
@Data
//@Configuration或者@Component
@Component
@RefreshScope
//@ConfigurationProperties(prefix = "teacher")
public class Teacher {
@Value("${teacher.name}")
private String name;
@Value("${teacher.age}")
private Integer age;
}
测试
package com.iot;
import com.iot.common.api.ApiResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TestDemo {
@Autowired
private Student students;
@Autowired
private Teacher teachers;
@GetMapping("/test/Teacher")
public ApiResult Teacher() {
Teacher teacher = new Teacher();
teacher.setAge(teachers.getAge());
teacher.setName(teachers.getName());
return ApiResult.ok(teacher);
}
@GetMapping("/test/Student")
public ApiResult Student() {
Student student = new Student();
student.setAge(students.getAge());
student.setName(students.getName());
return ApiResult.ok(student);
}
}
结果
读取文件不指定类型
@SneakyThrows
@Test
public void test6() {
String serverAddr="nacos地址";
String dataId="test-demo";
String group="DEFAULT_GROUP";
String namespace="配置管理分组uuid/为分组可不写";
Properties properties = new Properties();
properties.put(PropertyKeyConst.SERVER_ADDR,serverAddr);
properties.put(PropertyKeyConst.NAMESPACE, namespace);
ConfigService configService = NacosFactory.createConfigService(properties);
String config = configService.getConfig(dataId, group, 5000);
System.out.println("config = " + config);
System.out.println("end>>>>>>>>>");
}
结果:
注:获取文件的项目要注册到nacos注册中心上不然会出现获取null值得情况
也可以直接复制图片位置中得代码加以修改
版权声明:本文为lichao_qibu原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。