1. 나코스 통합
1) pom 파일 구성
<!--nacos-config 版本号和springboot对应-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
<version>2.1.0.RELEASE</version>
</dependency>
<!--nacos-discovery 版本号和springboot对应-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>2.1.0.RELEASE</version>
</dependency>
<!--nacos-web 需要此依赖,不然服务列表里面服务注册不成功-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
2) 구성 파일 수정
nacos는 기본적으로 bootstrap.xml 파일을 읽습니다.
spring:
application:
name: test-server-tcp
profiles:
active: dev
cloud:
nacos:
discovery:
namespace: ${spring.profiles.active}
server-addr: 192.168.0.1:8848
config:
namespace: ${spring.profiles.active}
server-addr: 192.168.0.1:8848
ext-config:
- data-id: test-server-tcp.yml
group: ${spring.profiles.active}
refresh: true
3) nacos 관리 인터페이스에서 구성
1. 새 네임스페이스 생성
2. 새 구성 파일 생성
둘째, 동적 읽기 구성 변경
구성을 참조해야 하는 클래스에 @RefreshScope 주석을 추가합니다.
샘플 코드:
@RefreshScope
@RestController
@RequestMapping(value="/config")
public class NacosConfig {
@Value(value = "${nacos.test-name}")
private String testName;
@Value(value = "${nacos.test-port}")
private String testPort;
@GetMapping(value = "/get")
public String getConfig(){
return "name: "+ testName +";" + "port: "+ testPort +";";
}
}
동적 구성이 적용되는지 여부는 http url을 통해 확인할 수 있습니다.
3. 동적 구성이 변경된 후 타이밍 작업이 실행되지 않습니다.
문제: 프로그램에 예약된 작업이 있습니다. 예약된 작업에서 읽은 구성이 동적으로 변경되면 예약된 작업이 실행되지 않습니다.
이유: 예약된 작업이 구성 파일을 읽기 위한 지연 로딩 방식
솔루션: 예약된 작업이 있는 클래스에 인터페이스 구현
ApplicationListener<RefreshScopeRefreshedEvent> 및 해당 메소드 구현, 빈 메소드
추신: 프로그램에서 시간 초과 작업을 사용하는 모든 클래스는 위의 인터페이스를 구현해야 합니다.